本文整理汇总了Python中theano.tensor.log方法的典型用法代码示例。如果您正苦于以下问题:Python tensor.log方法的具体用法?Python tensor.log怎么用?Python tensor.log使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.tensor
的用法示例。
在下文中一共展示了tensor.log方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ctc_update_log_p
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def ctc_update_log_p(skip_idxs, zeros, active, log_p_curr, log_p_prev):
active_skip_idxs = skip_idxs[(skip_idxs < active).nonzero()]
active_next = T.cast(T.minimum(
T.maximum(
active + 1,
T.max(T.concatenate([active_skip_idxs, [-1]])) + 2 + 1
), log_p_curr.shape[0]), 'int32')
common_factor = T.max(log_p_prev[:active])
p_prev = T.exp(log_p_prev[:active] - common_factor)
_p_prev = zeros[:active_next]
# copy over
_p_prev = T.set_subtensor(_p_prev[:active], p_prev)
# previous transitions
_p_prev = T.inc_subtensor(_p_prev[1:], _p_prev[:-1])
# skip transitions
_p_prev = T.inc_subtensor(_p_prev[active_skip_idxs + 2], p_prev[active_skip_idxs])
updated_log_p_prev = T.log(_p_prev) + common_factor
log_p_next = T.set_subtensor(
zeros[:active_next],
log_p_curr[:active_next] + updated_log_p_prev
)
return active_next, log_p_next
示例2: reduce_log_sum
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def reduce_log_sum(tensor, axis=None, guaranteed_finite=False):
"""
Sum probabilities in the log domain, i.e return
log(e^vec[0] + e^vec[1] + ...)
= log(e^x e^(vec[0]-x) + e^x e^(vec[1]-x) + ...)
= log(e^x [e^(vec[0]-x) + e^(vec[1]-x) + ...])
= log(e^x) + log(e^(vec[0]-x) + e^(vec[1]-x) + ...)
= x + log(e^(vec[0]-x) + e^(vec[1]-x) + ...)
For numerical stability, we choose x = max(vec)
Note that if x is -inf, that means all values are -inf,
so the answer should be -inf. In this case, choose x = 0
"""
maxval = T.max(tensor, axis)
maxval_full = T.max(tensor, axis, keepdims=True)
if not guaranteed_finite:
maxval = T.switch(T.isfinite(maxval), maxval, T.zeros_like(maxval))
maxval_full = T.switch(T.isfinite(maxval_full), maxval_full, T.zeros_like(maxval_full))
reduced_sum = T.sum(T.exp(tensor - maxval_full), axis)
logsum = maxval + T.log(reduced_sum)
return logsum
示例3: get_output_for
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def get_output_for(self, input, init=False, **kwargs):
if input.ndim > 2:
# if the input has more than two dimensions, flatten it into a
# batch of feature vectors.
input = input.flatten(2)
activation = T.tensordot(input, self.W, [[1], [0]])
abs_dif = (T.sum(abs(activation.dimshuffle(0,1,2,'x') - activation.dimshuffle('x',1,2,0)),axis=2)
+ 1e6 * T.eye(input.shape[0]).dimshuffle(0,'x',1))
if init:
mean_min_abs_dif = 0.5 * T.mean(T.min(abs_dif, axis=2),axis=0)
abs_dif /= mean_min_abs_dif.dimshuffle('x',0,'x')
self.init_updates = [(self.log_weight_scale, self.log_weight_scale-T.log(mean_min_abs_dif).dimshuffle(0,'x'))]
f = T.sum(T.exp(-abs_dif),axis=2)
if init:
mf = T.mean(f,axis=0)
f -= mf.dimshuffle('x',0)
self.init_updates.append((self.b, -mf))
else:
f += self.b.dimshuffle('x',0)
return T.concatenate([input, f], axis=1)
示例4: __init__
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def __init__(self,convolutional_layers,feature_maps,filter_shapes,poolsize,feedforward_layers,feedforward_nodes,classes,learning_rate,regularization):
self.input = T.tensor4()
self.convolutional_layers = []
self.convolutional_layers.append(convolutional_layer(self.input,feature_maps[1],feature_maps[0],filter_shapes[0][0],filter_shapes[0][1],poolsize[0]))
for i in range(1,convolutional_layers):
self.convolutional_layers.append(convolutional_layer(self.convolutional_layers[i-1].output,feature_maps[i+1],feature_maps[i],filter_shapes[i][0],filter_shapes[i][1],poolsize[i]))
self.feedforward_layers = []
self.feedforward_layers.append(feedforward_layer(self.convolutional_layers[-1].output.flatten(2),flattened,feedforward_nodes[0]))
for i in range(1,feedforward_layers):
self.feedforward_layers.append(feedforward_layer(self.feedforward_layers[i-1].output,feedforward_nodes[i-1],feedforward_nodes[i]))
self.output_layer = feedforward_layer(self.feedforward_layers[-1].output,feedforward_nodes[-1],classes)
self.params = []
for l in self.convolutional_layers + self.feedforward_layers:
self.params.extend(l.get_params())
self.params.extend(self.output_layer.get_params())
self.target = T.matrix()
self.output = self.output_layer.output
self.cost = -self.target*T.log(self.output)-(1-self.target)*T.log(1-self.output)
self.cost = self.cost.mean()
for i in range(convolutional_layers+feedforward_layers+1):
self.cost += regularization*(self.params[2*i]**2).mean()
self.gparams = [T.grad(self.cost, param) for param in self.params]
self.propogate = theano.function([self.input,self.target],self.cost,updates=[(param,param-learning_rate*gparam) for param,gparam in zip(self.params,self.gparams)],allow_input_downcast=True)
self.classify = theano.function([self.input],self.output,allow_input_downcast=True)
示例5: __init__
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def __init__(self,classes,hidden_layers,features,nodes_per_hidden_layer,learning_rate,regularization):
self.hidden_layers = []
self.hidden_layers.append(layer(features,nodes_per_hidden_layer))
for i in range(hidden_layers-1):
self.hidden_layers.append(layer(nodes_per_hidden_layer,nodes_per_hidden_layer))
self.output_layer = layer(nodes_per_hidden_layer,classes)
self.params = []
for l in self.hidden_layers:
self.params.extend(l.get_params())
self.params.extend(self.output_layer.get_params())
self.A = T.matrix()
self.t = T.matrix()
self.s = 1/(1+T.exp(-T.dot(self.A,self.params[0])-self.params[1]))
for i in range(hidden_layers):
self.s = 1/(1+T.exp(-T.dot(self.s,self.params[2*(i+1)])-self.params[2*(i+1)+1]))
self.cost = -self.t*T.log(self.s)-(1-self.t)*T.log(1-self.s)
self.cost = self.cost.mean()
for i in range(hidden_layers+1):
self.cost += regularization*(self.params[2*i]**2).mean()
self.gparams = [T.grad(self.cost, param) for param in self.params]
self.propogate = theano.function([self.A,self.t],self.cost,updates=[(param,param-learning_rate*gparam) for param,gparam in zip(self.params,self.gparams)],allow_input_downcast=True)
self.classify = theano.function([self.A],self.s,allow_input_downcast=True)
示例6: __init__
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def __init__(self,convolutional_layers,feature_maps,filter_shapes,poolsize,feedforward_layers,feedforward_nodes,classes,regularization):
self.input = T.tensor4()
self.convolutional_layers = []
self.convolutional_layers.append(convolutional_layer(self.input,feature_maps[1],feature_maps[0],filter_shapes[0][0],filter_shapes[0][1],poolsize[0]))
for i in range(1,convolutional_layers):
self.convolutional_layers.append(convolutional_layer(self.convolutional_layers[i-1].output,feature_maps[i+1],feature_maps[i],filter_shapes[i][0],filter_shapes[i][1],poolsize[i]))
self.feedforward_layers = []
self.feedforward_layers.append(feedforward_layer(self.convolutional_layers[-1].output.flatten(2),flattened,feedforward_nodes[0]))
for i in range(1,feedforward_layers):
self.feedforward_layers.append(feedforward_layer(self.feedforward_layers[i-1].output,feedforward_nodes[i-1],feedforward_nodes[i]))
self.output_layer = feedforward_layer(self.feedforward_layers[-1].output,feedforward_nodes[-1],classes)
self.params = []
for l in self.convolutional_layers + self.feedforward_layers:
self.params.extend(l.get_params())
self.params.extend(self.output_layer.get_params())
self.target = T.matrix()
self.output = self.output_layer.output
self.cost = -self.target*T.log(self.output)-(1-self.target)*T.log(1-self.output)
self.cost = self.cost.mean()
for i in range(convolutional_layers+feedforward_layers+1):
self.cost += regularization*(self.params[2*i]**2).mean()
self.updates = self.adam(self.cost,self.params)
self.propogate = theano.function([self.input,self.target],self.cost,updates=self.updates,allow_input_downcast=True)
self.classify = theano.function([self.input],self.output,allow_input_downcast=True)
示例7: __init__
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def __init__(self,convolutional_layers,feature_maps,filter_shapes,feedforward_layers,feedforward_nodes,classes):
self.input = T.tensor4()
self.convolutional_layers = []
self.convolutional_layers.append(convolutional_layer(self.input,feature_maps[1],feature_maps[0],filter_shapes[0][0],filter_shapes[0][1]))
for i in range(1,convolutional_layers):
if i==2 or i==4:
self.convolutional_layers.append(convolutional_layer(self.convolutional_layers[i-1].output,feature_maps[i+1],feature_maps[i],filter_shapes[i][0],filter_shapes[i][1],maxpool=(2,2)))
else:
self.convolutional_layers.append(convolutional_layer(self.convolutional_layers[i-1].output,feature_maps[i+1],feature_maps[i],filter_shapes[i][0],filter_shapes[i][1]))
self.feedforward_layers = []
self.feedforward_layers.append(feedforward_layer(self.convolutional_layers[-1].output.flatten(2),20480,feedforward_nodes[0]))
for i in range(1,feedforward_layers):
self.feedforward_layers.append(feedforward_layer(self.feedforward_layers[i-1].output,feedforward_nodes[i-1],feedforward_nodes[i]))
self.output_layer = feedforward_layer(self.feedforward_layers[-1].output,feedforward_nodes[-1],classes)
self.params = []
for l in self.convolutional_layers + self.feedforward_layers:
self.params.extend(l.get_params())
self.params.extend(self.output_layer.get_params())
self.target = T.matrix()
self.output = self.output_layer.output
self.cost = -self.target*T.log(self.output)-(1-self.target)*T.log(1-self.output)
self.cost = self.cost.mean()
self.updates = self.adam(self.cost, self.params)
self.propogate = theano.function([self.input,self.target],self.cost,updates=self.updates,allow_input_downcast=True)
self.classify = theano.function([self.input],self.output,allow_input_downcast=True)
示例8: __init__
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def __init__(self,convolutional_layers,feature_maps,filter_shapes,feedforward_layers,feedforward_nodes,classes):
self.input = T.tensor4()
self.convolutional_layers = []
self.convolutional_layers.append(convolutional_layer(self.input,feature_maps[1],feature_maps[0],filter_shapes[0][0],filter_shapes[0][1]))
for i in range(1,convolutional_layers):
if i==3:
self.convolutional_layers.append(convolutional_layer(self.convolutional_layers[i-1].output,feature_maps[i+1],feature_maps[i],filter_shapes[i][0],filter_shapes[i][1],maxpool=(2,2)))
else:
self.convolutional_layers.append(convolutional_layer(self.convolutional_layers[i-1].output,feature_maps[i+1],feature_maps[i],filter_shapes[i][0],filter_shapes[i][1]))
self.feedforward_layers = []
self.feedforward_layers.append(feedforward_layer(self.convolutional_layers[-1].output.flatten(2),40000,feedforward_nodes[0]))
for i in range(1,feedforward_layers):
self.feedforward_layers.append(feedforward_layer(self.feedforward_layers[i-1].output,feedforward_nodes[i-1],feedforward_nodes[i]))
self.output_layer = feedforward_layer(self.feedforward_layers[-1].output,feedforward_nodes[-1],classes)
self.params = []
for l in self.convolutional_layers + self.feedforward_layers:
self.params.extend(l.get_params())
self.params.extend(self.output_layer.get_params())
self.target = T.matrix()
self.output = self.output_layer.output
self.cost = -self.target*T.log(self.output)-(1-self.target)*T.log(1-self.output)
self.cost = self.cost.mean()
self.updates = self.adam(self.cost, self.params)
self.propogate = theano.function([self.input,self.target],self.cost,updates=self.updates,allow_input_downcast=True)
self.classify = theano.function([self.input],self.output,allow_input_downcast=True)
示例9: test_local_softmax_grad_optimization_and_big_input
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def test_local_softmax_grad_optimization_and_big_input(self):
"""Test the Logsoftmax's grad substitution.
Check that Log(Softmax(x))'s grad is substituted with Logsoftmax(x)'s
grad and that the new operation does not explode for big inputs.
Note that only the grad is checked.
"""
m = theano.config.mode
m = theano.compile.get_mode(m)
m.check_isfinite = False
# some inputs that are large to make the gradient explode in the non
# optimized case
a = numpy.exp(10 * numpy.random.rand(5, 10).astype(theano.config.floatX))
def myfunc(x):
sm = tensor.nnet.softmax(x)
logsm = tensor.log(sm)
return logsm
# We set step to 0.1 because for big values we need a big epsilon
utt.verify_grad(myfunc, [a], eps=0.1, mode=m)
f = theano.function([], myfunc(a))
self.assertTrue(hasattr(f.maker.fgraph.outputs[0].tag, 'trace'))
示例10: test_stabilize_log_softmax
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def test_stabilize_log_softmax():
mode = theano.compile.mode.get_default_mode()
mode = mode.including('local_log_softmax', 'specialize')
x = matrix()
y = softmax(x)
z = theano.tensor.log(y)
f = theano.function([x], z, mode=mode)
assert hasattr(f.maker.fgraph.outputs[0].tag, 'trace')
# check that the softmax has been optimized out
for node in f.maker.fgraph.toposort():
assert not isinstance(node.op, y.owner.op.__class__)
# call the function so debug mode can verify the optimized
# version matches the unoptimized version
rng = numpy.random.RandomState([2012, 8, 22])
f(numpy.cast[config.floatX](rng.randn(2, 3)))
示例11: test_grad_log1msigm
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def test_grad_log1msigm(self):
# At some point, this returned nan, because (1 - sigm(x)) was
# on both the numerator and the denominator of a fraction,
# but the two nodes in question had not been merged.
x = tensor.matrix('x')
lr = tensor.scalar('lr')
s = sigmoid(x)
l = T.log(1 - s)
c = l.mean()
ux = x - lr * theano.grad(c, x)
# Before the optimization, inf and NaN will be produced in the graph,
# and DebugMode will complain. Everything is fine afterwards.
mode = self.get_mode()
if not isinstance(mode, theano.compile.DebugMode):
f = theano.function([x, lr], ux, mode=mode)
assert hasattr(f.maker.fgraph.outputs[0].tag, 'trace')
ux_v = f([[50]], 0.1)
assert not numpy.isnan(ux_v)
示例12: compute_log_ei
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def compute_log_ei(self, x, incumbent):
Kzz = compute_kernel(self.lls, self.lsf, self.z, self.z) + T.eye(self.z.shape[ 0 ]) * self.jitter * T.exp(self.lsf)
KzzInv = T.nlinalg.MatrixInversePSD()(Kzz)
LLt = T.dot(self.LParamPost, T.transpose(self.LParamPost))
covCavityInv = KzzInv + LLt * casting(self.n_points - self.set_for_training) / casting(self.n_points)
covCavity = T.nlinalg.MatrixInversePSD()(covCavityInv)
meanCavity = T.dot(covCavity, casting(self.n_points - self.set_for_training) / casting(self.n_points) * self.mParamPost)
KzzInvcovCavity = T.dot(KzzInv, covCavity)
KzzInvmeanCavity = T.dot(KzzInv, meanCavity)
Kxz = compute_kernel(self.lls, self.lsf, x, self.z)
B = T.dot(KzzInvcovCavity, KzzInv) - KzzInv
v_out = T.exp(self.lsf) + T.dot(Kxz * T.dot(Kxz, B), T.ones_like(self.z[ : , 0 : 1 ])) # + T.exp(self.lvar_noise)
m_out = T.dot(Kxz, KzzInvmeanCavity)
s = (incumbent - m_out) / T.sqrt(v_out)
log_ei = T.log((incumbent - m_out) * ratio(s) + T.sqrt(v_out)) + log_n_pdf(s)
return log_ei
示例13: softmaxReScale
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def softmaxReScale(self,energy_,threshould):
#{{{
#in energy_, the goundthrud should be max
assert energy_.ndim==1;
#convert threshould from percentage to energy_;
threshould_=T.log(T.exp(energy_-energy_.max()).sum())+T.log(threshould)+energy_.max()
energy=self.reScale(energy_,threshould_);
return T.nnet.softmax(energy);
#}}}
示例14: log_sum_exp
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def log_sum_exp(x, axis=None):
"""
Sum probabilities in the log-space.
"""
xmax = x.max(axis=axis, keepdims=True)
xmax_ = x.max(axis=axis)
return xmax_ + T.log(T.exp(x - xmax).sum(axis=axis))
示例15: log
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import log [as 别名]
def log(x):
return T.log(x)