本文整理汇总了Python中theano.tensor.vector方法的典型用法代码示例。如果您正苦于以下问题:Python tensor.vector方法的具体用法?Python tensor.vector怎么用?Python tensor.vector使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.tensor
的用法示例。
在下文中一共展示了tensor.vector方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_softmax
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def test_softmax():
from keras.activations import softmax as s
# Test using a reference implementation of softmax
def softmax(values):
m = max(values)
values = numpy.array(values)
e = numpy.exp(values - m)
dist = list(e / numpy.sum(e))
return dist
x = T.vector()
exp = s(x)
f = theano.function([x], exp)
test_values=get_standard_values()
result = f(test_values)
expected = softmax(test_values)
print(str(result))
print(str(expected))
list_assert_equal(result, expected)
示例2: test_relu
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def test_relu():
'''
Relu implementation doesn't depend on the value being
a theano variable. Testing ints, floats and theano tensors.
'''
from keras.activations import relu as r
assert r(5) == 5
assert r(-5) == 0
assert r(-0.1) == 0
assert r(0.1) == 0.1
x = T.vector()
exp = r(x)
f = theano.function([x], exp)
test_values = get_standard_values()
result = f(test_values)
list_assert_equal(result, test_values) # because no negatives in test values
示例3: test_tanh
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def test_tanh():
from keras.activations import tanh as t
test_values = get_standard_values()
x = T.vector()
exp = t(x)
f = theano.function([x], exp)
result = f(test_values)
expected = [math.tanh(v) for v in test_values]
print(result)
print(expected)
list_assert_equal(result, expected)
示例4: __init__
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def __init__(self):
X_in = T.matrix('X_in')
u = T.matrix('u')
s = T.vector('s')
eps = T.scalar('eps')
X_ = X_in - T.mean(X_in, 0)
sigma = T.dot(X_.T, X_) / X_.shape[0]
self.sigma = theano.function([X_in], sigma, allow_input_downcast=True)
Z = T.dot(T.dot(u, T.nlinalg.diag(1. / T.sqrt(s + eps))), u.T)
X_zca = T.dot(X_, Z.T)
self.compute_zca = theano.function([X_in, u, s, eps], X_zca, allow_input_downcast=True)
self._u = None
self._s = None
示例5: var
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def var(name, label=None, observed=False, const=False, vector=False, lower=None, upper=None):
if vector and not observed:
raise ValueError('Currently, only observed variables can be vectors')
if observed and const:
raise ValueError('Observed variables are automatically const')
if vector:
var = T.vector(name)
else:
var = T.scalar(name)
var._name = name
var._label = label
var._observed = observed
var._const = observed or const
var._lower = lower or -np.inf
var._upper = upper or np.inf
return var
示例6: __init__
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def __init__(self):
super(M, self).__init__()
x = T.matrix('x') # input, target
self.w = module.Member(T.matrix('w')) # weights
self.a = module.Member(T.vector('a')) # hid bias
self.b = module.Member(T.vector('b')) # output bias
self.hid = T.tanh(T.dot(x, self.w) + self.a)
hid = self.hid
self.out = T.tanh(T.dot(hid, self.w.T) + self.b)
out = self.out
self.err = 0.5 * T.sum((out - x)**2)
err = self.err
params = [self.w, self.a, self.b]
gparams = T.grad(err, params)
updates = [(p, p - 0.01 * gp) for p, gp in zip(params, gparams)]
self.step = module.Method([x], err, updates=dict(updates))
示例7: test_local_csm_properties_csm
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def test_local_csm_properties_csm():
data = tensor.vector()
indices, indptr, shape = (tensor.ivector(), tensor.ivector(),
tensor.ivector())
mode = theano.compile.mode.get_default_mode()
mode = mode.including("specialize", "local_csm_properties_csm")
for CS, cast in [(sparse.CSC, sp.csc_matrix),
(sparse.CSR, sp.csr_matrix)]:
f = theano.function([data, indices, indptr, shape],
sparse.csm_properties(
CS(data, indices, indptr, shape)),
mode=mode)
assert not any(
isinstance(node.op, (sparse.CSM, sparse.CSMProperties))
for node in f.maker.fgraph.toposort())
v = cast(random_lil((10, 40),
config.floatX, 3))
f(v.data, v.indices, v.indptr, v.shape)
示例8: test_local_csm_grad_c
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def test_local_csm_grad_c():
raise SkipTest("Opt disabled as it don't support unsorted indices")
if not theano.config.cxx:
raise SkipTest("G++ not available, so we need to skip this test.")
data = tensor.vector()
indices, indptr, shape = (tensor.ivector(), tensor.ivector(),
tensor.ivector())
mode = theano.compile.mode.get_default_mode()
if theano.config.mode == 'FAST_COMPILE':
mode = theano.compile.Mode(linker='c|py', optimizer='fast_compile')
mode = mode.including("specialize", "local_csm_grad_c")
for CS, cast in [(sparse.CSC, sp.csc_matrix), (sparse.CSR, sp.csr_matrix)]:
cost = tensor.sum(sparse.DenseFromSparse()(CS(data, indices, indptr, shape)))
f = theano.function(
[data, indices, indptr, shape],
tensor.grad(cost, data),
mode=mode)
assert not any(isinstance(node.op, sparse.CSMGrad) for node
in f.maker.fgraph.toposort())
v = cast(random_lil((10, 40),
config.floatX, 3))
f(v.data, v.indices, v.indptr, v.shape)
示例9: test_local_mul_s_v
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def test_local_mul_s_v():
if not theano.config.cxx:
raise SkipTest("G++ not available, so we need to skip this test.")
mode = theano.compile.mode.get_default_mode()
mode = mode.including("specialize", "local_mul_s_v")
for sp_format in ['csr']: # Not implemented for other format
inputs = [getattr(theano.sparse, sp_format + '_matrix')(),
tensor.vector()]
f = theano.function(inputs,
sparse.mul_s_v(*inputs),
mode=mode)
assert not any(isinstance(node.op, sparse.MulSV) for node
in f.maker.fgraph.toposort())
示例10: test_csm_grad
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def test_csm_grad(self):
for sparsetype in ('csr', 'csc'):
x = tensor.vector()
y = tensor.ivector()
z = tensor.ivector()
s = tensor.ivector()
call = getattr(sp, sparsetype + '_matrix')
spm = call(random_lil((300, 400), config.floatX, 5))
out = tensor.grad(dense_from_sparse(
CSM(sparsetype)(x, y, z, s)
).sum(), x)
self._compile_and_check([x, y, z, s],
[out],
[spm.data, spm.indices, spm.indptr,
spm.shape],
(CSMGrad, CSMGradC)
)
示例11: test_csr_dense
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def test_csr_dense(self):
x = theano.sparse.csr_matrix('x')
y = theano.tensor.matrix('y')
v = theano.tensor.vector('v')
for (x, y, x_v, y_v) in [(x, y, self.x_csr, self.y),
(x, v, self.x_csr, self.v_100),
(v, x, self.v_10, self.x_csr)]:
f_a = theano.function([x, y], theano.sparse.dot(x, y))
f_b = lambda x, y: x * y
utt.assert_allclose(f_a(x_v, y_v), f_b(x_v, y_v))
# Test infer_shape
self._compile_and_check([x, y], [theano.sparse.dot(x, y)],
[x_v, y_v],
(Dot, Usmm, UsmmCscDense))
示例12: test_csc_dense
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def test_csc_dense(self):
x = theano.sparse.csc_matrix('x')
y = theano.tensor.matrix('y')
v = theano.tensor.vector('v')
for (x, y, x_v, y_v) in [(x, y, self.x_csc, self.y),
(x, v, self.x_csc, self.v_100),
(v, x, self.v_10, self.x_csc)]:
f_a = theano.function([x, y], theano.sparse.dot(x, y))
f_b = lambda x, y: x * y
utt.assert_allclose(f_a(x_v, y_v), f_b(x_v, y_v))
# Test infer_shape
self._compile_and_check([x, y], [theano.sparse.dot(x, y)],
[x_v, y_v],
(Dot, Usmm, UsmmCscDense))
示例13: test_mul_s_v
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def test_mul_s_v(self):
sp_types = {'csc': sp.csc_matrix,
'csr': sp.csr_matrix}
for format in ['csr', 'csc']:
for dtype in ['float32', 'float64']:
x = theano.sparse.SparseType(format, dtype=dtype)()
y = tensor.vector(dtype=dtype)
f = theano.function([x, y], mul_s_v(x, y))
spmat = sp_types[format](random_lil((4, 3), dtype, 3))
mat = numpy.asarray(numpy.random.rand(3), dtype=dtype)
out = f(spmat, mat)
utt.assert_allclose(spmat.toarray() * mat, out.toarray())
示例14: test_structured_add_s_v
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def test_structured_add_s_v(self):
sp_types = {'csc': sp.csc_matrix,
'csr': sp.csr_matrix}
for format in ['csr', 'csc']:
for dtype in ['float32', 'float64']:
x = theano.sparse.SparseType(format, dtype=dtype)()
y = tensor.vector(dtype=dtype)
f = theano.function([x, y], structured_add_s_v(x, y))
spmat = sp_types[format](random_lil((4, 3), dtype, 3))
spones = spmat.copy()
spones.data = numpy.ones_like(spones.data)
mat = numpy.asarray(numpy.random.rand(3), dtype=dtype)
out = f(spmat, mat)
utt.assert_allclose(as_ndarray(spones.multiply(spmat + mat)),
out.toarray())
示例15: test_broadcast
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import vector [as 别名]
def test_broadcast(self):
# test that we don't raise an error during optimization for no good
# reason as softmax_with_bias don't support correctly some/all
# broadcasted inputs pattern
initial_W = numpy.asarray([[0.1, 0.1, 0.1],
[0.1, 0.1, 0.1],
[0.1, 0.1, 0.1]],
dtype=theano.config.floatX)
W = theano.shared(value=initial_W, name='W')
vbias = theano.shared(value=0.1, name='vbias') # 0.01
hid = T.vector('hid')
f = theano.function([hid],
T.nnet.softmax_op(T.dot(hid, W.T) + vbias))
ops = [node.op for node in f.maker.fgraph.toposort()]
assert softmax_with_bias not in ops
assert softmax_op in ops
f([0, 1, 0])
# print f.maker.fgraph.toposort()