Не удается найти модель pytorch при загрузке модели BERT в Python

Слежу за этой статьей, чтобы найти сходство текста. У меня есть такой код:

      from sentence_transformers import SentenceTransformer
from tqdm import tqdm
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import pandas as pd

documents = [
         "Vodafone Wins ₹ 20,000 Crore Tax Arbitration Case Against Government",
         "Voda Idea shares jump nearly 15% as Vodafone wins retro tax case in Hague",
         "Gold prices today fall for 4th time in 5 days, down ₹6500 from last month high",
         "Silver futures slip 0.36% to Rs 59,415 per kg, down over 12% this week",
         "Amazon unveils drone that films inside your home. What could go wrong?",
         "IPHONE 12 MINI PERFORMANCE MAY DISAPPOINT DUE TO THE APPLE B14 CHIP",
         "Delhi Capitals vs Chennai Super Kings: Prithvi Shaw shines as DC beat CSK to post second consecutive win in IPL",
         "French Open 2020: Rafael Nadal handed tough draw in bid for record-equaling 20th Grand Slam"
]

model = SentenceTransformer('sentence-transformers/bert-base-nli-mean-tokens')

Я получаю сообщение об ошибке при запуске вышеуказанного кода:

Полный:

      ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\anaconda3\envs\py3_nlp\lib\tarfile.py in nti(s)
    188             s = nts(s, "ascii", "strict")
--> 189             n = int(s.strip() or "0", 8)
    190         except ValueError:

ValueError: invalid literal for int() with base 8: 'ld_tenso'

During handling of the above exception, another exception occurred:

InvalidHeaderError                        Traceback (most recent call last)
~\anaconda3\envs\py3_nlp\lib\tarfile.py in next(self)
   2298             try:
 -> 2299                 tarinfo = self.tarinfo.fromtarfile(self)
   2300             except EOFHeaderError as e:

 ~\anaconda3\envs\py3_nlp\lib\tarfile.py in fromtarfile(cls, tarfile)
   1092         buf = tarfile.fileobj.read(BLOCKSIZE)
 -> 1093         obj = cls.frombuf(buf, tarfile.encoding, tarfile.errors)
   1094         obj.offset = tarfile.fileobj.tell() - BLOCKSIZE

 ~\anaconda3\envs\py3_nlp\lib\tarfile.py in frombuf(cls, buf, encoding, errors)
    1034 
 -> 1035         chksum = nti(buf[148:156])
    1036         if chksum not in calc_chksums(buf):

  ~\anaconda3\envs\py3_nlp\lib\tarfile.py in nti(s)
    190         except ValueError:
--> 191             raise InvalidHeaderError("invalid header")
    192     return n

 InvalidHeaderError: invalid header

 During handling of the above exception, another exception occurred:

 ReadError                                 Traceback (most recent call last)
 ~\anaconda3\envs\py3_nlp\lib\site-packages\torch\serialization.py in _load(f, map_location, 
 pickle_module, **pickle_load_args)
     594         try:
 --> 595             return legacy_load(f)
     596         except tarfile.TarError:

 ~\anaconda3\envs\py3_nlp\lib\site-packages\torch\serialization.py in legacy_load(f)
    505 
--> 506         with closing(tarfile.open(fileobj=f, mode='r:', format=tarfile.PAX_FORMAT)) as 
tar, \
    507                 mkdtemp() as tmpdir:

 ~\anaconda3\envs\py3_nlp\lib\tarfile.py in open(cls, name, mode, fileobj, bufsize, **kwargs)
    1590                 raise CompressionError("unknown compression type %r" % comptype)
 -> 1591             return func(name, filemode, fileobj, **kwargs)
    1592 

~\anaconda3\envs\py3_nlp\lib\tarfile.py in taropen(cls, name, mode, fileobj, **kwargs)
   1620             raise ValueError("mode must be 'r', 'a', 'w' or 'x'")
-> 1621         return cls(name, mode, fileobj, **kwargs)
   1622 

 ~\anaconda3\envs\py3_nlp\lib\tarfile.py in __init__(self, name, mode, fileobj, format, tarinfo, dereference, ignore_zeros, encoding, errors, pax_headers, debug, errorlevel, copybufsize)
   1483                 self.firstmember = None
-> 1484                 self.firstmember = self.next()
   1485 

~\anaconda3\envs\py3_nlp\lib\tarfile.py in next(self)
   2310                 elif self.offset == 0:
 -> 2311                     raise ReadError(str(e))
   2312             except EmptyHeaderError:

 ReadError: invalid header

 During handling of the above exception, another exception occurred:

 RuntimeError                              Traceback (most recent call last)
 ~\anaconda3\envs\py3_nlp\lib\site-packages\transformers\modeling_utils.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
    1210                 try:
 -> 1211                     state_dict = torch.load(resolved_archive_file, map_location="cpu")
    1212                 except Exception:

 ~\anaconda3\envs\py3_nlp\lib\site-packages\torch\serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
     425             pickle_load_args['encoding'] = 'utf-8'
 --> 426         return _load(f, map_location, pickle_module, **pickle_load_args)
     427     finally:

 ~\anaconda3\envs\py3_nlp\lib\site-packages\torch\serialization.py in _load(f, map_location, pickle_module, **pickle_load_args)
    598                 # .zip is used for torch.jit.save and will throw an un-pickling error here
  --> 599                 raise RuntimeError("{} is a zip archive (did you mean to use torch.jit.load()?)".format(f.name))
     600             # if not a tarfile, reset file offset and proceed

 RuntimeError: C:\Users\user1/.cache\torch\sentence_transformers\sentence-transformers_bert-base-nli-mean-tokens\pytorch_model.bin is a zip archive (did you mean to use torch.jit.load()?)

 During handling of the above exception, another exception occurred:

 OSError                                   Traceback (most recent call last)
 <ipython-input-3-bba56aac60aa> in <module>
 ----> 1 model = SentenceTransformer('sentence-transformers/bert-base-nli-mean-tokens')

 ~\anaconda3\envs\py3_nlp\lib\site-packages\sentence_transformers\SentenceTransformer.py in __init__(self, model_name_or_path, modules, device, cache_folder)
     88 
     89             if os.path.exists(os.path.join(model_path, 'modules.json')):    #Load as SentenceTransformer model
---> 90                 modules = self._load_sbert_model(model_path)
     91             else:   #Load with AutoModel
     92                 modules = self._load_auto_model(model_path)

 ~\anaconda3\envs\py3_nlp\lib\site-packages\sentence_transformers\SentenceTransformer.py in _load_sbert_model(self, model_path)
     820         for module_config in modules_config:
     821             module_class = import_from_string(module_config['type'])
 --> 822             module = module_class.load(os.path.join(model_path, module_config['path']))
    823             modules[module_config['name']] = module
    824 

 ~\anaconda3\envs\py3_nlp\lib\site-packages\sentence_transformers\models\Transformer.py in load(input_path)
     122         with open(sbert_config_path) as fIn:
     123             config = json.load(fIn)
 --> 124         return Transformer(model_name_or_path=input_path, **config)
     125 
     126 

 ~\anaconda3\envs\py3_nlp\lib\site-packages\sentence_transformers\models\Transformer.py in __init__(self, model_name_or_path, max_seq_length, model_args, cache_dir, tokenizer_args, do_lower_case, tokenizer_name_or_path)
     27 
     28         config = AutoConfig.from_pretrained(model_name_or_path, **model_args, cache_dir=cache_dir)
---> 29         self.auto_model = AutoModel.from_pretrained(model_name_or_path, config=config, cache_dir=cache_dir)
    30         self.tokenizer = AutoTokenizer.from_pretrained(tokenizer_name_or_path if tokenizer_name_or_path is not None else model_name_or_path, cache_dir=cache_dir, **tokenizer_args)
   31 

  ~\anaconda3\envs\py3_nlp\lib\site-packages\transformers\models\auto\auto_factory.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
    393         if type(config) in cls._model_mapping.keys():
    394             model_class = _get_model_class(config, cls._model_mapping)
--> 395             return model_class.from_pretrained(pretrained_model_name_or_path, *model_args, config=config, **kwargs)
    396         raise ValueError(
    397             f"Unrecognized configuration class {config.__class__} for this kind of AutoModel: {cls.__name__}.\n"

 ~\anaconda3\envs\py3_nlp\lib\site-packages\transformers\modeling_utils.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
    1212                 except Exception:
    1213                     raise OSError(
 -> 1214                         f"Unable to load weights from pytorch checkpoint file for '{pretrained_model_name_or_path}' "
    1215                         f"at '{resolved_archive_file}'"
    1216                         "If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True. "

 OSError: Unable to load weights from pytorch checkpoint file for 'C:\Users\user1/.cache\torch\sentence_transformers\sentence-transformers_bert-base-nli-mean-tokens\' at 'C:\Users\user1/.cache\torch\sentence_transformers\sentence-transformers_bert-base-nli-mean-tokens\pytorch_model.bin'If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.

Короткий:

OSError: Невозможно загрузить веса из файла контрольной точки pytorch для 'C: \ Users \ user1 / .cache \ torch \ фраза_трансформаторы \ предложения-трансформеры_bert-base-nli-mean-tokens' в 'C: \ Users \ user1 / .cache \ torch \ предложения_трансформаторы \ предложения-трансформеры_bert-base-nli-mean-tokens \ pytorch_model.bin'Если вы пытались загрузить модель PyTorch из контрольной точки TF 2.0, установите from_tf=True.

У меня есть pytorch_model.bin в папке '.cache \ torch \ offer_transformers \ предложение-transformers_bert-base-nli-mean-tokens'.

Почему я получаю эту ошибку?

2 ответа

Возможно, вам придется использовать модель без предложений_трансформеров.

Следующий код изменен с https://www.sbert.net/examples/applications/computing-embeddings/README.html .

Насколько я понимаю, из исключения нужно передать from_tf=True в AutoModel.

      from transformers import AutoTokenizer, AutoModel
import torch


#Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
    token_embeddings = model_output[0] #First element of model_output contains all token embeddings
    input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
    sum_embeddings = torch.sum(token_embeddings * input_mask_expanded, 1)
    sum_mask = torch.clamp(input_mask_expanded.sum(1), min=1e-9)
    return sum_embeddings / sum_mask



#Sentences we want sentence embeddings for
sentences = ['This framework generates embeddings for each input sentence',
             'Sentences are passed as a list of string.',
             'The quick brown fox jumps over the lazy dog.']

#Load AutoModel from huggingface model repository
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/bert-base-nli-mean-tokens')
model = AutoModel.from_pretrained('sentence-transformers/bert-base-nli-mean-tokens',from_tf=True)

#Tokenize sentences
encoded_input = tokenizer(sentences, padding=True, truncation=True, max_length=128, return_tensors='pt')

#Compute token embeddings
with torch.no_grad():
    model_output = model(**encoded_input)

#Perform pooling. In this case, mean pooling
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])

Причина ошибки, по-видимому, в том, что предварительно обученные файлы весов модели недоступны или загружаются.

Вы можете попробовать это, чтобы загрузить предварительно обученный файл веса модели:

      from transformers import AutoModel
model = AutoModel.from_pretrained('sentence-transformers/bert-base-nli-mean-tokens')

Ссылка: https://huggingface.co/sentence-transformers/bert-base-nli-mean-tokens

Кроме того, на странице объятия модели говорится: «Эта модель устарела. Пожалуйста, не используйте его, так как он создает вложения предложений низкого качества. Вы можете найти рекомендуемые модели встраивания предложений здесь: SBERT.net - Предварительно подготовленные модели

Может быть, вы захотите взглянуть.

Другие вопросы по тегам