首页 > 解决方案 > 在 Colab 上训练的 simpletransformers 模型在本地无法工作

问题描述

我已按照此链接中的说明在Google Colab 上训练了一个 BERT 模型。然后我下载了它并尝试在我的计算机上本地使用它。但是出了点问题。

这是我训练模型的代码(在 Google Colab 上):

from simpletransformers.classification import ClassificationModel
train_args ={"reprocess_input_data": True,
             "fp16":False,
             "num_train_epochs": 1,
             "save_steps": 100_000,
             "logging_steps": 100}

model = ClassificationModel(
    "bert", "bert-base-german-cased",
    num_labels=21,
    args=train_args
)
model.train_model(train_df)

它在 Colab 上运行得很好,但我在本地需要它,所以我下载了使用以下代码创建的存档:

import os
import tarfile

def pack_model(model_path='',file_name=''):
  files = [files for root, dirs, files in os.walk(model_path)][0]
  with tarfile.open(file_name+ '.tar.gz', 'w:gz') as f:
    for file in files:
      f.add(f'{model_path}/{file}')

pack_model('outputs', 'model')

然后我将其解压缩并放置在包含以下代码的文件的目录中以测试模型:

from simpletransformers.classification import ClassificationModel

train_args ={"reprocess_input_data": True,
             "fp16":False,
             "num_train_epochs": 1,
             "save_steps": 100_000,
             "logging_steps": 100}

model = ClassificationModel(
    "bert", "outputs/",
    num_labels=21,
    args=train_args,
    use_cuda=False
)

test = "Boris Johnson hält eine Pressekonferenz zum Coronavirus-Aktionsplan der Regierung"

predictions, raw_outputs = model.predict([test])
print([predictions[0])

所以我如何创建模型的唯一区别是use_cuda=False(因为我在本地没有 GPU)。

当我执行代码时,出现以下输出:

I0526 17:16:12.761774 14508 file_utils.py:39] PyTorch version 1.5.0+cpu available.
I0526 17:16:32.703790 14508 configuration_utils.py:283] loading configuration file outputs/config.json
I0526 17:16:32.706786 14508 configuration_utils.py:321] Model config BertConfig {
  "architectures": [
    "BertForSequenceClassification"
  ],
  "attention_probs_dropout_prob": 0.1,
  "hidden_act": "gelu",
  "hidden_dropout_prob": 0.1,
  "hidden_size": 768,
  "id2label": {
    "0": "LABEL_0",
    "1": "LABEL_1",
    "2": "LABEL_2",
    "3": "LABEL_3",
    "4": "LABEL_4",
    "5": "LABEL_5",
    "6": "LABEL_6",
    "7": "LABEL_7",
    "8": "LABEL_8",
    "9": "LABEL_9",
    "10": "LABEL_10",
    "11": "LABEL_11",
    "12": "LABEL_12",
    "13": "LABEL_13",
    "14": "LABEL_14",
    "15": "LABEL_15",
    "16": "LABEL_16",
    "17": "LABEL_17",
    "18": "LABEL_18",
    "19": "LABEL_19",
    "20": "LABEL_20"
  },
  "initializer_range": 0.02,
  "intermediate_size": 3072,
  "label2id": {
    "LABEL_0": 0,
    "LABEL_1": 1,
    "LABEL_10": 10,
    "LABEL_11": 11,
    "LABEL_12": 12,
    "LABEL_13": 13,
    "LABEL_14": 14,
    "LABEL_15": 15,
    "LABEL_16": 16,
    "LABEL_17": 17,
    "LABEL_18": 18,
    "LABEL_19": 19,
    "LABEL_2": 2,
    "LABEL_20": 20,
    "LABEL_3": 3,
    "LABEL_4": 4,
    "LABEL_5": 5,
    "LABEL_6": 6,
    "LABEL_7": 7,
    "LABEL_8": 8,
    "LABEL_9": 9
  },
  "layer_norm_eps": 1e-12,
  "max_position_embeddings": 512,
  "model_type": "bert",
  "num_attention_heads": 12,
  "num_hidden_layers": 12,
  "pad_token_id": 0,
  "type_vocab_size": 2,
  "vocab_size": 30000
}

I0526 17:16:32.723798 14508 modeling_utils.py:648] loading weights file outputs/pytorch_model.bin
I0526 17:16:41.755029 14508 tokenization_utils.py:929] Model name 'outputs/' not found in model shortcut name list (bert-base-uncased, bert-large-uncased, bert-base-cased, bert-large-cased, bert-base-multilingual-uncased, bert-base-multilingual-cased, bert-base-chinese, bert-base-german-cased, bert-large-uncased-whole-word-masking, bert-large-cased-whole-word-masking, bert-large-uncased-whole-word-masking-finetuned-squad, bert-large-cased-whole-word-masking-finetuned-squad, bert-base-cased-finetuned-mrpc, bert-base-german-dbmdz-cased, bert-base-german-dbmdz-uncased, bert-base-finnish-cased-v1, bert-base-finnish-uncased-v1, bert-base-dutch-cased). Assuming 'outputs/' is a path, a model identifier, or url to a directory containing tokenizer files.
I0526 17:16:41.770021 14508 tokenization_utils.py:958] Didn't find file outputs/added_tokens.json. We won't load it.
I0526 17:16:41.779017 14508 tokenization_utils.py:1013] loading file outputs/vocab.txt
I0526 17:16:41.788011 14508 tokenization_utils.py:1013] loading file None
I0526 17:16:41.794008 14508 tokenization_utils.py:1013] loading file outputs/special_tokens_map.json
I0526 17:16:41.806000 14508 tokenization_utils.py:1013] loading file outputs/tokenizer_config.json
I0526 17:16:41.986897 14508 classification_model.py:801]  Converting to features started. Cache is not used.

  0%|          | 0/1 [00:00<?, ?it/s]

它冻结在这一刻。尽管有这个进度条,但什么也没有发生。我应该怎么做才能使它在本地工作?也许还有另一种方式?

提前感谢您的帮助!

标签: pythongoogle-colaboratorybert-language-modelsimpletransformers

解决方案


推荐阅读