python - TypeError:无法将 0.0 转换为 dtype int32 的 EagerTensor
问题描述
我正在研究 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
解决方案
我对以下消息有同样的问题
类型错误:无法将 1e-07 转换为 dtype uint8 的 EagerTensor
尝试将人声转换为所需的数据类型np.float32
,因为它询问Cannot convert 0.0 to EagerTensor of dtype int32
我认为您的人声数据类型在哪里int32
。
推荐阅读
- python - 将图层导出到 sde
- c++ - 为什么ios::trunc文件访问模式在我想打开文件时在ifstream类中不起作用?
- javascript - 如何在 JSON 中获取第二个密钥
- c# - UWP - MVVM - 计算的属性值更改时未更新值
- c# - 从文本框保存数据
- c# - DB2 连接字符串密码加密
- solr - 在 solr 中添加 CustomSimilarity 类
- c# - 在 Remove-Migration 和 Update-Database 之后删除 .cs 迁移文件是否安全
- listview - Flutter中如何根据视口高度约束ListView
- java - Swing - 调整大小时 GridBagLayout 中的 JScollArea 折叠