コード例 #1
0
    def test_isub(self):
        t1 = tensor.Tensor([[1., 2, 3], [4, 5, 6]], requires_grad=True)  #(2,3)
        t2 = tensor.Tensor([[7, 8, 9]], requires_grad=True)  #(1,3)

        t1 -= t2
        assert t1.data.shape == (2, 3)
        assert t1.grad is None
        assert t1.data.tolist() == [[-6, -6, -6], [-3, -3, -3]]
コード例 #2
0
    def test_iadd(self):
        t1 = tensor.Tensor([[1., 2, 3], [4, 5, 6]], requires_grad=True)  #(2,3)
        t2 = tensor.Tensor([[7, 8, 9]], requires_grad=True)  #(1,3)

        t1 += t2
        assert t1.data.shape == (2, 3)
        assert t1.grad is None
        assert t1.data.tolist() == [[8, 10, 12], [11, 13, 15]]
コード例 #3
0
    def test_imul(self):
        t1 = tensor.Tensor([[1., 2, 3], [4, 5, 6]], requires_grad=True)  #(2,3)
        t2 = tensor.Tensor([[7, 8, 9]], requires_grad=True)  #(1,3)

        t1 *= t2
        assert t1.data.shape == (2, 3)
        assert t1.grad is None
        assert t1.data.tolist() == [[7, 16, 27], [28, 40, 54]]
コード例 #4
0
    def test_broadcast_sub2(self):
        t1 = tensor.Tensor([[1., 2, 3], [4, 5, 6]], requires_grad=True)  #(2,3)
        t2 = tensor.Tensor([[7, 8, 9]], requires_grad=True)  #(1,3)

        # t3 = tensor.sub(t1, t2)
        t3 = t1 - t2
        t3.backward(tensor.Tensor([[1, 1, 1], [1, 1, 1]]))

        assert t3.data.shape == (2, 3)
        assert t3.data.tolist() == [[-6, -6, -6], [-3, -3, -3]]
        assert t1.grad.data.tolist() == [[1, 1, 1], [1, 1, 1]]
        assert t2.grad.data.tolist() == [[-2, -2, -2]]
コード例 #5
0
    def test_broadcast_add2(self):
        t1 = tensor.Tensor([[1., 2, 3], [4, 5, 6]], requires_grad=True)  #(2,3)
        t2 = tensor.Tensor([[7, 8, 9]], requires_grad=True)  #(1,3)

        # t3 = tensor.add(t1, t2)
        t3 = t1 + t2
        t3.backward(tensor.Tensor([[1, 1, 1], [1, 1, 1]]))

        assert t3.data.shape == (2, 3)
        assert t3.data.tolist() == [[8, 10, 12], [11, 13, 15]]
        assert t1.grad.data.tolist() == [[1, 1, 1], [1, 1, 1]]
        assert t2.grad.data.tolist() == [[2, 2, 2]]
コード例 #6
0
    def test_simple_mul(self):
        t1 = tensor.Tensor([1, 2, 3], requires_grad=True)
        t2 = tensor.Tensor([4, 5, 6], requires_grad=True)

        # t3 = autograd.tensor.Tensor([])
        t4 = t1 * t2

        t4.backward(tensor.Tensor([-1, -2, -3]))

        assert t4.data.tolist() == [4, 10, 18]
        assert t1.grad.data.tolist() == [-4, -10, -18]
        assert t2.grad.data.tolist() == [-1, -4, -9]
コード例 #7
0
    def test_broadcast_mul2(self):
        t1 = tensor.Tensor([[1., 2, 3], [4, 5, 6]], requires_grad=True)  #(2,3)
        t2 = tensor.Tensor([[7, 8, 9]], requires_grad=True)  #(1,3)

        # t3 = tensor.mul(t1, t2)
        t3 = t1 * t2
        t3.backward(tensor.Tensor([[1, 1, 1], [1, 1, 1]]))

        assert t3.data.shape == (2, 3)
        assert t3.data.tolist() == [[7, 16, 27], [28, 40, 54]]
        assert t1.grad.data.tolist() == [[7, 8, 9], [7, 8, 9]]
        assert t2.grad.data.tolist() == [[5, 7, 9]]
コード例 #8
0
    def test_simple_sub(self):
        t1 = tensor.Tensor([1, 2, 3], requires_grad=True)
        t2 = tensor.Tensor([4, 5, 6], requires_grad=True)

        # t3 = autograd.tensor.Tensor([])
        # t4 = tensor.sub(t1,t2)

        t4 = t1 - t2

        t4.backward(tensor.Tensor([-1, -2, -3]))
        assert t4.data.tolist() == [-3, -3, -3]
        assert t1.grad.data.tolist() == [-1, -2, -3]
        assert t2.grad.data.tolist() == [1, 2, 3]
コード例 #9
0
    def test_simple_sum(self):
        t1 = tensor.Tensor([1.0 ,2.4, 3], requires_grad=True)
        t2 = t1.sum()
        
        t2.backward()

        assert t1.grad is not None