本文整理汇总了Python中theano.tensor.dscalar方法的典型用法代码示例。如果您正苦于以下问题:Python tensor.dscalar方法的具体用法?Python tensor.dscalar怎么用?Python tensor.dscalar使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.tensor
的用法示例。
在下文中一共展示了tensor.dscalar方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [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()
示例2: test_infer_shape
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [as 别名]
def test_infer_shape(self):
z = tensor.dtensor3()
x = tensor.dmatrix()
y = tensor.dscalar()
self._compile_and_check([x, y], [self.op(x, y)],
[numpy.random.rand(8, 5),
numpy.random.rand()],
self.op_class)
self._compile_and_check([z, y], [self.op(z, y)],
# must be square when nd>2
[numpy.random.rand(8, 8, 8),
numpy.random.rand()],
self.op_class,
warn=False)
示例3: test_simple_2d
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [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)
示例4: test2
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [as 别名]
def test2(self):
"""Test that it works on scalar variables"""
a = T.dscalar()
d_a = T.DimShuffle([], [])(a)
d_a2 = T.DimShuffle([], ['x', 'x'])(a)
self.assertTrue(_as_scalar(a) is a)
self.assertTrue(_as_scalar(d_a) is a)
self.assertTrue(_as_scalar(d_a2) is a)
示例5: test_upcasting_scalar_nogemv
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [as 别名]
def test_upcasting_scalar_nogemv(self):
# Test that the optimization does not crash when the scale has
# an incorrect dtype, and forces upcasting of the result
# We put this test in this class to test it on the gpu too.
vs = self.get_data()
alpha_v, beta_v, a_v, x_v, y_v = vs
alpha_v = alpha_v.astype("float64")
a_v = a_v.astype("float32")
x_v = x_v.astype("float32")
y_v = y_v.astype("float32")
alpha = T.dscalar('alpha')
a = self.shared(a_v)
x = self.shared(x_v)
y = self.shared(y_v)
rval = T.dot(a, x) * alpha + y
f = theano.function([alpha], rval, mode=self.mode)
# this function is currently optimized so that the gemv is
# done inplace on a temporarily allocated-buffer, which is
# then scaled by alpha and to t with a fused elemwise.
n_gemvs = 0
#theano.printing.debugprint(f, print_type=True)
for node in f.maker.fgraph.toposort():
if node.op == self.gemv_inplace:
n_gemvs += 1
assert node.outputs[0].dtype == 'float32'
assert n_gemvs == 1, n_gemvs
self.assertFunctionContains1(f, self.gemv_inplace)
f(alpha_v)
示例6: test_default_dtype
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [as 别名]
def test_default_dtype(self):
random = RandomStreams(utt.fetch_seed())
low = tensor.dscalar()
high = tensor.dscalar()
# Should not silently downcast from low and high
out0 = random.uniform(low=low, high=high, size=(42,))
assert out0.dtype == 'float64'
f0 = function([low, high], out0)
val0 = f0(-2.1, 3.1)
assert val0.dtype == 'float64'
# Should downcast, since asked explicitly
out1 = random.uniform(low=low, high=high, size=(42,), dtype='float32')
assert out1.dtype == 'float32'
f1 = function([low, high], out1)
val1 = f1(-1.1, 1.1)
assert val1.dtype == 'float32'
# Should use floatX
lowf = tensor.fscalar()
highf = tensor.fscalar()
outf = random.uniform(low=lowf, high=highf, size=(42,))
assert outf.dtype == config.floatX
ff = function([lowf, highf], outf)
valf = ff(numpy.float32(-0.1), numpy.float32(0.3))
assert valf.dtype == config.floatX
示例7: test_givens_replaces_shared_variable
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [as 别名]
def test_givens_replaces_shared_variable(self):
a = shared(1., 'a')
a.default_update = a + 3.
b = tensor.dscalar('b')
c = a + 10
f = pfunc([b], c, givens={a: b})
assert len(f.maker.fgraph.inputs) == 1
assert len(f.maker.fgraph.outputs) == 1
示例8: test_divide_floats
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [as 别名]
def test_divide_floats(self):
a = T.dscalar('a')
b = T.dscalar('b')
c = theano.function([a, b], b / a)
d = theano.function([a, b], b // a)
assert c(6, 3) == 0.5
assert d(6, 3) == 0.0
示例9: __init__
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [as 别名]
def __init__(self, f, state_size, action_size, **kwargs):
"""Constructs a BatchAutoDiffDynamics model.
Args:
f: Symbolic function with the following signature:
Args:
x: Batch of state variables.
u: Batch of action variables.
i: Batch of time step variables.
Returns:
f: Batch of next state variables.
**kwargs: Additional keyword-arguments to pass to
`theano.function()`.
"""
self._fn = f
self._state_size = state_size
self._action_size = action_size
self._x = x = T.dvector("x")
self._u = u = T.dvector("u")
self._i = i = T.dscalar("i")
inputs = [x, u, i]
x_rep_1 = T.tile(x, (1, 1))
u_rep_1 = T.tile(u, (1, 1))
i_rep_1 = T.tile(i, (1, 1))
self._tensor = f(x_rep_1, u_rep_1, i_rep_1)
self._f = as_function(self._tensor, inputs, name="f", **kwargs)
self._J_x, self._J_u, _ = batch_jacobian(f, inputs, state_size)
self._f_x = as_function(self._J_x, inputs, name="f_x", **kwargs)
self._f_u = as_function(self._J_u, inputs, name="f_u", **kwargs)
super(BatchAutoDiffDynamics, self).__init__()
示例10: test_addition
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [as 别名]
def test_addition(self):
# Declare two symbolic floating-point scalars.
a = tensor.dscalar()
b = tensor.dscalar()
# Create a simple expression.
c = a + b
# Convert the expression into a callable object that takes (a,b)
# values as input and computes a value for 'c'.
f = theano.function([a,b], c)
# Bind 1.5 to 'a', 2.5 to 'b', and evaluate 'c'.
self.assertEqual(4.0, f(1.5, 2.5))
示例11: test_mixture_api
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [as 别名]
def test_mixture_api():
# Check basic API
p1 = Normal(mu=0.0, sigma=T.constant(1.0))
p2 = Normal(mu=1.0, sigma=2.0)
m = Mixture(components=[p1, p2], weights=[0.25])
assert len(m.components) == 2
assert len(m.weights) == 2
assert len(m.parameters_) == 4
assert len(m.constants_) == 1
assert len(m.observeds_) == 0
assert p1.mu in m.parameters_
assert p1.sigma in m.constants_
assert p2.mu in m.parameters_
assert p2.sigma in m.parameters_
assert m.X == p1.X
assert m.X == p2.X
assert m.ndim == p1.ndim
assert m.ndim == p2.ndim
m = Mixture(components=[p1, p2])
w = m.compute_weights()
assert_array_equal(w, [0.5, 0.5])
y = T.dscalar(name="y")
w1 = T.constant(0.25)
w2 = y * 2
m = Mixture(components=[p1, p2], weights=[w1, w2])
assert y in m.observeds_
# Check errors
assert_raises(ValueError, Mixture,
components=[p1, p1, p1], weights=[1.0])
示例12: test_abs_b_one
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [as 别名]
def test_abs_b_one():
"""Check derivs are finite when b=+/-1."""
b = tt.dscalar()
b.tag.test_value = -1.0
map = starry.Map(reflected=True)
def flux(b):
return map.flux(zs=-b, ys=0)
grad = theano.function([b], tt.grad(flux(b)[0], [b]))
assert not np.isnan(grad(-1.0)[0]) and not np.isnan(grad(1.0)[0])
示例13: test_simple_3d
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [as 别名]
def test_simple_3d(self):
"""Increments or sets part of a tensor by a scalar using full slice and
a partial slice depending on a scalar.
"""
a = tt.dtensor3()
increment = tt.dscalar()
sl1 = slice(None)
sl2_end = tt.lscalar()
sl2 = slice(sl2_end)
sl3 = 2
val_a = numpy.ones((5, 3, 4))
val_inc = 2.3
val_sl2_end = 2
for method in [tt.set_subtensor, tt.inc_subtensor]:
print("MethodSet", method)
resut = method(a[sl1, sl3, sl2], increment)
f = theano.function([a, increment, sl2_end], resut)
expected_result = numpy.copy(val_a)
result = f(val_a, val_inc, val_sl2_end)
if method is tt.set_subtensor:
expected_result[:, sl3, :val_sl2_end] = val_inc
else:
expected_result[:, sl3, :val_sl2_end] += val_inc
utt.assert_allclose(result, expected_result)
# Test when we broadcast the result
resut = method(a[sl1, sl2], increment)
f = theano.function([a, increment, sl2_end], resut)
expected_result = numpy.copy(val_a)
result = f(val_a, val_inc, val_sl2_end)
if method is tt.set_subtensor:
expected_result[:, :val_sl2_end] = val_inc
else:
expected_result[:, :val_sl2_end] += val_inc
utt.assert_allclose(result, expected_result)
示例14: test_local_dot22_to_dot22scalar
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [as 别名]
def test_local_dot22_to_dot22scalar():
"""
This test that the bug in gh-1507 is really fixed
"""
A = T.dmatrix()
mode = theano.compile.mode.get_default_mode()
opt = theano.tensor.opt.in2out(
theano.tensor.blas.local_dot22_to_dot22scalar)
mode = mode.__class__(optimizer=opt)
x = T.dscalar()
y = T.dscalar()
z = T.dscalar()
# make sure to don't have dimshuffle as we don't opt those cases
m = T.dmatrix()
r = T.drow()
for idx, node in enumerate([
# Old working cases
T.mul(_dot22(A, A), x),
T.mul(_dot22(A, A), x, y),
T.mul(_dot22(A, A), x, r),
T.mul(_dot22(A, A), m, x),
T.mul(_dot22(A, A), x, m),
T.mul(_dot22(A, A), x, (m * y)),
T.mul(_dot22(A, A), (m * y), x),
T.mul(_dot22(A, A), x, (r * y)),
T.mul(_dot22(A, A), (r * y), x),
T.mul(_dot22(A, A), (x * y), (m * x)),
T.mul(_dot22(A, A), (r * y), (y * x)),
# Case that was raising an assert that is fixed in gh-1507
T.mul(_dot22(A, A), (m * y), m),
T.mul(_dot22(A, A), m, (m * y)),
T.mul(_dot22(A, A), (r * y), (m * x)),
# assert fixed in gh-1507 and opt case added in gh-1515
T.mul(_dot22(A, A), (m * y * z), m),
T.mul(_dot22(A, A), m, (m * y * z)),
# Opt case added in gh-1515
T.mul(_dot22(A, A), T.mul(m, y, z), m),
T.mul(_dot22(A, A), m, T.mul(m, y, z)),
# Case that opt later in gh-1515
T.mul(_dot22(A, A), (r * m), (m * x)),
]):
node2 = theano.tensor.blas.local_dot22_to_dot22scalar.transform(
node.owner)
assert node2
f = theano.function([x, y, z, m, r, A], node,
mode=mode, on_unused_input='ignore')
f(.1, .2, .3, [[1, 2], [3, 4]], [[5, 6]], [[7, 8], [9, 10]])
示例15: __theano_train__
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import dscalar [as 别名]
def __theano_train__(self, ):
"""
训练阶段跑一遍训练序列
"""
uidx, pqidx = T.iscalar(), T.ivector()
adidx, tidx = T.dscalar(), T.iscalar()
du = self.du[uidx]
dppq = self.dp[pqidx]
dspq = self.ds[pqidx]
"""
# PRME
D_u,lc,c = D^P_u,l if delta(l, lc) > threshold
= alpha * D^P_u,l + (1 - alpha) * D^S_lc,l otherwise
"""
Dp_p = T.sum(T.pow(du - dppq[0], 2))
Dp_q = T.sum(T.pow(du - dppq[1], 2))
Ds_p = T.sum(T.pow(dspq[0] - dspq[2], 2))
Ds_q = T.sum(T.pow(dspq[1] - dspq[2], 2))
w = T.pow((1 + adidx), 0.25)
Dp = ifelse(T.gt(tidx, self.thd), Dp_p, w * (self.cw * Dp_p + (1 - self.cw) * Ds_p))
Dq = ifelse(T.gt(tidx, self.thd), Dp_q, w * (self.cw * Dp_q + (1 - self.cw) * Ds_q))
upq = T.sum(T.log(sigmoid(- Dp + Dq)))
# ----------------------------------------------------------------------------
# cost, gradients, learning rate, L2 regularization
lr, l2 = self.alpha_lambda[0], self.alpha_lambda[1]
bpr_l2_sqr = (
T.sum([T.sum(par ** 2) for par in [du, dppq, dspq]]))
costs = (
upq -
0.5 * l2 * bpr_l2_sqr)
# 1个user,2个items,这种更新求导是最快的。
pars_subs = [(self.du, du), (self.dp, dppq), (self.ds, dspq)]
seq_updates = [(par, T.set_subtensor(sub, sub + lr * T.grad(costs, sub)))
for par, sub in pars_subs]
# ----------------------------------------------------------------------------
# 输入用户、正负样本及其它参数后,更新变量,返回损失。
self.prme_train = theano.function(
inputs=[uidx, pqidx, adidx, tidx],
outputs=upq,
updates=seq_updates)