本文整理汇总了Python中theano.tensor.shape_padright函数的典型用法代码示例。如果您正苦于以下问题:Python shape_padright函数的具体用法?Python shape_padright怎么用?Python shape_padright使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了shape_padright函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_prediction
def create_prediction(self): # 做一次predict的方法
gfs = self.gfs
pm25in = self.pm25in
# 初始第一次前传
self.layerstatus = self.model.forward(
T.concatenate([gfs[:, 0], gfs[:, 1], gfs[:, 2], pm25in[:, 0], pm25in[:, 1], self.cnt[:, :, 0]], axis=1)
)
# results.shape?40*1
self.results = self.layerstatus[-1]
if self.steps > 1:
self.layerstatus = self.model.forward(
T.concatenate([gfs[:, 1], gfs[:, 2], gfs[:, 3], pm25in[:, 1], self.results, self.cnt[:, :, 1]], axis=1),
self.layerstatus,
)
self.results = T.concatenate([self.results, self.layerstatus[-1]], axis=1)
# 前传之后step-2次
for i in xrange(2, self.steps):
self.layerstatus = self.model.forward(
T.concatenate(
[
gfs[:, i],
gfs[:, i + 1],
gfs[:, i + 2],
T.shape_padright(self.results[:, i - 2]),
T.shape_padright(self.results[:, i - 1]),
self.cnt[:, :, i],
],
axis=1,
),
self.layerstatus,
)
# need T.shape_padright???
self.results = T.concatenate([self.results, self.layerstatus[-1]], axis=1)
return self.results
示例2: roc_curves
def roc_curves(y_true, y_predicted):
"returns roc curves calculated axis -1-wise"
fps, tps, thresholds = _binary_clf_curves(y_true, y_predicted)
last_col = _last_col_idx(y_true.ndim)
fpr = fps.astype('float32') / T.shape_padright(fps[last_col], 1)
tpr = tps.astype('float32') / T.shape_padright(tps[last_col], 1)
return fpr, tpr, thresholds
示例3: maxpool_3D
def maxpool_3D(input, ds, ignore_border=False):
#input.dimshuffle (0, 2, 1, 3, 4) # convert to make video in back.
# no need to reshuffle.
if input.ndim < 3:
raise NotImplementedError('max_pool_3d requires a dimension >= 3')
# extract nr dimensions
vid_dim = input.ndim
# max pool in two different steps, so we can use the 2d implementation of
# downsamplefactormax. First maxpool frames as usual.
# Then maxpool the time dimension. Shift the time dimension to the third
# position, so rows and cols are in the back
# extract dimensions
frame_shape = input.shape[-2:]
# count the number of "leading" dimensions, store as dmatrix
batch_size = T.prod(input.shape[:-2])
batch_size = T.shape_padright(batch_size,1)
# store as 4D tensor with shape: (batch_size,1,height,width)
new_shape = T.cast(T.join(0, batch_size,
T.as_tensor([1,]),
frame_shape), 'int32')
input_4D = T.reshape(input, new_shape, ndim=4)
# downsample mini-batch of videos in rows and cols
op = DownsampleFactorMax((ds[1],ds[2]), ignore_border) # so second and third dimensions of ds are for height and width
output = op(input_4D)
# restore to original shape
outshape = T.join(0, input.shape[:-2], output.shape[-2:])
out = T.reshape(output, outshape, ndim=input.ndim)
# now maxpool time
# output (time, rows, cols), reshape so that time is in the back
shufl = (list(range(vid_dim-3)) + [vid_dim-2]+[vid_dim-1]+[vid_dim-3])
input_time = out.dimshuffle(shufl)
# reset dimensions
vid_shape = input_time.shape[-2:]
# count the number of "leading" dimensions, store as dmatrix
batch_size = T.prod(input_time.shape[:-2])
batch_size = T.shape_padright(batch_size,1)
# store as 4D tensor with shape: (batch_size,1,width,time)
new_shape = T.cast(T.join(0, batch_size,
T.as_tensor([1,]),
vid_shape), 'int32')
input_4D_time = T.reshape(input_time, new_shape, ndim=4)
# downsample mini-batch of videos in time
op = DownsampleFactorMax((1,ds[0]), ignore_border) # Here the time dimension is downsampled.
outtime = op(input_4D_time)
# output
# restore to original shape (xxx, rows, cols, time)
outshape = T.join(0, input_time.shape[:-2], outtime.shape[-2:])
shufl = (list(range(vid_dim-3)) + [vid_dim-1]+[vid_dim-3]+[vid_dim-2])
#rval = T.reshape(outtime, outshape, ndim=input.ndim).dimshuffle(shufl)
return T.reshape(outtime, outshape, ndim=input.ndim).dimshuffle(shufl)
示例4: _warp_times
def _warp_times(self, t):
delta = tt.shape_padleft(t) / tt.shape_padright(self.period, t.ndim)
delta += tt.shape_padright(self._base_time, t.ndim)
ind = tt.cast(tt.floor(delta), "int64")
dt = tt.stack([ttv[tt.clip(ind[i], 0, ttv.shape[0]-1)]
for i, ttv in enumerate(self.ttvs)], -1)
return tt.shape_padright(t) + dt
示例5: prediction
def prediction(self, h, bias):
srng = RandomStreams(seed=42)
prop, mean_x, mean_y, std_x, std_y, rho, bernoulli = \
self.compute_parameters(h, bias)
mode = T.argmax(srng.multinomial(pvals=prop, dtype=prop.dtype), axis=1)
v = T.arange(0, mean_x.shape[0])
m_x = mean_x[v, mode]
m_y = mean_y[v, mode]
s_x = std_x[v, mode]
s_y = std_y[v, mode]
r = rho[v, mode]
# cov = r * (s_x * s_y)
normal = srng.normal((h.shape[0], 2))
x = normal[:, 0]
y = normal[:, 1]
# x_n = T.shape_padright(s_x * x + cov * y + m_x)
# y_n = T.shape_padright(s_y * y + cov * x + m_y)
x_n = T.shape_padright(m_x + s_x * x)
y_n = T.shape_padright(m_y + s_y * (x * r + y * T.sqrt(1.-r**2)))
uniform = srng.uniform((h.shape[0],))
pin = T.shape_padright(T.cast(bernoulli > uniform, floatX))
return T.concatenate([x_n, y_n, pin], axis=1)
示例6: __init__
def __init__(self, n, p, *args, **kwargs):
super(Multinomial, self).__init__(*args, **kwargs)
p = p / tt.sum(p, axis=-1, keepdims=True)
n = np.squeeze(n) # works also if n is a tensor
if len(self.shape) > 1:
m = self.shape[-2]
try:
assert n.shape == (m,)
except (AttributeError, AssertionError):
n = n * tt.ones(m)
self.n = tt.shape_padright(n)
self.p = p if p.ndim > 1 else tt.shape_padleft(p)
elif n.ndim == 1:
self.n = tt.shape_padright(n)
self.p = p if p.ndim > 1 else tt.shape_padleft(p)
else:
# n is a scalar, p is a 1d array
self.n = tt.as_tensor_variable(n)
self.p = tt.as_tensor_variable(p)
self.mean = self.n * self.p
mode = tt.cast(tt.round(self.mean), 'int32')
diff = self.n - tt.sum(mode, axis=-1, keepdims=True)
inc_bool_arr = tt.abs_(diff) > 0
mode = tt.inc_subtensor(mode[inc_bool_arr.nonzero()],
diff[inc_bool_arr.nonzero()])
self.mode = mode
示例7: getTheanoSimilarityFunction
def getTheanoSimilarityFunction():
"""
Return a theano function erforming valid convolution of a filter on an
image
"""
# Define the input variables to the function
patches = T.tensor3(dtype='float32') # AxBx(patchsize**2)
filters = T.matrix(dtype='float32') # Cx(patchsize**2)
globalMean = T.vector(dtype='float32')
globalStd = T.vector(dtype='float32')
# Perform canonical processing of the patches
meanstd = patches.std()
mean = T.shape_padright(patches.mean(2), n_ones=1)
std = T.shape_padright(patches.std(2) + 0.1 * meanstd, n_ones=1)
std = T.shape_padright(patches.std(2) + 1e-6, n_ones=1)
canonicalPatches_ = (patches - mean) / std
canonicalPatches = (canonicalPatches_ - globalMean) / globalStd
# Compute the similarities between each patch and each filter
similarities = T.tensordot(canonicalPatches, filters, axes=[[2],[1]]) # AxBxC
normFactor = ((canonicalPatches** 2).sum(2) ** 0.5)
normFactorPadded = T.shape_padright(normFactor, n_ones=1)
# Normalize the similarities by the norm of the patches
similaritiesNorm = (similarities / normFactorPadded)
# Compile and return the theano function
f = theano.function([patches, filters, globalMean, globalStd],
similaritiesNorm, on_unused_input='ignore')
return f
示例8: sym_mask_logdensity_estimator_intermediate
def sym_mask_logdensity_estimator_intermediate(self, x, mask):
non_linearity_name = self.parameters["nonlinearity"].get_name()
assert non_linearity_name == "sigmoid" or non_linearity_name == "RLU"
x = x.T # BxD
mask = mask.T # BxD
output_mask = constantX(1) - mask # BxD
D = constantX(self.n_visible)
d = mask.sum(1) # d is the 1-based index of the dimension whose value to infer (not the size of the context)
masked_input = x * mask # BxD
h = self.nonlinearity(T.dot(masked_input, self.W1) + T.dot(mask, self.Wflags) + self.b1) # BxH
for l in xrange(self.n_layers - 1):
h = self.nonlinearity(T.dot(h, self.Ws[l]) + self.bs[l]) # BxH
z_alpha = T.tensordot(h, self.V_alpha, [[1], [1]]) + T.shape_padleft(self.b_alpha)
z_mu = T.tensordot(h, self.V_mu, [[1], [1]]) + T.shape_padleft(self.b_mu)
z_sigma = T.tensordot(h, self.V_sigma, [[1], [1]]) + T.shape_padleft(self.b_sigma)
temp = T.exp(z_alpha) # + 1e-6
# temp += T.shape_padright(temp.sum(2)/1e-3)
Alpha = temp / T.shape_padright(temp.sum(2)) # BxDxC
Mu = z_mu # BxDxC
Sigma = T.exp(z_sigma) # + 1e-6 #BxDxC
# Alpha = Alpha * T.shape_padright(output_mask) + T.shape_padright(mask)
# Mu = Mu * T.shape_padright(output_mask)
# Sigma = Sigma * T.shape_padright(output_mask) + T.shape_padright(mask)
# Phi = -constantX(0.5) * T.sqr((Mu - T.shape_padright(x*output_mask)) / Sigma) - T.log(Sigma) - constantX(0.5 * np.log(2*np.pi)) #BxDxC
Phi = (
-constantX(0.5) * T.sqr((Mu - T.shape_padright(x)) / Sigma)
- T.log(Sigma)
- constantX(0.5 * np.log(2 * np.pi))
) # BxDxC
logdensity = (log_sum_exp(Phi + T.log(Alpha), axis=2) * output_mask).sum(1) * D / (D - d)
return (logdensity, z_alpha, z_mu, z_sigma, Alpha, Mu, Sigma, h)
示例9: create_prediction
def create_prediction(self):#做一次predict的方法
gfs=self.gfs
pm25in=self.pm25in
#初始第一次前传
gfs_x=T.concatenate([gfs[:,0],gfs[:,1],gfs[:,2]],axis=1)
pm25in_x=T.concatenate([pm25in[:,0],pm25in[:,1]],axis=1)
self.layerstatus=self.model.forward(T.concatenate([gfs_x,pm25in_x,self.cnt[:,:,0]],axis=1))
self.results=self.layerstatus[-1]
for i in xrange(1,7):#前6次(0-5),输出之前的先做的6个frame,之后第7次是第1个输出
gfs_x=T.concatenate([gfs_x[:,9:],gfs[:,i+2]],axis=1)
pm25in_x=T.concatenate([pm25in_x[:,1:],pm25in[:,i+1]],axis=1)
self.layerstatus=self.model.forward(T.concatenate([gfs_x,pm25in_x,self.cnt[:,:,i]],axis=1),self.layerstatus)
self.results=T.concatenate([self.results,self.layerstatus[-1]],axis=1)
if self.steps > 1:
gfs_x=T.concatenate([gfs_x[:,9:],gfs[:,9]],axis=1)
pm25in_x=T.concatenate([pm25in_x[:,1:],T.shape_padright(self.results[:,-1])],axis=1)
self.layerstatus=self.model.forward(T.concatenate([gfs_x,pm25in_x,self.cnt[:,:,7]],axis=1),self.layerstatus)
self.results=T.concatenate([self.results,self.layerstatus[-1]],axis=1)
#前传之后step-2次
for i in xrange(2,self.steps):
gfs_x=T.concatenate([gfs_x[:,9:],gfs[:,i+8]],axis=1)
pm25in_x=T.concatenate([pm25in_x[:,1:],T.shape_padright(self.results[:,-1])],axis=1)
self.layerstatus=self.model.forward(T.concatenate([gfs_x,pm25in_x,self.cnt[:,:,i+6]],axis=1),self.layerstatus)
#need T.shape_padright???
self.results=T.concatenate([self.results,self.layerstatus[-1]],axis=1)
return self.results
示例10: filter_spike_train
def filter_spike_train(n,S,taus):
""" Helper function to filter the spike train
"""
filt = T.shape_padright(filt_fn(taus[n]), n_ones=1)
filtered_S = conv2d(T.shape_padright(S[:,n], n_ones=1),
filt,
border_mode='full')
return filtered_S[0,:,0]
示例11: dfe_dlhat
def dfe_dlhat(self, g_hat, h_hat, l_hat, v):
# term from loss function
dloss_dl = self.label_multiplier * (T.dot(h_hat, self.Whl) + self.lbias)
rval = dloss_dl * l_hat - l_hat * T.shape_padright(T.sum(l_hat * dloss_dl, axis=1))
# term from entropy.
# dentropy = T.sum(-l_hat * T.log(l_hat), axis=1)
dentropy = - T.xlogx.xlogx(l_hat) - l_hat +\
l_hat * T.shape_padright(T.sum(T.xlogx.xlogx(l_hat) + l_hat, axis=1))
return rval + dentropy
示例12: density_given_previous_a_and_x
def density_given_previous_a_and_x(x, w, V_alpha, b_alpha, V_mu, b_mu, V_sigma, b_sigma, activations_factor, p_prev, a_prev, x_prev):
a = a_prev + T.dot(T.shape_padright(x_prev, 1), T.shape_padleft(w, 1))
h = self.nonlinearity(a * activations_factor) # BxH
Alpha = T.nnet.softmax(T.dot(h, V_alpha) + T.shape_padleft(b_alpha)) # BxC
Mu = T.dot(h, V_mu) + T.shape_padleft(b_mu) # BxC
Sigma = T.exp((T.dot(h, V_sigma) + T.shape_padleft(b_sigma))) # BxC
p = p_prev + log_sum_exp(T.log(Alpha) - T.log(2 * Sigma) - T.abs_(Mu - T.shape_padright(x, 1)) / Sigma)
return (p, a, x)
示例13: _theano_confusion
def _theano_confusion(self, Yh, Y, mask):
Yh = T.argmax(Yh, axis=-1)
shape = list(Yh.shape) + [self.n_out, self.n_out]
C = T.zeros(shape, dtype='int64')
i,j = T.mgrid[0:C.shape[0], 0:C.shape[1]]
C = T.set_subtensor(C[i,j,Y,Yh], 1)
mask = T.shape_padright(T.shape_padright(mask))
C = C*mask
return C
示例14: __call__
def __call__(self, crf, X, Y, mask=None, flank=0):
Yh = self.decode(crf, X, Y)
L = self.loss(Yh, Y)
C = confusion(T.argmax(Yh,axis=-1), Y, Yh.shape[-1])
if mask is not None:
L *= T.shape_padright(mask)
C *= T.shape_padright(T.shape_padright(mask))
n = Yh.shape[0]
return L[flank:n-flank], C[flank:n-flank]
示例15: loss
def loss(self, X, mask=None, flank=0, Z=None):
if Z is None:
Z = self.transform(self.noise(X), mask=mask)
E = self.emit(Z)
L = cross_entropy(E, X)
C = confusion(T.argmax(E,axis=-1), X, E.shape[-1])
if mask is not None:
L *= T.shape_padright(mask)
C *= T.shape_padright(T.shape_padright(mask))
n = X.shape[0]
return L[flank:n-flank], C[flank:n-flank]