MCPcopy Create free account
hub / github.com/apache/singa / _test_scatter_elements

Method _test_scatter_elements

test/python/test_operation.py:2906–2969  ·  view source on GitHub ↗
(self, dev)

Source from the content-addressed store, hash-verified

2904 self.floor_test(gpu_dev)
2905
2906 def _test_scatter_elements(self, dev):
2907 # testing witout axis
2908 data = np.zeros((3, 3), dtype=np.float32)
2909 indices = np.array([[1, 0, 2], [0, 2, 1]], dtype=np.int32)
2910 updates = np.array([[1.0, 1.1, 1.2], [2.0, 2.1, 2.2]], dtype=np.float32)
2911 output = np.array([[2.0, 1.1, 0.0], [1.0, 0.0, 2.2], [0.0, 2.1, 1.2]],
2912 dtype=np.float32)
2913
2914 data = tensor.from_numpy(data)
2915 indices = tensor.from_numpy(indices)
2916 updates = tensor.from_numpy(updates)
2917 data.to_device(dev)
2918 indices.to_device(dev)
2919 updates.to_device(dev)
2920
2921 result = autograd.scatter_elements(data, indices, updates)
2922 dy = tensor.from_numpy(np.ones(data.shape, dtype=np.float32))
2923 dx = result.creator.backward(dy.data)
2924 np.testing.assert_almost_equal(tensor.to_numpy(result),
2925 output,
2926 decimal=5)
2927 self.check_shape(dx.shape(), data.shape)
2928
2929 # testing with axis
2930 data = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)
2931 indices = np.array([[1, 3]], dtype=np.int32)
2932 updates = np.array([[1.1, 2.1]], dtype=np.float32)
2933 output = np.array([[1.0, 1.1, 3.0, 2.1, 5.0]], dtype=np.float32)
2934
2935 data = tensor.from_numpy(data)
2936 indices = tensor.from_numpy(indices)
2937 updates = tensor.from_numpy(updates)
2938 data.to_device(dev)
2939 indices.to_device(dev)
2940 updates.to_device(dev)
2941
2942 result = autograd.scatter_elements(data, indices, updates, axis=1)
2943 dy = tensor.from_numpy(np.ones(data.shape, dtype=np.float32))
2944 dx = result.creator.backward(dy.data)
2945 np.testing.assert_almost_equal(tensor.to_numpy(result),
2946 output,
2947 decimal=5)
2948 self.check_shape(dx.shape(), data.shape)
2949
2950 # testing with negative indices:
2951 data = np.array([[1.0, 2.0, 3.0, 4.0, 5.0]], dtype=np.float32)
2952 indices = np.array([[1, -3]], dtype=np.int64)
2953 updates = np.array([[1.1, 2.1]], dtype=np.float32)
2954 output = np.array([[1.0, 1.1, 2.1, 4.0, 5.0]], dtype=np.float32)
2955
2956 data = tensor.from_numpy(data)
2957 indices = tensor.from_numpy(indices)
2958 updates = tensor.from_numpy(updates)
2959 data.to_device(dev)
2960 indices.to_device(dev)
2961 updates.to_device(dev)
2962
2963 result = autograd.scatter_elements(data, indices, updates, axis=1)

Callers 2

Calls 4

check_shapeMethod · 0.95
shapeMethod · 0.80
to_deviceMethod · 0.45
backwardMethod · 0.45

Tested by

no test coverage detected