Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot convert 0.0 to EagerTensor of dtype int32

I'm studying TensorFlow 2.0 and Transformer, I have an error as title when I type

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

vocals is a tensor with shape of (100, 45). The code for this layer is:

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
like image 307
vincent Avatar asked Mar 19 '26 16:03

vincent


1 Answers

I have this same problem with the following message

Type error: cannot convert 1e-07 to EagerTensor of dtype uint8

Try to convert the vocals to required data type np.float32 as it is asking Cannot convert 0.0 to EagerTensor of dtype int32 where I believe your data type of vocals is int32.

like image 113
Rodo-Singh Avatar answered Mar 21 '26 05:03

Rodo-Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!