Drop a dimension of a tensor in Tensorflow

15,065

Solution 1

Generally tf.squeeze will drop the dimensions.

a = tf.constant([[[1,2,3],[3,4,5]]])

The above tensor shape is [1,2,3]. After performing squeeze operation,

b = tf.squeeze(a)

Now, Tensor shape is [2,3]

Solution 2

There are multiple ways to do it. Tensorflow has started supporting indexing. Try

a = a[:,:,0,:]

OR

a = a[:,:,-1,:]

OR

a = tf.reshape(a,[50,100,512])

Solution 3

I use the tf.slice wrong in this case, it's should be like this:

a = tf.slice(a, [0, 0, 0, 0], [50, 100, 1, 512])
b = tf.squeeze(a)

You can find out why by look at the tf.slice documentation

Share:
15,065
lamhoangtung
Author by

lamhoangtung

Hey! 👋 I’m an AI Researcher/Engineer at Techainer, which provides various AI solutions for businesses. My work is mostly about Computer Vision, AIOps, and Performance Optimization.

Updated on June 21, 2022

Comments

  • lamhoangtung
    lamhoangtung almost 2 years

    I have a tensor that have shape (50, 100, 1, 512) and i want to reshape it or drop the third dimension so that the new tensor have shape (50, 100, 512).

    I have tried tf.slice with tf.squeeze:

    a = tf.slice(a, [50, 100, 1, 512], [50, 100, 1, 512])
    b = tf.squeeze(a)
    

    Everything seem working when i tried to print the shape of a and b but when i start training my model this error came

    tensorflow.python.framework.errors_impl.InvalidArgumentError: Expected size[0] in [0, 0], but got 50
         [[Node: Slice = Slice[Index=DT_INT32, T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"](MaxPool_2, Slice/begin, Slice/size)]]
    

    Are there any problem with my slice. How can i fix it. Thanks

  • xdurch0
    xdurch0 over 5 years
    Why even use slice at all? Simply using squeeze should do the job.
  • lamhoangtung
    lamhoangtung over 5 years
    @xdurch0 I thought squeeze only remove dim with size = 1