【问题标题】:Locally opening a transformers saved model在本地打开一个变压器保存的模型
【发布时间】:2021-03-04 08:41:40
【问题描述】:

我有一个使用 BertModel.from_pretrained('test_model') 保存的变形金刚模型

我已经使用 google colab 的 GPU 训练了这个模型

然后,我想用BertModel.from_pretrained('test_model/')打开它 但我的本地 PC 中没有 GPU。我明白了:

/home/seiji/.local/lib/python3.8/site-packages/torch/cuda/__init__.py:52: UserWarning: CUDA initialization: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx (Triggered internally at  /pytorch/c10/cuda/CUDAFunctions.cpp:100.)
  return torch._C._cuda_getDeviceCount() > 0

我该怎么办?我不知道如何使用 CPU 打开它。有可能吗?

【问题讨论】:

    标签: python nlp huggingface-transformers


    【解决方案1】:

    你能做的最好的事情是保存模型的 CPU 版本,即:

    model.cpu().save_pretrained("model_directory")
    

    无论如何,所有预训练的 Huggingface 模型都保存为 CPU 模型,您始终需要明确地将它们移动到 GPU。

    PyTorch 允许在 CPU 上加载 GPU 模型(请参阅 https://discuss.pytorch.org/t/on-a-cpu-device-how-to-load-checkpoint-saved-on-gpu-device/349),但您需要设置的 torch.load 的参数不会通过 API 公开,因此您需要编写自己的 from_pretrained 方法。

    【讨论】:

      猜你喜欢
      • 2020-05-04
      • 2020-11-25
      • 2016-11-03
      • 2021-01-04
      • 2011-11-24
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 2021-09-03
      相关资源
      最近更新 更多