本文整理汇总了Python中keras.backend.tile方法的典型用法代码示例。如果您正苦于以下问题:Python backend.tile方法的具体用法?Python backend.tile怎么用?Python backend.tile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.tile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_constants
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [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.output_dim))
B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(3)]
constants.append(B_U)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(3)])
if 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) for _ in range(3)]
constants.append(B_W)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(3)])
return constants
示例2: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [as 别名]
def call(self, inputs, **kwargs):
# (batch_size, 1, input_num_capsule, input_dim_capsule)
expand_inputs = K.expand_dims(inputs, axis=1)
# (batch_size, num_capsule, input_num_capsule, input_dim_capsule)
expand_inputs = K.tile(expand_inputs, (1, self.num_capsule, 1, 1))
# (batch_size, num_capsule, input_num_capsule, dim_capsule)
u_hat = K.map_fn(lambda x: K.batch_dot(x, self.W, axes=[2, 3]), expand_inputs)
if self.num_routing <= 0:
self.num_routing = 3
# (batch_size, num_capsule, input_num_capsule)
b = K.zeros((K.shape(u_hat)[0], self.num_capsule, self.input_num_capsule))
for i in xrange(self.num_routing):
# (batch_size, num_capsule, input_num_capsule)
c = softmax(b, axis=1)
# (batch_size, num_capsule, dim_capsule)
s = K.batch_dot(c, u_hat, axes=[2, 2])
squashed_s = squash(s)
if i < self.num_routing - 1:
# (batch_size, num_capsule, input_num_capsule)
b += K.batch_dot(squashed_s, u_hat, axes=[2, 3])
return squashed_s
示例3: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [as 别名]
def call(self,x,training=None):
deta1 = 0.3
deta2 = 0.3
deta3 = 0.3
seed = np.random.randint(1, 10e6)
rng = RandomStreams(seed=seed)
theta1 = rng.uniform(size=(x.shape[0],1),low=-deta1,high=deta1,dtype='float32')
theta2 = rng.uniform(size=(x.shape[0],1),low=-deta2,high=deta2,dtype='float32')
theta3 = rng.uniform(size=(x.shape[0],1),low=-deta3,high=deta3,dtype='float32')
theta = K.concatenate([theta1,theta2,theta3],axis=-1)
theta = K.tile(theta,x.shape[1])
theta = theta.reshape((x.shape[0], x.shape[1], 3))
theta = theta.reshape((theta.shape[0]*theta.shape[1], theta.shape[2]))
M = _fusion(theta)
output = _transform_rot(M, x)
return K.in_train_phase(output,x,training = training)
开发者ID:microsoft,项目名称:View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition,代码行数:20,代码来源:transform_rnn.py
示例4: get_constants
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [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
示例5: get_constants
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [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
示例6: get_constants
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [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.output_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 = 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, 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
示例7: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [as 别名]
def call(self, inputs, training=None):
inputs_expand = K.expand_dims(inputs, 1)
inputs_tiled = K.tile(inputs_expand, [1, self.num_capsule, 1, 1])
if(self.channels!=0):
W2 = K.repeat_elements(self.W,int(self.input_num_capsule/self.channels),1)
else:
W2 = self.W
inputs_hat = K.map_fn(lambda x: K.batch_dot(x, W2, [2, 3]) , elems=inputs_tiled)
b = tf.zeros(shape=[K.shape(inputs_hat)[0], self.num_capsule, self.input_num_capsule])
assert self.routings > 0, 'The routings should be > 0.'
for i in range(self.routings):
c = tf.nn.softmax(b, dim=1)
outputs = squash(K.batch_dot(c, inputs_hat, [2, 2])+ self.B)
if i < self.routings - 1:
b += K.batch_dot(outputs, inputs_hat, [2, 3])
return outputs
示例8: tf_normal
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [as 别名]
def tf_normal(y_true, mu, sigma, pi):
rollout_length = K.shape(y_true)[1]
y_true = K.tile(y_true,(1,1,GAUSSIAN_MIXTURES))
y_true = K.reshape(y_true, [-1, rollout_length, GAUSSIAN_MIXTURES,Z_DIM])
oneDivSqrtTwoPI = 1 / math.sqrt(2*math.pi)
result = y_true - mu
# result = K.permute_dimensions(result, [2,1,0])
result = result * (1 / (sigma + 1e-8))
result = -K.square(result)/2
result = K.exp(result) * (1/(sigma + 1e-8))*oneDivSqrtTwoPI
result = result * pi
result = K.sum(result, axis=2) #### sum over gaussians
#result = K.prod(result, axis=2) #### multiply over latent dims
return result
示例9: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [as 别名]
def call(self, inputs, mask=None, **kwargs):
if isinstance(inputs, list):
query, key, value = inputs
else:
query = key = value = inputs
if isinstance(mask, list):
mask = mask[1]
feature_dim = K.shape(query)[-1]
e = K.batch_dot(query, key, axes=2) / K.sqrt(K.cast(feature_dim, dtype=K.floatx()))
e = K.exp(e - K.max(e, axis=-1, keepdims=True))
if self.history_only:
query_len, key_len = K.shape(query)[1], K.shape(key)[1]
indices = K.tile(K.expand_dims(K.arange(key_len), axis=0), [query_len, 1])
upper = K.expand_dims(K.arange(key_len), axis=-1)
e *= K.expand_dims(K.cast(indices <= upper, K.floatx()), axis=0)
if mask is not None:
e *= K.cast(K.expand_dims(mask, axis=-2), K.floatx())
a = e / (K.sum(e, axis=-1, keepdims=True) + K.epsilon())
v = K.batch_dot(a, value)
if self.return_attention:
return [v, a]
return v
示例10: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [as 别名]
def call(self, x, reconstruction=False):
self.reconstruction = reconstruction
self.output_dim = K.int_shape(x)[-1]
cross_channel_interp = self.cross_channel_interp
y = x[:, :self.d_dim, :]
w = x[:, self.d_dim:2*self.d_dim, :]
intensity = K.exp(w)
y = tf.transpose(y, perm=[0, 2, 1])
w = tf.transpose(w, perm=[0, 2, 1])
w2 = w
w = K.tile(w[:, :, :, None], (1, 1, 1, self.d_dim))
den = K.logsumexp(w, axis=2)
w = K.exp(w2 - den)
mean = K.mean(y, axis=1)
mean = K.tile(mean[:, None, :], (1, self.output_dim, 1))
w2 = K.dot(w*(y - mean), cross_channel_interp) + mean
rep1 = tf.transpose(w2, perm=[0, 2, 1])
if reconstruction is False:
y_trans = x[:, 2*self.d_dim:3*self.d_dim, :]
y_trans = y_trans - rep1 # subtracting smooth from transient part
rep1 = tf.concat([rep1, intensity, y_trans], 1)
return rep1
示例11: test_tile
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [as 别名]
def test_tile(self):
shape = (3, 4)
arr = np.arange(np.prod(shape)).reshape(shape)
check_single_tensor_operation('tile', arr, BACKENDS, n=[2, 1])
check_single_tensor_operation('tile', (2, 5), BACKENDS, n=[5, 2])
# test theano shape inference when
# input shape has None entries
if K.backend() == 'theano':
x = K.placeholder(shape=(None, 4))
n = 2
y = K.tile(x, n)
assert y._keras_shape == (None, 8)
n = (4, 3)
y = K.tile(x, n)
assert y._keras_shape == (None, 12)
示例12: tile_vector_as_image_channels
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [as 别名]
def tile_vector_as_image_channels(vector_op, image_shape):
"""
Takes a vector of length n and an image shape BHWC,
and repeat the vector as channels at each pixel.
# Params
vector_op: A tensor vector to tile.
image_shape: A list of integers [width, height] with the desired dimensions.
"""
with K.name_scope('tile_vector_as_image_channels'):
ivs = K.shape(vector_op)
# reshape the vector into a single pixel
vector_pixel_shape = [ivs[0], 1, 1, ivs[1]]
vector_op = K.reshape(vector_op, vector_pixel_shape)
# tile the pixel into a full image
tile_dimensions = [1, image_shape[1], image_shape[2], 1]
vector_op = K.tile(vector_op, tile_dimensions)
if K.backend() is 'tensorflow':
output_shape = [ivs[0], image_shape[1], image_shape[2], ivs[1]]
vector_op.set_shape(output_shape)
return vector_op
示例13: concat_images_with_tiled_vector
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [as 别名]
def concat_images_with_tiled_vector(images, vector):
"""Combine a set of images with a vector, tiling the vector at each pixel in the images and concatenating on the channel axis.
# Params
images: list of images with the same dimensions
vector: vector to tile on each image. If you have
more than one vector, simply concatenate them
all before calling this function.
# Returns
"""
with K.name_scope('concat_images_with_tiled_vector'):
if not isinstance(images, list):
images = [images]
image_shape = K.int_shape(images[0])
tiled_vector = tile_vector_as_image_channels(vector, image_shape)
images.append(tiled_vector)
combined = K.concatenate(images)
return combined
示例14: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [as 别名]
def call(self, x, mask=None):
uit = K.tanh(K.dot(x, self.Ws1))
ait = K.dot(uit, self.Ws2)
ait = K.permute_dimensions(ait, (0, 2, 1))
A = K.softmax(ait, axis=1)
M = K.batch_dot(A, x)
if self.punish:
A_T = K.permute_dimensions(A, (0, 2, 1))
tile_eye = K.tile(K.eye(self.weight_ws2), [self.batch_size, 1])
tile_eye = K.reshape(
tile_eye, shape=[-1, self.weight_ws2, self.weight_ws2])
AA_T = K.batch_dot(A, A_T) - tile_eye
P = K.l2_normalize(AA_T, axis=(1, 2))
return M, P
else:
return M
示例15: get_constants
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import tile [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.output_dim))
B_U = [K.in_train_phase(K.dropout(ones, self.dropout_U), ones) for _ in range(3)]
constants.append(B_U)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(3)])
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(3)]
constants.append(B_W)
else:
constants.append([K.cast_to_floatx(1.) for _ in range(3)])
return constants