【发布时间】:2022-09-27 22:25:03
【问题描述】:
我正在使用 Windows 11 pro x64,PyCharm 2022.2.2(专业版)- Build #PY-222.4167.33,构建于 2022 年 9 月 15 日。蟒蛇版本:
Microsoft Windows [Version 10.0.22621.521]
(c) Microsoft Corporation. All rights reserved.
C:\\Users\\donhu>python
Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.
>>>
我的代码
import argparse
import yaml
import pandas as pd
import torch
import torchcrf
import transformers
from data import Dataset
from engines import train_fn
import warnings
warnings.filterwarnings(\"ignore\")
parser = argparse.ArgumentParser()
parser.add_argument(\"--data_file\", type=str)
parser.add_argument(\"--hyps_file\", type=str)
args = parser.parse_args()
data_file = yaml.load(open(args.data_file), Loader=yaml.FullLoader)
hyps_file = yaml.load(open(args.hyps_file), Loader=yaml.FullLoader)
train_loader = torch.utils.data.DataLoader(
Dataset(
df=pd.read_csv(data_file[\"train_df_path\"]),
tag_names=data_file[\"tag_names\"],
tokenizer=transformers.AutoTokenizer.from_pretrained(hyps_file[\"encoder\"], use_fast=False),
),
num_workers=hyps_file[\"num_workers\"],
batch_size=hyps_file[\"batch_size\"],
shuffle=True,
)
val_loader = torch.utils.data.DataLoader(
Dataset(
df=pd.read_csv(data_file[\"val_df_path\"]),
tag_names=data_file[\"tag_names\"],
tokenizer=transformers.AutoTokenizer.from_pretrained(hyps_file[\"encoder\"], use_fast=False),
),
num_workers=hyps_file[\"num_workers\"],
batch_size=hyps_file[\"batch_size\"] * 2,
)
loaders = {
\"train\": train_loader,
\"val\": val_loader,
}
model = transformers.RobertaForTokenClassification.from_pretrained(hyps_file[\"encoder\"],
num_labels=data_file[\"num_tags\"])
if hyps_file[\"use_crf\"]:
criterion = torchcrf.CRF(num_tags=data_file[\"num_tags\"], batch_first=True)
else:
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=float(hyps_file[\"lr\"]))
train_fn(
loaders, model, torch.device(hyps_file[\"device\"]), hyps_file[\"device_ids\"],
criterion,
optimizer,
epochs=hyps_file[\"epochs\"],
ckp_path=\"../ckps/{}.pt\".format(hyps_file[\"encoder\"].split(\"/\")[-1]),
)
我也通过命令安装
pip install torchcrf
我也试试
pip install pytorch-crf
但我并不成功。
如何安装torchcrf 并修复导入错误?