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

Method _Sub_helper

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

Source from the content-addressed store, hash-verified

1154 self._Less_helper(gpu_dev)
1155
1156 def _Sub_helper(self, dev):
1157 X0 = np.array([7, -5, 0.2, -0.1, 0.3, 4]).reshape(3,
1158 2).astype(np.float32)
1159 X1 = np.array([0.6, -1.3, 0.1, -0.1, 0.4,
1160 0.3]).reshape(3, 2).astype(np.float32)
1161 XT = np.subtract(X0, X1)
1162
1163 DY = np.ones((3, 2), dtype=np.float32)
1164 x0 = tensor.from_numpy(X0)
1165 x1 = tensor.from_numpy(X1)
1166 dy = tensor.from_numpy(DY)
1167 x0.to_device(dev)
1168 x1.to_device(dev)
1169 dy.to_device(dev)
1170
1171 result = autograd.sub(x0, x1)
1172 dx0, dx1 = result.creator.backward(dy.data)
1173
1174 DX0 = np.multiply(DY, 1.0)
1175 DX1 = np.multiply(DY, -1.0)
1176
1177 np.testing.assert_array_almost_equal(tensor.to_numpy(result),
1178 XT,
1179 decimal=5)
1180 np.testing.assert_array_almost_equal(tensor.to_numpy(
1181 tensor.from_raw_tensor(dx0)),
1182 DX0,
1183 decimal=5)
1184 np.testing.assert_array_almost_equal(tensor.to_numpy(
1185 tensor.from_raw_tensor(dx1)),
1186 DX1,
1187 decimal=5)
1188
1189 def test_Sub_cpu(self):
1190 self._Sub_helper(cpu_dev)

Callers 2

test_Sub_cpuMethod · 0.95
test_Sub_gpuMethod · 0.95

Calls 3

reshapeMethod · 0.45
to_deviceMethod · 0.45
backwardMethod · 0.45

Tested by

no test coverage detected