Skip to content Skip to sidebar Skip to footer

How To Add Dimension To A Tensor Using Tensorflow

I have method reformat in which using numpy I convert a label(256,) to label(256,2) shape. Now I want to do same operation on a Tensor with shape (256,) My code looks like this (n

Solution 1:

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

In [1]: import tensorflow as tf    
        x = tf.constant([3., 2.])
        tf.expand_dims(x, 1).shape

Out[1]: TensorShape([Dimension(2), Dimension(1)])

You can also use tf.reshape() for this, but would recommend you to use expand_dims, as this will also carry some values to new dimension if new shape can be satisfied.

In [1]: tf.reshape(x, [2, 1])
Out[1]: TensorShape([Dimension(2), Dimension(1)])

Post a Comment for "How To Add Dimension To A Tensor Using Tensorflow"