【发布时间】:2020-10-25 00:27:28
【问题描述】:
我正在尝试复制this BERTweet code。
我在 Google Colab 笔记本中运行了以下代码:
!pip install fairseq
import fairseq
!pip install fastBPE
import fastBPE
# download the pre-trained BERTweet model zipped file
!wget https://public.vinai.io/BERTweet_base_fairseq.tar.gz
# unzip the pre-trained BERTweet model files
!tar -xzvf BERTweet_base_fairseq.tar.gz
!pip install transformers
import transformers
!wget https://public.vinai.io/BERTweet_base_transformers.tar.gz
!tar -xzvf BERTweet_base_transformers.tar.gz
import torch
import argparse
from transformers import RobertaConfig
from transformers import RobertaModel
from fairseq.data.encoders.fastbpe import fastBPE
from fairseq.data import Dictionary
# Load model
config = RobertaConfig.from_pretrained(
"/content/BERTweet_base_transformers/config.json"
)
BERTweet = RobertaModel.from_pretrained(
"/content/BERTweet_base_transformers/model.bin",
config=config
)
parser = argparse.ArgumentParser()
parser.add_argument('--bpe-codes',
default="/content/BERTweet_base_transformers/bpe.codes",
required=False,
type=str,
help='path to fastBPE BPE'
)
...这会成功运行。
然后我尝试运行:
args = parser.parse_args()
...导致以下错误:
usage: ipykernel_launcher.py [-h] [--bpe-codes BPE_CODES]
ipykernel_launcher.py: error: unrecognized arguments: -f /root/.local/share/jupyter/runtime/kernel-96d3f587-5881-4520-9402-8ca07a3fdc75.json
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
如果这是对调试有用的信息,这是我的文件存储库的样子:
为什么会抛出错误,我需要更改什么(以及更改什么)来修复它?
【问题讨论】:
-
argparse 是一个命令行选项解析器。你想在 colab 中用它做什么?
-
您的解析器看到的命令行值适用于
kernel,而不是您的脚本。 -
感谢 hpaulj。有没有办法可以改变它,让它在谷歌 Colab 中工作?我使用 GC 是因为我想在 GPU 上训练模型,因为它在我的 CPU 上可能会太慢。
-
是的,无需将代码粘贴到笔记本中的单元格中,只需将其中的 python 部分保存为 .py 文件,将其上传到 Colab VM 或您的 Google Drive,然后从命令行调用它。例如,如果您将文件命名为
script.py,那么您将在 Colab 的单元格中输入!script.py -<argument 1> -<argument 2>或类似名称。
标签: python jupyter-notebook google-colaboratory argparse