我正在通过python框架(PyTorch)应用转移学习。在Google Colab中加载PyTorch预训练模型时,出现以下错误。将代码1更改为代码2后,我遇到了相同的错误。
CODE 1: BertModel.from_pretrained
CODE 2: TFBertModel.from_pretrained
Error: AttributeError: module 'transformers' has no attribute 'TFBertModel'
我试图搜索互联网,但没有找到任何有用的内容。
答案 0 :(得分:3)
您可能应该在python和Colab链接中列出可用的软件包及其版本,因为TFBertModel
仅在具有张量流时可用。
为了重现您的错误。我在Colab中玩耍如下:
tensorflow
时没有TFBertModel
引起错误!pip install transformers
from transformers import BertModel, TFBertModel # no attribute 'TFBertModel'
!pip install tensorflow-gpu
from transformers import BertModel, TFBertModel # good to go
BertModel
!pip install transformers
from transformers import BertModel
BertModel.from_pretrained # good to go
作为我的测试结果,您可能应该检查一下是否在卸载tensorflow的同时导入TFBertModel
。
master分支下的变压器仅将if is_tf_available()
设置为True才能导入TFBertModel。这是if_is_tf_available()
的代码:
# transformers/src/transformers/file_utils.py
# >>> 107 lines
def is_tf_available():
return _tf_available
# >>> 48 lines
try:
USE_TF = os.environ.get("USE_TF", "AUTO").upper()
USE_TORCH = os.environ.get("USE_TORCH", "AUTO").upper()
if USE_TF in ("1", "ON", "YES", "AUTO") and USE_TORCH not in ("1", "ON", "YES"):
import tensorflow as tf
assert hasattr(tf, "__version__") and int(tf.__version__[0]) >= 2
_tf_available = True # pylint: disable=invalid-name
logger.info("TensorFlow version {} available.".format(tf.__version__))
else:
logger.info("Disabling Tensorflow because USE_TORCH is set")
_tf_available = False
except (ImportError, AssertionError):
_tf_available = False # pylint: disable=invalid-name