本文整理匯總了Python中keras.backend.repeat_elements方法的典型用法代碼示例。如果您正苦於以下問題:Python backend.repeat_elements方法的具體用法?Python backend.repeat_elements怎麽用?Python backend.repeat_elements使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類keras.backend
的用法示例。
在下文中一共展示了backend.repeat_elements方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: call
# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import repeat_elements [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 repeat_elements [as 別名]
def call(self, x, mask=None):
if K.image_dim_ordering == "th":
_, f, r, c = self.shape
else:
_, r, c, f = self.shape
squared = K.square(x)
pooled = K.pool2d(squared, (self.n, self.n), strides=(1, 1),
padding="same", pool_mode="avg")
if K.image_dim_ordering == "th":
summed = K.sum(pooled, axis=1, keepdims=True)
averaged = self.alpha * K.repeat_elements(summed, f, axis=1)
else:
summed = K.sum(pooled, axis=3, keepdims=True)
averaged = self.alpha * K.repeat_elements(summed, f, axis=3)
denom = K.pow(self.k + averaged, self.beta)
return x / denom
示例3: call
# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import repeat_elements [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
示例4: make_warped_stack
# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import repeat_elements [as 別名]
def make_warped_stack(args):
mask = args[0]
src_in = args[1]
trans_in = args[2]
for i in range(11):
mask_i = K.repeat_elements(tf.expand_dims(mask[:, :, :, i], 3), 3, 3)
src_masked = tf.multiply(mask_i, src_in)
if i == 0:
warps = src_masked
else:
warp_i = affine_warp(src_masked, trans_in[:, :, :, i])
warps = tf.concat([warps, warp_i], 3)
return warps
示例5: call
# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import repeat_elements [as 別名]
def call(self, input_tensor, mask=None):
x = input_tensor[0]
y = input_tensor[1]
mask = mask[0]
y = K.transpose(K.dot(self.W, K.transpose(y)))
y = K.expand_dims(y, axis=-2)
y = K.repeat_elements(y, self.steps, axis=1)
eij = K.sum(x * y, axis=-1)
if self.bias:
b = K.repeat_elements(self.b, self.steps, axis=0)
eij += b
eij = K.tanh(eij)
a = K.exp(eij)
if mask is not None:
a *= K.cast(mask, K.floatx())
a /= K.cast(K.sum(a, axis=1, keepdims=True) + K.epsilon(), K.floatx())
return a
示例6: QRNcell
# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import repeat_elements [as 別名]
def QRNcell():
xq = Input(batch_shape=(batch_size, embedding_dim * 2))
# Split into context and query
xt = Lambda(lambda x, dim: x[:, :dim], arguments={'dim': embedding_dim},
output_shape=lambda s: (s[0], s[1] / 2))(xq)
qt = Lambda(lambda x, dim: x[:, dim:], arguments={'dim': embedding_dim},
output_shape=lambda s: (s[0], s[1] / 2))(xq)
h_tm1 = Input(batch_shape=(batch_size, embedding_dim))
zt = Dense(1, activation='sigmoid', bias_initializer=Constant(2.5))(multiply([xt, qt]))
zt = Lambda(lambda x, dim: K.repeat_elements(x, dim, axis=1), arguments={'dim': embedding_dim})(zt)
ch = Dense(embedding_dim, activation='tanh')(concatenate([xt, qt], axis=-1))
rt = Dense(1, activation='sigmoid')(multiply([xt, qt]))
rt = Lambda(lambda x, dim: K.repeat_elements(x, dim, axis=1), arguments={'dim': embedding_dim})(rt)
ht = add([multiply([zt, ch, rt]), multiply([Lambda(lambda x: 1 - x, output_shape=lambda s: s)(zt), h_tm1])])
return RecurrentModel(input=xq, output=ht, initial_states=[h_tm1], final_states=[ht], return_sequences=True)
#
# Load data
#
示例7: kl_divergence
# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import repeat_elements [as 別名]
def kl_divergence(y_true, y_pred):
max_y_pred = K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.max(K.max(y_pred, axis=2), axis=2)),
shape_r_out, axis=-1)), shape_c_out, axis=-1)
y_pred /= max_y_pred
sum_y_true = K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.sum(K.sum(y_true, axis=2), axis=2)),
shape_r_out, axis=-1)), shape_c_out, axis=-1)
sum_y_pred = K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.sum(K.sum(y_pred, axis=2), axis=2)),
shape_r_out, axis=-1)), shape_c_out, axis=-1)
y_true /= (sum_y_true + K.epsilon())
y_pred /= (sum_y_pred + K.epsilon())
return 10 * K.sum(K.sum(y_true * K.log((y_true / (y_pred + K.epsilon())) + K.epsilon()), axis=-1), axis=-1)
# Correlation Coefficient Loss
示例8: nss
# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import repeat_elements [as 別名]
def nss(y_true, y_pred):
max_y_pred = K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.max(K.max(y_pred, axis=2), axis=2)),
shape_r_out, axis=-1)), shape_c_out, axis=-1)
y_pred /= max_y_pred
y_pred_flatten = K.batch_flatten(y_pred)
y_mean = K.mean(y_pred_flatten, axis=-1)
y_mean = K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.expand_dims(y_mean)),
shape_r_out, axis=-1)), shape_c_out, axis=-1)
y_std = K.std(y_pred_flatten, axis=-1)
y_std = K.repeat_elements(K.expand_dims(K.repeat_elements(K.expand_dims(K.expand_dims(y_std)),
shape_r_out, axis=-1)), shape_c_out, axis=-1)
y_pred = (y_pred - y_mean) / (y_std + K.epsilon())
return -(K.sum(K.sum(y_true * y_pred, axis=2), axis=2) / K.sum(K.sum(y_true, axis=2), axis=2))
# Gaussian priors initialization
示例9: step
# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import repeat_elements [as 別名]
def step(self, x, states):
x_shape = K.shape(x)
h_tm1 = states[0]
c_tm1 = states[1]
e = self.V_a(K.tanh(self.W_a(h_tm1) + self.U_a(x)))
a = K.reshape(K.softmax(K.batch_flatten(e)), (x_shape[0], 1, x_shape[2], x_shape[3]))
x_tilde = x * K.repeat_elements(a, x_shape[1], 1)
x_i = self.W_i(x_tilde)
x_f = self.W_f(x_tilde)
x_c = self.W_c(x_tilde)
x_o = self.W_o(x_tilde)
i = self.inner_activation(x_i + self.U_i(h_tm1))
f = self.inner_activation(x_f + self.U_f(h_tm1))
c = f * c_tm1 + i * self.activation(x_c + self.U_c(h_tm1))
o = self.inner_activation(x_o + self.U_o(h_tm1))
h = o * self.activation(c)
return h, [h, c]
示例10: test_repeat_elements
# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import repeat_elements [as 別名]
def test_repeat_elements(self):
reps = 3
for ndims in [1, 2, 3]:
shape = np.arange(2, 2 + ndims)
arr = np.arange(np.prod(shape)).reshape(shape)
for rep_axis in range(ndims):
np_rep = np.repeat(arr, reps, axis=rep_axis)
check_single_tensor_operation('repeat_elements', arr, BACKENDS,
rep=reps, axis=rep_axis,
assert_value_with_ref=np_rep)
if K.backend() != 'cntk':
shape = list(shape)
shape[rep_axis] = None
x = K.placeholder(shape=shape)
y = K.repeat_elements(x, reps, axis=rep_axis)
assert y._keras_shape == tuple(shape)
assert y._keras_shape == K.int_shape(y)
示例11: call
# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import repeat_elements [as 別名]
def call(self, input_tensor, mask=None):
x = input_tensor[0]
aspect = input_tensor[1]
mask = mask[0]
aspect = K.transpose(K.dot(self.W, K.transpose(aspect)))
aspect = K.expand_dims(aspect, axis=-2)
aspect = K.repeat_elements(aspect, self.steps, axis=1)
eij = K.sum(x*aspect, axis=-1)
if self.bias:
b = K.repeat_elements(self.b, self.steps, axis=0)
eij += b
eij = K.tanh(eij)
a = K.exp(eij)
if mask is not None:
a *= K.cast(mask, K.floatx())
a /= K.cast(K.sum(a, axis=1, keepdims=True) + K.epsilon(), K.floatx())
return a
示例12: step
# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import repeat_elements [as 別名]
def step(self, inputs, states):
h_tm1 = states[0] # previous memory
#B_U = states[1] # dropout matrices for recurrent units
#B_W = states[2]
h_tm1a = K.dot(h_tm1, self.Wa)
eij = K.dot(K.tanh(h_tm1a + K.dot(inputs[:, :self.h_dim], self.Ua)), self.Va)
eijs = K.repeat_elements(eij, self.h_dim, axis=1)
#alphaij = K.softmax(eijs) # batchsize * lenh h batchsize * lenh * ndim
#ci = K.permute_dimensions(K.permute_dimensions(self.h, [2,0,1]) * alphaij, [1,2,0])
#cisum = K.sum(ci, axis=1)
cisum = eijs*inputs[:, :self.h_dim]
#print(K.shape(cisum), cisum.shape, ci.shape, self.h.shape, alphaij.shape, x.shape)
zr = K.sigmoid(K.dot(inputs[:, self.h_dim:], self.Wzr) + K.dot(h_tm1, self.Uzr) + K.dot(cisum, self.Czr))
zi = zr[:, :self.units]
ri = zr[:, self.units: 2 * self.units]
si_ = K.tanh(K.dot(inputs[:, self.h_dim:], self.W) + K.dot(ri*h_tm1, self.U) + K.dot(cisum, self.C))
si = (1-zi) * h_tm1 + zi * si_
return si, [si] #h_tm1, [h_tm1]
示例13: call
# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import repeat_elements [as 別名]
def call(self, inputs, mask=None):
"""
Extract the GRU output for the target document index for the forward
and backwards GRU outputs, and then concatenate them. If the target word index
is at index l, and there are T total document words, the desired output
in the forward pass is at GRU_f[l] (ignoring the batched case) and the
desired output of the backwards pass is at GRU_b[T-l].
We need to get these two vectors and concatenate them. To do so, we'll
reverse the backwards GRU, which allows us to use the same index/mask for both.
"""
# TODO(nelson): deal with case where cloze token appears multiple times
# in a question.
word_indices, gru_f, gru_b = inputs
index_mask = K.cast(K.equal((K.ones_like(word_indices) * self.target_index),
word_indices), "float32")
gru_mask = K.repeat_elements(K.expand_dims(index_mask, -1), K.int_shape(gru_f)[-1], K.ndim(gru_f) - 1)
masked_gru_f = switch(gru_mask, gru_f, K.zeros_like(gru_f))
selected_gru_f = K.sum(masked_gru_f, axis=1)
masked_gru_b = switch(gru_mask, gru_b, K.zeros_like(gru_b))
selected_gru_b = K.sum(masked_gru_b, axis=1)
selected_bigru = K.concatenate([selected_gru_f, selected_gru_b], axis=-1)
return selected_bigru
示例14: call
# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import repeat_elements [as 別名]
def call(self, v, **kwargs):
assert (len(self.input_dims) == len(self.output_dims) and
self.input_dims[0] == self.output_dims[0])
# possibly shrink spatial axis by pooling elements
if len(self.input_dims) == 4 and (self.input_dims[1] > self.output_dims[1] or self.input_dims[2] > self.output_dims[2]):
assert (self.input_dims[1] % self.output_dims[1] == 0 and
self.input_dims[2] % self.output_dims[2] == 0)
pool_sizes = (self.input_dims[1] / self.output_dims[1],
self.input_dims[2] / self.output_dims[2])
strides = pool_sizes
v = K.pool2d(
v, pool_size=pool_sizes, strides=strides,
padding='same', data_format='channels_last', pool_mode='avg')
# possibly extend spatial axis by repeating elements
for i in range(1, len(self.input_dims) - 1):
if self.input_dims[i] < self.output_dims[i]:
assert self.output_dims[i] % self.input_dims[i] == 0
v = K.repeat_elements(
v, rep=int(self.output_dims[i] / self.input_dims[i]),
axis=i)
return v
開發者ID:PacktPublishing,項目名稱:Hands-On-Generative-Adversarial-Networks-with-Keras,代碼行數:27,代碼來源:layers.py
示例15: call
# 需要導入模塊: from keras import backend [as 別名]
# 或者: from keras.backend import repeat_elements [as 別名]
def call(self, x, mask=None):
a = x[0]
b = x[1]
a = K.mean(a, axis=0, keepdims=True)
b = K.mean(b, axis=0, keepdims=True)
a /= K.sum(a, keepdims=True)
b /= K.sum(b, keepdims=True)
a = K.clip(a, K.epsilon(), 1)
b = K.clip(b, K.epsilon(), 1)
loss = K.sum(a*K.log(a/b), axis=-1, keepdims=True) \
+ K.sum(b*K.log(b/a), axis=-1, keepdims=True)
loss = K.repeat_elements(loss, self.batch_size, axis=0)
return loss