【问题标题】:Concatenate two EagerTensor连接两个 EagerTensor
【发布时间】:2020-09-16 09:36:42
【问题描述】:

我有两个 EagerTensor 类型:

<class 'tensorflow.python.framework.ops.EagerTensor'> 

尺寸为(64, 100) 的张量1 和尺寸为(64, 10) 的张量2。 我希望输出在维度 (64, 110) 中。 我试过了:

tf.concat(axis=1, values = [tensor1, tensor2])

遇到错误:

InvalidArgumentError: cannot compute ConcatV2 as input #1(zero-based) was expected to be a uint8 tensor but is a float tensor [Op:ConcatV2] name: concat

但不起作用,请帮助。

【问题讨论】:

  • 明白了,谢谢@GPhilo

标签: python tensorflow tensor


【解决方案1】:

连接时需要确保两个张量具有相同的类型。将 uint8 转换为 float(更可能是您想要的),反之亦然(尽管将 float 转换为 uint8 可能不会给您预期的结果)

import tensorflow as tf
import numpy as np

tf.compat.v1.enable_eager_execution()

tensor1 = tf.ones([64, 10], dtype=tf.uint8)
tensor2 = tf.ones([64, 100], dtype=tf.float32)
# print(type(x))            # <type 'EagerTensor'>

tf.concat(axis=1, values = [tf.cast(tensor1, tf.float32), tensor2]) # <<<< note the cast

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 2016-11-18
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多