def time_distributed_linear(inputs: tf.Tensor,
output_size: int,
weight_initializer: Optional[Initializer] = None,
bias_initializer: Optional[Initializer] = None,
name: str = "time_dist_linear") -> tf.Tensor:
"""
Applies the same linear transformation to all time steps of a sequence.
Parameters
----------
inputs: tf.Tensor
The input sequences, of shape [max_time, batch_size, num_features]
output_size: int
The desired number of features in the output sequences
weight_initializer: tf.Initializer, optional
A custom initializer for the weight matrix of the linear transformation
bias_initializer: tf.Initializer, optional
A custom initializer for the bias vector of the linear transformation
name: str, optional
A name for the operation (default "time_dist_linear")
Returns
-------
tf.Tensor
The linearly transformed input sequences, of shape [max_time, batch_size, output_size]
"""
max_time, batch_size, _ = tf.unstack(tf.shape(inputs))
static_shape = inputs.shape.as_list()
with tf.variable_scope(name):
result = flatten_time(inputs)
result = linear(result,
output_size=output_size,
weight_initializer=weight_initializer,
bias_initializer=bias_initializer)
result = restore_time(result, max_time, batch_size, output_size)
result.set_shape([static_shape[0], static_shape[1], output_size])
return result
评论列表
文章目录