[英]How do you concatenate tensorflow tensors along axis 0 while preserving the shape of the other n>0 dimensions
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)
.我的目标是获取shape(1, 2, ...n)
的张量列表,并将它们连接成shape(len(list), 1, 2, ..., n)
的张量。
tf.concat(list, -1)
does not work. tf.concat(list, -1)
不起作用。 It returns shape(1, 2, ..., n-1*n)
, which is understandable.它返回shape(1, 2, ..., n-1*n)
,这是可以理解的。
tf.concat(list, 0)
does not work. tf.concat(list, 0)
不起作用。 It return shape(1*2, ..., n)
which I do not want.它返回我不想要的shape(1*2, ..., n)
。 I tried to take this intermediate and use features = tf.reshape(f, [len(list)])
, but I get one of two exceptions.我尝试使用这个中间体并使用features = tf.reshape(f, [len(list)])
,但我得到了两个例外之一。
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.我试过使用features = tf.reshape(f, [len(list), -1])
和 get shape(len(list), 1, 2, n-1*n)
这也是错误的,但可以理解。
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我唯一能想到的就是复制这样的形状, tf.shape([len(list), list[0].shape])
,但这会导致错误
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?有没有办法做到这一点,而无需通过形状的 n 个维度对 go 进行 hacky 循环?
Seems hacky, but this works看起来很老套,但这行得通
if time_features is not None:
s = [len(time_features)]
for i in time_features[0].shape[:]:
s.append(i)
f = tf.concat(time_features, 0)
features = tf.reshape(f, s)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.