| 3242 | self.cossim_helper(gpu_dev) |
| 3243 | |
| 3244 | def expand_helper(self, dev): |
| 3245 | shape = [3, 1] |
| 3246 | X = np.reshape(np.arange(1, np.prod(shape) + 1, dtype=np.float32), |
| 3247 | shape) |
| 3248 | x = tensor.from_numpy(X) |
| 3249 | x.to_device(dev) |
| 3250 | |
| 3251 | # dim_changed |
| 3252 | new_shape = [2, 1, 6] |
| 3253 | y_t = X * np.ones(new_shape, dtype=np.float32) |
| 3254 | dy = tensor.from_numpy(y_t) |
| 3255 | dy.to_device(dev) |
| 3256 | y = autograd.expand(x, new_shape) |
| 3257 | dx = y.creator.backward(dy.data) |
| 3258 | np.testing.assert_array_almost_equal(tensor.to_numpy(y), y_t) |
| 3259 | self.check_shape(dx.shape(), tuple(shape)) |
| 3260 | |
| 3261 | # dim_unchanged |
| 3262 | new_shape_2 = [3, 4] |
| 3263 | y_t2 = np.tile(X, 4) |
| 3264 | dy2 = tensor.from_numpy(y_t2) |
| 3265 | dy2.to_device(dev) |
| 3266 | y2 = autograd.expand(x, new_shape_2) |
| 3267 | dx2 = y2.creator.backward(dy2.data) |
| 3268 | np.testing.assert_array_almost_equal(tensor.to_numpy(y2), y_t2) |
| 3269 | self.check_shape(dx2.shape(), tuple(shape)) |
| 3270 | |
| 3271 | def test_expand_cpu(self): |
| 3272 | self.expand_helper(cpu_dev) |