From 66c1bf48de014c84ad0d1b91ddb90a71ca9cf665 Mon Sep 17 00:00:00 2001 From: Maxxhim <maxc2507@gmail.com> Date: Fri, 3 Apr 2020 23:14:45 +0200 Subject: [PATCH] finish python files --- nn.py | 8 ++++---- optim.py | 8 +++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/nn.py b/nn.py index 4c597c2..6300a2b 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 940ce77..86094f2 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 ------------------------ -- GitLab