diff --git a/nn.py b/nn.py index 4c597c20b7af2908c64c7a17bec587bb92336804..6300a2bcebb1a3aee1b47d1da6b0bef6fee9ac94 100644 --- a/nn.py +++ b/nn.py @@ -94,7 +94,7 @@ class Linear: # TODO: Initialize the weights accordind to the description above. # Ton't forget to wrap the data into a Variable. ####################################################################### - self.W = None + self.W = Variable(np.random.uniform(-np.sqrt(1/in_features), np.sqrt(1/in_features), (out_features, in_features))) ####################################################################### # --------------------------- END OF YOUR CODE ------------------------ ####################################################################### @@ -104,7 +104,7 @@ class Linear: # TODO: Initialize the bias accordind to the description above. # Ton't forget to wrap the data into a Variable. ####################################################################### - self.b = None + self.b = Variable(np.random.uniform(-np.sqrt(1/in_features), np.sqrt(1/in_features), (1, out_features))) ####################################################################### # --------------------------- END OF YOUR CODE ------------------------ ####################################################################### @@ -116,7 +116,7 @@ class Linear: # TODO: Use the functional module to compute the first part of the # linear transfomation -> y = XW.T ####################################################################### - y = None + y = F.matmul(X, self.W.t()) ####################################################################### # --------------------------- END OF YOUR CODE ------------------------ ####################################################################### @@ -124,7 +124,7 @@ class Linear: ####################################################################### # TODO: If the bias is true add the bias. ####################################################################### - y = None + y = y + self.b ####################################################################### # --------------------------- END OF YOUR CODE ------------------------ ####################################################################### diff --git a/optim.py b/optim.py index 940ce77e0207231d6740e350b16988e730e360b6..86094f2aeb7249f0c2657dcf5023195f739ac4ca 100644 --- a/optim.py +++ b/optim.py @@ -23,7 +23,7 @@ class Optimizer: class SGD(Optimizer): """ - Applies the SGD update to the weights W = lr * W.grad. + Applies the SGD update to the weights W = W - lr * W.grad. """ def __init__(self, parameters, lr=1e-3): @@ -37,6 +37,12 @@ class SGD(Optimizer): # to acces the data of parametes Variables: # - self.parameters.params[key].data ####################################################################### + w1 = self.parameters.params["layer1_W"] + w2 = self.parameters.params["layer2_W"] + + w1.data -= self.lr * w1.grad + w2.data -= self.lr * w2.grad + pass ####################################################################### # --------------------------- END OF YOUR CODE ------------------------