What do the functions tf.squeeze and tf.nn.rnn do?
I searched these API, but I can't find argument, examples etc. Also, what is the shape of p_inputs formed by the following code using tf.squeeze, and what is the meaning and case of using tf.nn.rnn?
batch_num = 10 step_num = 2000 elem_num = 26 p_input = tf.placeholder(tf.float32, [batch_num, step_num, elem_num]) p_inputs = [tf.squeeze(t, [1]) for t in tf.split(1, step_num, p_input)] 02 Answers
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, iftis 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.
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)