
CODE 1: BertModel.from_pretrained CODE 2: TFBertModel.from_pretrained Error: AttributeError: module 'transformers' has no attribute 'TFBertModel'
我尝试搜索互联网,但没有找到任何有用的内容。
投票
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