本文整理匯總了Python中theano.tensor.verify_grad方法的典型用法代碼示例。如果您正苦於以下問題:Python tensor.verify_grad方法的具體用法?Python tensor.verify_grad怎麽用?Python tensor.verify_grad使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類theano.tensor
的用法示例。
在下文中一共展示了tensor.verify_grad方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_cmrnorm
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import verify_grad [as 別名]
def test_cmrnorm():
from theano.tests.unittest_tools import verify_grad
xtest = np.random.rand(2,8,3,4)
xtest = xtest.astype(theano.config.floatX)
x = T.tensor4('x', dtype=theano.config.floatX)
x.tag.test_value = xtest
y = cmrnorm(x, input_shape=xtest.shape[1:])
f = theano.function([x], y, mode='DEBUG_MODE')
f(xtest)
f = theano.function([x], gpu_from_host(T.grad(T.sum(y), wrt=x)),
mode='DEBUG_MODE')
f(xtest)
theano.printing.debugprint(f)
T.verify_grad(lambda x: cmrnorm(x, input_shape=xtest.shape[1:]),
(xtest,),
rng=np.random.RandomState(0))
print 'cmrnorm passed'
示例2: test0
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import verify_grad [as 別名]
def test0(self):
n_classes = 5
n_samples = 3
# First test gradient when getting a gradient on the NLL output.
def grad_on_nll_dtype(dtype):
def grad_on_nll(x, b):
y_idx = numpy.random.randint(low=0, high=n_classes, size=n_samples).astype(dtype)
return self.op(x, b, y_idx=y_idx)[0]
return grad_on_nll
for dtype in ['uint8', 'int8', 'uint64', 'int64']:
utt.verify_grad(grad_on_nll_dtype(dtype),
[numpy.random.rand(n_samples, n_classes),
numpy.random.rand(n_classes)])
# Then test gradient when getting a gradient on the softmax output.
def grad_on_softmax(x, b):
return self.op(x, b, y_idx=numpy.random.randint(
low=0, high=n_classes, size=n_samples))[1]
utt.verify_grad(grad_on_softmax,
[numpy.random.rand(n_samples, n_classes),
numpy.random.rand(n_classes)])
示例3: test_cholesky_grad
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import verify_grad [as 別名]
def test_cholesky_grad():
if not imported_scipy:
raise SkipTest("Scipy needed for the Cholesky op.")
rng = numpy.random.RandomState(utt.fetch_seed())
r = rng.randn(5, 5).astype(config.floatX)
pd = numpy.dot(r, r.T)
eps = None
if config.floatX == "float64":
eps = 2e-8
# Check the default.
yield (lambda: utt.verify_grad(cholesky, [pd], 3, rng, eps=eps))
# Explicit lower-triangular.
yield (lambda: utt.verify_grad(Cholesky(lower=True), [pd], 3,
rng, eps=eps))
# Explicit upper-triangular.
yield (lambda: utt.verify_grad(Cholesky(lower=False), [pd], 3,
rng, eps=eps))
示例4: test_other_grad_tests
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import verify_grad [as 別名]
def test_other_grad_tests(self):
x = theano.tensor.dmatrix()
x_val1 = numpy.array([[1, 2, 3], [0, 5, 6], [0, 0, 9]],
dtype='float32')
x_val2 = numpy.array([[1, 2, 0], [0, 5, 6], [7, 8, 9], [9, 10, 0]],
dtype='float32')
rng = rng = numpy.random.RandomState(43)
p = Prod(axis=1)
grad_p = theano.tensor.grad(p(x).sum(), x)
grad_fn = theano.function([x], grad_p, mode=self.mode)
assert numpy.allclose(grad_fn(x_val1), [[6., 3., 2.], [30., 0.,
0.], [0., 0., 0.]])
assert numpy.allclose(grad_fn(x_val2), [[0., 0., 2.], [30.,
0., 0.], [72., 63., 56.], [0., 0., 90.]])
p_axis0 = Prod(axis=0)
grad_p_axis0 = theano.tensor.grad(p_axis0(x).sum(), x)
grad_fn_axis0 = theano.function([x], grad_p_axis0, mode=self.mode)
assert numpy.allclose(grad_fn_axis0(x_val2), [[0., 400.,
0.], [63., 160., 0.], [0., 100., 0.], [0., 80., 0.]])
tensor.verify_grad(p, [x_val1], rng=rng, mode=self.mode)
示例5: verify_grad
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import verify_grad [as 別名]
def verify_grad(op, pt, n_tests=2, rng=None, *args, **kwargs):
"""
Wrapper for tensor/basic.py:verify_grad
Takes care of seeding the random number generator if None is given
"""
if rng is None:
seed_rng()
rng = numpy.random
T.verify_grad(op, pt, n_tests, rng, *args, **kwargs)
#
# This supports the following syntax:
#
# try:
# verify_grad(...)
# except verify_grad.E_grad, e:
# print e.num_grad.gf
# print e.analytic_grad
# raise
#
示例6: test_elemwise
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import verify_grad [as 別名]
def test_elemwise(self):
utt.verify_grad(sigmoid, [numpy.random.rand(3, 4)])
示例7: test1
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import verify_grad [as 別名]
def test1(self):
def f(a):
return softmax_op(a)[:, 1]
utt.verify_grad(f, [numpy.random.rand(3, 4)])
示例8: test2
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import verify_grad [as 別名]
def test2(self):
def f(a):
return softmax_op(a)[:, 2]
utt.verify_grad(f, [numpy.random.rand(3, 4)])
示例9: test_vector_grad
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import verify_grad [as 別名]
def test_vector_grad(self):
def f(a):
return softmax_op(a)
utt.verify_grad(f, [numpy.random.rand(4)])
示例10: test3
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import verify_grad [as 別名]
def test3(self):
def f(a, b):
return softmax_with_bias(a, b)[:, 3]
utt.verify_grad(f, [numpy.random.rand(3, 4),
numpy.random.rand(4)])
示例11: test_matrix
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import verify_grad [as 別名]
def test_matrix(self):
def f(a):
return logsoftmax_op(a)
utt.verify_grad(f, [numpy.random.rand(3, 4)])
示例12: test_vector
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import verify_grad [as 別名]
def test_vector(self):
y_idx = [3]
def f(a):
return crossentropy_softmax_1hot(T.shape_padleft(a), y_idx)[0]
utt.verify_grad(f, [numpy.random.rand(4)])
示例13: test_vectors
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import verify_grad [as 別名]
def test_vectors(self):
y_idx = [3]
def f(a, b):
return crossentropy_softmax_1hot(T.shape_padleft(a) + b, y_idx)[0]
utt.verify_grad(f, [numpy.random.rand(4), numpy.random.rand(4)])
示例14: test_grad
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import verify_grad [as 別名]
def test_grad(self):
x = tensor.matrix('x')
one_of_n = tensor.lvector('one_of_n')
op = crossentropy_categorical_1hot
xe = op(x, one_of_n)
f = theano.function([x, one_of_n], xe)
x_val = numpy.asarray([[.4, .6, .0], [.1, .8, .1]],
dtype=config.floatX)
xe_val = f(x_val, [0, 1])
assert numpy.allclose(xe_val, -numpy.log([.4, .8]))
def oplike(x):
return op(x, [0, 1])
tensor.verify_grad(oplike, [x_val], rng=numpy.random)