加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
test.py 828 Bytes
一键复制 编辑 原始数据 按行查看 历史
cale 提交于 2023-03-01 07:06 . 主要文件
from config import *
from utils import *
from model import *
def test():
test_dataset = Dataset('test')
test_loader = data.DataLoader(test_dataset, collate_fn=collate_fn, batch_size=64, shuffle=False)
model = torch.load(MODEL_DIR + '0.pth').to(DEVICE)
loss_fn = nn.CrossEntropyLoss()
total = 0
correct = 0
model.eval()
with torch.no_grad():
for i, (input_ids, attention_mask, labels) in enumerate(test_loader):
out = model(input_ids, attention_mask)
loss = loss_fn(out, labels)
out = out.argmax(dim=1)
correct += (out == labels).sum().item()
total += len(labels)
print(' batch: ', i, ' loss ', loss.item())
acc = correct / total
print(' acc: ', round(acc, 2))
if __name__ == '__main__':
test()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化