Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: When use tf.expand_dims?

Tags:

tensorflow

Tensorflow tutorials include the use of tf.expand_dims to add a "batch dimension" to a tensor. I have read the docs for this function but it still is rather mysterious to me. Does anyone know exactly under what circumstances this must be used?

My code is below. My intent is to calculate a loss based on the distance between the predicted and actual bins. (E.g. if predictedBin = 10 and truthBin = 7 then binDistanceLoss = 3).

batch_size = tf.size(truthValues_placeholder) labels = tf.expand_dims(truthValues_placeholder, 1) predictedBin = tf.argmax(logits) binDistanceLoss = tf.abs(tf.sub(labels, logits)) 

In this case, do I need to apply tf.expand_dims to predictedBin and binDistanceLoss? Thanks in advance.

like image 204
Ron Cohen Avatar asked Aug 18 '16 01:08

Ron Cohen


People also ask

What is the use of TF Expand_dims?

expand_dims() TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning neural networks. expand_dims() is used to insert an addition dimension in input Tensor.

Why do we need Expand_dims?

The expand_dims() function in NumPy is used to expand the shape of an input array that is passed to it. This operation is done in such a way that when a new axis is inserted, it appears in the axis position of the resulting expanded array shape. Axis in NumPy is defined for arrays that have more than one dimension.

How do I add one dimension in Tensorflow?

You can use tf. expand_dims() to add a new dimension.

What is rank in Tensorflow?

The rank of a tensor is the number of indices required to uniquely select each element of the tensor. Rank is also known as "order", "degree", or "ndims."


2 Answers

expand_dims will not add or reduce elements in a tensor, it just changes the shape by adding 1 to dimensions. For example, a vector with 10 elements could be treated as a 10x1 matrix.

The situation I have met to use expand_dims is when I tried to build a ConvNet to classify grayscale images. The grayscale images will be loaded as matrix of size [320, 320]. However, tf.nn.conv2d require input to be [batch, in_height, in_width, in_channels], where the in_channels dimension is missing in my data which in this case should be 1. So I used expand_dims to add one more dimension.

In your case, I do not think you need expand_dims.

like image 173
Da Tong Avatar answered Sep 24 '22 12:09

Da Tong


To add to Da Tong's answer, you may want to expand more than one dimension at the same time. For instance, if you are performing TensorFlow's conv1d operation on vectors of rank 1, you need to feed them with rank three.

Performing expand_dims several times is readable, but might introduce some overhead into the computational graph. You can get the same functionality in a one-liner with reshape:

import tensorflow as tf  # having some tensor of rank 1, it could be an audio signal, a word vector... tensor = tf.ones(100) print(tensor.get_shape()) # => (100,)  # expand its dimensionality to fit into conv2d tensor_expand = tf.expand_dims(tensor, 0) tensor_expand = tf.expand_dims(tensor_expand, 0) tensor_expand = tf.expand_dims(tensor_expand, -1) print(tensor_expand.get_shape()) # => (1, 1, 100, 1)  # do the same in one line with reshape tensor_reshape = tf.reshape(tensor, [1, 1, tensor.get_shape().as_list()[0],1]) print(tensor_reshape.get_shape()) # => (1, 1, 100, 1) 

NOTE: In case you get the error TypeError: Failed to convert object of type <type 'list'> to Tensor., try to pass tf.shape(x)[0] instead of x.get_shape()[0] as suggested here.

Hope it helps!
Cheers,
Andres

like image 26
fr_andres Avatar answered Sep 25 '22 12:09

fr_andres