我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用theano.tensor.mean()。
def get_output_for(self, input, deterministic=False, **kwargs): if deterministic: norm_features = (input-self.avg_batch_mean.dimshuffle(*self.dimshuffle_args)) / T.sqrt(1e-6 + self.avg_batch_var).dimshuffle(*self.dimshuffle_args) else: batch_mean = T.mean(input,axis=self.axes_to_sum).flatten() centered_input = input-batch_mean.dimshuffle(*self.dimshuffle_args) batch_var = T.mean(T.square(centered_input),axis=self.axes_to_sum).flatten() batch_stdv = T.sqrt(1e-6 + batch_var) norm_features = centered_input / batch_stdv.dimshuffle(*self.dimshuffle_args) # BN updates new_m = 0.9*self.avg_batch_mean + 0.1*batch_mean new_v = 0.9*self.avg_batch_var + T.cast((0.1*input.shape[0])/(input.shape[0]-1),th.config.floatX)*batch_var self.bn_updates = [(self.avg_batch_mean, new_m), (self.avg_batch_var, new_v)] if hasattr(self, 'g'): activation = norm_features*self.g.dimshuffle(*self.dimshuffle_args) else: activation = norm_features if hasattr(self, 'b'): activation += self.b.dimshuffle(*self.dimshuffle_args) return self.nonlinearity(activation)
def get_output_for(self, input, init=False, deterministic=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.dot(input, self.W) if init: ma = T.mean(activation, axis=0) activation -= ma.dimshuffle('x',0) stdv = T.sqrt(T.mean(T.square(activation),axis=0)) activation /= stdv.dimshuffle('x',0) self.init_updates = [(self.weight_scale, self.weight_scale/stdv), (self.b, -ma/stdv)] else: activation += self.b.dimshuffle('x', 0) return self.nonlinearity(activation)
def build_objective(model, deterministic=False, epsilon=1e-12): predictions = nn.layers.get_output(model.l_out, deterministic=deterministic) targets = T.cast(nn.layers.get_output(model.l_target), 'int32') enable_targets = nn.layers.get_output(model.l_enable_target) predictions = T.clip(predictions, epsilon, 1.-epsilon) #is_nodule_ground_truth = T.cast(targets[:,0], 'float32') sum_of_objectives = 0 unit_ptr = 0 for obj_idx, obj_name in enumerate(order_objectives): n_classes = len(property_bin_borders[obj_name]) v_obj = objective(obj_idx, (unit_ptr, unit_ptr+n_classes), predictions, targets) if deterministic: d_objectives_deterministic[obj_name] = T.mean(v_obj) else: d_objectives[obj_name] = T.mean(v_obj) #sum_of_objectives += T.mean(enable_targets[obj_idx] * v_obj) sum_of_objectives += T.mean(enable_targets[:,obj_idx] * v_obj) unit_ptr = unit_ptr+n_classes #print 'for debug purposes: unit_ptr', unit_ptr return sum_of_objectives
def negative_log_likelihood(self, y): """ Return the mean of the negative log-likelihood of the prediction of this model under a given target distribution. .. math:: \frac{1}{|\mathcal{D}|} \mathcal{L} (\theta=\{W,b\}, \mathcal{D}) = \frac{1}{|\mathcal{D}|} \sum_{i=0}^{|\mathcal{D}|} \log(P(Y=y^{(i)}|x^{(i)}, W,b)) \\ \ell (\theta=\{W,b\}, \mathcal{D}) :type y: theano.tensor.TensorType :param y: corresponds to a vector that gives for each example the correct label. Note: We use the mean instead of the sum so that the learning rate is less dependent of the batch size. """ if self.is_binary: return -T.mean(T.log(self.p_y_given_x)) return -T.mean(T.log(self.p_y_given_x)[T.arange(y.shape[0]), y])
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 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 get_eval_fn(model, in3D=False, use_dice=False): """Compile the evaluation function of the model.""" if use_dice: insec = T.sum(model.trg * model.output, axis=1) tmp = 1 - 2.0 * insec/(T.sum(model.trg, axis=1) + T.sum(model.output, axis=1)) error = T.mean(tmp) else: error = T.mean(T.mean(T.power(model.output - model.trg, 2), axis=1)) if in3D: x = T.tensor4('x') else: x = T.fmatrix("x") y = T.fmatrix("y") theano_arg_vl = [x, y] output_fn_vl = [error, model.output] eval_fn = theano.function( theano_arg_vl, output_fn_vl, givens={model.x: x, model.trg: y}) return eval_fn
def eval_classificationT( self, y, p_y): """Calculate the error (100 - accuracy) of the DNN in the case of classification. :type y: vector :param y: vector (r,) of labels :type p_y: matrix :param p_y: matrix of the output of the network. Each raw is a vector of probailities (probablities of the classes) """ y_ = T.argmax(p_y, axis = 1) # Accuracy error = 1 - T.mean(T.eq(y_, y) * 1.) error = error * 100. return error
def eval_segmentation_bin( self, y, model_output, th=.5, path='../data/predict/'): '''Evaluation the performance of a binary segmentation. The default used threshold .5. The used evaluation is the mean squarre error. ''' # binarization nbr, dim = y.shape output_bin = np.float32((model_output > th) * 1.) mse = self.MeanSquareError(y, model_output) for i in xrange(nbr): im_gt = Image.fromarray(np.reshape(np.uint8(y[i,:] *255.), (128,128))) im_bin = Image.fromarray(np.reshape(np.uint8(output_bin[i,:] *255.), (128,128))) im_gr = Image.fromarray(np.reshape(np.uint8(model_output[i,:] *255.) , (128,128))) temp = np.concatenate((im_gt, im_bin, im_gr), axis=1) two_imgs = sc.misc.toimage(temp) sc.misc.imsave(path + str(i) +'.png', two_imgs) #two_imgs.show() #raw_input('Press ENTER to continue...') return mse
def save_unit_test(self, list_im_unit_test, output_unit_test_reproj, mean_shape_train, window,path='../data/unit_test/'): """ Save the output of the unit test. """ nbr = list_im_unit_test.shape[0] for i in xrange(nbr): mtx = list_im_unit_test[i][0] mtx3D = np.zeros((window[0],window[1],3)) mtx3D[:,:,0] = mtx mtx3D[:,:,1] = mtx mtx3D[:,:,2] = mtx im = Image.fromarray(np.uint8( mtx3D * 255)) bbx = [0,0, window[0], window[1]] pred = output_unit_test_reproj[i] # display the mean shape #self.show_landmarks_unit_test( im=im, phis_pred=pred, phis_mean_train=mean_shape_train, bbox=bbx, save=True, path=path+str(i)+'.jpg') # don't show the mean shape. #self.show_only_landmarks_unit_test( im=im, phis_pred=pred, bbox=bbx, save=True, path=path+str(i)+'.jpg') self.show_only_landmarks_unit_test( im=im, phis_pred=mean_shape_train, bbox=bbx, save=True, path=path+str(i)+'.jpg')
def get_output_for(self, input, deterministic=False, **kwargs): def _phase_shift(input,r): bsize,c,a,b = input.shape[0],1,self.output_shape[2]//r,self.output_shape[3]//r X = T.reshape(input, (bsize,r,r,a,b)) X = T.transpose(X, (0, 3,4,1,2)) # bsize, a, b, r2,r1 X = T.split(x=X,splits_size=[1]*a,n_splits=a,axis=1) # a, [bsize, b, r, r] X = [T.reshape(x,(bsize,b,r,r))for x in X] X = T.concatenate(X,axis=2) # bsize, b, a*r, r X = T.split(x=X,splits_size =[1]*b,n_splits=b,axis=1) # b, [bsize, a*r, r] X = [T.reshape(x,(bsize,a*r,r))for x in X] X = T.concatenate(X,axis=2) # bsize, a*r, b*r return X.dimshuffle(0,'x',1,2) Xc = T.split(x=input,splits_size =[input.shape[1]//self.c]*self.c,n_splits=self.c,axis=1) return T.concatenate([_phase_shift(xc,self.r) for xc in Xc],axis=1) # Multiscale Dilated Convolution Block # This function (not a layer in and of itself, though you could make it one) returns a set of concatenated conv2d and dilatedconv2d layers. # Each layer uses the same basic filter W, operating at a different dilation factor (or taken as the mean of W for the 1x1 conv). # The channel-wise output of each layer is weighted by a set of coefficients, which are initialized to 1 / the total number of dilation scales, # meaning that were starting by taking an elementwise mean. These should be learnable parameters. # NOTES: - I'm considering changing the variable names to be more descriptive, and look less like ridiculous academic code. It's on the to-do list. # - I keep the bias and nonlinearity out of the default definition for this layer, as I expect it to be batchnormed and nonlinearized in the model config.
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 fit(self, x): s = x.shape x = x.copy().reshape((s[0],np.prod(s[1:]))) m = np.mean(x, axis=0) x -= m sigma = np.dot(x.T,x) / x.shape[0] U, S, V = linalg.svd(sigma) tmp = np.dot(U, np.diag(1./np.sqrt(S+self.regularization))) tmp2 = np.dot(U, np.diag(np.sqrt(S+self.regularization))) self.ZCA_mat = th.shared(np.dot(tmp, U.T).astype(th.config.floatX)) self.inv_ZCA_mat = th.shared(np.dot(tmp2, U.T).astype(th.config.floatX)) self.mean = th.shared(m.astype(th.config.floatX))
def apply(self, x): s = x.shape if isinstance(x, np.ndarray): return np.dot(x.reshape((s[0],np.prod(s[1:]))) - self.mean.get_value(), self.ZCA_mat.get_value()).reshape(s) elif isinstance(x, T.TensorVariable): return T.dot(x.flatten(2) - self.mean.dimshuffle('x',0), self.ZCA_mat).reshape(s) else: raise NotImplementedError("Whitening only implemented for numpy arrays or Theano TensorVariables")
def invert(self, x): s = x.shape if isinstance(x, np.ndarray): return (np.dot(x.reshape((s[0],np.prod(s[1:]))), self.inv_ZCA_mat.get_value()) + self.mean.get_value()).reshape(s) elif isinstance(x, T.TensorVariable): return (T.dot(x.flatten(2), self.inv_ZCA_mat) + self.mean.dimshuffle('x',0)).reshape(s) else: raise NotImplementedError("Whitening only implemented for numpy arrays or Theano TensorVariables") # T.nnet.relu has some issues with very large inputs, this is more stable
def softmax_loss(p_true, output_before_softmax): output_before_softmax -= T.max(output_before_softmax, axis=1, keepdims=True) if p_true.ndim==2: return T.mean(T.log(T.sum(T.exp(output_before_softmax),axis=1)) - T.sum(p_true*output_before_softmax, axis=1)) else: return T.mean(T.log(T.sum(T.exp(output_before_softmax),axis=1)) - output_before_softmax[T.arange(p_true.shape[0]),p_true])
def get_output_for(self, input, **kwargs): return T.mean(input, axis=(2,3))
def build_model(model_): global fn_predict, fn_record global g_ozer, g_mdl g_ozer = dict(simple=VanillaSGD, adam=AdamSGD)[OZER]() g_ozer.lr = LEARN_RATE s_x = T.tensor4('x') s_y = T.ivector('y') s_pdpo = T.scalar() s_out = model_(s_x, s_pdpo) s_y_onehot = T.extra_ops.to_one_hot(s_y, len(g_dataset.label_map)) s_loss = T.mean(-s_y_onehot*T.log(s_out + 1e-3)) s_accr = T.mean( T.switch( T.eq(T.argmax(s_out, axis=1), T.argmax(s_y_onehot, axis=1)), 1, 0)) no_dropout = [(s_pdpo, T.constant(0., dtype=th.config.floatX))] fn_predict = th.function( [s_x, s_y], {'pred':s_out, 'accr':s_accr, 'loss':s_loss}, givens=no_dropout, profile=PROFILE) rec_fetches = { 'x': s_x, 'y': s_y, 'pred': s_out} rec_fetches.update(g_mdl.params_di) fn_record = th.function( [s_x, s_y], rec_fetches, givens=no_dropout, profile=PROFILE) g_ozer.compile( [s_x, s_y], s_loss, g_mdl.params_di.values(), fetches_={'pred': s_out, 'loss': s_loss, 'accr': s_accr}, givens_=[(s_pdpo, T.constant(TRAIN_PDPO, dtype=th.config.floatX))], profile_=PROFILE)
def get_output_for(self, input, init=False, **kwargs): if init: m = T.mean(input, self.axes_to_sum) input -= m.dimshuffle(*self.dimshuffle_args) inv_stdv = self.init_stdv/T.sqrt(T.mean(T.square(input), self.axes_to_sum)) input *= inv_stdv.dimshuffle(*self.dimshuffle_args) self.init_updates = [(self.b, -m*inv_stdv), (self.g, self.g*inv_stdv)] elif hasattr(self,'b'): input += self.b.dimshuffle(*self.dimshuffle_args) return self.nonlinearity(input)
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) # Input Mixture of Gaussian Layer
def mean_squared_error(y_true, y_pred): return T.sqr(y_pred - y_true).mean(axis=-1)
def mean_absolute_error(y_true, y_pred): return T.abs_(y_pred - y_true).mean(axis=-1)
def mean_absolute_percentage_error(y_true, y_pred): return T.abs_((y_true - y_pred) / T.clip(T.abs_(y_true), epsilon, np.inf)).mean(axis=-1) * 100.
def mean_squared_logarithmic_error(y_true, y_pred): return T.sqr(T.log(T.clip(y_pred, epsilon, np.inf) + 1.) - T.log(T.clip(y_true, epsilon, np.inf) + 1.)).mean(axis=-1)
def squared_hinge(y_true, y_pred): return T.sqr(T.maximum(1. - y_true * y_pred, 0.)).mean(axis=-1)
def binary_crossentropy(y_true, y_pred): y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon) bce = T.nnet.binary_crossentropy(y_pred, y_true).mean(axis=-1) return bce
def poisson_loss(y_true, y_pred): return T.mean(y_pred - y_true * T.log(y_pred + epsilon), axis=-1) #################################################### # Variational Auto-encoder
def gaussian_kl_divergence(mean, ln_var): """Computes the KL-divergence of Gaussian variables from the standard one. Given two variable ``mean`` representing :math:`\\mu` and ``ln_var`` representing :math:`\\log(\\sigma^2)`, this function returns a variable representing the KL-divergence between the given multi-dimensional Gaussian :math:`N(\\mu, S)` and the standard Gaussian :math:`N(0, I)` .. math:: D_{\\mathbf{KL}}(N(\\mu, S) \\| N(0, I)), where :math:`S` is a diagonal matrix such that :math:`S_{ii} = \\sigma_i^2` and :math:`I` is an identity matrix. Args: mean (~chainer.Variable): A variable representing mean of given gaussian distribution, :math:`\\mu`. ln_var (~chainer.Variable): A variable representing logarithm of variance of given gaussian distribution, :math:`\\log(\\sigma^2)`. Returns: ~chainer.Variable: A variable representing KL-divergence between given gaussian distribution and the standard gaussian. """ var = T.exp(ln_var) return 0.5 * T.sum(mean * mean + var - ln_var - 1, 1) # aliases
def build_objective(model, deterministic=False, epsilon=1e-12): predictions = nn.layers.get_output(model.l_out, deterministic=deterministic) targets = T.cast(T.flatten(nn.layers.get_output(model.l_target)), 'int32') p = predictions[T.arange(predictions.shape[0]), targets] p = T.clip(p, epsilon, 1.) loss = T.mean(T.log(p)) return -loss
def get_output_for(self, input, **kwargs): ps = nonlinearities.sigmoid(input) powd = ps ** self.exp tmean = T.mean(powd, axis=(1,2)) return tmean
def get_output_for(self, input, **kwargs): return T.log(T.mean(T.exp(self.r * input), axis=self.axis) + 1e-7) / self.r
def build_objective(model, deterministic=False, epsilon=1e-12): p = nn.layers.get_output(model.l_out, deterministic=deterministic) targets = T.flatten(nn.layers.get_output(model.l_target)) p = T.clip(p, epsilon, 1.-epsilon) bce = T.nnet.binary_crossentropy(p, targets) return T.mean(bce)