将一个 4D 张量重塑为维度为 None 的 2D 张量

Reshape one 4D-tensor into 2D whose dimension is None

我有一个关于 tf.reshape

的问题

例如一个张量 t1,其形状为 [None, h, c, w]。我想将张量重塑为二维,就像:

shape = t1.get_shape().as_list()
t2 = tf.reshape(t1, [shape[0]*shape[1], shape[2]*shape[3]])

然而,t1的第一个维度是None。

我该如何处理这个案例,有什么建议吗?

你可以这样做:

shape = tf.shape(t1)
t2 = tf.reshape(t1, [-1, shape[2]*shape[3]])