tensorflow - 运行 TensorFlow Transformer 教程时出现问题
问题描述
我正在检查 TensorFlow 教程“用于语言理解的转换器模型”,并将代码完全原样复制到我的 Spyder 4 环境中。但是,代码在运行时显示以下错误:
AttributeError: 'RepeatedCompositeFieldContainer' object has no attribute 'append'
我检查了代码,发现错误来自MultiHeadAttention类的调用函数。但是,我不明白问题出在哪里,因为代码在 Colab 笔记本中运行得很好。
class MultiHeadAttention(tf.keras.layers.Layer):
def __init__(self, d_model, num_heads):
super(MultiHeadAttention, self).__init__()
self.num_heads = num_heads
self.d_model = d_model
assert d_model % self.num_heads == 0
self.depth = d_model // self.num_heads
self.wq = tf.keras.layers.Dense(d_model)
self.wk = tf.keras.layers.Dense(d_model)
self.wv = tf.keras.layers.Dense(d_model)
self.dense = tf.keras.layers.Dense(d_model)
def split_heads(self, x, batch_size):
"""Split the last dimension into (num_heads, depth).
Transpose the result such that the shape is (batch_size, num_heads, seq_len, depth)
"""
x = tf.reshape(x, (batch_size, -1, self.num_heads, self.depth))
return tf.transpose(x, perm=[0, 2, 1, 3])
def call(self, v, k, q, mask):
batch_size = tf.shape(q)[0]
q = self.wq(q) # (batch_size, seq_len, d_model)
k = self.wk(k) # (batch_size, seq_len, d_model)
v = self.wv(v) # (batch_size, seq_len, d_model)
q = self.split_heads(q, batch_size) # (batch_size, num_heads, seq_len_q, depth)
k = self.split_heads(k, batch_size) # (batch_size, num_heads, seq_len_k, depth)
v = self.split_heads(v, batch_size) # (batch_size, num_heads, seq_len_v, depth)
# scaled_attention.shape == (batch_size, num_heads, seq_len_q, depth)
# attention_weights.shape == (batch_size, num_heads, seq_len_q, seq_len_k)
scaled_attention, attention_weights = scaled_dot_product_attention(
q, k, v, mask)
scaled_attention = tf.transpose(scaled_attention, perm=[0, 2, 1, 3]) # (batch_size, seq_len_q,
num_heads, depth)
concat_attention = tf.reshape(scaled_attention,
(batch_size, -1, self.d_model)) # (batch_size, seq_len_q, d_model)
output = self.dense(concat_attention) # (batch_size, seq_len_q, d_model)
return output, attention_weights
q = self.wq(q)
执行call 函数中的行时显示错误。任何帮助将不胜感激。
提前致谢。
解决方案
我怀疑问题是你的protobuf
Python 包版本太旧了。应该是>=3.8.0
。请参阅此处的故障排除。
我看到了同样的错误信息,升级protobuf
被证明是解决方案。在我的情况下,要确保 Python 在旧 Python 安装、虚拟环境和指向旧安装的 PYTHONPATH 的迷宫中找到升级包时会感到额外的痛苦protobuf
。
推荐阅读
- hadoop - select * 在存储在 hdfs 中的 orc 文件上创建外部表后返回时间戳的空值
- python - python中的Mime类型优化
- python - 有人可以解释一下 Python Division
- html - 在
- powershell-3.0 - 如何在不使用 New-Item (Powershell 3.0) 的情况下创建符号链接
- owl-carousel-2 - parallax.j 和 owl carousel 2 滑块
- ethernet - 以太网通讯中RGMII与MDI的关系
- spring-boot - 在 kafka 消费者中使用 SeekToCurrentErrorHandler 进行无限重试
- ios - 在 UIViewController 上执行的 UIView 上的动态创建 UIButton AddTarget
- javascript - 折叠包含子元素的可变数量的元素