def SampleRandomFrames(model_input, num_frames, num_samples):
"""Samples a random set of frames of size num_samples.
Args:
model_input: A tensor of size batch_size x max_frames x feature_size
num_frames: A tensor of size batch_size x 1
num_samples: A scalar
Returns:
`model_input`: A tensor of size batch_size x num_samples x feature_size
"""
batch_size = tf.shape(model_input)[0]
frame_index = tf.cast(
tf.multiply(
tf.random_uniform([batch_size, num_samples]),
tf.tile(tf.cast(num_frames, tf.float32), [1, num_samples])), tf.int32)
batch_index = tf.tile(
tf.expand_dims(tf.range(batch_size), 1), [1, num_samples])
index = tf.stack([batch_index, frame_index], 2)
return tf.gather_nd(model_input, index)
python类multiply()的实例源码
def should_distort_images(flip_left_right, random_crop, random_scale,
random_brightness):
"""Whether any distortions are enabled, from the input flags.
Args:
flip_left_right: Boolean whether to randomly mirror images horizontally.
random_crop: Integer percentage setting the total margin used around the
crop box.
random_scale: Integer percentage of how much to vary the scale by.
random_brightness: Integer range to randomly multiply the pixel values by.
Returns:
Boolean value indicating whether any distortions should be applied.
"""
return (flip_left_right or (random_crop != 0) or (random_scale != 0) or
(random_brightness != 0))
def should_distort_images(flip_left_right, random_crop, random_scale,
random_brightness):
"""Whether any distortions are enabled, from the input flags.
Args:
flip_left_right: Boolean whether to randomly mirror images horizontally.
random_crop: Integer percentage setting the total margin used around the
crop box.
random_scale: Integer percentage of how much to vary the scale by.
random_brightness: Integer range to randomly multiply the pixel values by.
Returns:
Boolean value indicating whether any distortions should be applied.
"""
return (flip_left_right or (random_crop != 0) or (random_scale != 0) or
(random_brightness != 0))
def should_distort_images(flip_left_right, random_crop, random_scale,
random_brightness):
"""Whether any distortions are enabled, from the input flags.
Args:
flip_left_right: Boolean whether to randomly mirror images horizontally.
random_crop: Integer percentage setting the total margin used around the
crop box.
random_scale: Integer percentage of how much to vary the scale by.
random_brightness: Integer range to randomly multiply the pixel values by.
Returns:
Boolean value indicating whether any distortions should be applied.
"""
return (flip_left_right or (random_crop != 0) or (random_scale != 0) or
(random_brightness != 0))
def _variable_with_weight_decay(name, shape, wd, initializer, trainable=True):
"""Helper to create an initialized Variable with weight decay.
Note that the Variable is initialized with a truncated normal distribution.
A weight decay is added only if one is specified.
Args:
name: name of the variable
shape: list of ints
wd: add L2Loss weight decay multiplied by this float. If None, weight
decay is not added for this Variable.
Returns:
Variable Tensor
"""
var = _variable_on_device(name, shape, initializer, trainable)
if wd is not None and trainable:
weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss')
tf.add_to_collection('losses', weight_decay)
return var
def SampleRandomFrames(model_input, num_frames, num_samples):
"""Samples a random set of frames of size num_samples.
Args:
model_input: A tensor of size batch_size x max_frames x feature_size
num_frames: A tensor of size batch_size x 1
num_samples: A scalar
Returns:
`model_input`: A tensor of size batch_size x num_samples x feature_size
"""
batch_size = tf.shape(model_input)[0]
frame_index = tf.cast(
tf.multiply(
tf.random_uniform([batch_size, num_samples]),
tf.tile(tf.cast(num_frames, tf.float32), [1, num_samples])), tf.int32)
batch_index = tf.tile(
tf.expand_dims(tf.range(batch_size), 1), [1, num_samples])
index = tf.stack([batch_index, frame_index], 2)
return tf.gather_nd(model_input, index)
def SampleRandomFrames(model_input, num_frames, num_samples):
"""Samples a random set of frames of size num_samples.
Args:
model_input: A tensor of size batch_size x max_frames x feature_size
num_frames: A tensor of size batch_size x 1
num_samples: A scalar
Returns:
`model_input`: A tensor of size batch_size x num_samples x feature_size
"""
batch_size = tf.shape(model_input)[0]
frame_index = tf.cast(
tf.multiply(
tf.random_uniform([batch_size, num_samples]),
tf.tile(tf.cast(num_frames, tf.float32), [1, num_samples])), tf.int32)
batch_index = tf.tile(
tf.expand_dims(tf.range(batch_size), 1), [1, num_samples])
index = tf.stack([batch_index, frame_index], 2)
return tf.gather_nd(model_input, index)
def _variable_with_weight_decay(name, shape, stddev, wd):
"""Helper to create an initialized Variable with weight decay.
Note that the Variable is initialized with a truncated normal distribution.
A weight decay is added only if one is specified.
Args:
name: name of the variable
shape: list of ints
stddev: standard deviation of a truncated Gaussian
wd: add L2Loss weight decay multiplied by this float. If None, weight
decay is not added for this Variable.
Returns:
Variable Tensor
"""
var = _variable_on_cpu(name, shape,
tf.truncated_normal_initializer(stddev=stddev))
if wd:
# weight_decay = tf.mul(tf.nn.l2_loss(var), wd, name='weight_loss')
weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss')
tf.add_to_collection('losses', weight_decay)
return var
def _variable_with_weight_decay(name, shape, stddev, wd):
"""Helper to create an initialized Variable with weight decay.
Note that the Variable is initialized with a truncated normal distribution.
A weight decay is added only if one is specified.
Args:
name: name of the variable
shape: list of ints
stddev: standard deviation of a truncated Gaussian
wd: add L2Loss weight decay multiplied by this float. If None, weight
decay is not added for this Variable.
Returns:
Variable Tensor
"""
var = _variable_on_cpu(name, shape,
tf.truncated_normal_initializer(stddev=stddev))
if wd:
# weight_decay = tf.mul(tf.nn.l2_loss(var), wd, name='weight_loss')
weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss')
tf.add_to_collection('losses', weight_decay)
return var
def _variable_with_weight_decay(name, shape, stddev, wd):
"""Helper to create an initialized Variable with weight decay.
Note that the Variable is initialized with a truncated normal distribution.
A weight decay is added only if one is specified.
Args:
name: name of the variable
shape: list of ints
stddev: standard deviation of a truncated Gaussian
wd: add L2Loss weight decay multiplied by this float. If None, weight
decay is not added for this Variable.
Returns:
Variable Tensor
"""
dtype = tf.float16 if FLAGS.use_fp16 else tf.float32
var = _variable_on_cpu(
name,
shape,
tf.truncated_normal_initializer(stddev=stddev, dtype=dtype))
if wd is not None and not tf.get_variable_scope().reuse:
weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss')
tf.add_to_collection('losses', weight_decay)
return var
def _add_cross_entropy(labels, logits, pref):
"""Compute average cross entropy and add to loss collection.
Args:
labels: Single dimension labels from distorted_inputs() or inputs().
logits: Output map from inference().
pref: Either 'c' or 's', for contours or segments, respectively.
"""
with tf.variable_scope('{}_cross_entropy'.format(pref)) as scope:
class_prop = C_CLASS_PROP if pref == 'c' else S_CLASS_PROP
weight_per_label = tf.scalar_mul(class_prop, tf.cast(tf.equal(labels, 0),
tf.float32)) + \
tf.scalar_mul(1.0 - class_prop, tf.cast(tf.equal(labels, 1),
tf.float32))
cross_entropy = tf.losses.sparse_softmax_cross_entropy(
labels=tf.squeeze(labels, squeeze_dims=[3]), logits=logits)
cross_entropy_weighted = tf.multiply(weight_per_label, cross_entropy)
cross_entropy_mean = tf.reduce_mean(cross_entropy_weighted, name=scope.name)
tf.add_to_collection('losses', cross_entropy_mean)
def get_dice_coef(logits, labels):
"""Compute dice coefficient.
Args:
logits: Softmax probability applied to fuse layers.
labels: Correct annotations (0 or 1).
Returns:
Mean dice coefficient over full tensor.
Source:
https://github.com/zsdonghao/tensorlayer/blob/master/tensorlayer/cost.py#L125
"""
smooth = 1e-5
inter = tf.reduce_sum(tf.multiply(logits, labels))
l = tf.reduce_sum(logits)
r = tf.reduce_sum(labels)
return tf.reduce_mean((2.0 * inter + smooth) / (l + r + smooth))
def mu_law_encode_nonlinear(audio, quantization_channels=256):
'''
Compress the waveform amplitudes using mu-law non-linearity.
NOTE: This mu-law functions as a non-linear function as opposed to
quantization.
'''
with tf.name_scope('encode'):
mu = tf.to_float(quantization_channels - 1)
# Perform mu-law companding transformation (ITU-T, 1988).
# Minimum operation is here to deal with rare large amplitudes caused
# by resampling.
safe_audio_abs = tf.minimum(tf.abs(audio), 1.0)
magnitude = tf.log1p(mu * safe_audio_abs) / tf.log1p(mu)
signal = tf.multiply(tf.sign(audio), magnitude, name='mulaw')
# Quantize signal to the specified number of levels.
# return tf.to_int32((signal + 1) / 2 * mu + 0.5)
return signal
def categorical_crossentropy_3d(y_true, y_predicted):
"""
Computes categorical cross-entropy loss for a softmax distribution in a hot-encoded 3D array
with shape (num_samples, num_classes, dim1, dim2, dim3)
Parameters
----------
y_true : keras.placeholder [batches, dim0,dim1,dim2]
Placeholder for data holding the ground-truth labels encoded in a one-hot representation
y_predicted : keras.placeholder [batches,channels,dim0,dim1,dim2]
Placeholder for data holding the softmax distribution over classes
Returns
-------
scalar
Categorical cross-entropy loss value
"""
y_true_flatten = K.flatten(y_true)
y_pred_flatten = K.flatten(y_predicted)
y_pred_flatten_log = -K.log(y_pred_flatten + K.epsilon())
num_total_elements = K.sum(y_true_flatten)
# cross_entropy = K.dot(y_true_flatten, K.transpose(y_pred_flatten_log))
cross_entropy = tf.reduce_sum(tf.multiply(y_true_flatten, y_pred_flatten_log))
mean_cross_entropy = cross_entropy / (num_total_elements + K.epsilon())
return mean_cross_entropy
def output_module(self):
"""
1.use attention mechanism between query and hidden states, to get weighted sum of hidden state. 2.non-linearity of query and hidden state to get label.
input: query_embedding:[batch_size,embed_size], hidden state:[batch_size,block_size,hidden_size] of memory
:return:y: predicted label.[]
"""
# 1.use attention mechanism between query and hidden states, to get weighted sum of hidden state.
# 1.1 get possibility distribution (of similiarity)
p=tf.nn.softmax(tf.multiply(tf.expand_dims(self.query_embedding,axis=1),self.hidden_state)) #shape:[batch_size,block_size,hidden_size]<---query_embedding_expand:[batch_size,1,hidden_size]; hidden_state:[batch_size,block_size,hidden_size]
# 1.2 get weighted sum of hidden state
u=tf.reduce_sum(tf.multiply(p,self.hidden_state),axis=1) #shape:[batch_size,hidden_size]<----------([batch_size,block_size,hidden_size],[batch_size,block_size,hidden_size])
# 2.non-linearity of query and hidden state to get label
H_u_matmul=tf.matmul(u,self.H)+self.h_u_bias #shape:[batch_size,hidden_size]<----([batch_size,hidden_size],[hidden_size,hidden_size])
activation=self.activation(self.query_embedding + H_u_matmul,scope="query_add_hidden") #shape:[batch_size,hidden_size]
activation = tf.nn.dropout(activation,keep_prob=self.dropout_keep_prob) #shape:[batch_size,hidden_size]
y=tf.matmul(activation,self.R)+self.y_bias #shape:[batch_size,vocab_size]<-----([batch_size,hidden_size],[hidden_size,vocab_size])
return y #shape:[batch_size,vocab_size]
a2_transformer_classification.py 文件源码
项目:text_classification
作者: brightmart
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def inference(self):
""" building blocks:
encoder:6 layers.each layers has two sub-layers. the first is multi-head self-attention mechanism; the second is position-wise fully connected feed-forward network.
for each sublayer. use LayerNorm(x+Sublayer(x)). all dimension=512.
decoder:6 layers.each layers has three sub-layers. the second layer is performs multi-head attention over the ouput of the encoder stack.
for each sublayer. use LayerNorm(x+Sublayer(x)).
"""
# 1.embedding for encoder input & decoder input
# 1.1 position embedding for encoder input
input_x_embeded = tf.nn.embedding_lookup(self.Embedding,self.input_x) #[None,sequence_length, embed_size]
input_x_embeded=tf.multiply(input_x_embeded,tf.sqrt(tf.cast(self.d_model,dtype=tf.float32)))
input_mask=tf.get_variable("input_mask",[self.sequence_length,1],initializer=self.initializer)
input_x_embeded=tf.add(input_x_embeded,input_mask) #[None,sequence_length,embed_size].position embedding.
# 2. encoder
encoder_class=Encoder(self.d_model,self.d_k,self.d_v,self.sequence_length,self.h,self.batch_size,self.num_layer,input_x_embeded,input_x_embeded,dropout_keep_prob=self.dropout_keep_prob,use_residual_conn=self.use_residual_conn)
Q_encoded,K_encoded = encoder_class.encoder_fn() #K_v_encoder
Q_encoded=tf.reshape(Q_encoded,shape=(self.batch_size,-1)) #[batch_size,sequence_length*d_model]
with tf.variable_scope("output"):
logits = tf.matmul(Q_encoded, self.W_projection) + self.b_projection #logits shape:[batch_size*decoder_sent_length,self.num_classes]
print("logits:",logits)
return logits
def masked_softmax(tensor, mask, expand=2, axis=1):
"""Masked soft-max using Lambda and merge-multiplication.
Args:
tensor: tensor containing scores
mask: mask for tensor where 1 - means values at this position and 0 - means void, padded, etc..
expand: axis along which to repeat mask
axis: axis along which to compute soft-max
Returns:
masked soft-max values
"""
mask = tf.expand_dims(mask, axis=expand)
exponentiate = Lambda(lambda x: K.exp(x - K.max(x, axis=axis, keepdims=True)))(tensor)
masked = tf.multiply(exponentiate, mask)
div = tf.expand_dims(tf.reduce_sum(masked, axis=axis), axis=axis)
predicted = tf.divide(masked, div)
return predicted
def answer_start_pred(context_encoding, question_attention_vector, context_mask, W, dropout_rate):
"""Answer start prediction layer."""
answer_start = Lambda(lambda arg:
concatenate([arg[0], arg[1], arg[2]]))([
context_encoding,
question_attention_vector,
multiply([context_encoding, question_attention_vector])])
answer_start = TimeDistributed(Dense(W, activation='relu'))(answer_start)
answer_start = Dropout(rate=dropout_rate)(answer_start)
answer_start = TimeDistributed(Dense(1))(answer_start)
# apply masking
answer_start = Lambda(lambda q: masked_softmax(q[0], q[1]))([answer_start, context_mask])
answer_start = Lambda(lambda q: flatten(q))(answer_start)
return answer_start
def pixel_wise_cross_entropy_loss_weighted(logits, labels, class_weights):
'''
Weighted cross entropy loss, with a weight per class
:param logits: Network output before softmax
:param labels: Ground truth masks
:param class_weights: A list of the weights for each class
:return: weighted cross entropy loss
'''
n_class = len(class_weights)
flat_logits = tf.reshape(logits, [-1, n_class])
flat_labels = tf.reshape(labels, [-1, n_class])
class_weights = tf.constant(np.array(class_weights, dtype=np.float32))
weight_map = tf.multiply(flat_labels, class_weights)
weight_map = tf.reduce_sum(weight_map, axis=1)
loss_map = tf.nn.softmax_cross_entropy_with_logits(logits=flat_logits, labels=flat_labels)
weighted_loss = tf.multiply(loss_map, weight_map)
loss = tf.reduce_mean(weighted_loss)
return loss
def mask_similarity_matrix(similarity_matrix, mask_a, mask_b):
"""
Given the mask of the two sentences, apply the mask to the similarity
matrix.
Parameters
----------
similarity_matrix: Tensor
Tensor of shape (batch_size, num_sentence_words, num_sentence_words).
mask_a: Tensor
Tensor of shape (batch_size, num_sentence_words). This mask should
correspond to the first vector (v1) used to calculate the similarity
matrix.
mask_b: Tensor
Tensor of shape (batch_size, num_sentence_words). This mask should
correspond to the second vector (v2) used to calculate the similarity
matrix.
"""
similarity_matrix = tf.multiply(similarity_matrix,
tf.expand_dims(tf.cast(mask_a, "float"), 1))
similarity_matrix = tf.multiply(similarity_matrix,
tf.expand_dims(tf.cast(mask_b, "float"), 2))
return similarity_matrix
def SampleRandomFrames(model_input, num_frames, num_samples):
"""Samples a random set of frames of size num_samples.
Args:
model_input: A tensor of size batch_size x max_frames x feature_size
num_frames: A tensor of size batch_size x 1
num_samples: A scalar
Returns:
`model_input`: A tensor of size batch_size x num_samples x feature_size
"""
batch_size = tf.shape(model_input)[0]
frame_index = tf.cast(
tf.multiply(
tf.random_uniform([batch_size, num_samples]),
tf.tile(tf.cast(num_frames, tf.float32), [1, num_samples])), tf.int32)
batch_index = tf.tile(
tf.expand_dims(tf.range(batch_size), 1), [1, num_samples])
index = tf.stack([batch_index, frame_index], 2)
return tf.gather_nd(model_input, index)
def create_training_batch(serialized_example, cfg, add_summaries):
features = get_region_data(serialized_example, cfg, fetch_ids=False,
fetch_labels=True, fetch_text_labels=False)
original_image = features['image']
bboxes = features['bboxes']
labels = features['labels']
distorted_inputs = get_distorted_inputs(original_image, bboxes, cfg, add_summaries)
distorted_inputs = tf.subtract(distorted_inputs, 0.5)
distorted_inputs = tf.multiply(distorted_inputs, 2.0)
names = ('inputs', 'labels')
tensors = [distorted_inputs, labels]
return [names, tensors]
def create_classification_batch(serialized_example, cfg, add_summaries):
features = get_region_data(serialized_example, cfg, fetch_ids=True,
fetch_labels=False, fetch_text_labels=False)
original_image = features['image']
bboxes = features['bboxes']
ids = features['ids']
distorted_inputs = get_distorted_inputs(original_image, bboxes, cfg, add_summaries)
distorted_inputs = tf.subtract(distorted_inputs, 0.5)
distorted_inputs = tf.multiply(distorted_inputs, 2.0)
names = ('inputs', 'ids')
tensors = [distorted_inputs, ids]
return [names, tensors]
def test_two_ops_network(self):
shape = loom.TypeShape('int64', (3,))
ops = {'add': BinaryLoomOp(shape, tf.add),
'mul': BinaryLoomOp(shape, tf.multiply)}
the_loom = loom.Loom(named_ops=ops)
output_tensor = the_loom.output_tensor(shape)
with self.test_session():
weaver = the_loom.make_weaver()
c1 = weaver(np.array([1, 2, 3], dtype='int64'))
c2 = weaver(np.array([2, 4, 6], dtype='int64'))
c3 = weaver(np.array([3, 6, 9], dtype='int64'))
sum_2_3 = weaver.add(c2, c3)
sum_12_13 = weaver.mul(c1, sum_2_3)
result = output_tensor.eval(
feed_dict=weaver.build_feed_dict([sum_12_13]))
self.assertTrue((result == np.array([[5, 20, 45]], dtype='int64')).all())
def test_two_ops_network_tagged_named_tensorx(self):
shape = loom.TypeShape('int64', (3,), tag='x')
ops = {'add': BinaryLoomOp(shape, tf.add),
'mul': BinaryLoomOp(shape, tf.multiply)}
named_tensors = {
'c1': (tf.constant(np.array([1, 2, 3], dtype='int64')), 'x'),
'c2': (tf.constant(np.array([2, 4, 6], dtype='int64')), 'x'),
'c3': (tf.constant(np.array([3, 6, 9], dtype='int64')), 'x')
}
the_loom = loom.Loom(named_ops=ops, named_tensors=named_tensors)
output_tensor = the_loom.output_tensor(shape)
with self.test_session():
weaver = the_loom.make_weaver()
sum_2_3 = weaver.add(weaver.c2, weaver.c3)
sum_12_13 = weaver.mul(weaver.c1, sum_2_3)
result = output_tensor.eval(
feed_dict=weaver.build_feed_dict([sum_12_13]))
self.assertTrue((result == np.array([[5, 20, 45]], dtype='int64')).all())
def test_gradient(self):
x_var = tf.Variable(tf.zeros([3], dtype='float64'), name='x')
shape = loom.TypeShape('float64', (3,))
ops = {'add': BinaryLoomOp(shape, tf.add),
'mul': BinaryLoomOp(shape, tf.multiply)}
the_loom = loom.Loom(named_tensors={'x': x_var}, named_ops=ops)
output_tensor = the_loom.output_tensor(shape)
output = tf.reduce_sum(output_tensor)
gradient = tf.gradients(output, [x_var])[0]
with self.test_session() as sess:
sess.run(tf.global_variables_initializer())
weaver = the_loom.make_weaver()
m = weaver(np.array([1, 2, 3], dtype='float64'))
b = weaver(np.array([47, 9, -1], dtype='float64'))
mx = weaver.mul(m, weaver.x)
mx_plus_b = weaver.add(mx, b)
result = gradient.eval(feed_dict=weaver.build_feed_dict([mx_plus_b]))
self.assertTrue((result == np.array(
[1.0, 2.0, 3.0], dtype='float64')).all())
def test_gradient_with_direct_feed_dict(self):
x_var = tf.Variable(tf.zeros([3], dtype='float64'), name='x')
shape = loom.TypeShape('float64', (3,))
ops = {'add': BinaryLoomOp(shape, tf.add),
'mul': BinaryLoomOp(shape, tf.multiply)}
the_loom = loom.Loom(named_tensors={'x': x_var}, named_ops=ops,
direct_feed_dict=True)
output_tensor = the_loom.output_tensor(shape)
output = tf.reduce_sum(output_tensor)
gradient = tf.gradients(output, [x_var])[0]
with self.test_session() as sess:
sess.run(tf.global_variables_initializer())
weaver = the_loom.make_weaver()
m = weaver(np.array([1, 2, 3], dtype='float64'))
b = weaver(np.array([47, 9, -1], dtype='float64'))
mx = weaver.mul(m, weaver.x)
mx_plus_b = weaver.add(mx, b)
result = gradient.eval(feed_dict=weaver.build_feed_dict([mx_plus_b]))
self.assertTrue((result == np.array(
[1.0, 2.0, 3.0], dtype='float64')).all())
def _variable_with_weight_decay(name, shape, stddev, wd):
"""Helper to create an initialized Variable with weight decay.
Note that the Variable is initialized with a truncated normal distribution.
A weight decay is added only if one is specified.
Args:
name: name of the variable
shape: list of ints
stddev: standard deviation of a truncated Gaussian
wd: add L2Loss weight decay multiplied by this float. If None, weight
decay is not added for this Variable.
Returns:
Variable Tensor
"""
var = _variable_on_cpu(
name,
shape,
tf.truncated_normal_initializer(stddev=stddev, dtype=tf.float32))
if wd is not None:
weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss')
tf.add_to_collection('losses', weight_decay)
return var
def dense(x, size, name, weight_init=None, bias_init=0, weight_loss_dict=None, reuse=None):
with tf.variable_scope(name, reuse=reuse):
assert (len(U.scope_name().split('/')) == 2)
w = tf.get_variable("w", [x.get_shape()[1], size], initializer=weight_init)
b = tf.get_variable("b", [size], initializer=tf.constant_initializer(bias_init))
weight_decay_fc = 3e-4
if weight_loss_dict is not None:
weight_decay = tf.multiply(tf.nn.l2_loss(w), weight_decay_fc, name='weight_decay_loss')
if weight_loss_dict is not None:
weight_loss_dict[w] = weight_decay_fc
weight_loss_dict[b] = 0.0
tf.add_to_collection(U.scope_name().split('/')[0] + '_' + 'losses', weight_decay)
return tf.nn.bias_add(tf.matmul(x, w), b)
def SampleRandomFrames(model_input, num_frames, num_samples):
"""Samples a random set of frames of size num_samples.
Args:
model_input: A tensor of size batch_size x max_frames x feature_size
num_frames: A tensor of size batch_size x 1
num_samples: A scalar
Returns:
`model_input`: A tensor of size batch_size x num_samples x feature_size
"""
batch_size = tf.shape(model_input)[0]
frame_index = tf.cast(
tf.multiply(
tf.random_uniform([batch_size, num_samples]),
tf.tile(tf.cast(num_frames, tf.float32), [1, num_samples])), tf.int32)
batch_index = tf.tile(
tf.expand_dims(tf.range(batch_size), 1), [1, num_samples])
index = tf.stack([batch_index, frame_index], 2)
return tf.gather_nd(model_input, index)