【问题标题】:TypeError: Cannot convert 0.0 to EagerTensor of dtype int32TypeError:无法将 0.0 转换为 dtype int32 的 EagerTensor
【发布时间】:2021-01-03 16:59:33
【问题描述】:

我正在学习 TensorFlow 2.0 和 Transformer,我在输入时出现标题错误

value = Embedding(tf.shape(vocals).numpy()[0], d_model=512)

vocals 是一个形状为 (100, 45) 的张量。 这一层的代码是:

def positional_encoding(length, d_model):
    encoded_vec = tf.Variable([pos/tf.pow(10000, 2*i/d_model) for pos in range(length) for i in range(d_model)],
                             dtype=tf.float32)
    encoded_vec[::2] = tf.sin(encoded_vec[::2])
    encoded_vec[1::2] = tf.cos(encoded_vec[1::2])

    return encoded_vec.reshape([sentence_length, dim])

class Embedding(tf.keras.layers.Layer):
    def __init__(self, vocab_size, d_model, dropout=0.1):
        super().__init__()
        self.d_model = d_model
        self.token_embedding = tf.keras.layers.Embedding(vocab_size, d_model)
        self.positional_encoding = positional_encoding(vocab_size, d_model)
        self.dropout = tf.keras.layers.Dropout(dropout)

    def call(self, x):
        seq_len = tf.shape(x)[1]
        x = self.token_embedding(x)
        x *= tf.math.sqrt(tf.cast(self.d_model, tf.float32))
        x += self.positional_encoding[:, :seq_len, :]
        x = self.dropout(x)
        return x

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    下面的消息我也有同样的问题

    类型错误:无法将 1e-07 转换为 dtype uint8 的 EagerTensor

    尝试将人声转换为所需的数据类型np.float32,因为它询问Cannot convert 0.0 to EagerTensor of dtype int32,我认为您的人声数据类型是int32

    【讨论】:

      【解决方案2】:

      改变

      encoded_vec = tf.Variable([pos/tf.pow(10000, 2*i/d_model) for pos in range(length) for i in range(d_model)],
                                   dtype=tf.float32)
      

      encoded_vec = np.array([pos/10000 ** (2*i/d_model) for pos in range(length) for i in range(d_model)],
                                dtype=tf.float32)
      

      【讨论】:

      • 那么为什么会这样呢?您是否能够弄清楚为什么此更改解决了问题?
      猜你喜欢
      • 2021-12-01
      • 2021-10-25
      • 2021-09-13
      • 1970-01-01
      • 2018-06-11
      • 2020-06-06
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      相关资源
      最近更新 更多