【问题标题】:tf.concat tensors with different lengthtf.concat 不同长度的张量
【发布时间】:2022-01-10 22:55:19
【问题描述】:

我有 2 个张量,例如:

a = tf.constant([[1, 2, 3], [1, 2, 3]])
b = tf.constant([1, 2, 3, 4, 5])

我想要的输出是:

<tf.Tensor: shape=(4, 2), dtype=int64, numpy=
 array([[1, 2, 3, 0, 0],
        [1, 2, 3, 0, 0],
        [1, 2, 3, 4, 5]], dtype=int64)>

但是当我尝试tf.concat([a, b], axis=0) 时,我得到了这个错误:

InvalidArgumentError: ConcatOp : Dimensions of inputs should match: shape[0] = [2,3] vs. shape[1] = [1,5] [Op:ConcatV2] name: concat

【问题讨论】:

标签: python tensorflow tensorflow2.0


【解决方案1】:

试试这个:

a = tf.constant([[1, 2, 3], [1, 2, 3]])
b = tf.constant([1, 2, 3, 4, 5])

c = tf.concat([tf.pad(a, tf.constant([[0,0], [0,2]])), tf.expand_dims(b, axis=0)], axis=0)
tf.print(c)

[[1 2 3 0 0]
 [1 2 3 0 0]
 [1 2 3 4 5]]

【讨论】:

    猜你喜欢
    • 2018-05-21
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 2020-06-06
    相关资源
    最近更新 更多