How to reshape a tensor with multiple `None` dimensions?

醉酒当歌 提交于 2020-01-13 11:13:59

问题


I encountered a problem to reshape an intermediate 4D tensorflow tensor X to a 3D tensor Y, where

  • X is of shape ( batch_size, nb_rows, nb_cols, nb_filters )
  • Y is of shape ( batch_size, nb_rows*nb_cols, nb_filters )
  • batch_size = None

Of course, when nb_rows and nb_cols are known integers, I can reshape X without any problem. However, in my application I need to deal with the case

nb_rows = nb_cols = None

What should I do? I tried Y = tf.reshape( X, (-1, -1, nb_filters)) but it clearly fails to work.

For me, this operation is deterministic because it always squeezes the two middle axes into a single one while keeping the first axis and the last axis unchanged. Can anyone help me?


回答1:


In this case you can access to the dynamic shape of X through tf.shape(X):

shape = [ tf.shape(X)[k] for k in range(4)].
Y = tf.reshape(X , [shape[0], shape[1]*shape[2], shape[3]])


来源:https://stackoverflow.com/questions/47026042/how-to-reshape-a-tensor-with-multiple-none-dimensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!