def _filter_negative_samples(labels, tensors):
"""keeps only samples with none-negative labels
Params:
-----
labels: of shape (N,)
tensors: a list of tensors, each of shape (N, .., ..) the first axis is sample number
Returns:
-----
tensors: filtered tensors
"""
# return tensors
keeps = tf.where(tf.greater_equal(labels, 0))
keeps = tf.reshape(keeps, [-1])
filtered = []
for t in tensors:
tf.assert_equal(tf.shape(t)[0], tf.shape(labels)[0])
f = tf.gather(t, keeps)
filtered.append(f)
return filtered
python类gather()的实例源码
def value_transition(self, curr_state, next_symbols, batch_size):
first_value_token = self.num_functions + self.num_begin_tokens + self.num_control_tokens
num_value_tokens = self.output_size - first_value_token
with tf.name_scope('grammar_transition'):
adjusted_next_symbols = tf.where(next_symbols >= self.num_control_tokens, next_symbols + (first_value_token - self.num_control_tokens), next_symbols)
assert1 = tf.Assert(tf.reduce_all(tf.logical_and(next_symbols < num_value_tokens, next_symbols >= 0)), [curr_state, next_symbols])
with tf.control_dependencies([assert1]):
transitions = tf.gather(tf.constant(self.transition_matrix), curr_state)
assert transitions.get_shape()[1:] == (self.output_size,)
indices = tf.stack((tf.range(0, batch_size), adjusted_next_symbols), axis=1)
next_state = tf.gather_nd(transitions, indices)
assert2 = tf.Assert(tf.reduce_all(next_state >= 0), [curr_state, adjusted_next_symbols, next_state])
with tf.control_dependencies([assert2]):
return tf.identity(next_state)
eval_output_embeddings.py 文件源码
项目:almond-nnparser
作者: Stanford-Mobisocial-IoT-Lab
项目源码
文件源码
阅读 35
收藏 0
点赞 0
评论 0
def bag_of_tokens(config, labels, label_lengths):
if config.train_output_embeddings:
with tf.variable_scope('embed', reuse=True):
output_embeddings = tf.get_variable('output_embedding')
else:
output_embeddings = tf.constant(config.output_embedding_matrix)
#everything_label_placeholder = tf.placeholder(shape=(None, config.max_length,), dtype=tf.int32)
#everything_label_length_placeholder = tf.placeholder(shape=(None,), dtype=tf.int32)
labels = tf.constant(np.array(labels))
embedded_output = tf.gather(output_embeddings, labels)
print('embedded_output before', embedded_output)
#mask = tf.sequence_mask(label_lengths, maxlen=config.max_length, dtype=tf.float32)
# note: this multiplication will broadcast the mask along all elements of the depth dimension
# (which is why we run the expand_dims to choose how to broadcast)
#embedded_output = embedded_output * tf.expand_dims(mask, axis=2)
#print('embedded_output after', embedded_output)
return tf.reduce_sum(embedded_output, axis=1)
def repeat(tensor: tf.Tensor, repeats: int, axis: int) -> tf.Tensor:
"""
Repeat elements of the input tensor in the specified axis ``repeats``-times.
.. note::
Chaining of this op may produce TF warnings although the performance seems to be unaffected.
:param tensor: TF tensor to be repeated
:param repeats: number of repeats
:param axis: axis to repeat
:return: tensor with repeated elements
"""
shape = tensor.get_shape().as_list()
dims = np.arange(len(tensor.shape))
prepare_perm = np.hstack(([axis], np.delete(dims, axis)))
restore_perm = np.hstack((dims[1:axis+1], [0], dims[axis+1:]))
indices = tf.cast(tf.floor(tf.range(0, shape[axis]*repeats)/tf.constant(repeats)), 'int32')
shuffled = tf.transpose(tensor, prepare_perm)
repeated = tf.gather(shuffled, indices)
return tf.transpose(repeated, restore_perm)
def extract_argmax_and_embed(embedding, output_projection=None):
"""
Get a loop_function that extracts the previous symbol and embeds it. Used by decoder.
:param embedding: embedding tensor for symbol
:param output_projection: None or a pair (W, B). If provided, each fed previous output will
first be multiplied by W and added B.
:return: A loop function
"""
def loop_function(prev, _):
if output_projection is not None:
prev = tf.matmul(prev, output_projection[0]) + output_projection[1]
prev_symbol = tf.argmax(prev, 1) #?????INDEX
emb_prev = tf.gather(embedding, prev_symbol) #????INDEX???embedding
return emb_prev
return loop_function
# RNN??????
# ???????????????????test,?t???????t+1???s??
def extract_argmax_and_embed(embedding, output_projection=None):
"""
Get a loop_function that extracts the previous symbol and embeds it. Used by decoder.
:param embedding: embedding tensor for symbol
:param output_projection: None or a pair (W, B). If provided, each fed previous output will
first be multiplied by W and added B.
:return: A loop function
"""
def loop_function(prev, _):
if output_projection is not None:
prev = tf.matmul(prev, output_projection[0]) + output_projection[1]
prev_symbol = tf.argmax(prev, 1) #?????INDEX
emb_prev = tf.gather(embedding, prev_symbol) #????INDEX???embedding
return emb_prev
return loop_function
# RNN??????
# ???????????????????test,?t???????t+1???s??
def batch_transformer(U, thetas, out_size, name='BatchSpatialTransformer'):
"""Batch Spatial Transformer Layer
Parameters
----------
U : float
tensor of inputs [num_batch,height,width,num_channels]
thetas : float
a set of transformations for each input [num_batch,num_transforms,6]
out_size : int
the size of the output [out_height,out_width]
Returns: float
Tensor of size [num_batch*num_transforms,out_height,out_width,num_channels]
"""
with tf.variable_scope(name):
num_batch, num_transforms = map(int, thetas.get_shape().as_list()[:2])
indices = [[i]*num_transforms for i in range(num_batch)]
input_repeated = tf.gather(U, tf.reshape(indices, [-1]))
return transformer(input_repeated, thetas, out_size)
def max_sentence_similarity(sentence_input, similarity_matrix):
"""
Parameters
----------
sentence_input: Tensor
Tensor of shape (batch_size, num_sentence_words, rnn_hidden_dim).
similarity_matrix: Tensor
Tensor of shape (batch_size, num_sentence_words, num_sentence_words).
"""
# Shape: (batch_size, passage_len)
def single_instance(inputs):
single_sentence = inputs[0]
argmax_index = inputs[1]
# Shape: (num_sentence_words, rnn_hidden_dim)
return tf.gather(single_sentence, argmax_index)
question_index = tf.arg_max(similarity_matrix, 2)
elems = (sentence_input, question_index)
# Shape: (batch_size, num_sentence_words, rnn_hidden_dim)
return tf.map_fn(single_instance, elems, dtype="float")
def calculate_allocation_weighting(self, usage_vector):
"""
:param: usage vector: tensor of shape [batch_size, memory_size]
:return: allocation tensor of shape [batch_size, memory_size]
"""
usage_vector = Memory.epsilon + (1 - Memory.epsilon) * usage_vector
# We're sorting the "-self.usage_vector" because top_k returns highest values and we need the lowest
highest_usage, inverse_indices = tf.nn.top_k(-usage_vector, k=self.memory_size)
lowest_usage = -highest_usage
allocation_scrambled = (1 - lowest_usage) * tf.cumprod(lowest_usage, axis=1, exclusive=True)
# allocation is not in the correct order. alloation[i] contains the sorted[i] value
# reversing the already inversed indices for each batch
indices = tf.stack([tf.invert_permutation(batch_indices) for batch_indices in tf.unstack(inverse_indices)])
allocation = tf.stack([tf.gather(mem, ind)
for mem, ind in
zip(tf.unstack(allocation_scrambled), tf.unstack(indices))])
return allocation
def build_feed_dict(self):
if FLAGS.serialize_and_merge:
return self.build_feed_dict_with_serialize_and_merge()
_logger.info("Traversing trees.")
# The weaver is an object that can invoke LoomOps, and create a feed_dict.
weaver = self._loom.make_weaver()
# Recurse over each tree in the batch
for _ in six.moves.xrange(0, self.batch_size):
root = self.traverse_tree(self.get_input_tree(), weaver)
weaver.add_output(root)
# Now build the feed_dict, which contains both indices into the embedding
# tables, and indices for the gather nodes inserted by Loom.
if FLAGS.direct_feed_dict:
_logger.info("Calling build_feed_dict in direct mode.")
else:
_logger.info("Calling build_feed_dict with serialization.")
return weaver.build_feed_dict()
def bilinear_answer_layer(size, encoded_question, question_length, encoded_support, support_length,
support2question, answer2support, is_eval, beam_size=1,
max_span_size=10000):
"""Answer layer for multiple paragraph QA."""
# computing single time attention over question
size = encoded_support.get_shape()[-1].value
question_state = compute_question_state(encoded_question, question_length)
# compute logits
hidden = tf.gather(tf.layers.dense(question_state, 2 * size, name="hidden"), support2question)
hidden_start, hidden_end = tf.split(hidden, 2, 1)
support_mask = misc.mask_for_lengths(support_length)
start_scores = tf.einsum('ik,ijk->ij', hidden_start, encoded_support)
start_scores = start_scores + support_mask
end_scores = tf.einsum('ik,ijk->ij', hidden_end, encoded_support)
end_scores = end_scores + support_mask
return compute_spans(start_scores, end_scores, answer2support, is_eval, support2question,
beam_size, max_span_size)
def _get_top_k(scores1, scores2, k, max_span_size, support2question):
max_support_length = tf.shape(scores1)[1]
doc_idx, pointer1, topk_scores1 = segment_top_k(scores1, support2question, k)
# [num_questions * beam_size]
doc_idx_flat = tf.reshape(doc_idx, [-1])
pointer_flat1 = tf.reshape(pointer1, [-1])
# [num_questions * beam_size, support_length]
scores_gathered2 = tf.gather(scores2, doc_idx_flat)
if max_span_size < 0:
pointer_flat1, max_span_size = pointer_flat1 + max_span_size + 1, -max_span_size
left_mask = misc.mask_for_lengths(tf.cast(pointer_flat1, tf.int32),
max_support_length, mask_right=False)
right_mask = misc.mask_for_lengths(tf.cast(pointer_flat1 + max_span_size, tf.int32),
max_support_length)
scores_gathered2 = scores_gathered2 + left_mask + right_mask
pointer2 = tf.argmax(scores_gathered2, axis=1, output_type=tf.int32)
topk_score2 = tf.gather_nd(scores2, tf.stack([doc_idx_flat, pointer2], 1))
return doc_idx, pointer1, tf.reshape(pointer2, [-1, k]), topk_scores1 + tf.reshape(topk_score2, [-1, k])
def interaction_layer(seq1, seq1_length, seq2, seq2_length, seq1_to_seq2,
module='attention_matching', attn_type='bilinear_diagonal', scaled=True, with_sentinel=False,
name='interaction_layer', reuse=False, num_layers=1, encoder=None, concat=True, **kwargs):
with tf.variable_scope(name, reuse=reuse):
if seq1_to_seq2 is not None:
seq2 = tf.gather(seq2, seq1_to_seq2)
seq2_length = tf.gather(seq2_length, seq1_to_seq2)
if module == 'attention_matching':
out = attention_matching_layer(seq1, seq1_length, seq2, seq2_length,
attn_type, scaled, with_sentinel)
elif module == 'bidaf':
out = bidaf_layer(seq1, seq1_length, seq2, seq2_length)
elif module == 'coattention':
out = coattention_layer(
seq1, seq1_length, seq2, seq2_length, attn_type, scaled, with_sentinel, num_layers, encoder)
else:
raise ValueError("Unknown interaction type: %s" % module)
if concat:
out = tf.concat([seq1, out], 2)
return out
def batch_gather(tensor, indices):
"""Gather in batch from a tensor of arbitrary size.
In pseduocode this module will produce the following:
output[i] = tf.gather(tensor[i], indices[i])
Args:
tensor: Tensor of arbitrary size.
indices: Vector of indices.
Returns:
output: A tensor of gathered values.
"""
shape = get_shape(tensor)
flat_first = tf.reshape(tensor, [shape[0] * shape[1]] + shape[2:])
indices = tf.convert_to_tensor(indices)
offset_shape = [shape[0]] + [1] * (indices.shape.ndims - 1)
offset = tf.reshape(tf.range(shape[0]) * shape[1], offset_shape)
output = tf.gather(flat_first, indices + offset)
return output
def test_sgld_sparse(self):
tf.reset_default_graph()
z = tf.Variable(tf.zeros((5, 2)), dtype=tf.float32)
idx = tf.placeholder(tf.int32)
zi = tf.gather(z, idx)
zloss = tf.square(zi - [10.0, 5.0])
sgld = SGLD(learning_rate=0.4)
train_op_sgld = sgld.minimize(zloss)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
self.assertTrue(np.alltrue(sess.run(z) == 0.0))
sess.run(train_op_sgld, feed_dict={idx: 3})
zh = sess.run(z)
self.assertTrue(np.alltrue(zh[[0, 1, 2, 4], :] == 0.0))
self.assertTrue(zh[3, 0] > 0)
def test_psgld_sparse(self):
tf.reset_default_graph()
z = tf.Variable(tf.zeros((5, 2)), dtype=tf.float32)
idx = tf.placeholder(tf.int32)
zi = tf.gather(z, idx)
zloss = tf.square(zi - [10.0, 5.0])
psgld = pSGLD(learning_rate=0.4)
train_op_psgld = psgld.minimize(zloss)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
self.assertTrue(np.alltrue(sess.run(z) == 0.0))
sess.run(train_op_psgld, feed_dict={idx: 3})
zh = sess.run(z)
self.assertTrue(np.alltrue(zh[[0, 1, 2, 4], :] == 0.0))
self.assertTrue(zh[3, 0] > 0)
def density_map(tensor, shape):
"""
"""
height, width, channels = shape
bins = max(height, width)
# values = value_map(tensor, shape, keep_dims=True)
# values = tf.minimum(tf.maximum(tensor, 0.0), 1.0) # TODO: Get this to work with HDR data
values = tensor
# https://stackoverflow.com/a/34143927
binned_values = tf.cast(tf.reshape(values * (bins - 1), [-1]), tf.int32)
ones = tf.ones_like(binned_values, dtype=tf.int32)
counts = tf.unsorted_segment_sum(ones, binned_values, bins)
out = tf.gather(counts, tf.cast(values[:, :] * (bins - 1), tf.int32))
return tf.ones(shape) * normalize(tf.cast(out, tf.float32))
def linearND(input_, output_size, scope, init_bias=0.0):
shape = input_.get_shape().as_list()
ndim = len(shape)
stddev = min(1.0 / math.sqrt(shape[-1]), 0.1)
with tf.variable_scope(scope):
W = tf.get_variable("Matrix", [shape[-1], output_size], tf.float32, tf.random_normal_initializer(stddev=stddev))
X_shape = tf.gather(tf.shape(input_), range(ndim-1))
target_shape = tf.concat(0, [X_shape, [output_size]])
exp_input = tf.reshape(input_, [-1, shape[-1]])
if init_bias is None:
res = tf.matmul(exp_input, W)
else:
with tf.variable_scope(scope):
b = tf.get_variable("bias", [output_size], initializer=tf.constant_initializer(init_bias))
res = tf.matmul(exp_input, W) + b
res = tf.reshape(res, target_shape)
res.set_shape(shape[:-1] + [output_size])
return res
text_classification_model_simple.py 文件源码
项目:kaggle_redefining_cancer_treatment
作者: jorgemf
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def rnn(self, sequence, sequence_length, max_length, dropout, batch_size, training,
num_hidden=TC_MODEL_HIDDEN, num_layers=TC_MODEL_LAYERS):
# Recurrent network.
cells = []
for _ in range(num_layers):
cell = tf.nn.rnn_cell.GRUCell(num_hidden)
if training:
cell = tf.nn.rnn_cell.DropoutWrapper(cell, output_keep_prob=dropout)
cells.append(cell)
network = tf.nn.rnn_cell.MultiRNNCell(cells)
type = sequence.dtype
sequence_output, _ = tf.nn.dynamic_rnn(network, sequence, dtype=tf.float32,
sequence_length=sequence_length,
initial_state=network.zero_state(batch_size, type))
# get last output of the dynamic_rnn
sequence_output = tf.reshape(sequence_output, [batch_size * max_length, num_hidden])
indexes = tf.range(batch_size) * max_length + (sequence_length - 1)
output = tf.gather(sequence_output, indexes)
return output
text_classification_model_simple_bidirectional.py 文件源码
项目:kaggle_redefining_cancer_treatment
作者: jorgemf
项目源码
文件源码
阅读 40
收藏 0
点赞 0
评论 0
def rnn(self, sequence, sequence_length, max_length, dropout, batch_size, training,
num_hidden=TC_MODEL_HIDDEN, num_layers=TC_MODEL_LAYERS):
# Recurrent network.
cell_fw = tf.nn.rnn_cell.GRUCell(num_hidden)
cell_bw = tf.nn.rnn_cell.GRUCell(num_hidden)
type = sequence.dtype
(fw_outputs, bw_outputs), _ = \
tf.nn.bidirectional_dynamic_rnn(cell_fw=cell_fw,
cell_bw=cell_bw,
initial_state_fw=cell_fw.zero_state(batch_size, type),
initial_state_bw=cell_bw.zero_state(batch_size, type),
inputs=sequence,
dtype=tf.float32,
swap_memory=True,
sequence_length=sequence_length)
sequence_output = tf.concat((fw_outputs, bw_outputs), 2)
# get last output of the dynamic_rnn
sequence_output = tf.reshape(sequence_output, [batch_size * max_length, num_hidden * 2])
indexes = tf.range(batch_size) * max_length + (sequence_length - 1)
output = tf.gather(sequence_output, indexes)
return output
def multilabel_image_to_class(label_image: tf.Tensor, classes_file: str) -> tf.Tensor:
classes_color_values, colors_labels = get_classes_color_from_file_multilabel(classes_file)
# Convert label_image [H,W,3] to the classes [H,W,C],int32 according to the classes [C,3]
with tf.name_scope('LabelAssign'):
if len(label_image.get_shape()) == 3:
diff = tf.cast(label_image[:, :, None, :], tf.float32) - tf.constant(classes_color_values[None, None, :, :]) # [H,W,C,3]
elif len(label_image.get_shape()) == 4:
diff = tf.cast(label_image[:, :, :, None, :], tf.float32) - tf.constant(
classes_color_values[None, None, None, :, :]) # [B,H,W,C,3]
else:
raise NotImplementedError('Length is : {}'.format(len(label_image.get_shape())))
pixel_class_diff = tf.reduce_sum(tf.square(diff), axis=-1) # [H,W,C] or [B,H,W,C]
class_label = tf.argmin(pixel_class_diff, axis=-1) # [H,W] or [B,H,W]
return tf.gather(colors_labels, class_label) > 0
def _build(self, X):
"""Build the graph of this layer."""
n_samples, input_dim = self._get_X_dims(X)
W_shape, _ = self._weight_shapes(self.n_categories)
n_batch = tf.shape(X)[1]
# Layer weights
self.pW = _make_prior(self.std, self.pW, W_shape)
self.qW = _make_posterior(self.std, self.qW, W_shape, self.full)
# Index into the relevant weights rather than using sparse matmul
Wsamples = _sample_W(self.qW, n_samples)
features = tf.map_fn(lambda wx: tf.gather(*wx, axis=0), (Wsamples, X),
dtype=Wsamples.dtype)
# Now concatenate the resulting features on the last axis
f_dims = int(np.prod(features.shape[2:])) # need this for placeholders
Net = tf.reshape(features, [n_samples, n_batch, f_dims])
# Regularizers
KL = kl_sum(self.qW, self.pW)
return Net, KL
def _build(self, X):
"""Build the graph of this layer."""
n_samples, input_dim = self._get_X_dims(X)
Wdim = (self.n_categories, self.output_dim)
n_batch = tf.shape(X)[1]
W = tf.Variable(tf.random_normal(shape=Wdim, seed=next(seedgen)),
name="W_map")
# Index into the relevant weights rather than using sparse matmul
features = tf.gather(W, X, axis=0)
f_dims = int(np.prod(features.shape[2:])) # need this for placeholders
Net = tf.reshape(features, [n_samples, n_batch, f_dims])
# Regularizers
penalty = self.l2 * tf.nn.l2_loss(W) + self.l1 * _l1_loss(W)
return Net, penalty
#
# Private module stuff
#
def _impute2D(self, X_2D):
r"""Mean impute a rank 2 tensor."""
# Fill zeros in for missing data initially
data_zeroed_missing_tf = X_2D * self.real_val_mask
# Sum the real values in each column
col_tot = tf.reduce_sum(data_zeroed_missing_tf, 0)
# Divide column totals by the number of non-nan values
num_values_col = tf.reduce_sum(self.real_val_mask, 0)
num_values_col = tf.maximum(num_values_col,
tf.ones(tf.shape(num_values_col)))
col_nan_means = tf.div(col_tot, num_values_col)
# Make an vector of the impute values for each missing point
imputed_vals = tf.gather(col_nan_means, self.missing_ind[:, 1])
# Fill the imputed values into the data tensor of zeros
shape = tf.cast(tf.shape(data_zeroed_missing_tf), dtype=tf.int64)
missing_imputed = tf.scatter_nd(self.missing_ind, imputed_vals, shape)
X_with_impute = data_zeroed_missing_tf + missing_imputed
return X_with_impute
def _impute2D(self, X_2D):
r"""Randomly impute a rank 2 tensor."""
# Fill zeros in for missing data initially
data_zeroed_missing_tf = X_2D * self.real_val_mask
# Divide column totals by the number of non-nan values
col_draws = [n.sample(seed=next(seedgen)) for n in self.normal_array]
# Make an vector of the impute values for each missing point
imputed_vals = tf.gather(col_draws, self.missing_ind[:, 1])
# Fill the imputed values into the data tensor of zeros
shape = tf.cast(tf.shape(data_zeroed_missing_tf), dtype=tf.int64)
missing_imputed = tf.scatter_nd(self.missing_ind, imputed_vals, shape)
X_with_impute = data_zeroed_missing_tf + missing_imputed
return X_with_impute
def construct(self):
net_opts = VGG_Face.OPTS()
net_opts.network_name = 'vgg_face_net'
net_opts.weight_path = 'pretrained/vgg-face.mat'
net_opts.apply_dropout = True
self.vgg_net = VGG_Face(net_opts)
x_normalized = self.vgg_net.normalize_input(self.x)
self.vgg_net.network(x_normalized)
self.keep_prob = self.vgg_net.keep_prob
self.embedded = self.vgg_net.fc6 #Fine tuning from FC6 of VGG-Face
self.embedded_with_class = tf.gather(self.embedded, self.with_class_idx, name='embedded_with_class')
self.dom_network(self.embedded)
self.class_network(self.embedded_with_class)
self.loss = self.dom_loss + self.class_loss
# Construct Domain Discriminator Network
def segment_indices(segment_ids, name=None):
"""Returns a `Tensor` of indices within each segment.
segment_ids should be a sequence of non-decreasing non-negative integers that
define a set of segments, e.g. [0, 0, 1, 2, 2, 2] defines 3 segments of length
2, 1 and 3. The return value is a `Tensor` containing the indices within each
segment.
Example input: [0, 0, 1, 2, 2, 2]
Example output: [0, 1, 0, 0, 1, 2]
Args:
segment_ids: A 1-d `Tensor` containing an non-decreasing sequence of
non-negative integers with type `tf.int32` or `tf.int64`.
name: (Optional) A name for this operation.
Returns:
A `Tensor` containing the indices within each segment.
"""
with tf.name_scope(name, 'segment_indices'):
segment_lengths = tf.segment_sum(tf.ones_like(segment_ids), segment_ids)
segment_starts = tf.gather(tf.concat([[0], tf.cumsum(segment_lengths)], 0),
segment_ids)
return (tf.range(tf.size(segment_ids, out_type=segment_ids.dtype)) -
segment_starts)
production_ex_train.py 文件源码
项目:TensorFlow-Machine-Learning-Cookbook
作者: PacktPublishing
项目源码
文件源码
阅读 31
收藏 0
点赞 0
评论 0
def rnn_model(x_data_ph, max_sequence_length, vocab_size, embedding_size,
rnn_size, dropout_keep_prob):
# Create embedding
embedding_mat = tf.Variable(tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0))
embedding_output = tf.nn.embedding_lookup(embedding_mat, x_data_ph)
# Define the RNN cell
cell = tf.nn.rnn_cell.BasicRNNCell(num_units = rnn_size)
output, state = tf.nn.dynamic_rnn(cell, embedding_output, dtype=tf.float32)
output = tf.nn.dropout(output, dropout_keep_prob)
# Get output of RNN sequence
output = tf.transpose(output, [1, 0, 2])
last = tf.gather(output, int(output.get_shape()[0]) - 1)
weight = tf.Variable(tf.truncated_normal([rnn_size, 2], stddev=0.1))
bias = tf.Variable(tf.constant(0.1, shape=[2]))
logits_out = tf.nn.softmax(tf.matmul(last, weight) + bias)
return(logits_out)
# Define accuracy function
def traditional_transition_loss_pred(self, i, j, combined_head, combined_dep):
rel_trans_feat_ids = self.trans_feat_ids[i*self.args.beam_size+j] if not self.train else self.trans_feat_ids[i, j]
rel_head = tf.reshape(tf.gather(combined_head, rel_trans_feat_ids[:4]), [4, self.args.rel_emb_dim])
rel_dep = tf.reshape(tf.gather(combined_dep, rel_trans_feat_ids[:4]), [4, self.args.rel_emb_dim])
mask = tf.cast(tf.reshape(tf.greater_equal(rel_trans_feat_ids[:4], 0), [4,1]), tf.float32)
rel_head = tf.multiply(mask, rel_head)
rel_dep = tf.multiply(mask, rel_dep)
rel_hid = self.rel_merge(rel_head, rel_dep)
rel_logit = self.rel_dense(tf.reshape(rel_hid, [1, -1]))
rel_logit = tf.reshape(rel_logit, [-1])
log_partition = tf.reduce_logsumexp(rel_logit)
if self.train:
res = log_partition - rel_logit[self.trans_labels[i, j]]
return res
else:
arc_pred = log_partition - rel_logit
return arc_pred
def pos_loss_pred(self, i, pos_embeddings, pos_logit, NUM_POS, gold_pos, pos_trainables):
if self.args.no_pos:
pos_emb = tf.nn.embedding_lookup(pos_embeddings, gold_pos[i])
if self.train:
return 0, pos_emb
else:
return tf.gather(gold_pos[i], tf.range(1, self.sent_length)), pos_emb
else:
pos_logit = pos_logit[1:]
log_partition = tf.reduce_logsumexp(pos_logit, [1])
pos_pred = tf.exp(pos_logit - tf.reshape(log_partition, (-1, 1)))
pos_emb = tf.concat([tf.reshape(tf.nn.embedding_lookup(pos_embeddings, NUM_POS), (1, -1)),
tf.matmul(pos_pred, pos_trainables)], 0)
if self.train:
loss = tf.reduce_sum(tf.gather(log_partition, tf.range(self.sent_lengths[i]-1))
- tf.gather(tf.reshape(pos_logit, [-1]),
tf.range(self.sent_lengths[i]-1) * NUM_POS
+ tf.gather(gold_pos[i], tf.range(1, self.sent_lengths[i]))))
return loss, pos_emb
else:
return tf.cast(tf.argmax(pos_pred, 1), tf.int32), pos_emb