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

Method where_helper

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

Source from the content-addressed store, hash-verified

3451 self.assertRaises(AssertionError, autograd.ranking_loss, _2d, _1d)
3452
3453 def where_helper(self, dev):
3454 X = np.array([[1, 2], [3, 4]], dtype=np.float32)
3455 x = tensor.from_numpy(X)
3456 x.to_device(dev)
3457
3458 X2 = np.array([[9, 8], [7, 6]], dtype=np.float32)
3459 x2 = tensor.from_numpy(X2)
3460 x2.to_device(dev)
3461
3462 condition = [[True, False], [True, True]]
3463 y_t = np.where(condition, X, X2)
3464 dx1_t = np.array([[1, 0], [3, 4]], dtype=np.float32)
3465 dx2_t = np.array([[0, 8], [0, 0]], dtype=np.float32)
3466 dy = tensor.from_numpy(y_t)
3467 dy.to_device(dev)
3468
3469 y = autograd.where(x, x2, condition)
3470 dx1, dx2 = y.creator.backward(dy.data)
3471 np.testing.assert_array_almost_equal(tensor.to_numpy(y), y_t)
3472 np.testing.assert_array_almost_equal(
3473 tensor.to_numpy(tensor.from_raw_tensor(dx1)), dx1_t)
3474 np.testing.assert_array_almost_equal(
3475 tensor.to_numpy(tensor.from_raw_tensor(dx2)), dx2_t)
3476
3477 def test_where_cpu(self):
3478 self.where_helper(cpu_dev)

Callers 2

test_where_cpuMethod · 0.95
test_where_gpuMethod · 0.95

Calls 2

to_deviceMethod · 0.45
backwardMethod · 0.45

Tested by

no test coverage detected