+-
错误:AttributeError:模块'transformers'没有属性'TFBertModel'
这个问题是来自人工智能堆栈交换的 migrated,因为可以在堆栈溢出时回答。 Migrated << [1小时前。
我正在通过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
投票
您可能应该在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分支下的变压器仅将TFBertModel导入if is_tf_available()设置为True。这是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