本文整理汇总了Python中theano.tensor.neq方法的典型用法代码示例。如果您正苦于以下问题:Python tensor.neq方法的具体用法?Python tensor.neq怎么用?Python tensor.neq使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.tensor
的用法示例。
在下文中一共展示了tensor.neq方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_cost
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def build_cost(logits, targets):
"""
Build a classification cost function.
"""
# Clip gradients coming from the cost function.
logits = theano.gradient.grad_clip(
logits, -1. * FLAGS.clipping_max_value, FLAGS.clipping_max_value)
predicted_dist = T.nnet.softmax(logits)
costs = T.nnet.categorical_crossentropy(predicted_dist, targets)
cost = costs.mean()
pred = T.argmax(logits, axis=1)
acc = 1. - T.mean(T.cast(T.neq(pred, targets), theano.config.floatX))
return cost, acc
示例2: errors
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def errors(self, y):
"""Return a float representing the number of errors in the minibatch
over the total number of examples of the minibatch ; zero one
loss over the size of the minibatch
:type y: theano.tensor.TensorType
:param y: corresponds to a vector that gives for each example the
correct label
"""
# check if y has same dimension of y_pred
if y.ndim != self.y_pred.ndim:
raise TypeError(
'y should have the same shape as self.y_pred',
('y', y.type, 'y_pred', self.y_pred.type)
)
# check if y is of the correct datatype
if y.dtype.startswith('int'):
# the T.neq operator returns a vector of 0s and 1s, where 1
# represents a mistake in prediction
return T.mean(T.neq(self.y_pred, y))
else:
raise NotImplementedError()
示例3: compute_emb
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def compute_emb(x, W):
def _step(xi, emb, W):
if prm.att_doc:
new_shape = (xi.shape[0], xi.shape[1], xi.shape[2], prm.dim_emb)
else:
new_shape = (xi.shape[0], xi.shape[1], prm.dim_emb)
out = W[xi.flatten()].reshape(new_shape).sum(-2)
return out / tensor.maximum(1., tensor.neq(xi,-1).astype('float32').sum(-1, keepdims=True))
if prm.att_doc:
emb_init = tensor.alloc(0., x.shape[1], x.shape[2], prm.dim_emb)
else:
emb_init = tensor.alloc(0., x.shape[1], prm.dim_emb)
(embs), scan_updates = theano.scan(_step,
sequences=[x],
outputs_info=[emb_init],
non_sequences=[W],
name='emb_scan',
n_steps=x.shape[0])
return embs
示例4: errors4one
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def errors4one(self, z, out, weight=None, distLabelType='12C'):
distBins = config.distCutoffs[distLabelType]
label8 = DistanceUtils.LabelsOfOneDistance(config.ContactDefinition, distBins)
label15 = DistanceUtils.LabelsOfOneDistance(config.InteractionLimit, distBins)
z3C = T.cast( T.ge(z, label8), 'int32') + T.cast( T.ge(z, label15), 'int32')
o3C = T.cast( T.ge(out, label8), 'int32') + T.cast( T.ge(out, label15), 'int32')
if weight is not None:
err = T.sum( T.mul(weight, T.neq(o3C, z3C) ) )*1./T.sum(weight)
else:
err = T.mean( T.neq(o3C , z3C) )
## err is s scalar, convert it to a tensor with ndim=1
return T.stack([err] )
## this function returns a vector of errors, the size of this vector is equal to the sum of ValueDims for all the responses
示例5: errors
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def errors(self, y, sampleWeight=None):
###Return the 0-1 error rate in a minibatch y: a vector of true labels
# check if y has same dimension of y_pred
if y.ndim != self.y_pred.ndim:
raise TypeError(
'y should have the same shape as self.y_pred',
('y', y.type, 'y_pred', self.y_pred.type)
)
# check if y is of the correct datatype
if y.dtype.startswith('int'):
# the T.neq operator returns a vector of 0s and 1s, where 1 represents a mistake in prediction
if sampleWeight is not None:
return T.sum( T.mul(sampleWeight, T.neq(self.y_pred, y) ) ) * 1./T.sum(sampleWeight)
else:
return T.mean(T.neq(self.y_pred, y))
else:
raise NotImplementedError()
### A neural network Logistic Regression for Classification
示例6: recurrence_relation
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def recurrence_relation(y, y_mask, blank_symbol):
n_y = y.shape[0]
blanks = tensor.zeros((2, y.shape[1])) + blank_symbol
ybb = tensor.concatenate((y, blanks), axis=0).T
sec_diag = (tensor.neq(ybb[:, :-2], ybb[:, 2:]) *
tensor.eq(ybb[:, 1:-1], blank_symbol) *
y_mask.T)
# r1: LxL
# r2: LxL
# r3: LxLxB
r2 = tensor.eye(n_y, k=1)
r3 = (tensor.eye(n_y, k=2).dimshuffle(0, 1, 'x') *
sec_diag.dimshuffle(1, 'x', 0))
return r2, r3
示例7: errors
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def errors(self, y):
"""Return a float representing the number of errors in the minibatch
over the total number of examples of the minibatch ; zero one
loss over the size of the minibatch
:type y: theano.tensor.TensorType
:param y: corresponds to a vector that gives for each example the
correct label
"""
# check if y has same dimension of y_pred
if y.ndim != self.class_prediction.ndim:
raise TypeError('y should have the same shape as self.class_prediction',
('y', y.type, 'class_prediction', self.class_prediction.type))
# check if y is of the correct datatype
if y.dtype.startswith('int'):
# the T.neq operator returns a vector of 0s and 1s, where 1
# represents a mistake in prediction
return T.mean(T.neq(self.class_prediction, y))
else:
print "something went wrong"
raise NotImplementedError()
示例8: errors
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def errors(self, y):
""" Return a float representing the rel. number of errors in the minibatch (0 to 1=all wrong)
0-1 loss over the size of the minibatch
:type y: theano.tensor.TensorType
:param y: corresponds to a vector that gives for each example the
correct label
"""
# check if y has same dimension of class_prediction
if y.ndim != self.class_prediction.ndim:
raise TypeError('y should have the same shape as self.class_prediction',
('y', y.type, 'class_prediction', self.class_prediction.type))
# check if y is of the correct datatype
if y.dtype.startswith('int'):
# the T.neq operator returns a vector of 0s and 1s, where 1
# represents a mistake in prediction
return T.mean(T.neq(self.class_prediction, y), dtype='float32')
else:
raise NotImplementedError()
示例9: apply
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def apply(self, y, y_hat):
# Support checkpoints that predate self.top_k
top_k = getattr(self, 'top_k', 1)
if top_k == 1:
mistakes = tensor.neq(y, y_hat.argmax(axis=1))
else:
row_offsets = theano.tensor.arange(0, y_hat.flatten().shape[0],
y_hat.shape[1])
truth_score = y_hat.flatten()[row_offsets + y]
# We use greater than _or equals_ here so that the model
# _must_ have its guess in the top k, and cannot extend
# its effective "list of predictions" by tying lots of things
# for k-th place.
higher_scoring = tensor.ge(y_hat, truth_score.dimshuffle(0, 'x'))
# Because we used greater-than-or-equal we have to correct for
# counting the true label.
num_higher = higher_scoring.sum(axis=1) - 1
mistakes = tensor.ge(num_higher, top_k)
return mistakes.mean(dtype=theano.config.floatX)
示例10: errors
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def errors(self, y):
"""Return a float representing the number of errors in the minibatch ;
zero one loss over the size of the minibatch
:type y: theano.tensor.TensorType
:param y: corresponds to a vector that gives for each example the
correct label
"""
# check if y has same dimension of y_pred
if y.ndim != self.y_pred.ndim:
raise TypeError('y should have the same shape as self.y_pred',
('y', target.type, 'y_pred', self.y_pred.type))
# check if y is of the correct datatype
if y.dtype.startswith('int'):
# the T.neq operator returns a vector of 0s and 1s, where 1
# represents a mistake in prediction
return T.mean(T.neq(self.y_pred, y))
else:
raise NotImplementedError()
示例11: _create_get_word_prob_function
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def _create_get_word_prob_function(self):
"""Creates a Theano function that returns the unigram probability of a
word within its class.
"""
word_id = tensor.scalar('word_id', dtype=self._count_type)
word_id.tag.test_value = 0
word_count = self._word_counts[word_id]
class_id = self._word_to_class[word_id]
class_count = self._class_counts[class_id]
result = tensor.switch(tensor.neq(class_count, 0),
word_count / class_count,
0)
self.get_word_prob = theano.function(
[word_id],
result,
name='get_word_prob')
示例12: errors
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def errors(self, y):
"""Return a float representing the number of errors in the minibatch ;
zero one loss over the size of the minibatch
:type y: theano.tensor.TensorType
:param y: corresponds to a vector that gives for each example the
correct label
"""
# check if y has same dimension of y_pred
if y.ndim != self.y_pred.ndim:
raise TypeError('y should have the same shape as self.y_pred',
('y', target.type, 'y_pred', self.y_pred.type))
# check if y is of the correct datatype
if y.dtype.startswith('int'):
# the T.neq operator returns a vector of 0s and 1s, where 1
# represents a mistake in prediction
return T.mean(T.neq(self.y_pred, y))
else:
raise NotImplementedError()
示例13: get_layer_monitoring_channels
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def get_layer_monitoring_channels(self, state_below=None, state=None,
target=None):
mx = state.max(axis=1)
rval = OrderedDict([('mean_max_class', mx.mean()),
('max_max_class', mx.max()),
('min_max_class', mx.min())])
if target is not None:
y_hat = T.argmax(state, axis=1)
y = T.argmax(target, axis=1)
misclass = T.neq(y, y_hat).mean()
misclass = T.cast(misclass, config.floatX)
rval['misclass'] = misclass
rval['nll'] = self.cost(Y_hat=state, Y=target)
rval['ppl'] = 2 ** (rval['nll'] / T.log(2))
return rval
示例14: errors
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def errors(self, y):
"""
误差计算函数。传入的参数参考negative_log_likehood.
其作用就是统计预测正确的样本数占本批次总样本数的比例。
"""
# 检查 传入正确标签向量y和前面做出的预测向量y_pred是否是具有相同的维度。如果不相同怎么去判断某个样本预测的对还是不对?
# y.ndim返回y的维数
# raise是抛出异常
if y.ndim != self.y_pred.ndim:
raise TypeError("y doesn't have the same shape as self.y_pred")
# 继续检查y是否是有效数据。依据就是本实验中正确标签数据的存储类型是int
# 如果数据有效,则计算:
# T.neq(y1, y2)是计算y1与y2对应元素是否相同,如果相同便是0,否则是1。
# 举例:如果y1=[1,2,3,4,5,6,7,8,9,0] y2=[1,1,3,3,5,6,7,8,9,0]
# 则,err = T.neq(y1,y2) = [0,1,0,1,0,0,0,0,0,0],其中有3个1,即3个元素不同
# T.mean()的作用就是求均值。那么T.mean(err) = (0+1+0+1+0+0+0+0+0+0)/10 = 0.3,即误差率为30%
if y.dtype.startswith('int'):
return T.mean(T.neq(self.y_pred, y))
else:
raise NotImplementedError()
示例15: compute_tensor
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import neq [as 别名]
def compute_tensor(self, x):
if self.cached:
if x.ndim == 1:
ret_tensor = self.onehot_list[x]
else:
ret_tensor = self.onehot_list[x.flatten()].reshape((x.shape[0], x.shape[1], self.vocab_size))
else:
ret_tensor = onehot_tensor(x, self.vocab_size)
if self.zero_index != None:
mask = T.neq(x, self.zero_index)
if x.ndim == 1:
ret_tensor *= mask[:, None]
else:
ret_tensor *= mask[:, :, None]
if self.mask:
if x.ndim == 1:
ret_tensor *= self.mask[:, None]
else:
ret_tensor *= self.mask[:, :, None]
return ret_tensor