From 8182ab8f82254c3b88bdab57aae0d5c66223942c Mon Sep 17 00:00:00 2001 From: young-hun-jo Date: Fri, 19 Aug 2022 17:10:00 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=EB=B3=B5=EC=8A=B5=20for=20bloging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- season3/dezero/layers.py | 2 -- season3/dezero/models.py | 10 +++++---- season3/dezero/optimizers.py | 25 +++++++++++---------- season3/test.py | 42 +++++++++++++----------------------- 4 files changed, 33 insertions(+), 46 deletions(-) diff --git a/season3/dezero/layers.py b/season3/dezero/layers.py index e3e06ce..7ca3f21 100644 --- a/season3/dezero/layers.py +++ b/season3/dezero/layers.py @@ -11,7 +11,6 @@ def __init__(self): def __setattr__(self, name, value): if isinstance(value, (Parameter, Layer)): self._params.add(name) - # avoid infinite callable super().__setattr__(name, value) def __call__(self, *inputs): @@ -67,4 +66,3 @@ def forward(self, x): return y - diff --git a/season3/dezero/models.py b/season3/dezero/models.py index b786c21..f119e7a 100644 --- a/season3/dezero/models.py +++ b/season3/dezero/models.py @@ -18,10 +18,12 @@ def __init__(self, fc_output_sizes, activation=F.sigmoid): for i, output_size in enumerate(fc_output_sizes): layer = L.Linear(output_size) - setattr(self, 'l' + str(i), layer) + setattr(self, 'layer' + str(i), layer) self.layers.append(layer) def forward(self, x): - for l in self.layers[:-1]: - x = self.activation(l(x)) - return self.layers[-1](x) \ No newline at end of file + for layer in self.layers[:-1]: + x = layer(x) + y = self.layers[-1](x) + return y + diff --git a/season3/dezero/optimizers.py b/season3/dezero/optimizers.py index 4e641c3..e21f280 100644 --- a/season3/dezero/optimizers.py +++ b/season3/dezero/optimizers.py @@ -12,16 +12,15 @@ def setup(self, target): def update(self): params = [p for p in self.target.params() if p.grad is not None] - - # (optional) preprocessing gradients ex) gradient clipping, weight decay, ... + # preprocess parametrs for f in self.hooks: - f(params) + f(p) - # update parameters + # update parametrs for param in params: self.update_one(param) - def update_one(self, param): + def update_one(self): raise NotImplementedError("This method should be run outside of Optimizer class.") def add_hook(self, f): @@ -29,27 +28,27 @@ def add_hook(self, f): class SGD(Optimizer): - def __init__(self, lr=0.01): + def __init__(self, learning_rate=0.01): super().__init__() - self.lr = lr + self.lr = learning_rate def update_one(self, param): param.data -= self.lr * param.grad.data class MomentumSGD(Optimizer): - def __init__(self, lr=0.01, momentum=0.9): + def __init__(self, learning_rate=0.01, momentum=0.9): super().__init__() - self.lr = lr + self.lr = learning_rate self.momentum = momentum self.vs = {} def update_one(self, param): v_key = id(param) if v_key not in self.vs: - self.vs[v_key] = np.zeros_like(param.data) + self.vs[v_key] = np.zeros_like(param) - v = self.vs[v_key] - v *= self.momentum + v = self.momentum * self.vs[v_key] v -= self.lr * param.grad.data - param.data += v \ No newline at end of file + param.data += v + diff --git a/season3/test.py b/season3/test.py index 38d1b25..35bf951 100644 --- a/season3/test.py +++ b/season3/test.py @@ -4,49 +4,37 @@ # sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import numpy as np -import dezero.layers as L +from dezero.models import MLP from dezero import functions as F -from dezero import Layer +from dezero.optimizers import SGD # SGD를 import # dataset np.random.seed(42) x = np.random.rand(100, 1) -y = np.sin(2 * np.pi * x) + np.random.rand(100, 1) # label +y = np.sin(2 * np.pi * x) + np.random.rand(100, 1) -# build model -model = Layer() -model.l1 = L.Linear(10) -model.l2 = L.Linear(1) - - -def predict(x): - y = model.l1(x) - y = F.sigmoid(y) - y = model.l2(y) - return y - - -lr = 0.2 +# build and compile model +model = MLP((25, 40, 3, 1)) +lr = 0.01 iters = 10000 +optimizer = SGD(lr).setup(model) # 달라진 부분! for i in range(iters): - # predict and get loss - y_pred = predict(x) + y_pred = model(x) loss = F.mean_squared_error(y, y_pred) - # clear grads model.clear_grads() + loss.backward() + + # 달라진 부분! + optimizer.update() + + if (0 <= i <= 30) or (i % 1000) == 0: + print(f"Epoch:{i+1} -> Loss:{loss.data}") - # backward - loss.backward(use_heap=True) - # update parameters - for param in model.params(): - param.data -= lr * param.grad.data - if i == 0 or (i+1) % 1000 == 0: - print(f"# Epoch:{i+1} -> Loss: {loss}")