如何沿轴 0 连接张量流张量,同时保留其他 n>0 维度的形状
我的目标是获取 shape(1, 2, ...n)
张量列表并将它们连接成 shape(len(list), 1, 2, 。 ..,n)
。
tf.concat(list, -1)
不起作用。它返回 shape(1, 2, ..., n-1*n)
,这是可以理解的。
tf.concat(list, 0)
不起作用。它返回我不想要的 shape(1*2, ..., n) 。我尝试采用此中间方法并使用features = tf.reshape(f, [len(list)]) ,但我遇到了两个异常之一。
tensorflow.python.framework.errors_impl.InvalidArgumentError:OpKernel'ConcatV2'对attr'T'有约束,不在NodeDef'[N = 0,Tidx = DT_INT32]'中,KernelDef:'op:“ConcatV2”device_type:“ CPU”约束{名称:“T”allowed_values {列表{类型:DT_QINT32} host_memory_arg: "axis"' [Op:ConcatV2] name: concat
或类似的东西
tensorflow.python.framework.errors_impl.InvalidArgumentError: 输入重塑是一个具有 120 个值的张量,但请求的shape 有 2 个 [Op:Reshape]
我尝试过使用 features = tf.reshape(f, [len(list), -1])
并得到 shape(len(list), 1, 2, n-1*n)
这也是错误的,但可以理解。
我唯一能想到的就是复制这样的形状,tf.shape([len(list), list[0].shape])
,但这会导致错误
ValueError:无法将具有混合类型的 Python 序列转换为张量。
我现在尝试了
f = tf.concat(list, 0)
f = tf.expand_dims(f, 0)
features = tf.reshape(f, [len(list)])
,但仍然收到错误
是否有某种方法可以做到这一点,而无需制作一个 hacky 循环来遍历形状的 n 维?
My goal is to take a list of tensors of shape(1, 2, ...n)
and concatenate them into a tensor of shape(len(list), 1, 2, ..., n)
.
tf.concat(list, -1)
does not work. It returns shape(1, 2, ..., n-1*n)
, which is understandable.
tf.concat(list, 0)
does not work. It return shape(1*2, ..., n)
which I do not want. I tried to take this intermediate and use features = tf.reshape(f, [len(list)])
, but I get one of two exceptions.
tensorflow.python.framework.errors_impl.InvalidArgumentError: OpKernel 'ConcatV2' has constraint on attr 'T' not in NodeDef '[N=0, Tidx=DT_INT32]', KernelDef: 'op: "ConcatV2" device_type: "CPU" constraint { name: "T" allowed_values { list { type: DT_QINT32 } } } host_memory_arg: "axis"' [Op:ConcatV2] name: concat
or something like this
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 120 values, but the requested shape has 2 [Op:Reshape]
I have tried using features = tf.reshape(f, [len(list), -1])
and get shape(len(list), 1, 2, n-1*n)
which is also wrong, but understandable.
Only other thing I can think of is copying the shape like this, tf.shape([len(list), list[0].shape])
, but that leads to error
ValueError: Can't convert Python sequence with mixed types to Tensor.
I now tried
f = tf.concat(list, 0)
f = tf.expand_dims(f, 0)
features = tf.reshape(f, [len(list)])
and still get an error
Is there some way to do this without making a hacky loop to go through the n dimensions of the shape?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来很老套,但这很有效
Seems hacky, but this works