Skip to content

when I open option(a) for assignment 2 it runs with error: #1

@CanoeFZH

Description

@CanoeFZH
diff --git a/assignment2/assignment2.py b/assignment2/assignment2.py
index 28e45bd..942fa2e 100644
--- a/assignment2/assignment2.py
+++ b/assignment2/assignment2.py
@@ -150,8 +150,8 @@ def train(epochs):

             # Options:
             # (a)
-            # embed_to_hid_weights_gradient = np.dot(
-            #    np.transpose(back_propagated_deriv_1), embedding_layer_state)
+            embed_to_hid_weights_gradient = np.dot(
+                    np.transpose(back_propagated_deriv_1), embedding_layer_state)

             # (b)
             # embed_to_hid_weights_gradient = np.dot(embedding_layer_state,
@@ -169,7 +169,7 @@ def train(epochs):

             # Options
             # (a)
-            # hid_bias_gradient = np.sum(back_propagated_deriv_1, axis=1)
+            hid_bias_gradient = np.sum(back_propagated_deriv_1, axis=1)

             # (b)
             # hid_bias_gradient = np.sum(back_propagated_deriv_1, axis=0)
@@ -188,8 +188,8 @@ def train(epochs):

             # Options
             # (a)
-            # back_propagated_deriv_2 = np.dot(embed_to_hid_weights,
-            #                                     back_propagated_deriv_1)
+            back_propagated_deriv_2 = np.dot(embed_to_hid_weights,
+                    back_propagated_deriv_1)

             # (b)
             # back_propagated_deriv_2 = np.dot(back_propagated_deriv_1,
@@ -385,11 +385,14 @@ def fprop(input_batch, word_embedding_weights, embed_to_hid_weights,

     # Combine three inputs, turn them into 1D vector
     ib_tmp = np.reshape(input_batch, (1,-1), order="F").ravel()
+    print (ib_tmp.shape)
     # Take rows from weight matrices, row number equal to word index.
     # Then transpose.
     wew_tmp = np.transpose(word_embedding_weights[ib_tmp])
+    print (wew_tmp.shape)
     # Combine three nearby columns
     embedding_layer_state = np.reshape(wew_tmp, (numhid1 * numwords,-1), order="F")
+    print(embed_to_hid_weights.shape)

     ## COMPUTE STATE OF HIDDEN LAYER
     ## Compute inputs to hidden units.
@@ -404,7 +407,7 @@ def fprop(input_batch, word_embedding_weights, embed_to_hid_weights,

     # Options
     # (a)
-    # hidden_layer_state = 1 / (1 + np.exp(inputs_to_hidden_units))
+    hidden_layer_state = 1 / (1 + np.exp(inputs_to_hidden_units))

     # (b)
     # hidden_layer_state = 1 / (1 - np.exp(-inputs_to_hidden_units))
@@ -423,8 +426,8 @@ def fprop(input_batch, word_embedding_weights, embed_to_hid_weights,

     # Options
     # (a)
-    # inputs_to_softmax = np.dot(np.transpose(hid_to_output_weights),
-    #                             hidden_layer_state)
+    inputs_to_softmax = np.dot(np.transpose(hid_to_output_weights),
+            hidden_layer_state)
     # output_bias_tmp = np.tile(output_bias,(1,batchsize))
     # inputs_to_softmax = inputs_to_softmax + output_bias_tmp

with error

→ python assignment2.py
('Epoch', 1)
(300,)
(50, 300)
(150, 200)
Traceback (most recent call last):
  File "assignment2.py", line 564, in <module>
    model = train(10)
  File "assignment2.py", line 154, in train
    np.transpose(back_propagated_deriv_1), embedding_layer_state)
ValueError: shapes (100,200) and (150,100) not aligned: 200 (dim 1) != 150 (dim 0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions