Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)]
like image 574
417mm Avatar asked Jan 05 '17 10:01

417mm


People also ask

What does TF squeeze do?

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.

What is TF Where?

tf. where will return the indices of condition that are non-zero, in the form of a 2-D tensor with shape [n, d] , where n is the number of non-zero elements in condition ( tf. count_nonzero(condition) ), and d is the number of axes of condition ( tf. rank(condition) ).

What is TF constant?

tf. constant is useful for asserting that the value can be embedded that way. If the argument dtype is not specified, then the type is inferred from the type of value . # Constant 1-D Tensor from a python list. tf.

How do you flatten a tensor in TensorFlow?

To flatten the tensor, we're going to use the TensorFlow reshape operation. So tf. reshape, we pass in our tensor currently represented by tf_initial_tensor_constant, and then the shape that we're going to give it is a -1 inside of a Python list.


2 Answers

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)
like image 134
Vikas Sharma Avatar answered Oct 17 '22 07:10

Vikas Sharma


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.

like image 35
mrry Avatar answered Oct 17 '22 08:10

mrry