PyTorch,Bert 模型:优化器 zero_grad() error

问题描述 投票:0回答:0

我对 Python 比较陌生,我正在学习一个教程(https://towardsdatascience.com/named-entity-recognition-with-bert-in-pytorch-a454405e0b6a)关于我需要的任务的伯特训练. 该教程相对较新,2022 年 5 月的。 我在 ubuntu 22.04 上使用 python 3.10.6 和 pytorch 1.13.1+cpu 和 tensorflow 2.11。

我收到以下有关 SGD 优化器的错误消息:

builtins.AttributeError: 'SGD' 对象没有属性 'zero_grad'

问题出在教程的train_loop()函数中。它有一行:

optimizer.zero_grad()

我读到 zero_grad() 已从 tf 中弃用,并尝试用一些类似的解决方案来修改它:

优化器.cleargrads()

但在那种情况下我收到以下错误:

builtins.AttributeError: 'BertModel' 对象没有属性 'cleargrads'

代码总共约200行。它存在于教程中(https://towardsdatascience.com/named-entity-recognition-with-bert-in-pytorch-a454405e0b6a)

如果需要出现在帖子中,我可以发布完整的代码。

python tensorflow pytorch bert-language-model named-entity-recognition
© www.soinside.com 2019 - 2024. All rights reserved.