本文整理汇总了Python中keras.backend.dot方法的典型用法代码示例。如果您正苦于以下问题:Python backend.dot方法的具体用法?Python backend.dot怎么用?Python backend.dot使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.dot方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: step
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [as 别名]
def step(self, x, states):
h_st, B_U, B_W = states
if self.consume_less == 'cpu':
x_t = x[:, :self.output_dim]
x_h = x[:, self.output_dim: 2 * self.output_dim]
elif self.consume_less == 'mem':
x_t = K.dot(x * B_W[0], self.W_t) + self.b_t
x_h = K.dot(x * B_W[1], self.W_h) + self.b_h
else:
raise Exception('Unknown `consume_less` mode.')
for l in xrange(self.L):
if l == 0:
t = self.inner_activation(x_t + K.dot(h_st * B_U[0], self.U_ts[l]) + self.b_ts[l])
h = self.activation(x_h + K.dot(h_st * B_U[1], self.U_hs[l]) + self.b_hs[l])
else:
t = self.inner_activation(K.dot(h_st * B_U[0], self.U_ts[l]) + self.b_ts[l])
h = self.activation(K.dot(h_st * B_U[1], self.U_hs[l]) + self.b_hs[l])
h_st = h * t + h_st * (1 - t)
return h_st, [h_st]
示例2: step
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [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
示例3: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [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
示例4: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [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
示例5: step
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [as 别名]
def step(self, x, states):
u_tm1 = states[0]
B_U = states[3]
B_W = states[4]
bv_t = self.bv + K.dot(u_tm1, self.Wuv)
bh_t = self.bh + K.dot(u_tm1, self.Wuh)
if self.consume_less == 'cpu':
h = x
else:
h = self.b + K.dot(x * B_W, self.W)
u_t = self.activation(h + K.dot(u_tm1 * B_U, self.U))
return x, [u_t, bv_t, bh_t]
示例6: step
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [as 别名]
def step(self, x, states):
h_tm1 = states[0]
c_tm1 = states[1]
x_t = self.activation(K.dot(h_tm1, self.A) + self.ba)
z = K.dot(x_t, self.W) + K.dot(h_tm1, self.U) + self.b
z0 = z[:, :self.input_dim]
z1 = z[:, self.input_dim: 2 * self.input_dim]
z2 = z[:, 2 * self.input_dim: 3 * self.input_dim]
z3 = z[:, 3 * self.input_dim:]
i = self.inner_activation(z0)
f = self.inner_activation(z1)
c = f * c_tm1 + i * self.activation(z2)
o = self.inner_activation(z3)
h = o * self.activation(c)
return x_t, [h, c]
示例7: sample_h_given_x
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [as 别名]
def sample_h_given_x(self, x):
h_pre = K.dot(x, self.Wrbm) + self.bh
h_sigm = self.activation(self.scaling_h_given_x * h_pre)
# drop out noise
#if(0.0 < self.p < 1.0):
# noise_shape = self._get_noise_shape(h_sigm)
# h_sigm = K.in_train_phase(K.dropout(h_sigm, self.p, noise_shape), h_sigm)
if(self.hidden_unit_type == 'binary'):
h_samp = K.random_binomial(shape=h_sigm.shape, p=h_sigm)
# random sample
# \hat{h} = 1, if p(h=1|x) > uniform(0, 1)
# 0, otherwise
elif(self.hidden_unit_type == 'nrlu'):
h_samp = nrlu(h_pre)
else:
h_samp = h_sigm
if(0.0 < self.p < 1.0):
noise_shape = self._get_noise_shape(h_samp)
h_samp = K.in_train_phase(K.dropout(h_samp, self.p, noise_shape), h_samp)
return h_samp, h_pre, h_sigm
示例8: reading
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [as 别名]
def reading(memory_t, weight_t):
"""
Reading memory.
:param memory_t: the $N \times M$ memory matrix at time $t$, where $N$
is the number of memory locations, and $M$ is the vector size at each
location.
:param weight_t: $w_t$ is a vector of weightings over the $N$ locations
emitted by a reading head at time $t$.
Since all weightings are normalized, the $N$ elements $w_t(i)$ of
$\textbf{w}_t$ obey the following constraints:
$$\sum_{i=1}^{N} w_t(i) = 1, 0 \le w_t(i) \le 1,\forall i$$
The length $M$ read vector $r_t$ returned by the head is defined as a
convex combination of the row-vectors $M_t(i)$ in memory:
$$\textbf{r}_t \leftarrow \sum_{i=1}^{N}w_t(i)\textbf{M}_t(i)$$
:return: the content reading from memory.
"""
r_t = K.dot(memory_t, weight_t)
return r_t
示例9: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [as 别名]
def call(self, inputs):
"""
In Keras, there are two way to do matrix multiplication (dot product)
1) K.dot : AxB -> when A has batchsize and B doesn't, use K.dot
2) tf.matmul: AxB -> when A and B both have batchsize, use tf.matmul
Error example: Use tf.matmul when A has batchsize (3 dim) and B doesn't (2 dim)
ValueError: Shape must be rank 2 but is rank 3 for 'net_vlad_1/MatMul' (op: 'MatMul') with input shapes: [?,21,64], [64,3]
tf.matmul might still work when the dim of A is (?,64), but this is too confusing.
Just follow the above rules.
"""
gates = K.dot(inputs, self.gating_weights)
gates += self.gating_biases
gates = tf.sigmoid(gates)
activation = tf.multiply(inputs,gates)
return activation
示例10: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [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
示例11: devise_ranking_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [as 别名]
def devise_ranking_loss(embedding, margin = 0.1):
""" The ranking loss used by DeViSE.
# Arguments:
- embedding: 2-d numpy array whose rows are class embeddings.
- margin: margin for the ranking loss.
# Returns:
a Keras loss function taking y_true and y_pred as inputs and returning a loss tensor.
"""
def _loss(y_true, y_pred):
embedding_t = K.constant(embedding.T)
true_sim = K.sum(y_true * y_pred, axis = -1)
other_sim = K.dot(y_pred, embedding_t)
return K.sum(K.relu(margin - true_sim[:,None] + other_sim), axis = -1) - margin
return _loss
示例12: gram_matrix
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [as 别名]
def gram_matrix(x):
"""
Computes the outer-product of the input tensor x.
Input
-----
- x: input tensor of shape (C x H x W)
Returns
-------
- x . x^T
Note that this can be computed efficiently if x is reshaped
as a tensor of shape (C x H*W).
"""
# assert K.ndim(x) == 3
if K.image_dim_ordering() == 'th':
features = K.batch_flatten(x)
else:
features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1)))
return K.dot(features, K.transpose(features))
示例13: mask_attention_if_needed
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [as 别名]
def mask_attention_if_needed(self, dot_product):
"""
Makes sure that (when enabled) each position
(of a decoder's self-attention) cannot attend to subsequent positions.
This is achieved by assigning -inf (or some large negative number)
to all invalid connections. Later softmax will turn them into zeros.
We need this to guarantee that decoder's predictions are based
on what has happened before the position, not after.
The method does nothing if masking is turned off.
:param dot_product: scaled dot-product of Q and K after reshaping them
to 3D tensors (batch * num_heads, rows, cols)
"""
if not self.use_masking:
return dot_product
last_dims = K.int_shape(dot_product)[-2:]
low_triangle_ones = (
np.tril(np.ones(last_dims))
# to ensure proper broadcasting
.reshape((1,) + last_dims))
inverse_low_triangle = 1 - low_triangle_ones
close_to_negative_inf = -1e9
result = (
K.constant(low_triangle_ones, dtype=K.floatx()) * dot_product +
K.constant(close_to_negative_inf * inverse_low_triangle))
return result
示例14: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [as 别名]
def call(self, inputs, **kwargs):
if not (isinstance(inputs, list) and len(inputs) == 2):
raise ValueError(
'You can call this layer only with a list of two tensors '
'(for keys/values and queries)')
key_values_input, query_input = inputs
_, value_seq_len, d_model = K.int_shape(key_values_input)
query_seq_len = K.int_shape(inputs[1])[-2]
# The first thing we need to do is to perform affine transformations
# of the inputs to get the Queries, the Keys and the Values.
kv = K.dot(K.reshape(key_values_input, [-1, d_model]), self.kv_weights)
# splitting the keys, the values and the queries before further
# processing
pre_k, pre_v = [
K.reshape(
# K.slice(kv, (0, i * d_model), (-1, d_model)),
kv[:, i * d_model: (i + 1) * d_model],
(-1, value_seq_len,
self.num_heads, d_model // self.num_heads))
for i in range(2)]
pre_q = K.reshape(
K.dot(K.reshape(query_input, [-1, d_model]), self.q_weights),
(-1, query_seq_len, self.num_heads, d_model // self.num_heads))
return self.attention(pre_q, pre_v, pre_k, query_seq_len, d_model,
training=kwargs.get('training'))
示例15: gram_matrix
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import dot [as 别名]
def gram_matrix(x):
features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1)))
gram = K.dot(features, K.transpose(features))
return gram