本文整理匯總了Python中theano.tensor.dmatrix方法的典型用法代碼示例。如果您正苦於以下問題:Python tensor.dmatrix方法的具體用法?Python tensor.dmatrix怎麽用?Python tensor.dmatrix使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類theano.tensor
的用法示例。
在下文中一共展示了tensor.dmatrix方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_max_pool_2d_2D
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def test_max_pool_2d_2D(self):
rng = numpy.random.RandomState(utt.fetch_seed())
maxpoolshps = ((1, 1), (3, 2))
imval = rng.rand(4, 5)
images = tensor.dmatrix()
for maxpoolshp, ignore_border, mode in product(maxpoolshps,
[True, False],
['max', 'sum',
'average_inc_pad',
'average_exc_pad']):
# print 'maxpoolshp =', maxpoolshp
# print 'ignore_border =', ignore_border
numpy_output_val = self.numpy_max_pool_2d(imval, maxpoolshp,
ignore_border,
mode=mode)
output = pool_2d(images, maxpoolshp, ignore_border,
mode=mode)
output_val = function([images], output)(imval)
utt.assert_allclose(output_val, numpy_output_val)
def mp(input):
return pool_2d(input, maxpoolshp, ignore_border,
mode=mode)
utt.verify_grad(mp, [imval], rng=rng)
示例2: test_dot22scalar_cast
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def test_dot22scalar_cast():
"""
Test that in `dot22_to_dot22scalar` we properly cast integers to floats.
"""
# Note that this test was failing before d5ff6904.
A = T.dmatrix()
for scalar_int_type in T.int_dtypes:
y = T.scalar(dtype=scalar_int_type)
f = theano.function([A, y], T.dot(A, A) * y, mode=mode_blas_opt)
assert _dot22scalar in [x.op for x in f.maker.fgraph.toposort()]
A = T.fmatrix()
for scalar_int_type in T.int_dtypes:
y = T.scalar(dtype=scalar_int_type)
f = theano.function([A, y], T.dot(A, A) * y, mode=mode_blas_opt)
if scalar_int_type in ['int32', 'int64']:
assert _dot22 in [x.op for x in f.maker.fgraph.toposort()]
else:
assert _dot22scalar in [x.op for x in f.maker.fgraph.toposort()]
示例3: setUp
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def setUp(self):
self.iv = T.tensor(dtype='int32', broadcastable=(False,))
self.fv = T.tensor(dtype='float32', broadcastable=(False,))
self.fv1 = T.tensor(dtype='float32', broadcastable=(True,))
self.dv = T.tensor(dtype='float64', broadcastable=(False,))
self.dv1 = T.tensor(dtype='float64', broadcastable=(True,))
self.cv = T.tensor(dtype='complex64', broadcastable=(False,))
self.zv = T.tensor(dtype='complex128', broadcastable=(False,))
self.fv_2 = T.tensor(dtype='float32', broadcastable=(False,))
self.fv1_2 = T.tensor(dtype='float32', broadcastable=(True,))
self.dv_2 = T.tensor(dtype='float64', broadcastable=(False,))
self.dv1_2 = T.tensor(dtype='float64', broadcastable=(True,))
self.cv_2 = T.tensor(dtype='complex64', broadcastable=(False,))
self.zv_2 = T.tensor(dtype='complex128', broadcastable=(False,))
self.fm = T.fmatrix()
self.dm = T.dmatrix()
self.cm = T.cmatrix()
self.zm = T.zmatrix()
self.fa = T.fscalar()
self.da = T.dscalar()
self.ca = T.cscalar()
self.za = T.zscalar()
示例4: test_infer_shape
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def test_infer_shape(self):
a = tensor.dvector()
self._compile_and_check([a], [self.op(a, 16, 0)],
[numpy.random.rand(12)],
self.op_class)
a = tensor.dmatrix()
for var in [self.op(a, 16, 1), self.op(a, None, 1),
self.op(a, 16, None), self.op(a, None, None)]:
self._compile_and_check([a], [var],
[numpy.random.rand(12, 4)],
self.op_class)
b = tensor.iscalar()
for var in [self.op(a, 16, b), self.op(a, None, b)]:
self._compile_and_check([a, b], [var],
[numpy.random.rand(12, 4), 0],
self.op_class)
示例5: test_adv_sub
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def test_adv_sub(self):
admat = dmatrix()
aivec = lvector()
bivec = lvector()
admat_val = rand(5, 4)
aivec_val = [1, 3, 2]
bivec_val = [0, 3, 3]
self._compile_and_check([admat, aivec, bivec],
[admat[aivec, bivec]],
[admat_val, aivec_val, bivec_val], AdvancedSubtensor)
# Test case that aren't implemented, but make sure they do not crash.
self._compile_and_check([admat, aivec],
[admat[aivec, 1:3]],
[admat_val, aivec_val], AdvancedSubtensor,
check_topo=False)
self._compile_and_check([admat, aivec],
[admat[1:3, aivec]],
[admat_val, aivec_val], AdvancedSubtensor,
check_topo=False)
示例6: test_borrow_input
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def test_borrow_input(self):
"""
Tests that the contract for io.In is respected. When borrow=False, it should be
impossible for outputs to be aliased to the input variables provided by the user,
either through a view-map or a destroy map. New tests should be added in the future
when borrow=True is implemented.
"""
a = T.dmatrix()
aval = numpy.random.rand(3, 3)
# when borrow=False, test that a destroy map cannot alias output to input
f = theano.function([In(a, borrow=False)], Out(a + 1, borrow=True))
assert numpy.all(f(aval) == aval + 1)
assert not numpy.may_share_memory(aval, f(aval))
# when borrow=False, test that a viewmap cannot alias output to input
f = theano.function([In(a, borrow=False)], Out(a[0, :], borrow=True))
assert numpy.all(f(aval) == aval[0, :])
assert not numpy.may_share_memory(aval, f(aval))
示例7: test_borrow_output
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def test_borrow_output(self):
a = T.dmatrix()
f = function([a], Out(a, borrow=False))
o = N.ones((3, 3))
assert o is not f(o) # function no longer permits aliasing outputs to inputs
f = function([a], Out(a * 4, borrow=False))
o = N.ones((3, 3))
four = f(o)
assert numpy.all(four == 4)
f(o + .1) # should not clobber the memory used to store four
assert numpy.all(four == 4)
f = function([a], Out(a * 4, borrow=True), mode=theano.Mode('c|py_nogc', 'fast_run'))
o = N.ones((3, 3))
four = f(o)
assert numpy.all(four == 4)
f(o + .1) # should clobber the memory used to store four
if theano.config.cxx:
assert not numpy.all(four == 4)
else:
# The Elemwise.perform method don't reuse memory
# as some numpy version don't support that correctly.
assert numpy.all(four == 4)
示例8: test_shared
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def test_shared(self):
# CHECK: two functions (f1 and f2) can share w
w = shared(numpy.random.rand(2, 2), 'w')
wval = w.get_value(borrow=False)
x = dmatrix()
out1 = w + x
out2 = w * x
f1 = pfunc([x], [out1])
f2 = pfunc([x], [out2])
xval = numpy.random.rand(2, 2)
assert numpy.all(f1(xval) == xval + wval)
assert numpy.all(f2(xval) == xval * wval)
# CHECK: updating a shared value
f3 = pfunc([x], out1, updates=[(w, (w - 1))])
# f3 changes the value of w
assert numpy.all(f3(xval) == xval + wval)
# this same value is read by f1
assert numpy.all(f1(xval) == xval + (wval - 1))
w.set_value(w.get_value(borrow=True) * 10, borrow=True)
# this same value is read by f1
assert numpy.all(f1(xval) == xval + w.get_value(borrow=True))
示例9: test_infer_shape
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def test_infer_shape(self):
x = dmatrix('x')
x.tag.test_value = np.zeros((2, 2))
y = dvector('y')
y.tag.test_value = [0, 0]
def infer_shape(node, shapes):
x, y = shapes
return [y]
@as_op([dmatrix, dvector], dvector, infer_shape)
def cumprod_plus(x, y):
return np.cumprod(x) + y
self._compile_and_check([x, y], [cumprod_plus(x, y)],
[[[1.5, 5], [2, 2]], [1, 100, 2, 200]],
cumprod_plus.__class__, warn=False)
示例10: test_no_make_node
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def test_no_make_node(self):
class DoubleOp(Op):
"""An Op without make_node"""
__props__ = ()
itypes = [T.dmatrix]
otypes = [T.dmatrix]
def perform(self, node, inputs, outputs):
inp = inputs[0]
output = outputs[0]
output[0] = inp * 2
x_input = T.dmatrix('x_input')
f = theano.function([x_input], DoubleOp()(x_input))
inp = numpy.random.rand(5, 4)
out = f(inp)
assert numpy.allclose(inp * 2, out)
示例11: __init__
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def __init__(self, nfeatures=100, noutputs=10, nhiddens=50, rng=None):
if rng is None:
rng = 0
if isinstance(rng, integer_types):
rng = np.random.RandomState(rng)
self.rng = rng
self.nfeatures = nfeatures
self.noutputs = noutputs
self.nhiddens = nhiddens
x = T.dmatrix('x')
wh = th.shared(self.rng.normal(0, 1, (nfeatures, nhiddens)),
borrow=True)
bh = th.shared(np.zeros(nhiddens), borrow=True)
h = T.nnet.sigmoid(T.dot(x, wh) + bh)
wy = th.shared(self.rng.normal(0, 1, (nhiddens, noutputs)))
by = th.shared(np.zeros(noutputs), borrow=True)
y = T.nnet.softmax(T.dot(h, wy) + by)
self.inputs = [x]
self.outputs = [y]
示例12: test_borrow_input
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def test_borrow_input(self):
"""
Tests that the contract for io.In is respected. When borrow=False, it should be
impossible for outputs to be aliased to the input variables provided by the user,
either through a view-map or a destroy map. New tests should be added in the future
when borrow=True is implemented.
"""
a = T.dmatrix()
aval = numpy.random.rand(3, 3)
# when borrow=False, test that a destroy map cannot alias output to input
f = theano.function([In(a, borrow=False)], Out(a+1, borrow=True))
assert numpy.all(f(aval) == aval+1)
assert not numpy.may_share_memory(aval, f(aval))
# when borrow=False, test that a viewmap cannot alias output to input
f = theano.function([In(a, borrow=False)], Out(a[0, :], borrow=True))
assert numpy.all(f(aval) == aval[0, :])
assert not numpy.may_share_memory(aval, f(aval))
示例13: test_incsubtensor_mixed
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def test_incsubtensor_mixed():
# This catches a bug that occurred when incrementing
# a float32 tensor by a float64 tensor.
# The result is defined to be float32, so it is OK
# to downcast the float64 increment in order to
# transfer it to the GPU.
# The bug was that the optimization called GpuFromHost
# without casting first, causing the optimization to
# fail.
X = tensor.fmatrix()
Y = tensor.dmatrix()
Z = tensor.inc_subtensor(X[0:1, 0:1], Y)
f = theano.function([X, Y], Z, mode=mode_with_gpu)
packed, = f.maker.fgraph.inputs[1].clients
client, idx = packed
print(client)
assert isinstance(client.op, tensor.Elemwise)
assert isinstance(client.op.scalar_op, theano.scalar.Cast)
packed, = client.outputs[0].clients
client, idx = packed
assert isinstance(client.op, cuda.GpuFromHost)
示例14: __init__
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def __init__(self, nfeatures=100, noutputs=10, nhiddens=50, rng=None):
if rng is None:
rng = 0
if isinstance(rng, int):
rng = np.random.RandomState(rng)
self.rng = rng
self.nfeatures = nfeatures
self.noutputs = noutputs
self.nhiddens = nhiddens
x = T.dmatrix('x')
wh = th.shared(self.rng.normal(0, 1, (nfeatures, nhiddens)),
borrow=True)
bh = th.shared(np.zeros(nhiddens), borrow=True)
h = T.nnet.sigmoid(T.dot(x, wh) + bh)
wy = th.shared(self.rng.normal(0, 1, (nhiddens, noutputs)))
by = th.shared(np.zeros(noutputs), borrow=True)
y = T.nnet.softmax(T.dot(h, wy) + by)
self.inputs = [x]
self.outputs = [y]
示例15: test_multilayer_conv
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import dmatrix [as 別名]
def test_multilayer_conv(self):
# fixed parameters
bsize = 10 # batch size
imshp = (5, 5)
kshp = ((3, 3), (2, 2))
nkerns = (3, 6) # per output pixel
ssizes = (((1, 1), (2, 2)),)
convmodes = ('full',) # 'valid',)
# symbolic stuff
kerns = [tensor.dmatrix(), tensor.dmatrix()]
input = tensor.dmatrix()
rng = numpy.random.RandomState(3423489)
# build actual input images
img2d = numpy.arange(bsize*numpy.prod(imshp)).reshape((bsize,)+imshp)
img1d = img2d.reshape(bsize, -1)
for mode in ('FAST_COMPILE', 'FAST_RUN'):
for conv_mode in convmodes:
for ss in ssizes:
l1hid, l1shp = sp.convolve(kerns[0], kshp[0],\
nkerns[0], input, imshp, ss[0], mode=conv_mode)
l1propup = function([kerns[0], input], l1hid, mode=mode)
#l1kernvals = numpy.random.rand(nkerns[0],numpy.prod(kshp[0]))
l1kernvals = numpy.arange(nkerns[0]*numpy.prod(kshp[0])).reshape(nkerns[0], numpy.prod(kshp[0]))
l1hidval = l1propup(l1kernvals, img1d)
# actual values
l2hid, l2shp = sp.convolve(kerns[1], kshp[1],\
nkerns[1], l1hid, l1shp, ss[1], mode=conv_mode)
l2propup = function([kerns[1], l1hid], l2hid, mode=mode)
#l2kernvals = numpy.random.rand(nkerns[1],numpy.prod(kshp[1])*nkerns[0])
l2kernvals = numpy.arange(nkerns[1]*numpy.prod(kshp[1])*nkerns[0]).reshape(nkerns[1], numpy.prod(kshp[1])*nkerns[0])
# for debugging, we bring things back to integers
l1hidval = numpy.arange(numpy.size(l1hidval)).reshape(l1hidval.shape)
l2hidval = l2propup(l2kernvals, l1hidval)