我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用theano.tensor.minimum()。
def iou_loss(p, t): # print "pass" tp, tt = p.reshape((p.shape[0], 2, 2)), t.reshape((t.shape[0], 2, 2)) overlaps_t0 = T.maximum(tp[:, 0, :], tt[:, 0, :]) overlaps_t1 = T.minimum(tp[:, 1, :], tt[:, 1, :]) intersection = overlaps_t1 - overlaps_t0 bool_overlap = T.min(intersection, axis=1) > 0 intersection = intersection[:, 0] * intersection[:, 1] intersection = T.maximum(intersection, np.float32(0.)) dims_p = tp[:, 1, :] - tp[:, 0, :] areas_p = dims_p[:, 0] * dims_p[:, 1] dims_t = tt[:, 1, :] - tt[:, 0, :] areas_t = dims_t[:, 0] * dims_t[:, 1] union = areas_p + areas_t - intersection loss = 1. - T.minimum( T.exp(T.log(T.abs_(intersection)) - T.log(T.abs_(union) + np.float32(1e-5))), np.float32(1.) ) # return loss return T.mean(loss)
def iou_loss_val(p, t): tp, tt = p.reshape((p.shape[0], 2, 2)), t.reshape((t.shape[0], 2, 2)) overlaps = np.zeros_like(tp, dtype=np.float32) overlaps[:, 0, :] = np.maximum(tp[:, 0, :], tt[:, 0, :]) overlaps[:, 1, :] = np.minimum(tp[:, 1, :], tt[:, 1, :]) intersection = overlaps[:, 1, :] - overlaps[:, 0, :] bool_overlap = np.min(intersection, axis=1) > 0 intersection = intersection[:, 0] * intersection[:, 1] intersection = np.maximum(intersection, 0.) # print "bool", bool_overlap # print "Int", intersection dims_p = tp[:, 1, :] - tp[:, 0, :] areas_p = dims_p[:, 0] * dims_p[:, 1] dims_t = tt[:, 1, :] - tt[:, 0, :] areas_t = dims_t[:, 0] * dims_t[:, 1] union = areas_p + areas_t - intersection # print "un", union loss = 1. - np.minimum( np.exp(np.log(np.abs(intersection)) - np.log(np.abs(union) + 1e-5)), 1. ) # print loss return np.mean(loss)
def ctc_update_log_p(skip_idxs, zeros, active, log_p_curr, log_p_prev): active_skip_idxs = skip_idxs[(skip_idxs < active).nonzero()] active_next = T.cast(T.minimum( T.maximum( active + 1, T.max(T.concatenate([active_skip_idxs, [-1]])) + 2 + 1 ), log_p_curr.shape[0]), 'int32') common_factor = T.max(log_p_prev[:active]) p_prev = T.exp(log_p_prev[:active] - common_factor) _p_prev = zeros[:active_next] # copy over _p_prev = T.set_subtensor(_p_prev[:active], p_prev) # previous transitions _p_prev = T.inc_subtensor(_p_prev[1:], _p_prev[:-1]) # skip transitions _p_prev = T.inc_subtensor(_p_prev[active_skip_idxs + 2], p_prev[active_skip_idxs]) updated_log_p_prev = T.log(_p_prev) + common_factor log_p_next = T.set_subtensor( zeros[:active_next], log_p_curr[:active_next] + updated_log_p_prev ) return active_next, log_p_next
def test_inequality_with_self(self): x = T.scalar('x', dtype=config.floatX) mode = theano.compile.get_default_mode().including('local_useless_elemwise_comparison') f = theano.function([x], T.lt(x, x), mode=mode) self.assert_eqs_const(f, 0) f = theano.function([x], T.le(x, x), mode=mode) self.assert_eqs_const(f, 1) f = theano.function([x], T.gt(x, x), mode=mode) self.assert_eqs_const(f, 0) f = theano.function([x], T.ge(x, x), mode=mode) self.assert_eqs_const(f, 1) f = theano.function([x], T.minimum(x, x), mode=mode) self.assert_identity(f) f = theano.function([x], T.maximum(x, x), mode=mode) self.assert_identity(f)
def editdist_np(source, target): if len(source) < len(target): return editdist_np(target, source) if len(target) == 0: return len(source) previous_row = np.arange(target.size + 1) for s in source: current_row = previous_row + 1 current_row[1:] = np.minimum(current_row[1:], np.add(previous_row[:-1], target != s)) current_row[1:] = np.minimum(current_row[1:], current_row[0:-1] + 1) previous_row = current_row return previous_row[-1] # Pure python version # from [https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Python, the 6th version]
def _editdist(s, t): """ Levenshtein's edit distance function :param s: vector, source string :param t: vector, target string :return: edit distance, scalar """ def update(x, previous_row): current_row = previous_row + 1 current_row = tensor.set_subtensor(current_row[1:], tensor.minimum(current_row[1:], tensor.add(previous_row[:-1], tensor.neq(target,x)))) current_row = tensor.set_subtensor(current_row[1:], tensor.minimum(current_row[1:], current_row[0:-1] + 1)) return current_row source, target = ifelse(tensor.lt(s.shape[0], t.shape[0]), (t, s), (s, t)) previous_row = tensor.arange(target.size + 1, dtype=theano.config.floatX) result, updates = theano.scan(fn=update, sequences=source, outputs_info=previous_row, name='editdist') return result[-1,-1]
def past_weight_grad_calculator(xs, es, kp_x, kd_x, kp_e, kd_e, shapes): """ Do an efficient update of the weights given the two spike-trains. This isn't actually implemented as an efficient update, but it will produce the identical result as if it were. :param xs: An (n_samples, n_in) array :param es: An (n_samples, n_out) array :param kp_x: kp for the x units :param kd_x: kd for the x units :param kp_e: kp for the e units :param kd_e: kd for the e units :param shapes: (minibatch_size, n_in, n_out) :return: An (n_in, n_out) approximate weight gradient. """ # TODO: Make this actually use sparsity, one of these days. kp_x, kd_x, kp_e, kd_e = [as_floatx(k) for k in (kp_x, kd_x, kp_e, kd_e)] n_samples, n_in, n_out = shapes rx = kd_x/(kp_x+kd_x) re = kd_e/(kp_e+kd_e) tx_last = create_shared_variable(np.zeros((n_samples, n_in))+1) te_last = create_shared_variable(np.zeros((n_samples, n_out))+1) x_last = create_shared_variable(np.zeros((n_samples, n_in))) e_last = create_shared_variable(np.zeros((n_samples, n_out))) t_last = tt.minimum(tx_last[:, :, None], te_last[:, None, :]) x_spikes = tt.neq(xs, 0) dw_potentials = x_last[:, :, None] * e_last[:, None, :] * \ rx**(tx_last[:, :, None]-t_last) \ * re**(te_last[:, None, :]-t_last) \ * geoseries_sum(rx*re, t_end=t_last, t_start=1) e_spikes = tt.neq(es, 0) dws = (x_spikes[:, :, None]+e_spikes[:, None, :]-x_spikes[:, :, None]*e_spikes[:, None, :])*dw_potentials # (n_samples, n_in, n_out) add_update(x_last, tt.switch(x_spikes, x_last*rx**tx_last + xs/as_floatx(kd_x), x_last)) add_update(e_last, tt.switch(e_spikes, e_last*rx**te_last + es/as_floatx(kd_e), e_last)) add_update(tx_last, tt.switch(x_spikes, 1, tx_last+1)) add_update(te_last, tt.switch(e_spikes, 1, te_last+1)) return dws.sum(axis=0)
def PReLU(a, x): return T.maximum(0.0, x) + a * T.minimum(0.0, x)
def minimum(x, y): return T.minimum(x, y)
def relu(x, alpha=0., max_value=None): _assert_has_capability(T.nnet, 'relu') x = T.nnet.relu(x, alpha) if max_value is not None: x = T.minimum(x, max_value) return x
def minimum(self, x, y): return T.minimum(x, y)
def relu(self, x, alpha=0., max_value=None): x = T.nnet.relu(x, alpha) if max_value is not None: x = T.minimum(x, max_value) return x
def create_objectives(self, deterministic=False): """ELBO objective with the analytic expectation trick""" # load network input X = self.inputs[0] # load network output if self.model == 'bernoulli': q_mu, q_logsigma, sample, _ \ = lasagne.layers.get_output(self.network[2:], deterministic=deterministic) elif self.model in ('gaussian', 'svhn'): p_mu, p_logsigma, q_mu, q_logsigma, _, _ \ = lasagne.layers.get_output(self.network, deterministic=deterministic) # first term of the ELBO: kl-divergence (using the closed form expression) kl_div = 0.5 * T.sum(1 + 2*q_logsigma - T.sqr(q_mu) - T.exp(2 * T.minimum(q_logsigma,50)), axis=1).mean() # second term: log-likelihood of the data under the model if self.model == 'bernoulli': logpxz = -lasagne.objectives.binary_crossentropy(sample, X.flatten(2)).sum(axis=1).mean() elif self.model in ('gaussian', 'svhn'): # def log_lik(x, mu, log_sig): # return T.sum(-(np.float32(0.5 * np.log(2 * np.pi)) + log_sig) # - 0.5 * T.sqr(x - mu) / T.exp(2 * log_sig), axis=1) # logpxz = log_lik(X.flatten(2), p_mu, p_logsigma).mean() logpxz = log_normal2(X.flatten(2), p_mu, p_logsigma).sum(axis=1).mean() loss = -1 * (logpxz + kl_div) # we don't use the spearate accuracy metric right now return loss, -kl_div
def leaky_relu(x): output = T.maximum(0., x) + leaky_slope*T.minimum(0, x) return output
def exp_cutoff(x): # 1 or less with heavy tail # the upper 80% of the values is at 1 b = 0.8 c = x.min() + b*(x.max()-x.min()) # the lower 20% of the values is below y y = 0.15 a = y/T.exp((0.2-b)*(x.max()-x.min())) return T.minimum(a * T.exp(x - c), 1.)
def __init__(self, lengthscale, v): covariance_function.__init__(self, lengthscale, v) self.f = theano.function([x1, x2], T.minimum(x1, x2), allow_input_downcast=True)
def prelu(inpt, a): ''' Parametric rectified linear unit, see: https://arxiv.org/pdf/1502.01852.pdf ''' return T.maximum(inpt, 0) + a * T.minimum(inpt, 0)
def minimum(x, y): return T.minimum(x, y) # SHAPE OPERATIONS
def relu(x, alpha=0., max_value=None): assert hasattr(T.nnet, 'relu'), ('It looks like like your version of ' 'Theano is out of date. ' 'Install the latest version with:\n' 'pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps') x = T.nnet.relu(x, alpha) if max_value is not None: x = T.minimum(x, max_value) return x
def get_function(self, func_name): if func_name == 'tanh': return T.tanh elif func_name == 'hardtanh': L.warning('Current hardTanh implementation is slow!') return lambda x: ((abs(x) <= 1) * x) + ((1 < abs(x)) * T.sgn(x)) elif func_name == 'xtanh': return lambda x: T.tanh(x) + 0.1 * x elif func_name == 'sigmoid': return T.nnet.sigmoid elif func_name == 'fastsigmoid': L.error('T.nnet.ultra_fast_sigmoid function has some problems') elif func_name == 'hardsigmoid': return T.nnet.hard_sigmoid elif func_name == 'xsigmoid': return lambda x: T.nnet.sigmoid(x) + 0.1 * x elif func_name == 'softplus': return T.nnet.softplus elif func_name == 'relu': #return lambda x: T.maximum(x, 0) return lambda x: x * (x > 0) #return T.nnet.relu # Update theano and then use this one instead elif func_name == 'leakyrelu': return lambda x: T.maximum(x, 0.01 * x) elif func_name == 'cappedrelu': return lambda x: T.minimum(x * (x > 0), 6) elif func_name == 'softmax': return T.nnet.softmax elif func_name == 'norm1': return lambda x: x / T.nlinalg.norm(x, 1) elif func_name == 'norm2': #return lambda x: x / T.nlinalg.norm(x, 2) return lambda x: x / T.dot(x, x)**0.5 else: L.error('Invalid function name given: ' + func_name)
def logadd(self, a, b): g = T.maximum(a,b) l = T.minimum(a,b) return g + T.log(1 + T.exp(l-g))
def build_loss_expression(predicted, target): abs_err = abs(predicted - target) quadratic_part = tensor.minimum(abs_err, 1) linear_part = abs_err - quadratic_part loss = (0.5 * quadratic_part ** 2 + linear_part) return loss
def queue_transform(feature_strengths, feature_vects, return_strengths=False): """ Process features according to a "fragmented queue", where each timestep gets a size-1 window onto a feature queue. Effectively, feature_strengths gives how much to push onto queue feature_vects gives what to push on pop weights are tied to feature_strengths output is a size-1 peek (without popping) Parameters: - feature_strengths: float32 tensor of shape (batch, push_timestep) in [0,1] - feature_vects: float32 tensor of shape (batch, push_timestep, feature_dim) Returns: - peek_vects: float32 tensor of shape (batch, timestep, feature_dim) """ n_batch, n_time, n_feature = feature_vects.shape cum_sum_str = T.extra_ops.cumsum(feature_strengths, 1) # We will be working in (batch, timestep, push_timestep) # For each timestep, if we subtract out the sum of pushes before that timestep # and then cap to 0-1 we get the cumsums for just the features active in that # timestep timestep_adjustments = T.shape_padright(cum_sum_str - feature_strengths) push_time_cumsum = T.shape_padaxis(cum_sum_str, 1) relative_cumsum = push_time_cumsum - timestep_adjustments capped_cumsum = T.minimum(T.maximum(relative_cumsum, 0), 1) # Now we can recover the peek strengths by taking a diff shifted = T.concatenate([T.zeros((n_batch, n_time, 1)), capped_cumsum[:,:,:-1]],2) peek_strengths = capped_cumsum-shifted # Peek strengths is now (batch, timestep, push_timestep) result = T.batched_dot(peek_strengths, feature_vects) if return_strengths: return peek_strengths, result else: return result
def _differentiate(self, params=None): '''Return a sequence of gradients for our parameters. If this optimizer has been configured with a gradient norm limit, or with elementwise gradient clipping, this method applies the appropriate rescaling and clipping operations before returning the gradient. Parameters ---------- params : list of Theano variables, optional Return the gradient with respect to these parameters. Defaults to all parameters that the optimizer knows about. Yields ------ pairs : (param, grad) tuples Generates a sequence of tuples representing each of the parameters requested and the corresponding Theano gradient expressions. ''' if params is None: params = self._params for param, grad in zip(params, TT.grad(self._loss, params)): if self.max_gradient_elem > 0: limit = util.as_float(self.max_gradient_elem) yield param, TT.clip(grad, -limit, limit) elif self.max_gradient_norm > 0: norm = TT.sqrt((grad * grad).sum()) limit = util.as_float(self.max_gradient_norm) yield param, grad * TT.minimum(1, limit / norm) else: yield param, grad
def _get_updates_for(self, param, grad): grad_tm1 = shared_like(param, 'grad') step_tm1 = shared_like(param, 'step', self.learning_rate.eval()) test = grad * grad_tm1 diff = TT.lt(test, 0) steps = step_tm1 * (TT.eq(test, 0) + TT.gt(test, 0) * self.step_increase + diff * self.step_decrease) step = TT.minimum(self.max_step, TT.maximum(self.min_step, steps)) grad = grad - diff * grad yield param, param - TT.sgn(grad) * step yield grad_tm1, grad yield step_tm1, step
def infer_shape(self, nodes, shapes): return [(tensor.minimum(*shapes[0]), )]
def structured_minimum(x, y): """ Structured elemwise minimum of sparse matrix x by scalar y. """ # see decorator for function body
def test_shape_inequality_with_self(self): x = T.vector('x', dtype=config.floatX) mode = theano.compile.get_default_mode().including('local_useless_elemwise_comparison', 'local_shape_to_shape_i', 'local_track_shape_i', 'local_subtensor_make_vector') f = theano.function([x], T.lt(x.shape[0], 0), mode=mode) self.assert_eqs_const(f, 0) f = theano.function([x], T.ge(x.shape[0], 0), mode=mode) self.assert_eqs_const(f, 1) f = theano.function([x], T.maximum(x.shape[0], 0), mode=mode) topo = f.maker.fgraph.toposort() assert len(topo) == 1 assert isinstance(topo[0].op, Shape_i), topo[0].op x_val = numpy.ones(100, dtype=config.floatX) assert f(x_val) == x_val.shape[0] f = theano.function([x], T.maximum(0, x.shape[0]), mode=mode) topo = f.maker.fgraph.toposort() assert len(topo) == 1 assert isinstance(topo[0].op, Shape_i), topo[0].op x_val = numpy.ones(100, dtype=config.floatX) assert f(x_val) == x_val.shape[0] f = theano.function([x], T.minimum(x.shape[0], 0), mode=mode) self.assert_eqs_const(f, 0) assert f(x_val) == 0 f = theano.function([x], T.minimum(0, x.shape[0]), mode=mode) self.assert_eqs_const(f, 0) assert f(x_val) == 0 f = theano.function([x], T.minimum([0, 0], x.shape[0]), mode=mode) # This case isn't optimized. # self.assert_eqs_const(f, 0) utt.assert_allclose(f(x_val), [0, 0])
def test_elemwise(self): # float Ops mats = theano.tensor.matrices('cabxy') c, a, b, x, y = mats s1 = T.switch(c, a, b) s2 = T.switch(c, x, y) for op in (T.add, T.sub, T.mul, T.true_div, T.int_div, T.floor_div, T.minimum, T.maximum, T.gt, T.lt, T.ge, T.le, T.eq, T.neq, T.pow): g = optimize(FunctionGraph(mats, [op(s1, s2)])) assert str(g).count('Switch') == 1 # integer Ops mats = theano.tensor.imatrices('cabxy') c, a, b, x, y = mats s1 = T.switch(c, a, b) s2 = T.switch(c, x, y) for op in (T.and_, T.or_, T.xor, T.bitwise_and, T.bitwise_or, T.bitwise_xor): g = optimize(FunctionGraph(mats, [op(s1, s2)])) assert str(g).count('Switch') == 1 # add/mul with more than two inputs u, v = theano.tensor.matrices('uv') s3 = T.switch(c, u, v) for op in (T.add, T.mul): g = optimize(FunctionGraph(mats + [u, v], [op(s1, s2, s3)])) assert str(g).count('Switch') == 1
def relu(x, alpha, max_value): y = T.nnet.relu(x, alpha) if max_value is not None: y = T.minimum(y, max_value) return y ### objectives # cross entropy loss. y_pred, y_gt should be 2D, DO NOT USE tensor.nnet.categorical_crossentropy
def q_loss(self, y_true, y_pred): # assume clip_delta is 1.0 # along with sum accumulator. diff = y_true - y_pred _quad = T.minimum(abs(diff), 1.0) _lin = abs(diff) - _quad loss = 0.5 * _quad ** 2 + _lin loss = T.sum(loss) return loss
def editdist(s, t): def update(x, previous_row): current_row = previous_row + 1 current_row = tensor.set_subtensor(current_row[1:], tensor.minimum(current_row[1:], tensor.add(previous_row[:-1], tensor.neq(target,x)))) current_row = tensor.set_subtensor(current_row[1:], tensor.minimum(current_row[1:], current_row[0:-1] + 1)) return current_row source, target = ifelse(tensor.lt(s.shape[0], t.shape[0]), (t, s), (s, t)) previous_row = tensor.arange(target.size + 1, dtype=theano.config.floatX) result, updates = theano.scan(fn = update, sequences=source, outputs_info=previous_row, name='editdist') return result[-1,-1] # numpy version # from [https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Python, the 5th version]
def minimum(x, y): return T.minimum(x, y) # =========================================================================== # SHAPE OPERATIONS # ===========================================================================