本文整理汇总了Python中keras.backend.expand_dims方法的典型用法代码示例。如果您正苦于以下问题:Python backend.expand_dims方法的具体用法?Python backend.expand_dims怎么用?Python backend.expand_dims使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.expand_dims方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: step
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [as 别名]
def step(self, x, states):
h = states[0]
# states[1] necessary?
# equals K.dot(X, self._W1) + self._b2 with X.shape=[bs, T, input_dim]
total_x_prod = states[-1]
# comes from the constants (equals the input sequence)
X = states[-2]
# expand dims to add the vector which is only valid for this time step
# to total_x_prod which is valid for all time steps
hw = K.expand_dims(K.dot(h, self._W2), 1)
additive_atn = total_x_prod + hw
attention = K.softmax(K.dot(additive_atn, self._V), axis=1)
x_weighted = K.sum(attention * X, [1])
x = K.dot(K.concatenate([x, x_weighted], 1), self._W3) + self._b3
h, new_states = self.layer.cell.call(x, states[:-2])
return h, new_states
示例2: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [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: time_distributed_masked_max
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [as 别名]
def time_distributed_masked_max(x, m):
"""
Computes max along the first (time) dimension.
In:
x - input; a 3D tensor
m - mask
m_value - value for masking
"""
# place infinities where mask is off
m_value = 0.0
tmp = K.switch(K.equal(m, 0.0), -numpy.inf, 0.0)
x_with_inf = x + K.expand_dims(tmp)
x_max = K.max(x_with_inf, axis=1)
r = K.switch(K.equal(x_max, -numpy.inf), m_value, x_max)
return r
## classes ##
# Transforms existing layers to masked layers
示例4: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [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
示例5: save_tmp_func
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [as 别名]
def save_tmp_func(self, step):
cur_mask = K.eval(self.mask_upsample_tensor)
cur_mask = cur_mask[0, ..., 0]
img_filename = (
'%s/%s' % (self.tmp_dir, 'tmp_mask_step_%d.png' % step))
utils_backdoor.dump_image(np.expand_dims(cur_mask, axis=2) * 255,
img_filename,
'png')
cur_fusion = K.eval(self.mask_upsample_tensor *
self.pattern_raw_tensor)
cur_fusion = cur_fusion[0, ...]
img_filename = (
'%s/%s' % (self.tmp_dir, 'tmp_fusion_step_%d.png' % step))
utils_backdoor.dump_image(cur_fusion, img_filename, 'png')
pass
示例6: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [as 别名]
def call(self, x, mask=None):
uit = dot_product(x, self.W)
if self.bias:
uit += self.b
uit = K.tanh(uit)
ait = dot_product(uit, self.u)
a = K.exp(ait)
# apply mask after the exp. will be re-normalized next
if mask is not None:
# Cast the mask to floatX to avoid float64 upcasting in theano
a *= K.cast(mask, K.floatx())
# in some cases especially in the early stages of training the sum may be almost zero
# and this results in NaN's. A workaround is to add a very small positive number ε to the sum.
# a /= K.cast(K.sum(a, axis=1, keepdims=True), K.floatx())
a /= K.cast(K.sum(a, axis=1, keepdims=True) + K.epsilon(), K.floatx())
a = K.expand_dims(a)
weighted_input = x * a
return K.sum(weighted_input, axis=1)
示例7: add_boundary_energy
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [as 别名]
def add_boundary_energy(x, b_start=None, b_end=None, mask=None):
'''Given the observations x, it adds the start boundary energy b_start (resp.
end boundary energy b_end on the start (resp. end) elements and multiplies
the mask.'''
if mask is None:
if b_start is not None:
x = K.concatenate([x[:, :1, :] + b_start, x[:, 1:, :]], axis=1)
if b_end is not None:
x = K.concatenate([x[:, :-1, :], x[:, -1:, :] + b_end], axis=1)
else:
mask = K.cast(mask, K.floatx())
mask = K.expand_dims(mask, 2)
x *= mask
if b_start is not None:
mask_r = K.concatenate([K.zeros_like(mask[:, :1]), mask[:, :-1]], axis=1)
start_mask = K.cast(K.greater(mask, mask_r), K.floatx())
x = x + start_mask * b_start
if b_end is not None:
mask_l = K.concatenate([mask[:, 1:], K.zeros_like(mask[:, -1:])], axis=1)
end_mask = K.cast(K.greater(mask, mask_l), K.floatx())
x = x + end_mask * b_end
return x
示例8: _forward
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [as 别名]
def _forward(x, reduce_step, initial_states, U, mask=None):
'''Forward recurrence of the linear chain crf.'''
def _forward_step(energy_matrix_t, states):
alpha_tm1 = states[-1]
new_states = reduce_step(K.expand_dims(alpha_tm1, 2) + energy_matrix_t)
return new_states[0], new_states
U_shared = K.expand_dims(K.expand_dims(U, 0), 0)
if mask is not None:
mask = K.cast(mask, K.floatx())
mask_U = K.expand_dims(K.expand_dims(mask[:, :-1] * mask[:, 1:], 2), 3)
U_shared = U_shared * mask_U
inputs = K.expand_dims(x[:, 1:, :], 2) + U_shared
inputs = K.concatenate([inputs, K.zeros_like(inputs[:, -1:, :, :])], axis=1)
last, values, _ = K.rnn(_forward_step, inputs, initial_states)
return last, values
示例9: _backward
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [as 别名]
def _backward(gamma, mask):
'''Backward recurrence of the linear chain crf.'''
gamma = K.cast(gamma, 'int32')
def _backward_step(gamma_t, states):
y_tm1 = K.squeeze(states[0], 0)
y_t = batch_gather(gamma_t, y_tm1)
return y_t, [K.expand_dims(y_t, 0)]
initial_states = [K.expand_dims(K.zeros_like(gamma[:, 0, 0]), 0)]
_, y_rev, _ = K.rnn(_backward_step,
gamma,
initial_states,
go_backwards=True)
y = K.reverse(y_rev, 1)
if mask is not None:
mask = K.cast(mask, dtype='int32')
# mask output
y *= mask
# set masked values to -1
y += -(1 - mask)
return y
示例10: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [as 别名]
def call(self, x, mask=None):
eij = dot_product(x, self.W)
if self.bias:
eij += self.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())
weighted_input = x * K.expand_dims(a)
result = K.sum(weighted_input, axis=1)
if self.return_attention:
return [result, a]
return result
示例11: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [as 别名]
def call(self, inputs, **kwargs):
if type(inputs) is list: # true label is provided with shape = [None, n_classes], i.e. one-hot code.
assert len(inputs) == 2
inputs, mask = inputs
else: # if no true label, mask by the max length of capsules. Mainly used for prediction
# compute lengths of capsules
x = K.sqrt(K.sum(K.square(inputs), -1))
# generate the mask which is a one-hot code.
# mask.shape=[None, n_classes]=[None, num_capsule]
mask = K.one_hot(indices=K.argmax(x, 1), num_classes=x.get_shape().as_list()[1])
# inputs.shape=[None, num_capsule, dim_capsule]
# mask.shape=[None, num_capsule]
# masked.shape=[None, num_capsule * dim_capsule]
masked = K.batch_flatten(inputs * K.expand_dims(mask, -1))
return masked
示例12: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [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
示例13: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [as 别名]
def call(self, x, mask=None):
# size of x :[batch_size, sel_len, attention_dim]
# size of u :[batch_size, attention_dim]
# uit = tanh(xW+b)
uit = K.tanh(K.bias_add(K.dot(x, self.W), self.b))
ait = K.dot(uit, self.u)
ait = K.squeeze(ait, -1)
ait = K.exp(ait)
if mask is not None:
# Cast the mask to floatX to avoid float64 upcasting in theano
ait *= K.cast(mask, K.floatx())
ait /= K.cast(K.sum(ait, axis=1, keepdims=True) + K.epsilon(), K.floatx())
ait = K.expand_dims(ait)
weighted_input = x * ait
output = K.sum(weighted_input, axis=1)
return output
示例14: compute_attention_mask
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [as 别名]
def compute_attention_mask(self, layer_id, segment_ids):
"""为seq2seq采用特定的attention mask
"""
if self.attention_mask is None:
def seq2seq_attention_mask(s, repeats=1):
seq_len = K.shape(s)[1]
ones = K.ones((1, repeats, seq_len, seq_len))
a_mask = tf.linalg.band_part(ones, -1, 0)
s_ex12 = K.expand_dims(K.expand_dims(s, 1), 2)
s_ex13 = K.expand_dims(K.expand_dims(s, 1), 3)
a_mask = (1 - s_ex13) * (1 - s_ex12) + s_ex13 * a_mask
a_mask = K.reshape(a_mask, (-1, seq_len, seq_len))
return a_mask
self.attention_mask = Lambda(
seq2seq_attention_mask,
arguments={"repeats": self.num_attention_heads},
name="Attention-Mask")(segment_ids)
return self.attention_mask
示例15: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import expand_dims [as 别名]
def call(self, x):
# previous mean
pre_mean = self.mean
# compute this batch stats
this_sum = tf.reduce_sum(x, 0)
this_bs = tf.cast(K.shape(x)[0], 'float32') # this batch size
# increase count and compute weights
new_count = self.count + this_bs
alpha = this_bs/K.minimum(new_count, self.cap)
# compute new mean. Note that once we reach self.cap (e.g. 1000), the 'previous mean' matters less
new_mean = pre_mean * (1-alpha) + (this_sum/this_bs) * alpha
updates = [(self.count, new_count), (self.mean, new_mean)]
self.add_update(updates, x)
# the first few 1000 should not matter that much towards this cost
return K.minimum(1., new_count/self.cap) * K.expand_dims(new_mean, 0)