| 2744 | self.dropout_test(gpu_dev) |
| 2745 | |
| 2746 | def reduceSum_test(self, dev): |
| 2747 | shape = [3, 2, 2] |
| 2748 | cases = [(None, 1), ([1], 0), ([1], 1), ([-2], 1), ([1, 2], 1)] |
| 2749 | for axes, keepdims in cases: |
| 2750 | X = np.random.uniform(-10, 10, shape).astype(np.float32) |
| 2751 | _axes = tuple(axes) if axes is not None else None |
| 2752 | y = np.sum(X, axis=_axes, keepdims=keepdims == 1) |
| 2753 | dy = np.random.randn(*y.shape).astype(np.float32) |
| 2754 | |
| 2755 | x = tensor.from_numpy(X) |
| 2756 | dy = tensor.from_numpy(dy) |
| 2757 | x.to_device(dev) |
| 2758 | dy.to_device(dev) |
| 2759 | |
| 2760 | result = autograd.reduce_sum(x, axes, keepdims) |
| 2761 | dx = result.creator.backward(dy.data) |
| 2762 | |
| 2763 | np.testing.assert_array_almost_equal(tensor.to_numpy(result), |
| 2764 | y, |
| 2765 | decimal=5) |
| 2766 | self.check_shape(dx.shape(), tuple(shape)) |
| 2767 | |
| 2768 | def test_reduceSum_cpu(self): |
| 2769 | self.reduceSum_test(cpu_dev) |