def test_sum(self):
        x = Variable(np.array([[1, 2, 3], [4, 5, 6]]))
        y = x.sum()
        y.backward()

        self.assertEqual(y.data, 21)
        self.assertTrue(
            np.allclose(x.grad.data, np.array([[1, 1, 1], [1, 1, 1]])))
        self.assertTrue(np.allclose(x.sum(axis=0).data, np.array([5, 7, 9])))
        self.assertTrue(np.allclose(x.sum(axis=1).data, np.array([6, 15])))
        self.assertTrue(
            np.allclose(
                x.sum(axis=0, keepdims=True).data, np.array([[5, 7, 9]])))
        self.assertTrue(
            np.allclose(
                x.sum(axis=1, keepdims=True).data, np.array([[6], [15]])))
예제 #2
0
if '__file__' in globals():
    import os, sys
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import numpy as np
from dezero import Variable
import dezero.functions as F

x = Variable(np.array([1, 2, 3, 4, 5, 6]))
y = F.sum(x)
y.backward()
print(y)
print(x.grad)

x = Variable(np.array([[1, 2, 3], [4, 5, 6]]))
y = F.sum(x)
y.backward()
print(y)
print(x.grad)

x = Variable(np.array([[1, 2, 3], [4, 5, 6]]))
y = F.sum(x, axis=0)
y.backward()
print(y)
print(x.grad)

x = Variable(np.random.randn(2, 3, 4, 5))
y = x.sum(keepdims=True)
print(y.shape)
예제 #3
0
if '__file__' in globals():
    import os
    import sys
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
    import numpy as np
    import matplotlib.pyplot as plt
    from dezero import Variable
    from dezero import setup_variable
    from dezero.utils import plot_dot_graph
    import dezero.functions as F
setup_variable()

if __name__ == '__main__':
    x = Variable(np.array([[1, 2, 3], [4, 5, 6]]))
    y = F.sum(x, axis=0)
    y.backward()
    print(y)
    print(x.grad)

    z = Variable(np.random.randn(2, 3, 4, 5))
    v = z.sum(keepdims=True)
    print(v.shape)