如何解决连接两个EagerTensor
我有两个类型的EagerTensor:
<class 'tensorflow.python.framework.ops.EagerTensor'>
size(64,100)的tensor1和size(64,10)的tensor2。 我希望输出尺寸为(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
但不起作用,请帮助。
解决方法
在连接时,您需要确保两个张量具有相同的类型。将uint8强制转换为浮点(更有可能是您想要的),反之亦然(尽管将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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。