Code Monkey home page Code Monkey logo

transformer's Introduction

Transformer pytorch实现

笔记

setting.py:模型相关参数,文件目录的配置文件。
utils.py:一些工具函数,比如不等长句子的padding等等。
data_pre.py:数据的预处理,得到输出模型的batch数据和相关的mask矩阵
model.py:模型文件。通过调用make_model方法传入相关模型初始化参数,来对模型进行初始化。
train.py:进行模型的训练。和最好模型的保存。
test.py:对测试集句子的测试输出。
bleu_score.py:对机器翻译评分。
one_trans.py:实现单个句子进行翻译。
app.py:通过使用one_trans文件封装的单个句子翻译的方法,实现flask api

flask api请求参数

简单api,没有进行检查校验和异常处理

// POST请求参数
{
  "sentence": "your  translation sentences"
}
// return
{
  "result": "翻译结果",
  "msg": 'success',
  "code": 200
}

模型训练数据

使用14533条翻译数据进行训练。
数据文件格式:en\tcn

结果评估

使用BLEU算法进行翻译效果评估BLEU BLEU算法评价结果:

对399条翻译句子效果进行评估
验证集:0.1075088492716548,n-gram权重:(1,0,0,0)
      0.03417978514554449,n-gram权重:(1,0.2,0,0)

Attention:运行代码之前需要自己在项目目录下新建一个save文件夹
PyTorch官方Transformer接口

Transformer讲解

运行项目

  1. python train.py:训练模型,保存模型
  2. python app.py启动服务。

python test.py,测试模型的测试集上的效果(这里用的是验证集)

transformer's People

Contributors

taoztw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

transformer's Issues

如何将模型修改成中文翻译英文

想问一下,现在这个模型是吧英文翻译成中文。如果我想中文翻译成英文应该如何修改呢。我自己把data的batch中英文对调了。但是结果很差。顺便感谢作者,注释写的很详细,很方便理解。

关于 LabelSmoothing 的一些问题

你好,很感谢你的代码, 在里面学到了很多,但是关于LabelSmoothing我感觉有一点问题。
return self.criterion(x, Variable(true_dist, requires_grad=False)) 中, 我感觉因为使用nn.KLDivLoss(reduction='sum')所以理论上需要对x取log。但是我尝试修改之后发现loss会变成nan。请问这个需要怎么解决。能否指点一下。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.