加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
train.py 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
我没得冰阔落 提交于 2023-07-04 09:13 . v1
import time
import torch
import parsee
from lib.loss import SimpleLossCompute
def run_epoch(data, model, loss_compute, epoch):
start = time.time()
total_tokens = 0
total_loss = 0
tokens = 0
for i , batch in enumerate(data):
out = model(batch.src, batch.trg, batch.src_mask, batch.trg_mask)
loss = loss_compute(out, batch.trg_y, batch.ntokens)
total_loss += loss
total_tokens += batch.ntokens
tokens += batch.ntokens
if i % 50 == 1:
elapsed = time.time() - start
print("Epoch %d Batch: %d Loss: %f Tokens per Sec: %fs" % (epoch, i - 1, loss / batch.ntokens, tokens / elapsed / 1000))
start = time.time()
tokens = 0
return total_loss / total_tokens
def train(data, model, criterion, optimizer):
for epoch in range(parsee.epochs):
model.train()
run_epoch(data.train_data, model, SimpleLossCompute(model.generator, criterion, optimizer), epoch)
model.eval()
print('>>>>> Evaluate')
loss = run_epoch(data.dev_data, model, SimpleLossCompute(model.generator, criterion, None), epoch)
print('<<<<< Evaluate loss: %f' % loss)
torch.save(model.state_dict(), parsee.save_file)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化