def backward(self, gy): # TODO(Koki): This is simple implementation N, C, OH, OW = gy.shape KW, KH = pair(self.kernel_size) gy /= (KW*KH) gcol = broadcast_to(gy.reshape(-1), (KH, KW, N*C*OH*OW)) gcol = gcol.reshape(KH, KW, N, C, OH, OW).transpose(2, 3, 0, 1, 4, 5) gx = col2im(gcol, self.input_shape, self.kernel_size, self.stride, self.pad, to_matrix=False) return gx
def test_backward(self): x = Variable(np.array([1, 2, 3])) y = broadcast_to(x, (2, 3)) y.backward() self.assertEqual(x.grad.shape, (3, )) assert_equal(x.grad.data, np.array([2, 2, 2]))
def test_forward(self): x = Variable(np.array([1, 2, 3])) y = broadcast_to(x, (2, 3)) self.assertEqual(y.data.shape, (2, 3)) assert_equal(np.array([[1, 2, 3], [1, 2, 3]]), y.data)