本文整理汇总了Python中tensorflow.expand_dims函数的典型用法代码示例。如果您正苦于以下问题:Python expand_dims函数的具体用法?Python expand_dims怎么用?Python expand_dims使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了expand_dims函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: copy_net_logit_function
def copy_net_logit_function(state):
state = tf.nn.dropout(state, self.dropout_placeholder)
# the logits for generating the next word are computed in
# the standard way
generate_logits = tf.matmul(state, decoding_w) + decoding_b
# Equation 8 in the paper ... in shape of source sentence
# (batch x time)
copy_logits_in_time = tf.reduce_sum(
projected_inputs * tf.expand_dims(state, 1), [2])
# mask out the padding in exponential domain
copy_logits_in_time_exp_masked = tf.exp(
tf.minimum([[80.0]], copy_logits_in_time)) * copy_mask
# ... in shape of vocabulary (batch x time x vocabulary)
copy_logits_in_vocabulary = tf.expand_dims(
copy_logits_in_time_exp_masked,
2) * vocabulary_shaped_indices
# Equation 6 without normalization
copy_logits_exp = tf.reduce_sum(copy_logits_in_vocabulary,
[1])
logits_exp = copy_logits_exp \
+ tf.exp(tf.minimum([[80.0]], generate_logits))
return (tf.log(tf.maximum([[1e-40]], logits_exp)),
copy_logits_in_time)
开发者ID:alvaz16,项目名称:neuralmonkey,代码行数:30,代码来源:decoder.py
示例2: bond_conv_layer
def bond_conv_layer(activated_atoms, bv_params, layer):
flow_depth = flow_layer_depths[layer]
next_activated_atoms = tf.zeros(tf.pack([N_atoms_ph, flow_depth]))
for deg in range(1, 6):
indices = tf.sub(deg_list_ph, tf.constant(1,dtype=tf.int32))
flow_param = bv_params['A_flow'+str(layer)+'_'+str(deg)]
flow_map = tf.gather(flow_param, type_adj_ph)
multiples = tf.pack([N_atoms_ph, 1, 1])
activated_atoms_dim = tf.expand_dims(tf.tile(tf.expand_dims(activated_atoms, 0), multiples), 2)
adj_mul = tf.batch_matmul(activated_atoms_dim, flow_map)
adj_mul = tf.squeeze(adj_mul, [2])
deg_mask = tf.to_float(tf.equal(deg_list_ph, deg))
multiples = tf.pack([1, N_atoms_ph, flow_depth])
deg_list_dim = tf.tile(tf.expand_dims(tf.expand_dims(deg_mask, 1), 1), multiples)
multiples = tf.pack([N_atoms_ph, N_atoms_ph, 1])
biases = tf.tile(bv_params['b_flow'+str(layer)+'_'+str(deg)], multiples)
filtered_atoms = tf.add(tf.mul(adj_mul, deg_list_dim), biases)
next_activated_atoms = next_activated_atoms + tf.reduce_sum(filtered_atoms, 1)
next_activated_atoms = tf.nn.relu(next_activated_atoms)
return next_activated_atoms
开发者ID:rbharath,项目名称:deepchem,代码行数:29,代码来源:bondvolution.py
示例3: encode_coordinates_alt
def encode_coordinates_alt(self, net):
"""An alternative implemenation for the encoding coordinates.
Args:
net: a tensor of shape=[batch_size, height, width, num_features]
Returns:
a list of tensors with encoded image coordinates in them.
"""
batch_size, h, w, _ = net.shape.as_list()
h_loc = [
tf.tile(
tf.reshape(
tf.contrib.layers.one_hot_encoding(
tf.constant([i]), num_classes=h), [h, 1]), [1, w])
for i in xrange(h)
]
h_loc = tf.concat([tf.expand_dims(t, 2) for t in h_loc], 2)
w_loc = [
tf.tile(
tf.contrib.layers.one_hot_encoding(tf.constant([i]), num_classes=w),
[h, 1]) for i in xrange(w)
]
w_loc = tf.concat([tf.expand_dims(t, 2) for t in w_loc], 2)
loc = tf.concat([h_loc, w_loc], 2)
loc = tf.tile(tf.expand_dims(loc, 0), [batch_size, 1, 1, 1])
return tf.concat([net, loc], 3)
开发者ID:banjocui,项目名称:models,代码行数:27,代码来源:model_test.py
示例4: dot
def dot(x, y):
"""Compute dot product between a Tensor matrix and a Tensor vector.
If x is a ``[M x N]`` matrix, then y is a ``M``-vector.
If x is a ``M``-vector, then y is a ``[M x N]`` matrix.
Parameters
----------
x : tf.Tensor
``M x N`` matrix or ``M`` vector (see above)
y : tf.Tensor
``M`` vector or ``M x N`` matrix (see above)
Returns
-------
tf.Tensor
``N``-vector
"""
if len(x.get_shape()) == 1:
vec = x
mat = y
return tf.matmul(tf.expand_dims(vec, 0), mat)
else:
mat = x
vec = y
return tf.matmul(mat, tf.expand_dims(vec, 1))
开发者ID:jf0510310315,项目名称:edward,代码行数:27,代码来源:util.py
示例5: call
def call(self, x):
"""Execute this layer on input tensors.
Parameters
----------
x: list of Tensor
should be [atom_features(batch_size*max_n_atoms*n_embedding),
distance_matrix(batch_size*max_n_atoms*max_n_atoms*n_distance),
distance_matrix_mask(batch_size*max_n_atoms*max_n_atoms)]
Returns
-------
tf.Tensor
new embeddings for atoms, same shape as x[0]
"""
self.build()
atom_features = x[0]
distance_matrix = x[1]
distance_matrix_mask = x[2]
outputs = tf.multiply(
(tf.tensordot(distance_matrix, self.W_df, [[3], [0]]) + self.b_df),
tf.expand_dims(
tf.tensordot(atom_features, self.W_cf, [[2], [0]]) + self.b_cf,
axis=1))
# for atom i in a molecule m, this step multiplies together distance info of atom pair(i,j)
# and embeddings of atom j(both gone through a hidden layer)
outputs = tf.tensordot(outputs, self.W_fc, [[3], [0]])
outputs = tf.multiply(outputs, tf.expand_dims(distance_matrix_mask, axis=3))
# masking the outputs tensor for pair(i,i) and all paddings
outputs = self.activation(outputs)
outputs = tf.reduce_sum(outputs, axis=2) + atom_features
# for atom i, sum the influence from all other atom j in the molecule
return outputs
开发者ID:joegomes,项目名称:deepchem,代码行数:34,代码来源:layers.py
示例6: _mean_image_subtraction
def _mean_image_subtraction(image, means):
"""Subtracts the given means from each image channel.
For example:
means = [123.68, 116.779, 103.939]
image = _mean_image_subtraction(image, means)
Note that the rank of `image` must be known.
Args:
image: a tensor of size [height, width, C].
means: a C-vector of values to subtract from each channel.
Returns:
the centered image.
Raises:
ValueError: If the rank of `image` is unknown, if `image` has a rank other
than three or if the number of channels in `image` doesn't match the
number of values in `means`.
"""
if image.get_shape().ndims != 3:
raise ValueError('Input must be of size [height, width, C>0]')
num_channels = image.get_shape().as_list()[-1]
if len(means) != num_channels:
raise ValueError('len(means) must match the number of channels')
# We have a 1-D tensor of means; convert to 3-D.
means = tf.expand_dims(tf.expand_dims(means, 0), 0)
return image - means
开发者ID:forging2012,项目名称:models,代码行数:31,代码来源:vgg_preprocessing.py
示例7: softmax
def softmax(x):
"""
Compute the softmax function in tensorflow.
You might find the tensorflow functions tf.exp, tf.reduce_max,
tf.reduce_sum, tf.expand_dims useful. (Many solutions are possible, so you may
not need to use all of these functions). Recall also that many common
tensorflow operations are sugared (e.g. x * y does a tensor multiplication
if x and y are both tensors). Make sure to implement the numerical stability
fixes as in the previous homework!
Args:
x: tf.Tensor with shape (n_samples, n_features). Note feature vectors are
represented by row-vectors. (For simplicity, no need to handle 1-d
input as in the previous homework)
Returns:
out: tf.Tensor with shape (n_sample, n_features). You need to construct this
tensor in this problem.
"""
### YOUR CODE HERE
maxes = tf.expand_dims(tf.reduce_max(x, reduction_indices=[1]), 1)
stable = x - maxes
e = tf.exp(stable)
sums = tf.expand_dims(tf.reduce_sum(e, reduction_indices=[1]), 1)
out = tf.div(e, sums)
### END YOUR CODE
return out
开发者ID:Sundrique,项目名称:cs224d,代码行数:29,代码来源:q1_softmax.py
示例8: reward_prediction_big
def reward_prediction_big(
self, input_images, input_reward, action, latent, mid_outputs):
"""Builds a reward prediction network."""
del mid_outputs
conv_size = self.tinyify([32, 32, 16, 8])
with tf.variable_scope("reward_pred", reuse=tf.AUTO_REUSE):
x = tf.concat(input_images, axis=3)
x = tfcl.layer_norm(x)
if not self.hparams.small_mode:
x = tfl.conv2d(x, conv_size[1], [3, 3], strides=(2, 2),
activation=tf.nn.relu, name="reward_conv1")
x = tfcl.layer_norm(x)
# Inject additional inputs
if action is not None:
x = common_video.inject_additional_input(
x, action, "action_enc", self.hparams.action_injection)
if input_reward is not None:
x = common_video.inject_additional_input(x, input_reward, "reward_enc")
if latent is not None:
latent = tfl.flatten(latent)
latent = tf.expand_dims(latent, axis=1)
latent = tf.expand_dims(latent, axis=1)
x = common_video.inject_additional_input(x, latent, "latent_enc")
x = tfl.conv2d(x, conv_size[2], [3, 3], strides=(2, 2),
activation=tf.nn.relu, name="reward_conv2")
x = tfcl.layer_norm(x)
x = tfl.conv2d(x, conv_size[3], [3, 3], strides=(2, 2),
activation=tf.nn.relu, name="reward_conv3")
开发者ID:qixiuai,项目名称:tensor2tensor,代码行数:32,代码来源:sv2p.py
示例9: build
def build(self):
""" tensorflow computation graph for transform """
graph = tf.Graph()
with graph.as_default():
self.inputs = tf.placeholder(tf.float32, shape=(None, self.max_atoms, 4))
atom_numbers = tf.cast(self.inputs[:, :, 0], tf.int32)
flags = tf.sign(atom_numbers)
flags = tf.cast(
tf.expand_dims(flags, 1) * tf.expand_dims(flags, 2), tf.float32)
coordinates = self.inputs[:, :, 1:]
if self.coordinates_in_bohr:
coordinates = coordinates * 0.52917721092
d = self.distance_matrix(coordinates, flags)
d_radial_cutoff = self.distance_cutoff(d, self.radial_cutoff, flags)
d_angular_cutoff = self.distance_cutoff(d, self.angular_cutoff, flags)
radial_sym = self.radial_symmetry(d_radial_cutoff, d, atom_numbers)
angular_sym = self.angular_symmetry(d_angular_cutoff, d, atom_numbers,
coordinates)
self.outputs = tf.concat(
[
tf.cast(tf.expand_dims(atom_numbers, 2), tf.float32), radial_sym,
angular_sym
],
axis=2)
return graph
开发者ID:ktaneishi,项目名称:deepchem,代码行数:25,代码来源:transformers.py
示例10: testExpandAndSqueeze
def testExpandAndSqueeze(self):
with self.cached_session():
# TODO(aselle): sparse_split, sparse_reduce_sum,
# sparse_reduce_sum_sparse, reduce_join
a = [[1, 2, 3]]
self.assertAllEqual(tf.expand_dims(tf.squeeze(a, [0]), 0).eval(),
a)
self.assertAllEqual(tf.squeeze(tf.expand_dims(a, 1), [1]).eval(),
a)
self.assertAllEqual(
tf.expand_dims(
tf.squeeze(
[[1, 2, 3]], squeeze_dims=[0]), dim=0).eval(),
a)
self.assertAllEqual(
tf.squeeze(
tf.expand_dims(
[[1, 2, 3]], dim=1), squeeze_dims=[1]).eval(),
a)
self.assertAllEqual(
tf.squeeze(
tf.expand_dims(
[[1, 2, 3]], dim=1), squeeze_dims=[1]).eval(),
a)
开发者ID:JonathanRaiman,项目名称:tensorflow,代码行数:26,代码来源:test_file_v0_11.py
示例11: roc_auc_score
def roc_auc_score(y_pred, y_true):
""" ROC AUC Score.
Approximates the Area Under Curve score, using approximation based on
the Wilcoxon-Mann-Whitney U statistic.
Yan, L., Dodier, R., Mozer, M. C., & Wolniewicz, R. (2003).
Optimizing Classifier Performance via an Approximation to the Wilcoxon-Mann-Whitney Statistic.
Measures overall performance for a full range of threshold levels.
Arguments:
y_pred: `Tensor`. Predicted values.
y_true: `Tensor` . Targets (labels), a probability distribution.
"""
with tf.name_scope("RocAucScore"):
pos = tf.boolean_mask(y_pred, tf.cast(y_true, tf.bool))
neg = tf.boolean_mask(y_pred, ~tf.cast(y_true, tf.bool))
pos = tf.expand_dims(pos, 0)
neg = tf.expand_dims(neg, 1)
# original paper suggests performance is robust to exact parameter choice
gamma = 0.2
p = 3
difference = tf.zeros_like(pos * neg) + pos - neg - gamma
masked = tf.boolean_mask(difference, difference < 0.0)
return tf.reduce_sum(tf.pow(-masked, p))
开发者ID:21hub,项目名称:tflearn,代码行数:33,代码来源:objectives.py
示例12: dna_transformation
def dna_transformation(prev_image, dna_input, dna_kernel_size, relu_shift):
"""Apply dynamic neural advection to previous image.
Args:
prev_image: previous image to be transformed.
dna_input: hidden lyaer to be used for computing DNA transformation.
dna_kernel_size: dna kernel size.
relu_shift: shift for ReLU function.
Returns:
List of images transformed by the predicted CDNA kernels.
"""
# Construct translated images.
prev_image_pad = tf.pad(prev_image, [[0, 0], [2, 2], [2, 2], [0, 0]])
image_height = int(prev_image.get_shape()[1])
image_width = int(prev_image.get_shape()[2])
inputs = []
for xkern in range(dna_kernel_size):
for ykern in range(dna_kernel_size):
inputs.append(
tf.expand_dims(
tf.slice(prev_image_pad, [0, xkern, ykern, 0],
[-1, image_height, image_width, -1]), [3]))
inputs = tf.concat(axis=3, values=inputs)
# Normalize channels to 1.
kernel = tf.nn.relu(dna_input - relu_shift) + relu_shift
kernel = tf.expand_dims(
kernel / tf.reduce_sum(kernel, [3], keep_dims=True), [4])
return tf.reduce_sum(kernel * inputs, [3], keep_dims=False)
开发者ID:qixiuai,项目名称:tensor2tensor,代码行数:30,代码来源:common_video.py
示例13: train
def train():
image_name = tf.constant("lily.jpg", tf.string)
image1 = uf.read_image(image_name, IMG_ROW, IMG_COL)
image1 = tf.expand_dims(image1, 0)
image2 = uf.read_image(image_name, IMG_ROW, IMG_COL)
image2 = tf.expand_dims(image2, 0)
image = tf.concat(0, (image1, image2))
clstm = crnn.con_lstm_cell(BATCH_SIZE, IMG_ROW, IMG_COL, 3, 3, CELL_C)
input_ = tf.placeholder(tf.float32, (BATCH_SIZE, IMG_ROW, IMG_COL, 3))
inputs = []
inputs.append(input_)
inputs.append(input_)
outputs, state = crnn.clstm_encode(clstm, inputs)
sess = tf.Session()
init_op = tf.initialize_all_variables()
sess.run(init_op)
for i in xrange(100):
image_v = sess.run(image)
feed_data = dict()
feed_data[inputs[0]] = image_v
feed_data[inputs[1]] = image_v
outputs_v = sess.run(outputs, feed_dict = feed_data)
print(outputs_v)
开发者ID:polltooh,项目名称:CNN_LSTM,代码行数:28,代码来源:test_clstm.py
示例14: _att
def _att(self, context, context_encode, h):
with tf.variable_scope('att') as scope:
hidden_att_W = self._variable_trunc_normal('hidden_att_W',
[self.dim_hidden, self.dim_ctx])
pre_att_b = self._variable_constant('pre_att_b',
[self.dim_ctx])
att_W = self._variable_trunc_normal('att_W',
[self.dim_ctx, 1])
att_b = self._variable_constant('att_b', [1])
# evaluate context_encode (e_ti)
context_encode = context_encode + \
tf.expand_dims(tf.matmul(h, hidden_att_W), 1) + \
pre_att_b
context_encode = tf.nn.tanh(context_encode)
context_encode_flat = tf.reshape(context_encode,
[self.batch_size*self.ctx_shape[0], self.dim_ctx])
alpha = tf.reshape(
tf.matmul(context_encode_flat, att_W) + att_b,
[self.batch_size, self.ctx_shape[0]])
alpha = tf.nn.softmax(alpha)
weighted_context = tf.reduce_sum(context * \
tf.expand_dims(alpha, 2), 1)
return weighted_context
开发者ID:guduxingzou,项目名称:show-attend-and-tell,代码行数:25,代码来源:model.py
示例15: build_psi_stats_rbf_plus_linear
def build_psi_stats_rbf_plus_linear(Z, kern, mu, S):
# TODO: make sure the acvite dimensions are overlapping completely
# use only active dimensions
mu, S = kern._slice(mu, S) # only use the active dimensions.
Z, _ = kern._slice(Z, None)
psi0_lin, psi1_lin, psi2_lin = build_psi_stats_linear(Z, kern.linear, mu, S)
psi0_rbf, psi1_rbf, psi2_rbf = build_psi_stats_rbf(Z, kern.rbf, mu, S)
psi0, psi1, psi2 = psi0_lin + psi0_rbf, psi1_lin + psi1_rbf, psi2_lin + psi2_rbf
# extra terms for the 'interaction' of linear and rbf
l2 = tf.square(kern.rbf.lengthscales)
A = tf.expand_dims(1./S + 1./l2, 1) # N x 1 x Q
m = (tf.expand_dims(mu/S, 1) + tf.expand_dims(Z/l2, 0)) / A # N x M x Q
mTAZ = tf.reduce_sum(tf.expand_dims(m * kern.linear.variance, 1) *
tf.expand_dims(tf.expand_dims(Z, 0), 0), 3) # N x M x M
Z2 = tf.reduce_sum(tf.square(Z) / l2, 1) # M,
mu2 = tf.reduce_sum(tf.square(mu) / S, 1) # N
mAm = tf.reduce_sum(tf.square(m) * A, 2) # N x M
exp_term = tf.exp(-(tf.reshape(Z2, (1, -1)) + tf.reshape(mu2, (-1, 1))-mAm) / 2.) # N x M
psi2_extra = tf.reduce_sum(kern.rbf.variance *
tf.expand_dims(exp_term, 2) *
tf.expand_dims(tf.expand_dims(tf.reduce_prod(S, 1), 1), 2) *
tf.expand_dims(tf.reduce_prod(A, 2), 1) *
mTAZ, 0)
psi2 = psi2 + psi2_extra + tf.transpose(psi2_extra)
return psi0, psi1, psi2
开发者ID:blutooth,项目名称:dgp,代码行数:29,代码来源:kernel_expectations.py
示例16: radial_symmetry
def radial_symmetry(self, d_cutoff, d, atom_numbers):
""" Radial Symmetry Function """
embedding = tf.eye(np.max(self.atom_cases) + 1)
atom_numbers_embedded = tf.nn.embedding_lookup(embedding, atom_numbers)
Rs = np.linspace(0., self.radial_cutoff, self.radial_length)
ita = np.ones_like(Rs) * 3 / (Rs[1] - Rs[0])**2
Rs = tf.cast(np.reshape(Rs, (1, 1, 1, -1)), tf.float32)
ita = tf.cast(np.reshape(ita, (1, 1, 1, -1)), tf.float32)
length = ita.get_shape().as_list()[-1]
d_cutoff = tf.stack([d_cutoff] * length, axis=3)
d = tf.stack([d] * length, axis=3)
out = tf.exp(-ita * tf.square(d - Rs)) * d_cutoff
if self.atomic_number_differentiated:
out_tensors = []
for atom_type in self.atom_cases:
selected_atoms = tf.expand_dims(
tf.expand_dims(atom_numbers_embedded[:, :, atom_type], axis=1),
axis=3)
out_tensors.append(tf.reduce_sum(out * selected_atoms, axis=2))
return tf.concat(out_tensors, axis=2)
else:
return tf.reduce_sum(out, axis=2)
开发者ID:ktaneishi,项目名称:deepchem,代码行数:25,代码来源:transformers.py
示例17: _build_predict
def _build_predict(self, Xnew, full_cov=False):
"""
Compute the mean and variance of the latent function at some new points
Xnew. For a derivation of the terms in here, see the associated SGPR
notebook.
"""
num_inducing = len(self.feature)
err = self.Y - self.mean_function(self.X)
Kuf = self.feature.Kuf(self.kern, self.X)
Kuu = self.feature.Kuu(self.kern, jitter=settings.numerics.jitter_level)
Kus = self.feature.Kuf(self.kern, Xnew)
sigma = tf.sqrt(self.likelihood.variance)
L = tf.cholesky(Kuu)
A = tf.matrix_triangular_solve(L, Kuf, lower=True) / sigma
B = tf.matmul(A, A, transpose_b=True) + tf.eye(num_inducing, dtype=settings.float_type)
LB = tf.cholesky(B)
Aerr = tf.matmul(A, err)
c = tf.matrix_triangular_solve(LB, Aerr, lower=True) / sigma
tmp1 = tf.matrix_triangular_solve(L, Kus, lower=True)
tmp2 = tf.matrix_triangular_solve(LB, tmp1, lower=True)
mean = tf.matmul(tmp2, c, transpose_a=True)
if full_cov:
var = self.kern.K(Xnew) + tf.matmul(tmp2, tmp2, transpose_a=True) \
- tf.matmul(tmp1, tmp1, transpose_a=True)
shape = tf.stack([1, 1, tf.shape(self.Y)[1]])
var = tf.tile(tf.expand_dims(var, 2), shape)
else:
var = self.kern.Kdiag(Xnew) + tf.reduce_sum(tf.square(tmp2), 0) \
- tf.reduce_sum(tf.square(tmp1), 0)
shape = tf.stack([1, tf.shape(self.Y)[1]])
var = tf.tile(tf.expand_dims(var, 1), shape)
return mean + self.mean_function(Xnew), var
开发者ID:vincentadam87,项目名称:GPflow,代码行数:32,代码来源:sgpr.py
示例18: __init__
def __init__(self, num_layers, num_units, batch_size, input_size, keep_prob=1.0):
self.num_layers = num_layers
self.grus = []
self.inits = []
self.dropout_mask = []
for layer in range(num_layers):
input_size_ = input_size if layer == 0 else 2 * num_units
gru_fw = tf.nn.rnn_cell.MultiRNNCell([
tf.contrib.cudnn_rnn.CudnnCompatibleGRUCell(num_units=num_units)])
gru_bw = tf.nn.rnn_cell.MultiRNNCell([
tf.contrib.cudnn_rnn.CudnnCompatibleGRUCell(num_units=num_units)])
init_fw = tf.Variable(tf.zeros([num_units]))
init_fw = tf.expand_dims(tf.tile(tf.expand_dims(init_fw, axis=0), [batch_size, 1]), axis=0)
init_bw = tf.Variable(tf.zeros([num_units]))
init_bw = tf.expand_dims(tf.tile(tf.expand_dims(init_bw, axis=0), [batch_size, 1]), axis=0)
mask_fw = tf.nn.dropout(tf.ones([1, batch_size, input_size_], dtype=tf.float32),
keep_prob=keep_prob)
mask_bw = tf.nn.dropout(tf.ones([1, batch_size, input_size_], dtype=tf.float32),
keep_prob=keep_prob)
self.grus.append((gru_fw, gru_bw,))
self.inits.append((init_fw, init_bw,))
self.dropout_mask.append((mask_fw, mask_bw,))
开发者ID:RileyShe,项目名称:DeepPavlov,代码行数:26,代码来源:utils.py
示例19: __init__
def __init__(self, memory_cells, query, project_query=False):
"""Define Attention.
Args:
memory_cells (SequenceBatch): a SequenceBatch containing a Tensor of shape (batch_size, num_cells, cell_dim)
query (Tensor): a tensor of shape (batch_size, query_dim).
project_query (bool): defaults to False. If True, the query goes through an extra projection layer to
coerce it to cell_dim.
"""
cell_dim = memory_cells.values.get_shape().as_list()[2]
if project_query:
# project the query up/down to cell_dim
self._projection_layer = Dense(cell_dim, activation='linear')
query = self._projection_layer(query) # (batch_size, cand_dim)
memory_values, memory_mask = memory_cells.values, memory_cells.mask
# batch matrix multiply to compute logit scores for all choices in all batches
query = tf.expand_dims(query, 2) # (batch_size, cell_dim, 1)
logit_values = tf.batch_matmul(memory_values, query) # (batch_size, num_cells, 1)
logit_values = tf.squeeze(logit_values, [2]) # (batch_size, num_cells)
# set all pad logits to negative infinity
logits = SequenceBatch(logit_values, memory_mask)
logits = logits.with_pad_value(-float('inf'))
# normalize to get probs
probs = tf.nn.softmax(logits.values) # (batch_size, num_cells)
retrieved = tf.batch_matmul(tf.expand_dims(probs, 1), memory_values) # (batch_size, 1, cell_dim)
retrieved = tf.squeeze(retrieved, [1]) # (batch_size, cell_dim)
self._logits = logits.values
self._probs = probs
self._retrieved = retrieved
开发者ID:siddk,项目名称:lang2program,代码行数:35,代码来源:model.py
示例20: additive_attention
def additive_attention(a, b, a_lengths, b_lengths, max_seq_len, hidden_units=150,
scope='additive-attention', reuse=False):
"""
For sequences a and b of lengths a_lengths and b_lengths, computes an attention matrix attn,
where attn(i, j) = dot(v, tanh(W*a_i + W*b_j)). v is a learnable vector and W is a learnable
matrix. The rows of attn are softmax normalized.
Args:
a: Input sequence a. Tensor of shape [batch_size, max_seq_len, input_size].
b: Input sequence b. Tensor of shape [batch_size, max_seq_len, input_size].
a_lengths: Lengths of sequences in a. Tensor of shape [batch_size].
b_lengths: Lengths of sequences in b. Tensor of shape [batch_size].
max_seq_len: Length of padded sequences a and b. Integer.
hidden_units: Number of hidden units. Integer.
Returns:
Attention matrix. Tensor of shape [max_seq_len, max_seq_len].
"""
with tf.variable_scope(scope, reuse=reuse):
aW = time_distributed_dense_layer(a, hidden_units, bias=False, scope='dense', reuse=False)
bW = time_distributed_dense_layer(b, hidden_units, bias=False, scope='dense', reuse=True)
aW = tf.expand_dims(aW, 2)
bW = tf.expand_dims(bW, 1)
v = tf.get_variable(
name='dot_weights',
initializer=tf.variance_scaling_initializer(),
shape=[hidden_units]
)
logits = tf.einsum('ijkl,l->ijk', tf.nn.tanh(aW + bW), v)
logits = logits - tf.expand_dims(tf.reduce_max(logits, axis=2), 2)
attn = tf.exp(logits)
attn = mask_attention_weights(attn, a_lengths, b_lengths, max_seq_len)
return attn / tf.expand_dims(tf.reduce_sum(attn, axis=2) + 1e-10, 2)
开发者ID:charlesjansen,项目名称:quora-duplicate-questions,代码行数:34,代码来源:attend.py
注:本文中的tensorflow.expand_dims函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论