我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用theano.tensor.clip()。
def crossentropy(y_pred, y_true, void_labels, one_hot=False): # Clip predictions y_pred = T.clip(y_pred, _EPSILON, 1.0 - _EPSILON) if one_hot: y_true = T.argmax(y_true, axis=1) # Create mask mask = T.ones_like(y_true, dtype=_FLOATX) for el in void_labels: mask = T.set_subtensor(mask[T.eq(y_true, el).nonzero()], 0.) # Modify y_true temporarily y_true_tmp = y_true * mask y_true_tmp = y_true_tmp.astype('int32') # Compute cross-entropy loss = T.nnet.categorical_crossentropy(y_pred, y_true_tmp) # Compute masked mean loss loss *= mask loss = T.sum(loss) / T.sum(mask) return loss
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 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) 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) # take the mean of the objectives where it matters (enabled targets) obj_scalar = T.sum(enable_targets[:,obj_idx] * v_obj) / (0.00001 + T.sum(enable_targets[:,obj_idx])) if deterministic: d_objectives_deterministic[obj_name] = obj_scalar else: d_objectives[obj_name] = obj_scalar sum_of_objectives += T.mean(v_obj) unit_ptr = unit_ptr+n_classes return sum_of_objectives
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) 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) # take the mean of the objectives where it matters (enabled targets) obj_scalar = T.sum(enable_targets[:,obj_idx] * v_obj) / (0.00001 + T.sum(enable_targets[:,obj_idx])) if deterministic: d_objectives_deterministic[obj_name] = obj_scalar else: d_objectives[obj_name] = obj_scalar sum_of_objectives += obj_scalar unit_ptr = unit_ptr+n_classes return sum_of_objectives
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') 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]) if deterministic: d_objectives_deterministic[obj_name] = objective(obj_idx, (unit_ptr, unit_ptr+n_classes), predictions, targets) else: d_objectives[obj_name] = objective(obj_idx, (unit_ptr, unit_ptr+n_classes), predictions, targets) sum_of_objectives += objective(obj_idx, (unit_ptr, unit_ptr+n_classes), predictions, targets) unit_ptr = unit_ptr+n_classes #print 'for debug purposes: unit_ptr', unit_ptr return sum_of_objectives
def clip(x, min_value, max_value): """Element-wise value clipping. If min_value > max_value, clipping range is [min_value,min_value]. # Arguments x: Tensor or variable. min_value: Tensor, float, int, or None. If min_value is None, defaults to -infinity. max_value: Tensor, float, int, or None. If max_value is None, defaults to infinity. # Returns A tensor. """ if max_value is None: max_value = np.inf if min_value is None: min_value = -np.inf max_value = T.maximum(min_value, max_value) return T.clip(x, min_value, max_value)
def categorical_crossentropy(logit, y, mask, length_var, need_softmax=False): logit_shp = logit.shape # (n_samples, n_timesteps_f, n_labels) # softmax, predict label prob # (n_samples * n_timesteps_f, n_labels) if need_softmax: probs = T.nnet.softmax(logit.reshape([logit_shp[0]*logit_shp[1], logit_shp[2]])) else: probs = logit.reshape([logit_shp[0]*logit_shp[1], logit_shp[2]]) # (n_samples * n_timesteps_f) y_flat = y.flatten() # clip to avoid nan loss probs = T.clip(probs, _EPSILON, 1.0 - _EPSILON) loss = lasagne.objectives.categorical_crossentropy(probs, y_flat) # (n_samples, n_timesteps_f) loss = loss.reshape((logit_shp[0], logit_shp[1])) loss = loss * mask loss = T.sum(loss, axis=1) / length_var probs = probs.reshape(logit_shp) return loss, probs
def adam(cost, params, lr=0.001, b1=0.9, b2=0.999, e=1e-8): updates = [] grads = T.grad(cost, params) i = theano.shared(np.dtype(theano.config.floatX).type(1)) i_t = i + 1. fix1 = 1. - (1. - b1)**i_t fix2 = 1. - (1. - b2)**i_t lr_t = lr * (T.sqrt(fix2) / fix1) for p, g in zip(params, grads): g = T.clip(g, -grad_clip, grad_clip) m = theano.shared(p.get_value() * 0.) v = theano.shared(p.get_value() * 0.) m_t = (b1 * g) + ((1. - b1) * m) v_t = (b2 * T.sqr(g)) + ((1. - b2) * v) g_t = m_t / (T.sqrt(v_t) + e) p_t = p - (lr_t * g_t) updates.append((m, m_t)) updates.append((v, v_t)) updates.append((p, p_t)) updates.append((i, i_t)) return updates
def dual_copy_rounding(W,integer_bits=0,fractional_bits=1): """ Rounding as described in as in "Robustness of spiking Deep Belief Networks to noise and reduced bit precision of neuro-inspired hardware platforms" by Stromatidis et al. See http://dx.doi.org/10.3389/fnins.2015.00222 :param W: Weights :param integer_bits: number of bits to represent the integer part :param fractional_bits: number of bits to represent the fractional part :return:quantized weights """ #print "Dual copy rounding!" power = T.cast(2.**fractional_bits, theano.config.floatX) # float ! max_val = T.cast((2.**(fractional_bits+integer_bits))-1, theano.config.floatX) value = W*power value = GradPreserveRoundTensor(value) # rounding value = T.clip(value, -max_val, max_val) # saturation arithmetic Wb = value/power return Wb
def score_metrics(out, target_var, weight_map, l2_loss=0): _EPSILON=1e-8 out_flat = out.dimshuffle(1,0,2,3).flatten(ndim=2).dimshuffle(1,0) target_flat = target_var.dimshuffle(1,0,2,3).flatten(ndim=1) weight_flat = weight_map.dimshuffle(1,0,2,3).flatten(ndim=1) prediction = lasagne.nonlinearities.softmax(out_flat) prediction_binary = T.argmax(prediction, axis=1) dice_score = (T.sum(T.eq(2, prediction_binary+target_flat))*2.0 / (T.sum(prediction_binary) + T.sum(target_flat))) loss = lasagne.objectives.categorical_crossentropy(T.clip(prediction,_EPSILON,1-_EPSILON), target_flat) loss = loss * weight_flat loss = loss.mean() loss += l2_loss accuracy = T.mean(T.eq(prediction_binary, target_flat), dtype=theano.config.floatX) return loss, accuracy, dice_score, target_flat, prediction, prediction_binary
def log_bernoulli(x, p, eps=1e-5): """ Compute log pdf of a Bernoulli distribution with success probability p, at values x. .. math:: \log p(x; p) = \log \mathcal{B}(x; p) Parameters ---------- x : Theano tensor Values at which to evaluate pdf. p : Theano tensor Success probability :math:`p(x=1)`, which is also the mean of the Bernoulli distribution. eps : float Small number used to avoid NaNs by clipping p in range [eps;1-eps]. Returns ------- Theano tensor Element-wise log probability, this has to be summed for multi-variate distributions. """ p = T.clip(p, eps, 1.0 - eps) return -T.nnet.binary_crossentropy(p, x)
def log_negative_binomial(x, p, log_r, eps = 0.0): """ Compute log pdf of a negative binomial distribution with success probability p and number of failures, r, until the experiment is stopped, at values x. A simple variation of Stirling's approximation is used: log x! = x log x - x. """ x = T.clip(x, eps, x) p = T.clip(p, eps, 1.0 - eps) r = T.exp(log_r) r = T.clip(r, eps, r) y = T.gammaln(x + r) - T.gammaln(x + 1) - T.gammaln(r) \ + x * T.log(p) + r * T.log(1 - p) return y
def log_zero_inflated_poisson(x, pi, log_lambda, eps = 0.0): """ Compute log pdf of a zero-inflated Poisson distribution with success probability pi and number of failures, r, until the experiment is stopped, at values x. A simple variation of Stirling's approximation is used: log x! = x log x - x. """ pi = T.clip(pi, eps, 1.0 - eps) lambda_ = T.exp(log_lambda) lambda_ = T.clip(lambda_, eps, lambda_) y_0 = T.log(pi + (1 - pi) * T.exp(-lambda_)) y_1 = T.log(1 - pi) + log_poisson(x, log_lambda, eps) y = T.eq(x, 0) * y_0 + T.gt(x, 0) * y_1 return y
def multilabel_loss(preds, labels): eps = 1e-4 preds = T.clip(preds, eps, 1-eps) return -(labels * T.log(preds) + (1 - labels) * T.log(1 - preds)).mean(axis=1).mean(axis=0)
def multilabel_loss_with_mask(preds, labels, mask): eps = 1e-4 preds = T.clip(preds, eps, 1-eps) return -(mask * (labels * T.log(preds) + (1 - labels) * T.log(1 - preds))).mean(axis=1).mean(axis=0)
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 categorical_crossentropy(y_true, y_pred): '''Expects a binary class matrix instead of a vector of scalar classes ''' y_pred = T.clip(y_pred, epsilon, 1.0 - epsilon) # scale preds so that the class probas of each sample sum to 1 y_pred /= y_pred.sum(axis=-1, keepdims=True) cce = T.nnet.categorical_crossentropy(y_pred, y_true) return cce
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 get_output_for(self, input, deterministic=False, **kwargs): """ Parameters ---------- input : tensor output from the previous layer deterministic : bool If true noise is disabled, see notes """ if deterministic or self.sigma == 0: return input else: return T.clip(input + self._srng.normal(input.shape, avg=0.0, std=self.sigma), 0.0, 1.0)
def binary_crossentropy(y_pred, y_true): # Clip predictions to avoid numerical instability y_pred = T.clip(y_pred, _EPSILON, 1.0 - _EPSILON) loss = T.nnet.binary_crossentropy(y_pred, y_true) return loss.mean()
def entropy(y_pred): # Clip predictions to avoid numerical instability y_pred = T.clip(y_pred, _EPSILON, 1.0 - _EPSILON) ent = - T.sum(y_pred * T.log(y_pred), axis=1) return ent.mean()
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 build_objective(model, deterministic=False, epsilon=1e-12): network_predictions = nn.layers.get_output(model.l_out) target_values = nn.layers.get_output(model.l_target) target_values = T.clip(target_values, 1e-6, 1.) network_predictions, target_values = nn.layers.merge.autocrop([network_predictions, target_values], [None, None, 'center', 'center', 'center']) y_true_f = target_values y_pred_f = network_predictions intersection = T.sum(y_true_f * y_pred_f) return -1. * (2 * intersection + epsilon) / (T.sum(y_true_f) + T.sum(y_pred_f) + epsilon)
def build_objective(model, deterministic=False, epsilon=1e-12): predictions = T.flatten(nn.layers.get_output(model.l_out)) targets = T.flatten(nn.layers.get_output(model.l_target)) targets = T.clip(targets, 1e-6, 1.) dice = (2. * T.sum(targets * predictions) + epsilon) / (T.sum(predictions) + T.sum(targets) + epsilon) return -1. * dice
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)