可能是由于Allennlp训练时的文件路径或参数配置有误导致model.tar.gz文件未生成。可以通过以下方法解决:
allennlp train \
--serialization-dir /path/to/serialization_dir \
--include-package my_package \
--model my_package.models.my_model \
--overrides "{'my_param': 1}" \
train_data.json
--model-archive /path/to/model.tar.gz
{
"model": {
"type": "my_model",
"my_param": 1
},
"train_data_path": "train_data.json",
"model_archive": "/path/to/model.tar.gz"
}
from allennlp.models import Model
from allennlp.common.params import Params
model = Model.from_params(params=Params.from_file('config.json'))
model.train()
model.save_serialization_to_directory('/path/to/serialization_dir')
model_archive = '/path/to/model.tar.gz'
model_archive_dir = '/path/to/model_archive_dir'
# 将保存的文件打包成model.tar.gz
with tarfile.open(model_archive, 'w:gz') as archive:
archive.add(model_archive_dir, arcname=os.path.basename(model_archive_dir))