def test_neural_regression_layer(self): np.random.seed(0) x = np.random.rand(100, 1) y = np.sin(2 * np.pi * x) + np.random.rand(100, 1) model = L.Layer() model.l1 = L.Linear(10) model.l2 = L.Linear(1) def predict(model, x): y = model.l1(x) y = F.sigmoid(y) y = model.l2(y) return y lr = 0.2 iters = 10000 for i in range(iters): y_pred = predict(model, x) loss = F.mean_squared_error(y, y_pred) model.cleargrads() loss.backward() for p in model.params(): p.data -= lr * p.grad.data
def test_linear_forward(self, linear_object): l1 = L.Linear(10) l2 = L.Linear(1) y_pred = predict(linear_object[0], l1, l2) loss = F.mean_squared_error(linear_object[1], y_pred) assert np.allclose(loss.data, 0.81651785)
def test_linear_regressoion(self): np.random.seed(0) x = np.random.rand(100, 1) y = 5 + 2 * x + np.random.rand(100, 1) x, y = Variable(x), Variable(y) W = Variable(np.zeros((1, 1))) b = Variable(np.zeros(1)) def predict(x): y = F.matmul(x, W) + b return y def mean_squared_error(x0, x1): diff = x0 - x1 return F.sum(diff**2) / len(diff) lr = 0.1 iters = 100 for i in range(iters): y_pred = predict(x) loss = F.mean_squared_error(y, y_pred) W.cleargrad() b.cleargrad() loss.backward() W.data -= lr * W.grad.data b.data -= lr * b.grad.data print(W, b, loss)
def test_neural_regression(self): np.random.seed(0) x = np.random.rand(100, 1) y = np.sin(2 * np.pi * x) + np.random.rand(100, 1) I, H, O = 1, 10, 1 W1 = Variable(0.01 * np.random.randn(I, H)) b1 = Variable(np.zeros(H)) W2 = Variable(0.01 + np.random.randn(H, O)) b2 = Variable(np.zeros(O)) def predict(x): y = F.linear(x, W1, b1) y = F.sigmoid(y) y = F.linear(y, W2, b2) return y lr = 0.2 iters = 10000 for i in range(iters): y_pred = predict(x) loss = F.mean_squared_error(y, y_pred) W1.cleargrad() b1.cleargrad() W2.cleargrad() b2.cleargrad() loss.backward() W1.data -= lr * W1.grad.data b1.data -= lr * b1.grad.data W2.data -= lr * W2.grad.data b2.data -= lr * b2.grad.data
def sin_train(self): max_epoch = 100 hidden_size = 100 bptt_length = 30 train_set = dezero.datasets.SinCurve(train=True) seqlen = len(train_set) model = SimpleRNN(hidden_size, 1) optimizer = dezero.optimizers.Adam().setup(model) for epoch in range(max_epoch): model.reset_state() loss, count = 0, 0 for x, t in train_set: x = x.reshape(1, 1) y = model(x) loss += F.mean_squared_error(y, t) count += 1 if count % bptt_length == 0 or count == seqlen: model.cleargrads() loss.backward() loss.unchain_backward() optimizer.update() avg_loss = float(loss.data) / count print('| epoch %d | loss %f' % (epoch + 1, avg_loss))
def test_linear_backward(self, linear_object): l1 = L.Linear(10) l2 = L.Linear(1) y_pred = predict(linear_object[0], l1, l2) loss = F.mean_squared_error(linear_object[1], y_pred) l1.cleargrads() l2.cleargrads() loss.backward() for l in [l1, l2]: for p in l.params(): assert p.grad.data is not None
def test_mlp(self): x = np.random.rand(100, 1) y = np.sin(2 * np.pi * x) + np.random.rand(100, 1) max_iters = 100 model = M.MLP((10, 20, 30, 40, 1)) for i in range(max_iters): y_pred = model(x) loss = F.mean_squared_error(y, y_pred) model.cleargrads() loss.backward() for p in model.params(): assert p.grad.data is not None
def test_simple_rnn(): seq_data = [np.random.randn(1, 1) for _ in range(1000)] xs = seq_data[0:-1] ts = seq_data[1:] model = SimpleRNN(10, 1) loss, cnt = 0, 0 for x, t in zip(xs, ts): y = model(x) loss += F.mean_squared_error(y, t) cnt += 1 if cnt == 2: model.cleargrads() loss.backward() break
def test_MomentumSDG(self): x = np.random.rand(100, 1) y = np.sin(2 * np.pi * x) + np.random.rand(100, 1) hidden_size = 10 model = MLP((hidden_size, 1)) optimizer = optimizers.MomentumSGD(lr) optimizer.setup(model) for i in range(max_iters): y_pred = model(x) loss = F.mean_squared_error(y, y_pred) model.cleargrads() loss.backward() optimizer.update() assert True
def update(self, state, action, reward, next_state, done): if done: next_q = np.zeros(1) # [0.] else: next_qs = self.qnet(next_state) next_q = next_qs.max(axis=1) next_q.unchain() target = self.gamma * next_q + reward qs = self.qnet(state) q = qs[:, action] loss = F.mean_squared_error(target, q) self.qnet.cleargrads() loss.backward() self.optimizer.update() return loss.data
def update(self, state, action, reward, next_state, done): self.replay_buffer.add(state, action, reward, next_state, done) if len(self.replay_buffer) < self.batch_size: return state, action, reward, next_state, done = self.replay_buffer.get_batch( ) qs = self.qnet(state) q = qs[np.arange(self.batch_size), action] next_qs = self.qnet_target(next_state) next_q = next_qs.max(axis=1) next_q.unchain() target = reward + (1 - done) * self.gamma * next_q loss = F.mean_squared_error(q, target) self.qnet.cleargrads() loss.backward() self.optimizer.update()
def update(self, state, action_prob, reward, next_state, done): state = state[np.newaxis, :] # add batch axis next_state = next_state[np.newaxis, :] # ========== (1) Update V network =========== target = reward + self.gamma * self.v(next_state) * (1 - done) target.unchain() v = self.v(state) loss_v = F.mean_squared_error(v, target) # ========== (2) Update pi network =========== delta = target - v delta.unchain() loss_pi = -F.log(action_prob) * delta self.v.cleargrads() self.pi.cleargrads() loss_v.backward() loss_pi.backward() self.optimizer_v.update() self.optimizer_pi.update()
def test_sgd(self): np.random.seed(0) x = np.random.rand(100, 1) y = np.sin(2 * np.pi * x) + np.random.rand(100, 1) lr = 0.2 max_iter = 10000 hidden_size = 10 model = models.MLP((hidden_size, 1)) optimizer = optimizers.SGD(lr) optimizer.setup(model) for i in range(max_iter): y_pred = model(x) loss = F.mean_squared_error(y, y_pred) model.cleargrads() loss.backward() optimizer.update() if i % 1000 == 0: print(loss)
def test_mean_squared_error_forward(self): x0 = Variable(np.array([2, 1])) x1 = Variable(np.array([1, 2])) y = F.mean_squared_error(x0, x1) assert y.data == 1
def test_backward2(self): x0 = np.random.rand(100) x1 = np.random.rand(100) f = lambda x0: F.mean_squared_error(x0, x1) self.assertTrue(gradient_check(f, x0))
def test_forward1(self): x0 = np.array([0.0, 1.0, 2.0]) x1 = np.array([0.0, 1.0, 2.0]) expected = ((x0 - x1)**2).sum() / x0.size y = F.mean_squared_error(x0, x1) self.assertTrue(array_allclose(y.data, expected))
y = self.h2y(h) return y model = SimpleRNN(hidden_size, 1) optimizer = dezero.optimizers.Adam().setup(model) # Start training. for epoch in range(max_epoch): model.reset_state() loss, count = 0, 0 for x, t in train_set: x = x[:, np.newaxis] # Add axis for batch. y = model(x) loss += F.mean_squared_error(y, t) count += 1 if count % bptt_length == 0 or count == seqlen: model.cleargrads() loss.backward() loss.unchain_backward() optimizer.update() avg_loss = float(loss.data) / count print('| epoch %d | loss %f' % (epoch + 1, avg_loss)) # Plot xs = np.cos(np.linspace(0, 4 * np.pi, 1000)) model.reset_state() pred_list = []
def style_loss(style, comb): S = gram_mat(style) C = gram_mat(comb) N, ch, H, W = style.shape return F.mean_squared_error(S, C) / (4 * (ch * W * H)**2)
def test_backward_2D(self): x0 = Variable(np.arange(10).reshape(2, 5)) x1 = np.arange(2, 12).reshape(2, 5) L = mean_squared_error(x0, x1) L.backward() assert_equal(x0.grad.data, 2 / 2 * (x0.data - x1))
def test_forward_2D(self): x0 = Variable(np.arange(10).reshape(2, 5)) x1 = np.arange(2, 12).reshape(2, 5) L = mean_squared_error(x0, x1) self.assertEqual(L.data, 20)
def test_forward(self): x0 = Variable(np.arange(10)) x1 = np.arange(2, 12) L = mean_squared_error(x0, x1) self.assertEqual(L.data, np.array(4))
def content_loss(base, comb): return F.mean_squared_error(base, comb) / 2
l2 = L.Linear(1) # ニューラルネットワークの推論 def predict(x): y = l1(x) y = F.sigmoid(y) y = l2(y) return y lr = 0.2 iters = 10000 # ニューラルネットワークの学習 for i in range(iters): y_pred = predict(x) loss = F.mean_squared_error(y, y_pred) l1.cleargrads() l2.cleargrads() loss.backward() for l in [l1, l2]: for p in l.params(): p.data -= lr*p.grad.data if i % 1000 == 0: print(loss) plt.scatter(x, y, s=10) plt.xlabel('x') plt.xlabel('y')
def test_backward1(self): x0 = np.random.rand(10) x1 = np.random.rand(10) f = lambda x0: F.mean_squared_error(x0, x1) self.assertTrue(check_backward(f, x0))