在 Tensorflow 中删除张量的维度
Drop a dimension of a tensor in Tensorflow
我有一个形状为 (50, 100, 1, 512)
的张量,我想重塑它或删除第三维,以便新张量的形状为 (50, 100, 512)
。
我已经尝试 tf.slice
和 tf.squeeze
:
a = tf.slice(a, [50, 100, 1, 512], [50, 100, 1, 512])
b = tf.squeeze(a)
当我尝试打印 a
和 b
的形状时,一切似乎都正常,但是当我开始训练我的模型时,出现了这个错误
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)]]
我的 slice
有什么问题吗?我该如何解决。谢谢
有多种方法可以做到这一点。 Tensorflow 已经开始支持索引。尝试
a = a[:,:,0,:]
或
a = a[:,:,-1,:]
或
a = tf.reshape(a,[50,100,512])
我这里用的tf.slice
错了,应该是这样的:
a = tf.slice(a, [0, 0, 0, 0], [50, 100, 1, 512])
b = tf.squeeze(a)
您可以通过查看 tf.slice
documentation
找出原因
通常 tf.squeeze
会降低维度。
a = tf.constant([[[1,2,3],[3,4,5]]])
上面的张量形状是[1,2,3]
。执行挤压操作后,
b = tf.squeeze(a)
现在,Tensor 的形状是 [2,3]
我有一个形状为 (50, 100, 1, 512)
的张量,我想重塑它或删除第三维,以便新张量的形状为 (50, 100, 512)
。
我已经尝试 tf.slice
和 tf.squeeze
:
a = tf.slice(a, [50, 100, 1, 512], [50, 100, 1, 512])
b = tf.squeeze(a)
当我尝试打印 a
和 b
的形状时,一切似乎都正常,但是当我开始训练我的模型时,出现了这个错误
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)]]
我的 slice
有什么问题吗?我该如何解决。谢谢
有多种方法可以做到这一点。 Tensorflow 已经开始支持索引。尝试
a = a[:,:,0,:]
或
a = a[:,:,-1,:]
或
a = tf.reshape(a,[50,100,512])
我这里用的tf.slice
错了,应该是这样的:
a = tf.slice(a, [0, 0, 0, 0], [50, 100, 1, 512])
b = tf.squeeze(a)
您可以通过查看 tf.slice
documentation
通常 tf.squeeze
会降低维度。
a = tf.constant([[[1,2,3],[3,4,5]]])
上面的张量形状是[1,2,3]
。执行挤压操作后,
b = tf.squeeze(a)
现在,Tensor 的形状是 [2,3]