首页 > 解决方案 > 如何在python中改变张量形状?

问题描述

假设我在 Tensorflow 中有一个 shape 的张量[3,4,4],它是未知的。我怎样才能改变它的形状[3,4,4,1]
我尝试使用 reshape 函数,但因为我不明确知道张量形状,所以不能直接使用它。

标签: pythontensorflow

解决方案


Given t of shape [3,4,4], it can be reshape to [3,4,4, 1]

tf.reshape(t, [3,4,4, 1]) # by explicitly specifying the shape
tf.expandims(t, 3) # implicitly we expand the dimension 3 with size 1

推荐阅读