本文整理汇总了Python中theano.tensor.lscalar方法的典型用法代码示例。如果您正苦于以下问题:Python tensor.lscalar方法的具体用法?Python tensor.lscalar怎么用?Python tensor.lscalar使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.tensor
的用法示例。
在下文中一共展示了tensor.lscalar方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_op
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def test_op(self):
n = tensor.lscalar()
f = theano.function([self.p, n], multinomial(n, self.p))
_n = 5
tested = f(self._p, _n)
assert tested.shape == self._p.shape
assert numpy.allclose(numpy.floor(tested.todense()), tested.todense())
assert tested[2, 1] == _n
n = tensor.lvector()
f = theano.function([self.p, n], multinomial(n, self.p))
_n = numpy.asarray([1, 2, 3, 4], dtype='int64')
tested = f(self._p, _n)
assert tested.shape == self._p.shape
assert numpy.allclose(numpy.floor(tested.todense()), tested.todense())
assert tested[2, 1] == _n[2]
示例2: test_mixed_shape_bcastable
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def test_mixed_shape_bcastable(self):
# Test when the provided shape is a tuple of ints and scalar vars
random = RandomStreams(utt.fetch_seed())
shape0 = tensor.lscalar()
shape = (shape0, 1)
u = random.uniform(size=shape, ndim=2)
assert u.broadcastable == (False, True)
f = function([shape0], u)
assert f(2).shape == (2, 1)
assert f(8).shape == (8, 1)
v = random.uniform(size=shape)
assert v.broadcastable == (False, True)
g = function([shape0], v)
assert g(2).shape == (2, 1)
assert g(8).shape == (8, 1)
示例3: test_mixed_shape
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def test_mixed_shape(self):
# Test when the provided shape is a tuple of ints and scalar vars
rng_R = random_state_type()
shape0 = tensor.lscalar()
shape = (shape0, 3)
post_r, u = uniform(rng_R, size=shape, ndim=2)
f = compile.function([rng_R, shape0], u)
rng_state0 = numpy.random.RandomState(utt.fetch_seed())
assert f(rng_state0, 2).shape == (2, 3)
assert f(rng_state0, 8).shape == (8, 3)
post_r, v = uniform(rng_R, size=shape)
g = compile.function([rng_R, shape0], v)
assert g(rng_state0, 2).shape == (2, 3)
assert g(rng_state0, 8).shape == (8, 3)
示例4: test_dtype
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def test_dtype(self):
rng_R = random_state_type()
low = tensor.lscalar()
high = tensor.lscalar()
post_r, out = random_integers(rng_R, low=low, high=high, size=(20, ),
dtype='int8')
assert out.dtype == 'int8'
f = compile.function([rng_R, low, high], [post_r, out])
rng = numpy.random.RandomState(utt.fetch_seed())
rng0, val0 = f(rng, 0, 9)
assert val0.dtype == 'int8'
rng1, val1 = f(rng0, 255, 257)
assert val1.dtype == 'int8'
assert numpy.all(abs(val1) <= 1)
示例5: test_doc
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def test_doc(self):
"""Ensure the code given in pfunc.txt works as expected"""
# Example #1.
a = lscalar()
b = shared(1)
f1 = pfunc([a], (a + b))
f2 = pfunc([In(a, value=44)], a + b, updates={b: b + 1})
self.assertTrue(b.get_value() == 1)
self.assertTrue(f1(3) == 4)
self.assertTrue(f2(3) == 4)
self.assertTrue(b.get_value() == 2)
self.assertTrue(f1(3) == 5)
b.set_value(0)
self.assertTrue(f1(3) == 3)
# Example #2.
a = tensor.lscalar()
b = shared(7)
f1 = pfunc([a], a + b)
f2 = pfunc([a], a * b)
self.assertTrue(f1(5) == 12)
b.set_value(8)
self.assertTrue(f1(5) == 13)
self.assertTrue(f2(4) == 32)
示例6: test_default_updates_expressions
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def test_default_updates_expressions(self):
x = shared(0)
y = shared(1)
a = lscalar('a')
z = a * x
x.default_update = x + y
f1 = pfunc([a], z)
f1(12)
assert x.get_value() == 1
f2 = pfunc([a], z, no_default_updates=True)
assert f2(7) == 7
assert x.get_value() == 1
f3 = pfunc([a], z, no_default_updates=[x])
assert f3(9) == 9
assert x.get_value() == 1
示例7: estimate_lld
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def estimate_lld(model,minibatch,num_sam,size=1):
n_examples = minibatch.shape[0]
num_minibatches = n_examples/size
minibatch = minibatch.astype(np.float32)
srng = MRG_RandomStreams(seed=132)
batch = T.fmatrix()
index = T.lscalar('i')
mini = sharedX(minibatch)
print('num_samples: '+str(num_sam))
lld = model.log_marginal_likelihood_estimate(batch,num_sam,srng)
get_log_marginal_likelihood = theano.function([index], T.sum(lld),givens = {batch:mini[index*size:(index+1)*size]})
pbar = progressbar.ProgressBar(maxval=num_minibatches).start()
sum_of_log_likelihoods = 0.
for i in xrange(num_minibatches):
summand = get_log_marginal_likelihood(i)
sum_of_log_likelihoods += summand
pbar.update(i)
pbar.finish()
marginal_log_likelihood = sum_of_log_likelihoods/n_examples
print("estimate lld: "+str(marginal_log_likelihood))
示例8: build
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def build(self):
# input and output variables
x = T.matrix('x')
y = T.matrix('y')
index = T.lscalar()
batch_count = T.lscalar()
LR = T.scalar('LR', dtype=theano.config.floatX)
M = T.scalar('M', dtype=theano.config.floatX)
# before the build, you work with symbolic variables
# after the build, you work with numeric variables
self.train_batch = theano.function(inputs=[index,LR,M], updates=self.model.updates(x,y,LR,M),givens={
x: self.shared_x[index * self.batch_size:(index + 1) * self.batch_size],
y: self.shared_y[index * self.batch_size:(index + 1) * self.batch_size]},
name = "train_batch", on_unused_input='warn')
self.test_batch = theano.function(inputs=[index],outputs=self.model.errors(x,y),givens={
x: self.shared_x[index * self.batch_size:(index + 1) * self.batch_size],
y: self.shared_y[index * self.batch_size:(index + 1) * self.batch_size]},
name = "test_batch")
if self.format == "DFXP" :
self.update_range = theano.function(inputs=[batch_count],updates=self.model.range_updates(batch_count), name = "update_range")
示例9: get_test_model
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def get_test_model(self, x_data, y_data, aux_data=None, preds_feats=False):
print('Compiling testing function... ')
idx = tt.lscalar('test batch index')
bth_sz = self.tr_prms['BATCH_SZ']
givens = {
self.test_x: x_data[idx * bth_sz:(idx + 1) * bth_sz],
self.y: y_data[idx * bth_sz:(idx + 1) * bth_sz]}
if hasattr(self, 'aux_inpt_te'):
assert aux_data is not None, "Auxillary data not supplied"
givens[self.aux_inpt_te] = \
aux_data[idx * bth_sz:(idx + 1) * bth_sz]
outputs = self.te_layers[-1].sym_and_oth_err_rate(self.y)
if preds_feats:
outputs += self.te_layers[-1].features_and_predictions()
return theano.function([idx],
outputs,
givens=givens)
示例10: return_activity
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def return_activity(self, train_set_x):
'''Given an input, this function returns the activity
value of all the nodes in each hidden layer.'''
activity_each_layer = []
index = T.lscalar('index') # index to a sample
for dA in self.dA_layers:
activity_fn = theano.function(inputs=[index],outputs = dA.output,
givens={self.x: train_set_x[index:(index+1)]})
activity_each_layer.append(activity_fn)
return activity_each_layer
示例11: return_raw_activity
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def return_raw_activity(self, train_set_x):
'''Given an input, this function returns the raw activity
value of all the nodes in each layer.'''
raw_activity_each_layer = []
index = T.lscalar('index') # index to a sample
for dA in self.dA_layers:
raw_activity_fn = theano.function(inputs=[index],outputs = dA.raw_output,
givens={self.x: train_set_x[index:(index+1)]})
raw_activity_each_layer.append(raw_activity_fn)
return raw_activity_each_layer
示例12: test_perform
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def test_perform(self):
x = tensor.lscalar()
f = function([x], self.op(x))
M = numpy.random.random_integers(3, 50, size=())
assert numpy.allclose(f(M), numpy.bartlett(M))
assert numpy.allclose(f(0), numpy.bartlett(0))
assert numpy.allclose(f(-1), numpy.bartlett(-1))
b = numpy.array([17], dtype='uint8')
assert numpy.allclose(f(b[0]), numpy.bartlett(b[0]))
示例13: test_infer_shape
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def test_infer_shape(self):
x = tensor.lscalar()
self._compile_and_check([x], [self.op(x)],
[numpy.random.random_integers(3, 50, size=())],
self.op_class)
self._compile_and_check([x], [self.op(x)], [0], self.op_class)
self._compile_and_check([x], [self.op(x)], [1], self.op_class)
示例14: test_simple_2d
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def test_simple_2d(self):
"""Increments or sets part of a tensor by a scalar using full slice and
a partial slice depending on a scalar.
"""
a = tt.dmatrix()
increment = tt.dscalar()
sl1 = slice(None)
sl2_end = tt.lscalar()
sl2 = slice(sl2_end)
for do_set in [False, True]:
if do_set:
resut = tt.set_subtensor(a[sl1, sl2], increment)
else:
resut = tt.inc_subtensor(a[sl1, sl2], increment)
f = theano.function([a, increment, sl2_end], resut)
val_a = numpy.ones((5, 5))
val_inc = 2.3
val_sl2_end = 2
result = f(val_a, val_inc, val_sl2_end)
expected_result = numpy.copy(val_a)
if do_set:
expected_result[:, :val_sl2_end] = val_inc
else:
expected_result[:, :val_sl2_end] += val_inc
utt.assert_allclose(result, expected_result)
示例15: test_correct_solution
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import lscalar [as 别名]
def test_correct_solution(self):
x = tensor.lmatrix()
y = tensor.lmatrix()
z = tensor.lscalar()
b = theano.tensor.nlinalg.lstsq()(x, y, z)
f = function([x, y, z], b)
TestMatrix1 = numpy.asarray([[2, 1], [3, 4]])
TestMatrix2 = numpy.asarray([[17, 20], [43, 50]])
TestScalar = numpy.asarray(1)
f = function([x, y, z], b)
m = f(TestMatrix1, TestMatrix2, TestScalar)
self.assertTrue(numpy.allclose(TestMatrix2, numpy.dot(TestMatrix1, m[0])))