本文整理汇总了Python中theano.tensor.addbroadcast函数的典型用法代码示例。如果您正苦于以下问题:Python addbroadcast函数的具体用法?Python addbroadcast怎么用?Python addbroadcast使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了addbroadcast函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Softmax
def Softmax(x, temp = 1):
"""
Softmax Units.
Applies row-wise softmax to the input supplied.
Args:
x: could be a ``theano.tensor`` or a ``theano.shared`` or ``numpy`` arrays or
``python lists``.
temp: temperature of type ``float``. Mainly used during distillation, normal
softmax prefer ``T=1``.
Notes:
Refer [3] for details.
.. [#] Hinton, Geoffrey, Oriol Vinyals, and Jeff Dean. "Distilling the knowledge in
a neural network." arXiv preprint arXiv:1503.02531 (2015).
Returns:
same as input: returns a row-wise softmax output of the same shape as the input.
"""
if temp != 1:
expo = T.exp(x / float(temp)) # at this moment this is mini_batch_size X num_classes.
normalizer = T.sum(expo,axis=1,keepdims=True) # at this moment this is mini_batch_size X 1.
T.addbroadcast(normalizer,1)
return expo / normalizer
else:
return T.nnet.softmax(x)
示例2: fprop
def fprop(self, x, mode='train'):
if mode == 'test':
# this is for use during test/validation time
x_avg = self.params.getParameter('x_avg')
elif mode == 'calculate':
x_avg = x.mean(self.norm_axis, keepdims=True)
elif mode == 'train':
# otherwise calculate the batch mean and std
x_avg = x.mean(self.norm_axis, keepdims=True)
# the following trick is learend from lasagne implementation
running_mean = theano.clone(self.params.getParameter('x_avg'), share_inputs=False)
running_mean_udpate = ((1 - self.alpha) * running_mean
+self.alpha * x_avg)
# set a default update for them
running_mean.default_update = running_mean_udpate
x_avg += 0 * running_mean
else:
raise "mode can only take ['train', 'test', 'calculate']"
self.x_avg = x_avg
x_avg = T.addbroadcast(x_avg, *self.norm_axis)
beta = T.addbroadcast(self.params.getParameter('beta'), *self.norm_axis)
bn_x = x / (x_avg + 1e-18) * beta
return bn_x if self.actFunc is None else self.actFunc(bn_x)
# End BatchExpNormLayer
#-------------------------------------------------------------------------------
示例3: output
def output(self, input):
W_shuffled = self.W.dimshuffle(3, 0, 1, 2) # c01b to bc01
print "input ndim", input.ndim
conv_out = dnn.dnn_conv(img=input,
kerns=W_shuffled,
subsample=(self.stride, self.stride),
border_mode=self.padsize)
conv_out = conv_out + self.b.dimshuffle('x', 0, 'x', 'x')
if self.batch_norm:
conv_out = (conv_out - T.mean(conv_out, axis = (0,2,3), keepdims = True)) / (1.0 + T.std(conv_out, axis=(0,2,3), keepdims = True))
conv_out = conv_out * T.addbroadcast(self.bn_std,0,2,3) + T.addbroadcast(self.bn_mean, 0,2,3)
self.out_store = conv_out
if self.activation == "relu":
self.out = T.maximum(0.0, conv_out)
elif self.activation == "tanh":
self.out = T.tanh(conv_out)
elif self.activation == None:
self.out = conv_out
return T.specify_shape(self.out, (self.batch_size, self.out_channels, self.in_length / self.stride, self.in_length / self.stride))
示例4: get_state
def get_state(self):
st = super(LatentTypeWithTuningCurve, self).get_state()
# The filters are non-identifiable as we can negate both the
# temporal and the spatial filters and get the same net effect.
# By convention, choose the sign that results in the most
# positive temporal filter.
sign = T.sgn(T.sum(self.stim_resp_t, axis=0))
T.addbroadcast(sign, 0)
# Similarly, we can trade a constant between the spatial and temporal
# pieces. By convention, set the temporal filter to norm 1.
Z = T.sqrt(T.sum(self.stim_resp_t**2, axis=0))
T.addbroadcast(Z, 0)
# Compute the normalized temporal response
stim_resp_t = sign*(1.0/Z)*self.stim_resp_t
# Finally, reshape the spatial component as necessary
if self.spatial_ndim == 2:
stim_resp_x = sign*Z*self.stim_resp_x
stim_resp_x = T.reshape(stim_resp_x,
self.spatial_shape + (self.R,))
else:
stim_resp_x = sign*Z*self.stim_resp_x
st.update({'stim_response_x' : stim_resp_x,
'stim_response_t' : stim_resp_t})
return st
示例5: resample_step
def resample_step(self):
idx=self.theano_rng.multinomial(pvals=T.reshape(self.weights_now,(1,self.npcl))).T
s_samp=T.sum(self.s_now*T.addbroadcast(idx,1),axis=0)
h_samp=T.sum(self.h_now*T.addbroadcast(idx,1),axis=0)
return T.cast(s_samp,'float32'), T.cast(h_samp,'float32')
示例6: output
def output(self, input_raw):
if self.flatten_input:
input = input_raw.flatten(2)
else:
input = input_raw
lin_output = T.dot(input, self.W) + self.b
if self.batch_norm:
lin_output = (lin_output - T.mean(lin_output, axis = 0, keepdims = True)) / (1.0 + T.std(lin_output, axis = 0, keepdims = True))
lin_output = (lin_output * T.addbroadcast(self.bn_std,0) + T.addbroadcast(self.bn_mean,0))
self.out_store = lin_output
if self.activation == None:
activation = lambda x: x
elif self.activation == "relu":
activation = lambda x: T.maximum(0.0, x)
elif self.activation == "exp":
activation = lambda x: T.exp(x)
elif self.activation == "tanh":
activation = lambda x: T.tanh(x)
elif self.activation == 'softplus':
activation = lambda x: T.nnet.softplus(x)
else:
raise Exception("Activation not found")
out = activation(lin_output)
#if self.residual:
# return out + input_raw
#else:
# return out
return out
示例7: output
def output(self, input):
if self.unflatten_input != None:
input = T.reshape(input, self.unflatten_input)
conv_out = deconv(input, self.W, subsample=(2, 2), border_mode=(2,2))
conv_out = conv_out + self.b.dimshuffle('x', 0, 'x', 'x')
if self.batch_norm:
conv_out = (conv_out - conv_out.mean(axis = (0,2,3), keepdims = True)) / (1.0 + conv_out.std(axis = (0,2,3), keepdims = True))
conv_out = conv_out * T.addbroadcast(self.bn_std,0,2,3) + T.addbroadcast(self.bn_mean,0,2,3)
if self.activation == "relu":
out = T.maximum(0.0, conv_out)
elif self.activation == "tanh":
out = T.tanh(conv_out)
elif self.activation == None:
out = conv_out
else:
raise Exception()
self.params = {'W' : self.W, 'b' : self.b}
if self.batch_norm:
self.params["mu"] = self.bn_mean
self.params["sigma"] = self.bn_std
return out
示例8: keep_max
def keep_max(input, theta, k, sent_mask):
sig_input = T.nnet.sigmoid(T.dot(input, theta))
sent_mask = sent_mask.dimshuffle(0, 'x', 1, 'x')
sig_input = sig_input * sent_mask
#sig_input = T.dot(input, theta)
if k == 0:
result = input * T.addbroadcast(sig_input, 3)
return result, sig_input
# get the sorted idx
sort_idx = T.argsort(sig_input, axis=2)
k_max_ids = sort_idx[:,:,-k:,:]
dim0, dim1, dim2, dim3 = k_max_ids.shape
batchids = T.repeat(T.arange(dim0), dim1*dim2*dim3)
mapids = T.repeat(T.arange(dim1), dim2*dim3).reshape((1, dim2*dim3))
mapids = T.repeat(mapids, dim0, axis=0).flatten()
rowids = k_max_ids.flatten()
colids = T.arange(dim3).reshape((1, dim3))
colids = T.repeat(colids, dim0*dim1*dim2, axis=0).flatten()
sig_mask = T.zeros_like(sig_input)
choosed = sig_input[batchids, mapids, rowids, colids]
sig_mask = T.set_subtensor(sig_mask[batchids, mapids, rowids, colids], 1)
input_mask = sig_mask * sig_input
result = input * T.addbroadcast(input_mask, 3)
return result, sig_input
示例9: get_output_for
def get_output_for(self, input, deterministic=False, **kwargs):
if deterministic:
# use stored mean and std
mean = self.mean
std = self.std
else:
# use this batch's mean and std
mean = input.mean(self.axes, keepdims=True)
std = input.std(self.axes, keepdims=True)
# and update the stored mean and std:
# we create (memory-aliased) clones of the stored mean and std
running_mean = theano.clone(self.mean, share_inputs=False)
running_std = theano.clone(self.std, share_inputs=False)
# set a default update for them
running_mean.default_update = (1 - self.alpha) * running_mean + self.alpha * mean
running_std.default_update = (1 - self.alpha) * running_std + self.alpha * std
# and include them in the graph so their default updates will be
# applied (although the expressions will be optimized away later)
mean += 0 * running_mean
std += 0 * running_std
std += self.epsilon
mean = T.addbroadcast(mean, *self.axes)
std = T.addbroadcast(std, *self.axes)
beta = T.addbroadcast(self.beta, *self.axes)
gamma = T.addbroadcast(self.gamma, *self.axes)
normalized = (input - mean) * (gamma / std) + beta
return self.nonlinearity(normalized)
示例10: keep_max
def keep_max(input, theta, k):
"""
:type input: theano.tensor.tensor4
:param input: the input data
:type theta: theano.tensor.matrix
:param theta: the parameter for sigmoid function
:type k: int
:param k: the number k used to define top k sentence to remain
"""
sig_input = T.nnet.sigmoid(T.dot(input, theta))
if k == 0: # using all the sentences
result = input * T.addbroadcast(sig_input, 3)
return result, sig_input
# get the sorted idx
sort_idx = T.argsort(sig_input, axis=2)
k_max_ids = sort_idx[:,:,-k:,:]
dim0, dim1, dim2, dim3 = k_max_ids.shape
batchids = T.repeat(T.arange(dim0), dim1*dim2*dim3)
mapids = T.repeat(T.arange(dim1), dim2*dim3).reshape((1, dim2*dim3))
mapids = T.repeat(mapids, dim0, axis=0).flatten()
rowids = k_max_ids.flatten()
colids = T.arange(dim3).reshape((1, dim3))
colids = T.repeat(colids, dim0*dim1*dim2, axis=0).flatten()
# construct masked data
sig_mask = T.zeros_like(sig_input)
choosed = sig_input[batchids, mapids, rowids, colids]
sig_mask = T.set_subtensor(sig_mask[batchids, mapids, rowids, colids], 1)
input_mask = sig_mask * sig_input
result = input * T.addbroadcast(input_mask, 3)
return result, sig_input
示例11: log_p
def log_p(self, L):
""" Compute log prob of the given value under this prior
Input: L ~ NxD
"""
assert L.ndim == 2, "L must be 2d!"
# Compute pairwise L2 norm
L1 = L.dimshuffle(0,'x',1) # Nx1xD
L2 = L.dimshuffle('x',0,1) # 1xNxD
T.addbroadcast(L1,1)
T.addbroadcast(L2,0)
# Compute pairwise distances
D = ((L1-L2)**2).sum(axis=2)
# Compute the kernel
K = T.exp(-D / self.sigma**2)
# Log prob is the log determinant of the pairwise distances
lp_det = T.log(self.d(K))
# Also multiply by a spherical Gaussian with standard deviation of 'bound'
# to prevent points from diverging to infinity
lp_gauss = self.gaussian.log_p(L)
return lp_det + lp_gauss
示例12: output
def output(self, input):
if self.unflatten_input != None:
input = T.reshape(input, self.unflatten_input)
W_shuffled = self.W.val.dimshuffle(3, 0, 1, 2) # c01b to bc01
conv_out = dnn.dnn_conv(img=input,
kerns=W_shuffled,
subsample=(self.convstride, self.convstride),
border_mode=self.padsize)
conv_out = conv_out + self.b.val.dimshuffle('x', 0, 'x', 'x')
if self.batch_norm:
conv_out = (conv_out - T.mean(conv_out, axis = (0,2,3), keepdims = True)) / (1.0 + T.std(conv_out, axis=(0,2,3), keepdims = True))
conv_out = conv_out * T.addbroadcast(self.bn_std,0,2,3) + T.addbroadcast(self.bn_mean, 0,2,3)
self.out_store = conv_out
if self.activation == "relu":
self.out = T.maximum(0.0, conv_out)
elif self.activation == "tanh":
self.out = T.tanh(conv_out)
elif self.activation == None:
self.out = conv_out
#if self.residual:
# print "USING RESIDUAL"
# self.out += input
return self.out
示例13: local_contrast_normalize
def local_contrast_normalize(X, window, img_shape):
"""Return normalized X and the convolution transform
"""
batchsize, channels, R, C = img_shape
assert window.shape[0] == 1
assert window.shape[1] == channels
N = window.shape[2]
assert window.shape[3] == N
blur = tlinear.Conv2d(
filters=sharedX(window, 'LCN_window'),
img_shape=img_shape,
border_mode='full')
N2 = N//2
# remove global mean
X = X - X.mean(axis=[1, 2, 3]).dimshuffle(0, 'x', 'x', 'x')
#remove local mean
blurred_x = tensor.addbroadcast(blur.lmul(X), 1)
x2c = X - blurred_x[:, :, N2:R + N2, N2:C + N2]
# standardize contrast
blurred_x2c_sqr = tensor.addbroadcast(blur.lmul(x2c ** 2), 1)
x2c_lcn = x2c / tensor.sqrt((10 + blurred_x2c_sqr[:, :, N2:R + N2, N2:C + N2]))
return x2c_lcn, blur
示例14: _ct
def _ct(self, other):
''' Helper function to make tensors dimensions compatible'''
if (other.var_set == self.var_set):
return (self.pt_tensor, other.pt_tensor)
union_var_set = other.scope.union(self.scope)
vidx1 = frozenset(self.var_indices)
vidx2 = frozenset(other.var_indices)
union_indices = vidx1.union(vidx2)
shape1 = []
shape2 = []
b1 = []
b2 = []
u1 = []
u2 = []
for i,vidx in enumerate(sorted(union_indices)):
if (vidx in vidx1):
shape1.append(self.discrete_pgm.cardinalities[vidx])
u1.append(i)
else:
shape1.append(1)
b1.append(i)
if (vidx in vidx2):
shape2.append(self.discrete_pgm.cardinalities[vidx])
u2.append(i)
else:
shape2.append(1)
b2.append(i)
t1 = T.addbroadcast(T.unbroadcast(self.pt_tensor.reshape(shape1, len(shape1)), *u1), *b1)
t2 = T.addbroadcast(T.unbroadcast(other.pt_tensor.reshape(shape2, len(shape2)), *u2), *b2)
return (t1, t2)
示例15: embed
def embed(self,x, y, kth):
hidden = self.hidden_k(x,self.superw,self.dicw, kth)
size = y.ndim
y = T.addbroadcast(y,size - 1)
embedding = T.sum(hidden*y,0)/T.addbroadcast(T.cast(T.sum(y,0), 'int16'), size - 2)
return embedding