Ejemplo n.º 1
0
 def test_mm_001(self):
     a1 = Tensor(np.array([[1.0], [2.0], [3.0]]), autograd=True)
     w_2 = Tensor(np.array([[11.0, 21.0, 31.0], [12.0, 22.0, 32.0],
                            [13.0, 23.0, 33.0], [14.0, 24.0, 34.0]]),
                  autograd=True)
     z2 = w_2.mm(a1)
     print('z2: {0};\r\n{1}'.format(z2.data.shape, z2))
     z2.backward(Tensor(np.ones_like(z2.data)))
     print('a1.grad: {0};'.format(a1.grad))
     print('w_2.grad: {0};'.format(w_2.grad))
Ejemplo n.º 2
0
 def test_train_nn_002(self):
     np.random.seed(0)
     data = Tensor(np.array([[0, 0], [0, 1], [1, 0], [1, 1]]),
                   autograd=True)
     target = Tensor(np.array([[0], [1], [0], [1]]), autograd=True)
     w = list()
     w.append(Tensor(np.random.rand(2, 3), autograd=True))
     w.append(Tensor(np.random.rand(3, 1), autograd=True))
     optim = OptimSgd(parameters=w, alpha=0.1)
     for i in range(10):
         pred = data.mm(w[0]).mm(w[1])
         loss = ((pred - target) * (pred - target)).sum(0)
         loss.backward(Tensor(np.ones_like(loss.data)))
         optim.step()
         print('epoch_{0}: loss={1};'.format(i, loss))
     print(loss)
Ejemplo n.º 3
0
 def test_train_nn_001(self):
     np.random.seed(0)
     data = Tensor(np.array([[0, 0], [0, 1], [1, 0], [1, 1]]),
                   autograd=True)
     target = Tensor(np.array([[0], [1], [0], [1]]), autograd=True)
     w = list()
     w.append(Tensor(np.random.rand(2, 3), autograd=True))
     w.append(Tensor(np.random.rand(3, 1), autograd=True))
     for i in range(10):
         pred = data.mm(w[0]).mm(w[1])
         loss = ((pred - target) * (pred - target)).sum(0)
         loss.backward(Tensor(np.ones_like(loss.data)))
         for w_ in w:
             w_.data -= w_.grad.data * 0.1
             w_.grad.data *= 0
         print('epoch_{0}: loss={1};'.format(i, loss))
     print(loss)