What do the functions tf.squeeze and tf.nn.rnn do?

tf.squeeze removes deimesion whose size is "1".Below example will show use of tf.squeeze.

import tensorflow as tf
tf.enable_eager_execution() ##if using TF1.4 for TF2.0 eager mode is the default mode.
####example 1
a = tf.constant(value=[1,3,4,5],shape=(1,4))
print(a)
Output : tf.Tensor([[1 3 4 5]], shape=(1, 4), dtype=int32)

#after applying tf.squeeze shape has been changed from  (4,1) to (4, )
b = tf.squeeze(input=a)
print(b)
output: tf.Tensor([1 3 4 5], shape=(4,), dtype=int32)
####example2
a = tf.constant(value=[1,3,4,5,4,6], shape=(3,1,2))
print(a)
Output:tf.Tensor(
[[[1 3]]
 [[4 5]]
 [[4 6]]], shape=(3, 1, 2), dtype=int32)

#after applying tf.squeeze shape has been chnaged from (3, 1, 2) to (3, 2)
b = tf.squeeze(input=a)
print(b)
Output:tf.Tensor(
[[1 3]
 [4 5]
 [4 6]], shape=(3, 2), dtype=int32)

The best source of answers to questions like these is the TensorFlow API documentation. The two functions you mentioned create operations and symbolic tensors in a dataflow graph. In particular:

  • The tf.squeeze() function returns a tensor with the same value as its first argument, but a different shape. It removes dimensions whose size is one. For example, if t is a tensor with shape [batch_num, 1, elem_num] (as in your question), tf.squeeze(t, [1]) will return a tensor with the same contents but size [batch_num, elem_num].

  • The tf.nn.rnn() function returns a pair of results, where the first element represents the outputs of a recurrent neural network for some given input, and the second element represents the final state of that network for that input. The TensorFlow website has a tutorial on recurrent neural networks with more details.