本文整理汇总了Python中theano.tensor.maximum函数的典型用法代码示例。如果您正苦于以下问题:Python maximum函数的具体用法?Python maximum怎么用?Python maximum使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了maximum函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, factor=numpy.sqrt(2), decay=1.0, min_factor=None, padding=False, **kwargs):
super(ConvFMPLayer, self).__init__(**kwargs)
if min_factor is None:
min_factor = factor
factor = T.maximum(factor * (decay ** self.network.epoch), numpy.float32(min_factor))
sizes_raw = self.source.output_sizes
# handle size problems
if not padding:
padding = T.min(self.source.output_sizes / factor) <= 0
padding = theano.printing.Print(global_fn=maybe_print_pad_warning)(padding)
fixed_sizes = T.maximum(sizes_raw, T.cast(T.as_tensor(
[factor + self.filter_height - 1, factor + self.filter_width - 1]), 'float32'))
sizes = ifelse(padding, fixed_sizes, sizes_raw)
X_size = T.cast(T.max(sizes, axis=0), "int32")
def pad_fn(x_t, s):
x = T.alloc(numpy.cast["float32"](0), X_size[0], X_size[1], self.X.shape[3])
x = T.set_subtensor(x[:s[0], :s[1]], x_t[:s[0], :s[1]])
return x
fixed_X, _ = theano.scan(pad_fn, [self.X.dimshuffle(2, 0, 1, 3), T.cast(sizes_raw, "int32")])
fixed_X = fixed_X.dimshuffle(1, 2, 0, 3)
self.X = ifelse(padding, T.unbroadcast(fixed_X, 3), self.X)
conv_out = CuDNNConvHWBCOpValidInstance(self.X, self.W, self.b)
conv_out_sizes = self.conv_output_size_from_input_size(sizes)
self.output, self.output_sizes = fmp(conv_out, conv_out_sizes, T.cast(factor,'float32'))
示例2: grad_init
def grad_init(self):
mask_ = self.mask.flatten()
rewards_ = self.rewards.flatten()
actions_ = self.actions.reshape([self.actions.shape[0]*self.actions.shape[1],-1])
#self.mov_std = theano.shared(numpy.float32(1.), 'std')
pp = self.params.values()
mean_rewards = (mask_ * rewards_).sum(-1, keepdims=True) / tensor.maximum(1., mask_.sum(-1, keepdims=True))
centered_rewards = rewards_ - self.vapprox.v[:,0] - mean_rewards
mean2_rewards = (mask_ * (rewards_ ** 2)).sum(-1, keepdims=True) / tensor.maximum(1., mask_.sum(-1, keepdims=True))
var_rewards = mean2_rewards - (mean_rewards ** 2)
scaled_rewards = centered_rewards / tensor.maximum(1., tensor.sqrt(tensor.maximum(0., var_rewards)))
#scaled_rewards = centered_rewards
logprob = 0.
reg = 0.
for oi in xrange(self.n_out):
labs = actions_[:,oi].flatten()
labs_idx = tensor.arange(labs.shape[0]) * self.out_dim + labs
logprob = logprob + (mask_ * tensor.log(self.pi[oi].flatten()+1e-6)[labs_idx])
reg = reg - (self.pi[oi] * tensor.log(self.pi[oi]+1e-6)).sum(-1).sum(0)
self.cost = -tensor.mean(scaled_rewards * logprob + self.reg_c * reg)
self.grads = tensor.grad(self.cost, wrt=pp)
示例3: updates
def updates(self, cost, params, learning_rate = 0.1, momentum= 0.95, rescale=5.):
grads = T.grad(cost, params)
grad_norm = T.sqrt(sum(map(lambda x: T.sqr(x).sum(), grads)))
not_finite = T.or_(T.isnan(grad_norm), T.isinf(grad_norm))
grad_norm = T.sqrt(grad_norm)
scaling_num = rescale
scaling_den = T.maximum(rescale, grad_norm)
# Magic constants
combination_coeff = 0.9
minimum_grad = 1e-4
updates = []
for n, (param, grad) in enumerate(zip(params, grads)):
grad = T.switch(not_finite, 0.1 * param,
grad * (scaling_num / scaling_den))
old_square = self.running_square_[n]
new_square = combination_coeff * old_square + (
1. - combination_coeff) * T.sqr(grad)
old_avg = self.running_avg_[n]
new_avg = combination_coeff * old_avg + (
1. - combination_coeff) * grad
rms_grad = T.sqrt(new_square - new_avg ** 2)
rms_grad = T.maximum(rms_grad, minimum_grad)
memory = self.memory_[n]
update = momentum * memory - learning_rate * grad / rms_grad
update2 = momentum * momentum * memory - (
1 + momentum) * learning_rate * grad / rms_grad
updates.append((old_square, new_square))
updates.append((old_avg, new_avg))
updates.append((memory, update))
updates.append((param, param + update2))
return updates
示例4: infer_shape
def infer_shape(self, node, ishapes):
mus_shape, prior_mu, prior_sigma = ishapes
return [
(tensor.maximum(1, mus_shape[0]),),
(tensor.maximum(1, mus_shape[0]),),
(tensor.maximum(1, mus_shape[0]),),
]
示例5: _local_error
def _local_error(self, targetM, i):
pull_error = 0.
ivectors = self._x[:, i, :][self._neighborpairs[:, 0]]
jvectors = self._x[:, i, :][self._neighborpairs[:, 1]]
diffv = ivectors - jvectors
pull_error = linalg.trace(diffv.dot(targetM).dot(diffv.T))
push_error = 0.0
ivectors = self._x[:, i, :][self._set[:, 0]]
jvectors = self._x[:, i, :][self._set[:, 1]]
lvectors = self._x[:, i, :][self._set[:, 2]]
diffij = ivectors - jvectors
diffil = ivectors - lvectors
lossij = diffij.dot(targetM).dot(diffij.T)
lossil = diffil.dot(targetM).dot(diffil.T)
mask = T.neq(self._y[self._set[:, 0]], self._y[self._set[:, 2]])
push_error = linalg.trace(mask*T.maximum(lossij - lossil + 1, 0))
self.zerocount = T.eq(linalg.diag(mask*T.maximum(lossij - lossil + 1, 0)), 0).sum()
# print np.sqrt((i+1.0)/self.M)
# pull_error = pull_error * np.sqrt((i+1.0)/self.M)
# push_error = push_error * np.sqrt((i+1.0)/self.M)
return pull_error, push_error
示例6: convolutional_model
def convolutional_model(X, w_1, w_2, w_3, w_4, w_5, w_6, p_1, p_2, p_3, p_4, p_5):
l1 = dropout(T.tanh( max_pool_2d(T.maximum(conv2d(X, w_1, border_mode='full'),0.), (2, 2),ignore_border=True) + b_1.dimshuffle('x', 0, 'x', 'x') ), p_1)
l2 = dropout(T.tanh( max_pool_2d(T.maximum(conv2d(l1, w_2), 0.), (2, 2),ignore_border=True) + b_2.dimshuffle('x', 0, 'x', 'x') ), p_2)
l3 = dropout(T.flatten(T.tanh( max_pool_2d(T.maximum(conv2d(l2, w_3), 0.), (2, 2),ignore_border=True) + b_3.dimshuffle('x', 0, 'x', 'x') ), outdim=2), p_3)# flatten to switch back to 1d layers
l4 = dropout(T.maximum(T.dot(l3, w_4), 0.), p_4)
l5 = dropout(T.maximum(T.dot(l4, w_5), 0.), p_5)
return T.dot(l5, w_6)
示例7: cost
def cost(self):
known_grads = None
xd = self.z.reshape((self.z.shape[0]*self.z.shape[1],self.z.shape[2]))
epsilon = numpy.float32(1e-10)
# cross-entropy
nll, _ = T.nnet.crossentropy_softmax_1hot(x=xd[self.i], y_idx=self.y_data_flat[self.i])
ce = T.sum(nll)
# entropy
def entropy(p, axis=None):
if self.use_max and axis is not None:
q = p.dimshuffle(axis, *(range(axis) + range(axis+1,p.ndim)))
#return -T.mean(T.log(T.maximum(T.max(q,axis=0),epsilon)))
return -T.mean(T.max(q,axis=0)+epsilon) + T.log(T.cast(p.shape[axis],'float32'))
else:
return -T.mean(p*T.log(p+epsilon)) + T.log(T.cast(p.shape[axis],'float32'))
ez = T.exp(self.z) * T.cast(self.index.dimshuffle(0,1,'x').repeat(self.z.shape[2],axis=2), 'float32')
et = entropy(ez / T.maximum(epsilon,T.sum(ez,axis=0,keepdims=True)),axis=0)
eb = entropy(ez / T.maximum(epsilon,T.sum(ez,axis=1,keepdims=True)),axis=1)
ed = entropy(ez / T.maximum(epsilon,T.sum(ez,axis=2,keepdims=True)),axis=2)
# maximize entropy across T and B and minimize entropy across D
e = self.e_d * ed - (self.e_t * et + self.e_b * eb) / numpy.float32(self.e_t + self.e_b)
import theano.ifelse
if self.train_flag:
return theano.ifelse.ifelse(T.cast(self.xflag,'int8'),e,ce), known_grads
else:
return ce, known_grads
示例8: advanced_indexing
def advanced_indexing(volume, *indices_list, **kwargs):
""" Performs advanced indexing on `volume`.
This function exists because in Theano<=0.9 advanced indexing is
only supported along the first dimension.
Notes
-----
Assuming `volume` is C contiguous.
"""
strides = kwargs.get("strides")
if strides is None:
shapes = T.cast(volume.shape[:len(indices_list)], dtype=theano.config.floatX)
strides = T.concatenate([T.ones((1,)), T.cumprod(shapes[::-1])[:-1]], axis=0)[::-1]
shapes = T.cast(volume.shape, dtype=theano.config.floatX)
indices = T.maximum(0, T.minimum(indices_list[-1], shapes[len(indices_list)-1]-1))
for i in range(len(indices_list)-1):
clipped_idx = T.maximum(0, T.minimum(indices_list[i], shapes[i]-1))
indices += clipped_idx * strides[i]
# indices = T.sum(T.stack(indices_list, axis=1)*strides[:len(indices_list)], axis=1)
indices = T.cast(indices, dtype="int32")
return volume.reshape((-1, volume.shape[-1]))[indices]
示例9: init_lpool
def init_lpool(self, x, x_shp,
ker_shape=(3, 3),
order=1,
stride=1,
mode='valid'):
if hasattr(order, '__iter__'):
o1 = (order == 1).all()
o2 = (order == order.astype(np.int)).all()
else:
o1 = order == 1
o2 = (order == int(order))
if o1:
r, r_shp = self.boxconv(x, x_shp, ker_shape)
elif o2:
r, r_shp = self.boxconv(x ** order, x_shp, ker_shape)
r = tensor.maximum(r, 0) ** (1.0 / order)
else:
r, r_shp = self.boxconv(abs(x) ** order, x_shp, ker_shape)
r = tensor.maximum(r, 0) ** (1.0 / order)
if stride > 1:
r = r[:, :, ::stride, ::stride]
# intdiv is tricky... so just use numpy
r_shp = np.empty(r_shp)[:, :, ::stride, ::stride].shape
return r, r_shp
示例10: get_updates
def get_updates(self, v):
# Contrastive divergence
chain_end, updates_CD = self.CD(self, chain_start=v, cdk=self.CDk)
# [Expected] negative log-likelihood
cost = T.mean(self.free_energy(v), axis=0) - T.mean(self.free_energy(chain_end), axis=0)
# L2 Regularization
if isinstance(self.regularize, L2Regularization):
cost += self.regularization
# Gradients (use automatic differentiation)
# We must not compute the gradient through the gibbs sampling, i.e. use consider_constant
gparams = T.grad(cost, self.parameters, consider_constant=[chain_end])
gradients = dict(zip(self.parameters, gparams))
# Get learning rates for all params given their gradient.
lr, updates_lr = self.learning_rate(gradients)
updates = OrderedDict()
updates.update(updates_CD) # Add updates from CD
updates.update(updates_lr) # Add updates from learning_rate
# Updates parameters
for param, gparam in gradients.items():
updates[param] = param - lr[param] * gradients[param]
if isinstance(self.regularize, L1Regularization):
updates[self.b] = T.sgn(updates[self.b]) * T.maximum(abs(updates[self.b]) - lr[self.b]*self.regularize.decay, 0)
updates[self.W] = T.sgn(updates[self.W]) * T.maximum(abs(updates[self.W]) - lr[self.W]*self.regularize.decay, 0)
return updates
示例11: setup
def setup(self, bottom, top):
from caffe_helper.theano_util import init_theano
init_theano()
import theano as tn
import theano.tensor as T
assert len(bottom) == 2
assert len(top) == 1
s_y = T.matrix('y') # y in [-inf, inf]
s_t = T.matrix('t') # t in {-1, 0, 1} where 0 is ignored
s_dloss = T.scalar('dloss')
# Forward
# s_loss = T.mean(abs(s_t) * T.log1p(T.exp(-s_y * s_t))) # unstable
s_loss = -T.sum(
abs(s_t) * (
s_y * ((s_t >= 0) - (s_y >= 0)) - T.log1p(T.exp(-abs(s_y)))))\
/ T.maximum(T.sum(abs(s_t)), 1)
# Backward
s_p = 1 / (1 + T.exp(-s_y))
s_dy = s_dloss * abs(s_t) * (s_p - (s_t >= 0)) / \
T.maximum(T.sum(abs(s_t)), 1)
def _o(s):
return tn.Out(s, borrow=True)
self.tn_forward = tn.function([s_y, s_t], s_loss)
self.tn_backward = tn.function([s_y, s_t, s_dloss], _o(s_dy))
示例12: tiled_eye
def tiled_eye(n1, n2, dtype="float32"):
r1 = T.maximum((n1 - 1) / n2 + 1, 1)
r2 = T.maximum((n2 - 1) / n1 + 1, 1)
small_eye = T.eye(T.minimum(n1, n2), dtype=dtype)
tiled_big = T.tile(small_eye, (r1, r2))
tiled_part = tiled_big[:n1,:n2]
return tiled_part
示例13: maxout_func
def maxout_func(n_out, last_start, pool_size, rectify, lin_output):
tmp_output = lin_output[:,0:last_start+1:pool_size]
for i in range(1, pool_size):
cur = lin_output[:,i:last_start+i+1:pool_size]
tmp_output = T.maximum(cur, tmp_output)
if rectify:
self.tmp_output = T.maximum(0, self.tmp_output)
示例14: grad_init
def grad_init(self):
#self.mov_std = theano.shared(numpy.float32(1.), 'std')
rewards_ = self.rewards[0]
mean_rewards = rewards_.mean()
var_rewards = rewards_.var()
pp = self.params.values()
#mean_rewards = (self.mask * self.rewards).sum(-1, keepdims=True) / tensor.maximum(1., self.mask.sum(-1, keepdims=True))
##centered_rewards = self.rewards - self.vapprox.v[:,:,0] - mean_rewards
centered_rewards = rewards_ - mean_rewards - self.vapprox.v[:,0]
#mean2_rewards = (self.mask * (self.rewards ** 2)).sum(-1, keepdims=True) / tensor.maximum(1., self.mask.sum(-1, keepdims=True))
#var_rewards = mean2_rewards - (mean_rewards ** 2)
scaled_rewards = centered_rewards / tensor.maximum(1., tensor.sqrt(tensor.maximum(0., var_rewards)))
logprob = 0.
reg = 0.
for oi in xrange(self.n_out):
labs = self.actions[:,:,oi].flatten()
labs_idx = tensor.arange(labs.shape[0]) * self.out_dim + labs
logprob = logprob + ((self.mask *
tensor.log(self.pi[oi].flatten()+1e-6)[labs_idx]
.reshape([self.actions.shape[0],
self.actions.shape[1]])).sum(0))
reg = reg - (self.pi[oi] * tensor.log(self.pi[oi]+1e-6)).sum(-1).sum(0)
self.cost = -tensor.mean(scaled_rewards * logprob + self.reg_c * reg)
self.grads = tensor.grad(self.cost, wrt=pp)
示例15: inner
def inner(target, embedding):
"""Return a theano expression of a vector containing the sample wise
loss of drlim.
The push_margin, pull_margin and coefficient for the contrastives
used are %.f, %.f and %.f respectively.
Parameters
----------
target : array_like
A vector of length `n`. If 1, sample `2 * n` and sample
`2 * n + 1` are deemed similar.
embedding : array_like
Array containing the embeddings of samples row wise.
""" % (push_margin, pull_margin, c_contrastive)
target = target[:, 0]
n_pair = embedding.shape[0] // 2
n_feature = embedding.shape[1]
# Reshape array to get pairs.
embedding = embedding.reshape((n_pair, n_feature * 2))
# Calculate distances of pairs.
diff = (embedding[:, :n_feature] - embedding[:, n_feature:])
dist = T.sqrt((diff ** 2).sum(axis=1) + 1e-8)
pull = target * f_pull_loss(T.maximum(0, dist - pull_margin))
push = (1 - target) * f_push_loss(T.maximum(0, push_margin - dist))
loss = pull + c_contrastive * push
return loss.dimshuffle(0, 'x')