def test_prod(): inp = np.ones((2, INT_OVERFLOW)) inp[0, 0], inp[-1, -1] = 2, 10 inp.attach_grad() with mx.autograd.record(): out1 = np.prod(inp, axis=1) out1.backward() assert out1.shape == (2, ) assert out1[0] == 2 and out1[1] == 10 assert inp.grad.shape == inp.shape assert inp.grad[-1, -1] == 1 with mx.autograd.record(): out2 = np.sum(inp, axis=0) out2.backward() assert out2.shape == (INT_OVERFLOW, ) assert out2[0] == 2 and out2[-1] == 10 assert inp.grad.shape == inp.shape
def test_np_prod(): class TestProd(HybridBlock): def __init__(self, axis=None, dtype=None, keepdims=False): super(TestProd, self).__init__() self._axis = axis self._dtype = dtype self._keepdims = keepdims def hybrid_forward(self, F, a, *args, **kwargs): return F.np.prod(a, axis=self._axis, dtype=self._dtype, keepdims=self._keepdims) in_data_dim = random.choice([3, 4]) shape = rand_shape_nd(in_data_dim, dim=3) for hybridize in [False, True]: for keepdims in [True, False]: for axis in ([i for i in range(in_data_dim)] + [(), None]): for itype in ['float32', 'float64']: for dtype in ['float32', 'float64']: # test gluon test_prod = TestProd(axis=axis, dtype=dtype, keepdims=keepdims) if hybridize: test_prod.hybridize() x = np.array(_np.random.uniform(-2.0, 2.0, size=shape), dtype=itype) x.attach_grad() print(x.grad.dtype) expected_ret = _np.prod(x.asnumpy(), axis=axis, keepdims=keepdims) expected_ret = expected_ret.astype(dtype) with mx.autograd.record(): y = test_prod(x) assert y.shape == expected_ret.shape assert_almost_equal(y.asnumpy(), expected_ret, rtol=1e-3, atol=1e-5, use_broadcast=False) y.backward() # use keepdims=True so that broadcast divide can be used to calculate # grad of input expected_ret = _np.prod(x.asnumpy(), axis=axis, keepdims=True) assert_almost_equal(x.grad.asnumpy(), expected_ret / x.asnumpy(), rtol=1e-3, atol=1e-3, use_broadcast=False) # test numeric if itype == 'float32' and dtype == 'float32': x_sym = mx.sym.Variable("x").as_np_ndarray() mx_sym = mx.sym.np.prod( x_sym, axis=axis, dtype=dtype, keepdims=keepdims).as_nd_ndarray() check_numeric_gradient(mx_sym, [x.as_nd_ndarray()], numeric_eps=1e-3, rtol=1e-3, atol=1e-4, dtype=_np.float32) # test imperative mx_out = np.prod(x, axis=axis, dtype=dtype, keepdims=keepdims) np_out = _np.prod(x.asnumpy(), axis=axis, keepdims=keepdims).astype(dtype) assert_almost_equal(mx_out.asnumpy(), np_out, rtol=1e-3, atol=1e-5, use_broadcast=False)
train_acc_sum = sum( d2l.accuracy(py.asnumpy(), y.asnumpy()) for py, y in zip(pys, ys)) l, acc = train_loss_sum, train_acc_sum metric.add(l, acc, ys_in.shape[0], ys_in.size) timer.stop() if (i + 1) % (num_batches // 5) == 0: animator.add( epoch + i / num_batches, (metric[0] / metric[2], metric[1] / metric[3], None, None)) # val_acc = d2l.evaluate_accuracy_gpus(net, val_iter, split_f) metric_val = d2l.Accumulator(2) # num_corrected_examples, num_examples for i, (Xs_in, ys_in) in enumerate(DataLoader_Single_test): Xs = gluon.utils.split_and_load(Xs_in.astype("float32"), ctx) ys = gluon.utils.split_and_load(ys_in.astype("float32"), ctx) pys = [net(X) for X in Xs] ls = [loss(py, y) for py, y in zip(pys, ys)] val_loss_sum = sum([float(l.sum().asnumpy()[0]) for l in ls]) OA_val = np.sum( np.argmax(pys[0].asnumpy(), axis=1) == ys[0].asnumpy()).astype( "float32") / np.prod(ys[0].shape) metric_val.add(OA_val, len(ys)) val_acc = OA_val animator.add(epoch + 1, (None, None, val_loss_sum / ys_in.shape[0], val_acc)) print('loss %.3f, train acc %.3f, val acc %.3f' % (metric[0] / metric[2], metric[1] / metric[3], val_acc)) print('%.1f examples/sec on %s' % (metric[2] * num_epochs / timer.sum(), d2l.try_all_gpus()))