本文整理匯總了Python中theano.tensor.add方法的典型用法代碼示例。如果您正苦於以下問題:Python tensor.add方法的具體用法?Python tensor.add怎麽用?Python tensor.add使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類theano.tensor
的用法示例。
在下文中一共展示了tensor.add方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: build_bilinear_net
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def build_bilinear_net(input_shapes, X_var=None, U_var=None, X_diff_var=None, axis=1):
x_shape, u_shape = input_shapes
X_var = X_var or T.tensor4('X')
U_var = U_var or T.matrix('U')
X_diff_var = X_diff_var or T.tensor4('X_diff')
X_next_var = X_var + X_diff_var
l_x = L.InputLayer(shape=(None,) + x_shape, input_var=X_var)
l_u = L.InputLayer(shape=(None,) + u_shape, input_var=U_var)
l_x_diff_pred = LT.BilinearLayer([l_x, l_u], axis=axis)
l_x_next_pred = L.ElemwiseMergeLayer([l_x, l_x_diff_pred], T.add)
l_y = L.flatten(l_x)
l_y_diff_pred = L.flatten(l_x_diff_pred)
X_next_pred_var = lasagne.layers.get_output(l_x_next_pred)
loss = ((X_next_var - X_next_pred_var) ** 2).mean(axis=0).sum() / 2.
net_name = 'BilinearNet'
input_vars = OrderedDict([(var.name, var) for var in [X_var, U_var, X_diff_var]])
pred_layers = OrderedDict([('y_diff_pred', l_y_diff_pred), ('y', l_y), ('x0_next_pred', l_x_next_pred)])
return net_name, input_vars, pred_layers, loss
示例2: create_structure
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def create_structure(self):
"""Creates the symbolic graph of this layer.
Sets self.output to a symbolic matrix that describes the output of this
layer. If the inputs are the same size as the output, the output will be
the elementwise sum of the inputs. If needed, the inputs will be
projected to the same size.
"""
for input_index, input_layer in enumerate(self._input_layers):
input_size = input_layer.output_size
if input_size == self.output_size:
input_matrix = input_layer.output
else:
input_matrix = self._tensor_preact(input_layer.output,
'input{}'.format(input_index),
use_bias=False)
if self.output is None:
self.output = input_matrix
else:
self.output = tensor.add(self.output, input_matrix)
示例3: rbf_kernel
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def rbf_kernel(X):
XY = T.dot(X, X.T)
x2 = T.sum(X**2, axis=1).dimshuffle(0, 'x')
X2e = T.repeat(x2, X.shape[0], axis=1)
H = X2e + X2e.T - 2. * XY
V = H.flatten()
# median distance
h = T.switch(T.eq((V.shape[0] % 2), 0),
# if even vector
T.mean(T.sort(V)[ ((V.shape[0] // 2) - 1) : ((V.shape[0] // 2) + 1) ]),
# if odd vector
T.sort(V)[V.shape[0] // 2])
h = T.sqrt(.5 * h / T.log(H.shape[0].astype('float32') + 1.))
# compute the rbf kernel
kxy = T.exp(-H / (h ** 2) / 2.0)
dxkxy = -T.dot(kxy, X)
sumkxy = T.sum(kxy, axis=1).dimshuffle(0, 'x')
dxkxy = T.add(dxkxy, T.mul(X, sumkxy)) / (h ** 2)
return kxy, dxkxy
示例4: input_to_h_from_v
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def input_to_h_from_v(self, v):
"""
.. todo::
WRITEME
"""
D = self.Lambda
alpha = self.alpha
def sum_s(x):
return x.reshape((
-1,
self.nhid,
self.n_s_per_h)).sum(axis=2)
return tensor.add(
self.b,
-0.5 * tensor.dot(v * v, D),
sum_s(self.mu * tensor.dot(v, self.W)),
sum_s(0.5 * tensor.sqr(tensor.dot(v, self.W)) / alpha))
#def mean_h_given_v(self, v):
# inherited version is OK:
# return nnet.sigmoid(self.input_to_h_from_v(v))
示例5: free_energy_given_v
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def free_energy_given_v(self, v):
"""
.. todo::
WRITEME
"""
sigmoid_arg = self.input_to_h_from_v(v)
return tensor.add(
0.5 * (self.B * (v ** 2)).sum(axis=1),
-tensor.nnet.softplus(sigmoid_arg).sum(axis=1))
#def __call__(self, v):
# inherited version is OK
#def reconstruction_error:
# inherited version should be OK
#def params(self):
# inherited version is OK.
示例6: sequence_iteration
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def sequence_iteration(self, output, mask,use_dropout=0,dropout_value=0.5):
dot_product = T.dot(output , self.t_w_out)
net_o = T.add( dot_product , self.t_b_out )
ex_net = T.exp(net_o)
sum_net = T.sum(ex_net, axis=2, keepdims=True)
softmax_o = ex_net / sum_net
mask = T.addbroadcast(mask, 2) # to do nesseccary?
output = T.mul(mask, softmax_o) + T.mul( (1. - mask) , 1e-6 )
return output #result
###### Linear Layer
########################################
示例7: sequence_iteration
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def sequence_iteration(self, in_seq, mask, use_dropout,dropout_value=1):
in_seq_d = T.switch(use_dropout,
(in_seq *
self.trng.binomial(in_seq.shape,
p=dropout_value, n=1,
dtype=in_seq.dtype)),
in_seq)
rz_in_seq = T.add( T.dot(in_seq_d, self.weights[0]) , self.weights[1] )
out_seq, updates = theano.scan(
fn=self.t_forward_step,
sequences=[mask, rz_in_seq], # in_seq_d],
outputs_info=[self.t_ol_t00],
non_sequences=[i for i in self.weights][2:] + [self.t_n_out],
go_backwards = self.go_backwards,
truncate_gradient=-1,
#n_steps=50,
strict=True,
allow_gc=False,
)
return out_seq
示例8: fit
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def fit(self, weights, o_error, tpo ):
gradients = T.grad(o_error ,weights)
updates = []
for c, v, w, g in zip(self.t_cache, self.t_velocity, weights,gradients):
new_velocity = T.sub( T.mul(tpo["momentum_rate"], v) , T.mul(tpo["learn_rate"], g) )
new_cache = T.add( T.mul(tpo["decay_rate"] , c) , T.mul(T.sub( 1, tpo["decay_rate"]) , T.sqr(g)))
new_weights = T.sub(T.add(w , new_velocity) , T.true_div( T.mul(g,tpo["learn_rate"]) , T.sqrt(T.add(new_cache,0.1**8))))
updates.append((w, new_weights))
updates.append((v, new_velocity))
updates.append((c, new_cache))
return updates
###### Nesterov momentum
########################################
示例9: __init__
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def __init__(self, net, mixfrac=1.0, maxiter=25):
EzPickle.__init__(self, net, mixfrac, maxiter)
self.net = net
self.mixfrac = mixfrac
x_nx = net.input
self.predict = theano.function([x_nx], net.output, **FNOPTS)
ypred_ny = net.output
ytarg_ny = T.matrix("ytarg")
var_list = net.trainable_weights
l2 = 1e-3 * T.add(*[T.square(v).sum() for v in var_list])
N = x_nx.shape[0]
mse = T.sum(T.square(ytarg_ny - ypred_ny))/N
symb_args = [x_nx, ytarg_ny]
loss = mse + l2
self.opt = LbfgsOptimizer(loss, var_list, symb_args, maxiter=maxiter, extra_losses={"mse":mse, "l2":l2})
示例10: __add__
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def __add__(left, right):
return add(left, right)
示例11: __radd__
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def __radd__(right, left):
return add(left, right)
示例12: test_softmax_optimizations_w_bias2
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def test_softmax_optimizations_w_bias2(self):
x = tensor.matrix('x')
b = tensor.vector('b')
c = tensor.vector('c')
one_of_n = tensor.lvector('one_of_n')
op = crossentropy_categorical_1hot
fgraph = gof.FunctionGraph(
[x, b, c, one_of_n],
[op(softmax_op(T.add(x, b, c)), one_of_n)])
assert fgraph.outputs[0].owner.op == op
# print 'BEFORE'
# for node in fgraph.toposort():
# print node.op
# print '----'
theano.compile.mode.optdb.query(
theano.compile.mode.OPT_FAST_RUN).optimize(fgraph)
# print 'AFTER'
# for node in fgraph.toposort():
# print node.op
# print '===='
assert len(fgraph.toposort()) == 3
assert str(fgraph.outputs[0].owner.op) == 'OutputGuard'
assert (fgraph.outputs[0].owner.inputs[0].owner.op ==
crossentropy_softmax_argmax_1hot_with_bias)
示例13: set_layer_param_tags
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def set_layer_param_tags(layer, params=None, **tags):
"""
If params is None, update tags of all parameters, else only update tags of parameters in params.
"""
for param, param_tags in layer.params.items():
if params is None or param in params:
for tag, value in tags.items():
if value:
param_tags.add(tag)
else:
param_tags.discard(tag)
示例14: __init__
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def __init__(self, incomings, **kwargs):
super(BatchwiseSumLayer, self).__init__(incomings, T.add, **kwargs)
示例15: __init__
# 需要導入模塊: from theano import tensor [as 別名]
# 或者: from theano.tensor import add [as 別名]
def __init__(self, x, y, args):
self.params_theta = []
self.params_lambda = []
self.params_weight = []
if args.dataset == 'mnist':
input_size = (None, 1, 28, 28)
elif args.dataset == 'cifar10':
input_size = (None, 3, 32, 32)
else:
raise AssertionError
layers = [ll.InputLayer(input_size)]
self.penalty = theano.shared(np.array(0.))
#conv1
layers.append(Conv2DLayerWithReg(args, layers[-1], 20, 5))
self.add_params_to_self(args, layers[-1])
layers.append(ll.MaxPool2DLayer(layers[-1], pool_size=2, stride=2))
#conv1
layers.append(Conv2DLayerWithReg(args, layers[-1], 50, 5))
self.add_params_to_self(args, layers[-1])
layers.append(ll.MaxPool2DLayer(layers[-1], pool_size=2, stride=2))
#fc1
layers.append(DenseLayerWithReg(args, layers[-1], num_units=500))
self.add_params_to_self(args, layers[-1])
#softmax
layers.append(DenseLayerWithReg(args, layers[-1], num_units=10, nonlinearity=nonlinearities.softmax))
self.add_params_to_self(args, layers[-1])
self.layers = layers
self.y = ll.get_output(layers[-1], x, deterministic=False)
self.prediction = T.argmax(self.y, axis=1)
# self.penalty = penalty if penalty != 0. else T.constant(0.)
print(self.params_lambda)
# time.sleep(20)
# cost function
self.loss = T.mean(categorical_crossentropy(self.y, y))
self.lossWithPenalty = T.add(self.loss, self.penalty)
print "loss and losswithpenalty", type(self.loss), type(self.lossWithPenalty)