def forward(self, x): self.x_shape = x.shape y = utils.sum_to(x, self.shape) return y
def backward(self, gy): gx = utils.sum_to(gy, self.x_shape) return gx
def test_sum_to(self): y = np.ones((3, 4, 5, 6)) self.assertEqual(sum_to(y, (5, 6)).shape, (5, 6)) self.assertEqual(sum_to(y, (5, 1)).shape, (5, 1))
def forward(self, x): y = utils.sum_to(x, self.shape) return y
def forward(self, *xs: np.ndarray) -> tuple[np.ndarray, ...]: x, = xs self.x_shape = x.shape y = utils.sum_to(x, self.shape) return y,
if '__file__' in globals(): import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import numpy as np from dezero.utils import sum_to x = np.array([[1, 2, 3], [4, 5, 6]]) y = sum_to(x, (1, 3)) print(y) y = sum_to(x, (2, 1)) print(y)