我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用theano.tensor.neq()。
def create_training_computation_graphs(): x = tensor.tensor4('features') y = tensor.imatrix('targets') convnet, mlp = create_model_bricks() y_hat = mlp.apply(convnet.apply(x).flatten(ndim=2)) cost = BinaryCrossEntropy().apply(y, y_hat) accuracy = 1 - tensor.neq(y > 0.5, y_hat > 0.5).mean() cg = ComputationGraph([cost, accuracy]) # Create a graph which uses batch statistics for batch normalization # as well as dropout on selected variables bn_cg = apply_batch_normalization(cg) bricks_to_drop = ([convnet.layers[i] for i in (5, 11, 17)] + [mlp.application_methods[1].brick]) variables_to_drop = VariableFilter( roles=[OUTPUT], bricks=bricks_to_drop)(bn_cg.variables) bn_dropout_cg = apply_dropout(bn_cg, variables_to_drop, 0.5) return cg, bn_dropout_cg
def dice_loss(y_pred, y_true, void_class, class_for_dice=1): ''' Dice loss -- works for only binary classes. y_pred is a softmax output y_true is one hot ''' smooth = 1 y_true_f = T.flatten(y_true[:, class_for_dice, :, :]) y_true_f = T.cast(y_true_f, 'int32') y_pred_f = T.flatten(y_pred[:, class_for_dice, :, :]) # remove void classes from dice if len(void_class): for i in range(len(void_class)): # get idx of non void classes and remove void classes # from y_true and y_pred idxs = T.neq(y_true_f, void_class[i]).nonzero() y_pred_f = y_pred_f[idxs] y_true_f = y_true_f[idxs] intersection = T.sum(y_true_f * y_pred_f) return -(2.*intersection+smooth) / (T.sum(y_true_f)+T.sum(y_pred_f)+smooth)
def errors(self, y): """ returns 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. """ if y.ndim != self.y_decision.ndim: raise TypeError("y should have the same shape as self.y_decision", ('y', y.type, "y_decision", self.y_decision.type)) if y.dtype.startswith('int') or y.dtype.startswith('uint'): # The T.neq operator returns a vector of 0s and 1s, where: # 1 represents a mistake in classification return T.mean(T.neq(self.y_decision, y)) else: raise NotImplementedError()
def raw_prediction_errors(self, y): """ Returns a binary array where each each element indicates if the corresponding sample has been correctly classified (0) or not (1) in the minibatch. :type y: theano.tensor.TensorType :param y: corresponds to a vector that gives for each example the correct label. """ if y.ndim != self.y_decision.ndim: raise TypeError("y should have the same shape as self.y_decision", ('y', y.type, "y_decision", self.y_decision.type)) if y.dtype.startswith('int') or y.dtype.startswith('uint'): # The T.neq operator returns a vector of 0s and 1s, where: # 1 represents a mistake in classification return T.neq(self.y_decision, y) else: raise NotImplementedError()
def error_per_calss(self, y): """ Return an array where each value is the error for the corresponding classe in the minibatch. :type y: theano.tensor.TensorType :param y: corresponds to a vector that gives for each example the correct label. """ if y.ndim != self.y_decision.ndim: raise TypeError("y should have the same shape as self.y_decision", ('y', y.type, "y_decision", self.y_decision.type)) if y.dtype.startswith('int') or y.dtype.startswith('uint'): y_decision_res = T.neq(self.y_decision, y) for (i, y_decision_r) in enumerate(y_decision_res): self.n_classes_seen[y[i]] += 1 if y_decision_r: self.n_wrong_classif_made[y[i]] += 1 pred_per_class = self.n_wrong_classif_made / self.n_classes_seen return T.mean(y_decision_res), pred_per_class else: raise NotImplementedError()
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()
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', 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()
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: return T.sum((y - self.y_pred) ** 2);
def errors_top_x(self, y, num_top=5): 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)) if num_top != 5: print('val errors from top %d' % num_top) ############TOP 5 VERSION########## # 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 y_pred_top_x = T.argsort(self.p_y_given_x, axis=1)[:, -num_top:] y_top_x = y.reshape((y.shape[0], 1)).repeat(num_top, axis=1) return T.mean(T.min(T.neq(y_pred_top_x, y_top_x).astype('int8'), axis=1)) else: raise NotImplementedError()
def errors_top_x(self, y, num_top=5): 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)) if num_top != 5: print 'val errors from top %d' % num_top ############TOP 5 VERSION########## # 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 y_pred_top_x = T.argsort(self.p_y_given_x, axis=1)[:, -num_top:] y_top_x = y.reshape((y.shape[0], 1)).repeat(num_top, axis=1) # return T.mean(T.min( return T.neq(y_pred_top_x, y_top_x) # , axis=1)) else: raise NotImplementedError()
def _ctc_normal(self, predict,labels): n = labels.shape[0] labels2 = T.concatenate((labels, [self.tpo["CTC_blank"], self.tpo["CTC_blank"]])) sec_diag = T.neq(labels2[:-2], labels2[2:]) * \ T.eq(labels2[1:-1], self.tpo["CTC_blank"]) recurrence_relation = \ T.eye(n) + \ T.eye(n, k=1) + \ T.eye(n, k=2) * sec_diag.dimshuffle((0, 'x')) pred_y = predict[:, labels] probabilities, _ = theano.scan( lambda curr, accum: curr * T.dot(accum, recurrence_relation), sequences=[pred_y], outputs_info=[T.eye(n)[0]] ) labels_probab = T.sum(probabilities[-1, -2:]) return -T.log(labels_probab)
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()
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.sum(T.neq(self.y_pred, y)) else: raise NotImplementedError()
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
def errors(self, y): """ Return a float representing the number of errors in the data; zero-one loss """ #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.sum(T.neq(self.y_pred, y)) else: raise NotImplementedError()
def test_neq(self): x = T.dmatrix() y = T.dmatrix() f = theano.function([x, y], T.neq(x, y), mode=self.mode) vx = numpy.random.rand(5, 4) vy = numpy.random.rand(5, 4) f(vx, vy) topo = f.maker.fgraph.toposort() assert len(topo) == 1 assert isinstance(topo[0].op, T.Elemwise) assert isinstance(topo[0].op.scalar_op, theano.scalar.NEQ) f2 = theano.function([x], T.neq(x, x), mode=self.mode) assert numpy.all(f2(vx) == numpy.zeros((5, 4))) topo2 = f2.maker.fgraph.toposort() assert len(topo2) == 3 assert isinstance(topo2[-1].op, T.Alloc)
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() pass
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 dimensions 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 isof 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()
def errors(self, y): """Return a float representing the number of errors in the sequence over the total number of examples in the sequence ; zero one loss over the size of the sequence :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_out.ndim: raise TypeError('y should have the same shape as self.y_out', ('y', y.type, 'y_out', self.y_out.type)) if self.output_type in ('binary', 'softmax'): # 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_out, y)) else: raise NotImplementedError()