本文整理汇总了Python中theano.tensor.le函数的典型用法代码示例。如果您正苦于以下问题:Python le函数的具体用法?Python le怎么用?Python le使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了le函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prepareTraining
def prepareTraining(self):
'''
Prepares the relevant functions
(details on neural_net_creator's prepareTraining)
'''
#loss objective to minimize
self.prediction = lasagne.layers.get_output(self.network)
self.prediction=self.prediction[:,0]
#self.loss = lasagne.objectives.categorical_crossentropy(self.prediction, self.target_var)
#the loss is now the squared error in the output
self.loss = lasagne.objectives.squared_error(self.prediction, self.target_var)
self.loss = self.loss.mean()
self.params = lasagne.layers.get_all_params(self.network, trainable=True)
self.updates = lasagne.updates.nesterov_momentum(
self.loss, self.params, learning_rate=0.01, momentum=0.9)
self.test_prediction = lasagne.layers.get_output(self.network, deterministic=True)
self.test_prediction=self.test_prediction[:,0]
self.test_loss = lasagne.objectives.squared_error(self.test_prediction, self.target_var)
self.test_loss = self.test_loss.mean()
#the accuracy is now the number of sample that achieve a 0.01 precision (can be changed)
self.test_acc = T.mean(T.le(T.abs_(T.sub(self.test_prediction,self.target_var)),0.01)
, dtype=theano.config.floatX)
self.test_acc2 = T.mean(T.le(T.abs_(T.sub(self.test_prediction,self.target_var)),0.05)
, dtype=theano.config.floatX)
self.test_acc3 = T.mean(T.le(T.abs_(T.sub(self.test_prediction,self.target_var)),0.1)
, dtype=theano.config.floatX)
self.train_fn = theano.function([self.input_var, self.target_var], self.loss, updates=self.updates)
self.val_fn = theano.function([self.input_var, self.target_var], [self.test_loss,self.test_acc,self.test_acc2,self.test_acc3])
self.use = theano.function([self.input_var],[self.test_prediction])
示例2: _get_targets
def _get_targets(y, log_y_hat, y_mask, y_hat_mask):
'''
Returns the target values according to the CTC cost with respect to y_hat.
Note that this is part of the gradient with respect to the softmax output
and not with respect to the input of the original softmax function.
All computations are done in log scale
'''
num_classes = log_y_hat.shape[2] - 1
blanked_y, blanked_y_mask = _add_blanks(
y=y,
blank_symbol=num_classes,
y_mask=y_mask)
log_alpha, log_beta = _log_forward_backward(blanked_y,
log_y_hat, blanked_y_mask,
y_hat_mask, num_classes)
# explicitly not using a mask to prevent inf - inf
y_prob = _class_batch_to_labeling_batch(blanked_y, log_y_hat,
y_hat_mask=None)
marginals = log_alpha + log_beta - y_prob
max_marg = marginals.max(2)
max_marg = T.switch(T.le(max_marg, -np.inf), 0, max_marg)
log_Z = T.log(T.exp(marginals - max_marg[:,:, None]).sum(2))
log_Z = log_Z + max_marg
log_Z = T.switch(T.le(log_Z, -np.inf), 0, log_Z)
targets = _labeling_batch_to_class_batch(blanked_y,
T.exp(marginals -
log_Z[:,:, None]),
num_classes + 1)
return targets
示例3: call
def call(self, X):
if type(X) is not list or len(X) != 2:
raise Exception("SquareAttention must be called on a list of two tensors. Got: " + str(X))
frame, position = X[0], X[1]
# Reshaping the input to exclude the time dimension
frameShape = K.shape(frame)
positionShape = K.shape(position)
(chans, height, width) = frameShape[-3:]
targetDim = positionShape[-1]
frame = K.reshape(frame, (-1, chans, height, width))
position = K.reshape(position, (-1, ) + (targetDim, ))
# Applying the attention
hw = THT.abs_(position[:, 2] - position[:, 0]) * self.scale / 2.0
hh = THT.abs_(position[:, 3] - position[:, 1]) * self.scale / 2.0
position = THT.maximum(THT.set_subtensor(position[:, 0], position[:, 0] - hw), -1.0)
position = THT.minimum(THT.set_subtensor(position[:, 2], position[:, 2] + hw), 1.0)
position = THT.maximum(THT.set_subtensor(position[:, 1], position[:, 1] - hh), -1.0)
position = THT.minimum(THT.set_subtensor(position[:, 3], position[:, 3] + hh), 1.0)
rX = Data.linspace(-1.0, 1.0, width)
rY = Data.linspace(-1.0, 1.0, height)
FX = THT.gt(rX, position[:,0].dimshuffle(0,'x')) * THT.le(rX, position[:,2].dimshuffle(0,'x'))
FY = THT.gt(rY, position[:,1].dimshuffle(0,'x')) * THT.le(rY, position[:,3].dimshuffle(0,'x'))
m = FY.dimshuffle(0, 1, 'x') * FX.dimshuffle(0, 'x', 1)
m = m + self.alpha - THT.gt(m, 0.) * self.alpha
frame = frame * m.dimshuffle(0, 'x', 1, 2)
# Reshaping the frame to include time dimension
output = K.reshape(frame, frameShape)
return output
示例4: calc_time_gate
def calc_time_gate(time_input_n):
# Broadcast the time across all units
t_broadcast = time_input_n.dimshuffle([0,'x'])
# Get the time within the period
in_cycle_time = T.mod(t_broadcast + shift_broadcast, period_broadcast)
# Find the phase
is_up_phase = T.le(in_cycle_time, on_mid_broadcast)
is_down_phase = T.gt(in_cycle_time, on_mid_broadcast)*T.le(in_cycle_time, on_end_broadcast)
# Set the mask
sleep_wake_mask = T.switch(is_up_phase, in_cycle_time/on_mid_broadcast,
T.switch(is_down_phase,
(on_end_broadcast-in_cycle_time)/on_mid_broadcast,
off_slope*(in_cycle_time/period_broadcast)))
return sleep_wake_mask
示例5: tied_neighbours
def tied_neighbours(preds, n_sample_preds, n_classes):
eps = 1e-8
#preds = T.clip(preds, eps, 1-eps)
preds_per_trial_row = preds.reshape((-1, n_sample_preds, n_classes))
earlier_neighbours = preds_per_trial_row[:,:-1]
later_neighbours = preds_per_trial_row[:,1:]
# Have to now ensure first values are larger zero
# for numerical stability :/
# Example of problem otherwise:
"""
a = T.fmatrix()
b = T.fmatrix()
soft_out_a =softmax(a)
soft_out_b =softmax(b)
loss = categorical_crossentropy(soft_out_a[:,1:],soft_out_b[:,:-1])
neigh_fn = theano.function([a,b], loss)
neigh_fn(np.array([[0,1000,0]], dtype=np.float32),
np.array([[0.1,0.9,0.3]], dtype=np.float32))
-> inf
"""
# renormalize(?)
earlier_neighbours = (T.gt(earlier_neighbours, eps) * earlier_neighbours +
T.le(earlier_neighbours, eps) * earlier_neighbours + eps)
loss = categorical_crossentropy(earlier_neighbours, later_neighbours)
return loss
示例6: each_loss
def each_loss(outpt, inpt):
# y 是填充了blank之后的ans
blank = 26
y_nblank = T.neq(inpt, blank)
n = T.dot(y_nblank, y_nblank) # 真实的字符长度
N = 2 * n + 1 # 填充后的字符长度,去除尾部多余的填充
labels = inpt[:N]
labels2 = T.concatenate((labels, [blank, blank]))
sec_diag = T.neq(labels2[:-2], labels2[2:]) * T.eq(labels2[1:-1], blank)
recurrence_relation = \
T.eye(N) + \
T.eye(N, k=1) + \
T.eye(N, k=2) * sec_diag.dimshuffle((0, 'x'))
pred_y = outpt[:, labels]
fwd_pbblts, _ = theano.scan(
lambda curr, accum: T.switch(T.eq(curr*T.dot(accum, recurrence_relation), 0.0),
T.dot(accum, recurrence_relation)
, curr*T.dot(accum, recurrence_relation)),
sequences=[pred_y],
outputs_info=[T.eye(N)[0]]
)
#return fwd_pbblts
#liklihood = fwd_pbblts[0, 0]
liklihood = fwd_pbblts[-1, -1] + fwd_pbblts[-1, -2]
#liklihood = T.switch(T.lt(liklihood, 1e-35), 1e-35, liklihood)
#loss = -T.log(T.cast(liklihood, "float32"))
#loss = 10 * (liklihood - 1) * (liklihood - 100)
loss = (T.le(liklihood, 1.0)*(10*(liklihood-1)*(liklihood-100)))+(T.gt(liklihood, 1.0)*(-T.log(T.cast(liklihood, "float32"))))
return loss
示例7: __init__
def __init__(self, input, sigma=20.0, window_radius=60):
self.input = input
self.sigma = theano.shared(value=np.array(sigma, dtype=theano.config.floatX), name='sigma')
apply_blur = T.gt(self.sigma, 0.0)
no_blur = T.le(self.sigma, 0.0)
self.output = ifelse(no_blur, input, gaussian_filter(input.dimshuffle('x', 0, 1), self.sigma, window_radius)[0, :, :])
self.params = [self.sigma]
示例8: compile
def compile(self):
# 1D: n_words, 2D: batch * n_cands
self.x = T.imatrix()
self.y = T.fvector()
self.train_inputs = [self.x, self.y]
self.pred_inputs = [self.x]
self.activation = self.args.activation
self.n_d = self.args.hidden_dim
self.n_e = self.emb_layers[0].n_d
self.pad_id = self.emb_layers[0].vocab_map[PAD]
self.dropout = theano.shared(np.float32(self.args.dropout).astype(theano.config.floatX))
self._set_layers(args=self.args, n_d=self.n_d, n_e=self.n_e)
###########
# Network #
###########
h_in = self._input_layer(x=self.x)
h = self._mid_layer(h_prev=h_in, x=self.x, pad_id=self.pad_id)
y_scores = self._output_layer(h=h)
self.y_pred = T.le(0.5, y_scores)
#########################
# Set an objective func #
#########################
self.set_params(layers=self.layers)
self.loss = self.set_loss(self.y, y_scores)
self.cost = self.set_cost(args=self.args, params=self.params, loss=self.loss)
示例9: gate_layer
def gate_layer(tparams, X_word, X_char, options, prefix, pretrain_mode, activ='lambda x: x', **kwargs):
"""
compute the forward pass for a gate layer
Parameters
----------
tparams : OrderedDict of theano shared variables, {parameter name: value}
X_word : theano 3d tensor, word input, dimensions: (num of time steps, batch size, dim of vector)
X_char : theano 3d tensor, char input, dimensions: (num of time steps, batch size, dim of vector)
options : dictionary, {hyperparameter: value}
prefix : string, layer name
pretrain_mode : theano shared scalar, 0. = word only, 1. = char only, 2. = word & char
activ : string, activation function: 'liner', 'tanh', or 'rectifier'
Returns
-------
X : theano 3d tensor, final vector, dimensions: (num of time steps, batch size, dim of vector)
"""
# compute gating values, Eq.(3)
G = tensor.nnet.sigmoid(tensor.dot(X_word, tparams[p_name(prefix, 'v')]) + tparams[p_name(prefix, 'b')][0])
X = ifelse(tensor.le(pretrain_mode, numpy.float32(1.)),
ifelse(tensor.eq(pretrain_mode, numpy.float32(0.)), X_word, X_char),
G[:, :, None] * X_char + (1. - G)[:, :, None] * X_word)
return eval(activ)(X)
示例10: logp_loss3
def logp_loss3(self, x, y, fake_label,neg_label, pos_ratio = 0.5): #adopt maxout for negative
# pos_rati0 means pos examples weight (0.5 means equal 1:1)
print "adopt positives weight ............. "+str(pos_ratio)
y = y.dimshuffle((1,0))
inx = x.dimshuffle((1,0))
fake_mask = T.neq(y, fake_label)
y = y*fake_mask
pos_mask = T.and_(fake_mask, T.le(y, neg_label-1))*pos_ratio
neg_mask = T.ge(y, neg_label)*(1- pos_ratio)
pos_score, neg_score = self.structure2(inx,False)
maxneg = T.max(neg_score, axis = -1)
scores = T.concatenate((pos_score, maxneg.dimshuffle((0,1,'x'))), axis = 2)
d3shape = scores.shape
#seq*batch , label
scores = scores.reshape((d3shape[0]*d3shape[1], d3shape[2]))
pro = T.nnet.softmax(scores)
_logp = T.nnet.categorical_crossentropy(pro, y.flatten())
_logp = _logp.reshape(fake_mask.shape)
loss = (T.sum(_logp*pos_mask)+ T.sum(_logp*neg_mask))/ (T.sum(pos_mask)+T.sum(neg_mask))
pos_loss = T.sum(_logp*pos_mask)
neg_loss = T.sum(_logp*neg_mask)
return loss, pos_loss, neg_loss
示例11: logp
def logp(self, value):
p = self.p
k = self.k
sumto1 = theano.gradient.zero_grad(T.le(abs(T.sum(p) - 1), 1e-5))
return bound(T.log(p[value]),
value >= 0, value <= (k - 1),
sumto1)
示例12: objective
def objective(y_true, y_pred, P, Q, alpha=0., beta=0.15, dbeta=0., gamma=0.01, gamma1=-1., poos=0.23, eps=1e-6):
'''Expects a binary class matrix instead of a vector of scalar classes.
'''
beta = np.float32(beta)
dbeta = np.float32(dbeta)
gamma = np.float32(gamma)
poos = np.float32(poos)
eps = np.float32(eps)
# scale preds so that the class probas of each sample sum to 1
y_pred += eps
y_pred /= y_pred.sum(axis=-1, keepdims=True)
y_true = T.cast(y_true.flatten(), 'int64')
y1 = T.and_(T.gt(y_true, 0), T.le(y_true, Q)) # in-set
y0 = T.or_(T.eq(y_true, 0), T.gt(y_true, Q)) # out-of-set or unlabeled
y0sum = y0.sum() + eps # number of oos
y1sum = y1.sum() + eps # number of in-set
# we want to reduce cross entrophy of labeled data
# convert all oos/unlabeled to label=0
cost0 = T.nnet.categorical_crossentropy(y_pred, T.switch(y_true <= Q, y_true, 0))
cost0 = T.dot(y1, cost0) / y1sum # average cost per labeled example
if alpha:
cost1 = T.nnet.categorical_crossentropy(y_pred, y_pred)
cost1 = T.dot(y0, cost1) / y0sum # average cost per labeled example
cost0 += alpha*cost1
# we want to increase the average entrophy in each batch
# average over batch
if beta:
y_pred_avg0 = T.dot(y0, y_pred) / y0sum
y_pred_avg0 = T.clip(y_pred_avg0, eps, np.float32(1) - eps)
y_pred_avg0 /= y_pred_avg0.sum(axis=-1, keepdims=True)
cost2 = T.nnet.categorical_crossentropy(y_pred_avg0.reshape((1,-1)), P-dbeta)[0] # [None,:]
cost2 = T.switch(y0sum > 0.5, cost2, 0.) # ignore cost2 if no samples
cost0 += beta*cost2
# binary classifier score
if gamma:
y_pred0 = T.clip(y_pred[:,0], eps, np.float32(1) - eps)
if gamma1 < 0.:
cost3 = - T.dot(poos*y0,T.log(y_pred0)) - T.dot(np.float32(1)-poos*y0.T,T.log(np.float32(1)-y_pred0))
cost3 /= y_pred.shape[0]
cost0 += gamma*cost3
elif gamma1 > 0.:
cost3 = - T.dot(poos*y0,T.log(y_pred0)) - T.dot((np.float32(1)-poos)*y0,T.log(np.float32(1)-y_pred0))
cost3 /= y0sum
cost31 = - T.dot(y1,T.log(np.float32(1)-y_pred0))
cost3 /= y1sum
cost0 += gamma*cost3 + gamma1*cost31
else: # gamma1 == 0.
cost3 = - T.dot(poos*y0,T.log(y_pred0)) - T.dot((np.float32(1)-poos)*y0, T.log(np.float32(1)-y_pred0))
cost3 /= y0sum
cost0 += gamma*cost3
return cost0
示例13: cost
def cost(self,Y,Y_hat):
w = T.fscalar()
r = self.r
w = 0.05
i = T.le(Y,w)
j = T.eq(i,0)
z = T.join(0,Y[i]/r,Y[j])
z_hat = T.join(0,Y_hat[i]/r,Y_hat[j])
return super(linear_mlp_bayesian_cost,self).cost(z,z_hat)
示例14: clip_gradients
def clip_gradients(gparams, threshold=5.):
clipped_gparams = []
for gparam in gparams:
norm_gparam = T.sqrt(T.sqr(gparam).sum())
clipped_gparams.append(T.switch(T.le(norm_gparam, threshold),
gparam,
(gparam/norm_gparam)*threshold))
return clipped_gparams
示例15: huber_loss
def huber_loss(y_true, y_pred, delta=1., axis=None):
a = y_true - y_pred
squared_loss = 0.5*T.sqr(a)
absolute_loss = (delta*abs(a) - 0.5*T.sqr(delta))
cost = T.switch(T.le(abs(a), delta),
squared_loss,
absolute_loss)
return cost.mean(axis=axis)