def autograd_assert(*args, **kwargs): f = kwargs["func"] grad_f = kwargs["grad_func"] grad_func = grad_and_loss(f) grad_vals, output = grad_func(*args) res = f(*args) assert same(output.asnumpy(), res.asnumpy()) grad_res = grad_f(*args) assert len(grad_vals) == len(grad_res) for a, b in zip(grad_vals, grad_res): assert same(a.asnumpy(), b.asnumpy())
def autograd_assert(*args, **kwargs): f = kwargs["func"] grad_f = kwargs["grad_func"] grad_func = grad_and_loss(f) grad_vals, output = grad_func(*args) res = f(*args) assert output == res grad_res = grad_f(*args) assert len(grad_vals) == len(grad_res) for a, b in zip(grad_vals, grad_res): assert a == b
def autograd_assert(*args, **kwargs): func = kwargs["func"] grad_f = kwargs["grad_func"] argnum = kwargs["argnum"] if 'argnum' in kwargs else None grad_func = grad_and_loss(func, argnum) grad_vals, output = grad_func(*args) res = func(*args) assert same(output.asnumpy(), res.asnumpy()) grad_res = grad_f(*args) assert len(grad_vals) == len(grad_res) for a, b in zip(grad_vals, grad_res): assert same(a.asnumpy(), b.asnumpy())
def test_operator_with_state(): def f_fc(a, b, weight, bias): x = a * b fc = nd.FullyConnected(x, weight, bias, num_hidden=32) return fc a = nd.uniform(shape=(64, 50)) b = nd.uniform(shape=(64, 50)) weight = nd.uniform(shape=(32, 50)) bias = nd.uniform(shape=(32, )) grad_func = grad_and_loss(f_fc) grad_vals, outputs = grad_func(a, b, weight, bias)
def test_operator_with_state(): def f_fc(a, b, weight, bias): x = a*b fc = nd.FullyConnected( x, weight, bias, num_hidden=32) return fc a = nd.uniform(shape=(64, 50)) b = nd.uniform(shape=(64, 50)) weight = nd.uniform(shape=(32, 50)) bias = nd.uniform(shape=(32, )) grad_func = grad_and_loss(f_fc) grad_vals, outputs = grad_func(a, b, weight, bias)