本文整理汇总了Python中keras.backend.reshape方法的典型用法代码示例。如果您正苦于以下问题:Python backend.reshape方法的具体用法?Python backend.reshape怎么用?Python backend.reshape使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.reshape方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def call(self, x):
mean = K.mean(x, axis=-1)
std = K.std(x, axis=-1)
if len(x.shape) == 3:
mean = K.permute_dimensions(
K.repeat(mean, x.shape.as_list()[-1]),
[0,2,1]
)
std = K.permute_dimensions(
K.repeat(std, x.shape.as_list()[-1]),
[0,2,1]
)
elif len(x.shape) == 2:
mean = K.reshape(
K.repeat_elements(mean, x.shape.as_list()[-1], 0),
(-1, x.shape.as_list()[-1])
)
std = K.reshape(
K.repeat_elements(mean, x.shape.as_list()[-1], 0),
(-1, x.shape.as_list()[-1])
)
return self._g * (x - mean) / (std + self._epsilon) + self._b
示例2: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def call(self, x, mask=None):
# computes a probability distribution over the timesteps
# uses 'max trick' for numerical stability
# reshape is done to avoid issue with Tensorflow
# and 1-dimensional weights
logits = K.dot(x, self.W)
x_shape = K.shape(x)
logits = K.reshape(logits, (x_shape[0], x_shape[1]))
ai = K.exp(logits - K.max(logits, axis=-1, keepdims=True))
# masked timesteps have zero weight
if mask is not None:
mask = K.cast(mask, K.floatx())
ai = ai * mask
att_weights = ai / (K.sum(ai, axis=1, keepdims=True) + K.epsilon())
weighted_input = x * K.expand_dims(att_weights)
result = K.sum(weighted_input, axis=1)
if self.return_attention:
return [result, att_weights]
return result
示例3: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def call(self,x,mask=None):
conv_input,theta = x
s = theta.shape
theta = T.reshape(theta,[-1,s[2]])
m = K.not_equal(conv_input,0.)
#### For translation
trans = _trans(theta)
output = _transform_trans(trans, conv_input)
output = output * K.cast(m,K.floatx())
### For rotation
M = _fusion(theta)
output = _transform_rot(M,output)
return output
开发者ID:microsoft,项目名称:View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition,代码行数:18,代码来源:transform_rnn.py
示例4: get_output
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def get_output(self, train=False):
def format_shape(shape):
if K._BACKEND == 'tensorflow':
def trf(x):
try:
return int(x)
except TypeError:
return x
return map(trf, shape)
return shape
X = self.get_input(train)
in_shape = format_shape(K.shape(X))
batch_flatten_len = K.prod(in_shape[:2])
cast_in_shape = (batch_flatten_len, ) + tuple(in_shape[i] for i in range(2, K.ndim(X)))
pre_outs = self.layer(K.reshape(X, cast_in_shape))
out_shape = format_shape(K.shape(pre_outs))
cast_out_shape = (in_shape[0], in_shape[1]) + tuple(out_shape[i] for i in range(1, K.ndim(pre_outs)))
outputs = K.reshape(pre_outs, cast_out_shape)
return outputs
示例5: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def call(self , x, mask=None):
e1=x[0].T
e2=x[1].T
batch_size = K.shape(x[0])[0]
sim = []
V_out = K.dot(self.V, K.concatenate([e1,e2],axis=0))
for i in range(self.k):
temp = K.batch_dot(K.dot(e1.T,self.W[i,:,:]),e2.T,axes=1)
sim.append(temp)
sim=K.reshape(sim,(self.k,batch_size))
tensor_bi_product = self.activation(V_out+sim)
tensor_bi_product = K.dot(self.U.T, tensor_bi_product).T
return tensor_bi_product
示例6: get_constants
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def get_constants(self, x):
constants = []
if 0 < self.dropout_U < 1:
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, self.hidden_recurrent_dim))
B_U = K.in_train_phase(K.dropout(ones, self.dropout_U), ones)
constants.append(B_U)
else:
constants.append(K.cast_to_floatx(1.))
if self.consume_less == 'cpu' and 0 < self.dropout_W < 1:
input_shape = self.input_spec[0].shape
input_dim = input_shape[-1]
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, input_dim))
B_W = K.in_train_phase(K.dropout(ones, self.dropout_W), ones)
constants.append(B_W)
else:
constants.append(K.cast_to_floatx(1.))
return constants
示例7: get_constants
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def get_constants(self, x):
constants = []
if 0 < self.dropout_U < 1:
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, self.input_dim))
B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(4)]
constants.append(B_U)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(4)])
if 0 < self.dropout_W < 1:
input_shape = K.int_shape(x)
input_dim = input_shape[-1]
ones = K.ones_like(K.reshape(x[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, int(input_dim)))
B_W = [K.in_train_phase(K.dropout(ones, self.dropout_W), ones) for _ in range(4)]
constants.append(B_W)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(4)])
return constants
示例8: contrastive_divergence_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def contrastive_divergence_loss(self, y_true, y_pred):
x = y_pred
#x = K.reshape(x, (-1, self.input_dim))
if(self.is_persistent):
chain_start = self.persistent_chain
else:
chain_start = x
def loss(chain_start, x):
x_rec, _, _ = self.mcmc_chain(chain_start, self.nb_gibbs_steps)
cd = K.mean(self.free_energy(x)) - K.mean(self.free_energy(x_rec))
return cd, x_rec
y, x_rec = loss(chain_start, x)
if(self.is_persistent):
self.updates = [(self.persistent_chain, x_rec)]
return y
示例9: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def call(self, inputs):
def wrapper(rois, mrcnn_class, mrcnn_bbox, image_meta):
# currently supports one image per batch
b = 0
_, _, window, _ = parse_image_meta(image_meta)
detections = refine_detections(
rois[b], mrcnn_class[b], mrcnn_bbox[b], window[b], self.config)
# Pad with zeros if detections < DETECTION_MAX_INSTANCES
gap = self.config.DETECTION_MAX_INSTANCES - detections.shape[0]
assert gap >= 0
if gap > 0:
detections = np.pad(detections, [(0, gap), (0, 0)],
'constant', constant_values=0)
# Cast to float32
# TODO: track where float64 is introduced
detections = detections.astype(np.float32)
# Reshape output
# [batch, num_detections, (y1, x1, y2, x2, class_score)] in pixels
return np.reshape(detections,
[1, self.config.DETECTION_MAX_INSTANCES, 6])
# Return wrapped function
return tf.py_func(wrapper, inputs, tf.float32)
示例10: ifft2
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def ifft2(x):
ff = x
ff = KB.permute_dimensions(ff, (0, 2, 1))
ff = KB.reshape(ff, (x.shape[0] *x.shape[2], x.shape[1]))
tf = ifft(ff)
tf = KB.reshape(tf, (x.shape[0], x.shape[2], x.shape[1]))
tf = KB.permute_dimensions(tf, (0, 2, 1))
tf = KB.reshape(tf, (x.shape[0] *x.shape[1], x.shape[2]))
tt = ifft(tf)
tt = KB.reshape(tt, (x.shape[0], x.shape[1], x.shape[2]))
return tt
#
# FFT Layers:
#
# FFT: Batched 1-D FFT (Input: (Batch, FeatureMaps, TimeSamples))
# IFFT: Batched 1-D IFFT (Input: (Batch, FeatureMaps, FreqSamples))
# FFT2: Batched 2-D FFT (Input: (Batch, FeatureMaps, TimeSamplesH, TimeSamplesW))
# IFFT2: Batched 2-D IFFT (Input: (Batch, FeatureMaps, FreqSamplesH, FreqSamplesW))
#
示例11: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def call(self, x, mask=None):
a = KB.permute_dimensions(x, (1,0,2,3))
a = KB.reshape(a, (x.shape[1] *x.shape[0], x.shape[2], x.shape[3]))
a = ifft2(a)
a = KB.reshape(a, (x.shape[1], x.shape[0], x.shape[2], x.shape[3]))
return KB.permute_dimensions(a, (1,0,2,3))
#
# Tests
#
# Note: The IFFT is the conjugate of the FFT of the conjugate.
#
# np.fft.ifft(x) == np.conj(np.fft.fft(np.conj(x)))
#
示例12: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def call(self, x, mask=None):
assert self.built, 'Layer must be built before being called'
input_shape = K.int_shape(x)
reduction_axes = list(range(len(input_shape)))
del reduction_axes[self.axis]
broadcast_shape = [1] * len(input_shape)
broadcast_shape[self.axis] = input_shape[self.axis]
if sorted(reduction_axes) == range(K.ndim(x))[:-1]:
x_normed = K.batch_normalization(
x, self.running_mean, self.running_std,
self.beta, self.gamma,
epsilon=self.epsilon)
else:
# need broadcasting
broadcast_running_mean = K.reshape(self.running_mean, broadcast_shape)
broadcast_running_std = K.reshape(self.running_std, broadcast_shape)
broadcast_beta = K.reshape(self.beta, broadcast_shape)
broadcast_gamma = K.reshape(self.gamma, broadcast_shape)
x_normed = K.batch_normalization(
x, broadcast_running_mean, broadcast_running_std,
broadcast_beta, broadcast_gamma,
epsilon=self.epsilon)
return x_normed
示例13: _transform_trans
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def _transform_trans(theta,input):
batch1, step1, dim1 = input.shape
input = K.reshape(input,(batch1,step1,dim1//3,3))
input = K.reshape(input,(batch1*step1,dim1//3,3))
input = K.permute_dimensions(input,[0,2,1])
add = T.ones((batch1*step1,1,dim1//3))
input= K.concatenate([input,add],axis=1)
output = K.batch_dot(theta,input)
output = K.permute_dimensions(output,[0,2,1])
output = K.reshape(output,(output.shape[0],dim1))
output = K.reshape(output,(batch1,step1,output.shape[1]))
return output
开发者ID:microsoft,项目名称:View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition,代码行数:16,代码来源:transform_rnn.py
示例14: _trans
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def _trans(theta):
tx = theta[:,3:4]
ty = theta[:,4:5]
tz = theta[:,5:6]
zero = K.zeros_like(tx)
one = K.ones_like(tx)
first = K.reshape(K.concatenate([one,zero,zero,tx],axis=1),(-1,1,4))
second = K.reshape(K.concatenate([zero,one,zero,ty],axis=1),(-1,1,4))
third = K.reshape(K.concatenate([zero,zero,one,tz],axis=1),(-1,1,4))
trans = K.concatenate([first,second,third],axis=1)
trans = trans.reshape((trans.shape[0],3,4))
return trans
开发者ID:microsoft,项目名称:View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition,代码行数:15,代码来源:transform_rnn.py
示例15: _rotation_y
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import reshape [as 别名]
def _rotation_y(theta):
r1 = K.cos(theta[:,0:1])
r2 = K.sin(theta[:,0:1])
zero = K.zeros_like(r1)
one = K.ones_like(r1)
first = K.reshape(K.concatenate([r1,zero,r2,zero],axis=1),(-1,1,4))
second = K.reshape(K.concatenate([zero,one,zero,zero],axis=1),(-1,1,4))
third = K.reshape(K.concatenate([-r2,zero,r1,zero],axis=1),(-1,1,4))
fourth = K.reshape(K.concatenate([zero,zero,zero,one],axis=1),(-1,1,4))
rotation_y = K.concatenate([first,second,third,fourth],axis=1)
rotation_y = T.reshape(rotation_y,[-1,4,4])
return rotation_y
开发者ID:microsoft,项目名称:View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition,代码行数:14,代码来源:transform_rnn.py