简体   繁体   中英

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) .

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM