Giter Site home page Giter Site logo

just_another_seq2seq's Introduction

A lot of Chinese in codes and docs

Just another seq2seq repo

  • 主要是从个人角度梳理了一下seq2seq的代码
  • 加入了可选基本的CRF支持,loss和infer(还不确定对
  • 加入了一些中文注释
  • 相对于其他一些repo,bug可能会少一些
    • 有些repo的实现在不同参数下会有问题:例如有些支持gru不支持lstm,有些不支持bidirectional,有些选择depth > 1的时候会有各种bug之类的,这些问题我都尽量修正了,虽然不保证实现肯定是对的
  • 后续我可能会添加一些中文的例子,例如对联、古诗、闲聊、NER
  • 根据本repo,我会整理一份seq2seq中间的各种trick和实现细节的坑
  • pretrained embedding support
  • 后续这个repo会作为一个基础完成一个dialogue system(的一部分,例如NLU)
    • seq2seq模型至少可以作为通用NER实现
    • 截止2018年初,最好的NER应该还是bi-LSTM + CRF,也有不加CRF效果好的

作者的一系列小文章,欢迎吐槽

Update Log

2018-03-10

我把一些代码内的trick设置的更接近NMT了。

尝试训练更好的chatbot模型(嘬死)。

添加了一个支持加载训练好的embedding的模型,参考chatbot_cut/, 这个例子是“词级的”, 分词用的jieba, 默认的预训练模型是fasttext, 详情点击看文档、代码。

2018-03-06

增加了chatbot中anti-lm的训练方法样例,在chatbot/train_anti.py中。 这个模式参考了Li et al., 2015和代码 Marsan-Ma/tf_chatbot_seq2seq_antilm

加入anti-lm来看,diversity是有提高,不过整体来看,并不是说就很好好。 但是明显降低了机器回答“我不知道”和“我不知道你在说什么”这样的语言概率。

虽然我在不同的地方在还尝试实现了下面这两个(其实都是一个人写的啦) Li et al., 2016 Li et al., 2017 不过基本上不太成功的感觉,虽然我也没做太严格的做法。

Known issues

Example里的例子和整个项目,虽然未经验证,但是在内存较小的电脑上(<8GB),可能会有问题。 这涉及到数据处理、数据输入、模型参数等部分,所以严格来说并不算BUG。

chatbot模型根本没有一个什么是“好”的评价标准, 也根本没有“好”的数据。 所以不要对结果有过度期待,仅供娱乐。 如果你问我仅供娱乐还写它干嘛? 本repo只是为了实现各种seq2seq技术, 也有有用的翻译和NER啊, 当然很多部分都是学习与研究性质的,工业化需要很多改进。 chatbot部分虽然我花了不少时间, 但是那个还只是娱乐而已, 实际应用起来,对话质量、系统成本可能很高。 我能保证的只是,这个模型基本上没原则性问题而已, 至少给一个参考,看看我写的垃圾代码和别人写的代码的区别,是吧。

当然也不是说就是不能用,例如你能自己搞一些质量很高的数据啦。 比如说这位仁兄的repo 他就自己弄了一份质量很高的数据, 搭配一些合理的扩展, 例如给数据添加功能性词汇 _func_get_current_time 之类感觉的东西, 就能让chatbot实现一些简单功能。

简单的说就是把训练数据设置为, 上一句是现 在 几 点, 下一句是现 在 时 间 _func_get_current_time, 这样在输出部分如果解析到_func_get_current_time这个词 就自动替换为时间的话, 就可以得到类似“报时”的功能了。 (技术没有好坏,应用在哪最重要!~~这句话是不是很装逼)

Platform

作者在一台64GB内存 + GTX1070 6GB + Ubuntu 16.04电脑上运行。

内存肯定不需要这么大,不过显存如果在2GB,如果要在GPU上运行模型,可能需要调节batch_size等模型参数。

Example

Example里面用到的数据,都是比较小且粗糙的。 作者只基本验证了可行性,所以也不可能实用了,例如英汉翻译就别期待准确率很高了, 大概意思到了就代表模型的一定有效性了。

英汉句子翻译实例

测试结果样例

我不保证能重复实现能得到一模一样的结果

Input English Sentence:go to hell
[[30475 71929 33464]] [3]
[[41337 48900 41337 44789     3]]
['go', 'to', 'hell']
['去', '地狱', '去', '吧', '</s>']
Input English Sentence:nothing, but the best for you
[[50448   467 13008 71007 10118 27982 79204]] [7]
[[ 25904 132783  90185      4  28145  81577  80498  28798      3]]
['nothing', ',', 'but', 'the', 'best', 'for', 'you']
['什么', '都', '没有', ' ', '但', '最好', '是', '你', '</s>']
Input English Sentence:i'm a bad boy
[[35437   268  4018  8498 11775]] [5]
[[ 69313  80498  21899  49069 100342      3     -1]]
['i', "'m", 'a', 'bad', 'boy']
['我', '是', '个', '坏', '男孩', '</s>', '<unk>']
Input English Sentence:i'm really a bad boy
[[35437   268 58417  4018  8498 11775]] [6]
[[ 69313 103249  80498  17043  49069 100342      3      3      3      3
       3      3]]
['i', "'m", 'really', 'a', 'bad', 'boy']
['我', '真的', '是', '一个', '坏', '男孩', '</s>', '</s>', '</s>', '</s>', '</s>', '</s>']

NER实例

Chatbot实例

test_atten.py 脚本,测试并展示 attention 的热力图

TensorFlow alert

Test in

import tensorflow as tf
tf.__version__ >= '1.4.0' and tf.__version__ <= '1.5.0'

TensorFlow的API总是变,不能保证后续的更新兼容

本repo本质是一个学习性质的repo,作者只是希望尽量保持代码的整齐、理解、可读,并不对不同平台(尤其windows)的兼容,或者后续更新做保证,对不起

Related work

As mention in the head of sequence_to_sequence.py, At beginning, the code is heavily borrow from here

I have modified a lot of code, some Chinese comments in the code. And fix many bugs, restructure many things, add more features.

Code was borrow heavily from:

https://github.com/JayParks/tf-seq2seq/blob/master/seq2seq_model.py

Another wonderful example is:

https://github.com/Marsan-Ma/tf_chatbot_seq2seq_antilm

Official sequence2sequence tutorial

https://www.tensorflow.org/tutorials/seq2seq

Official sequence2sequence project:

https://github.com/tensorflow/nmt

Another official sequence2sequence model:

https://github.com/tensorflow/tensor2tensor

Another seq2seq repo:

https://github.com/ematvey/tensorflow-seq2seq-tutorials

A very nice chatbot example:

https://github.com/bshao001/ChatLearner

pylint

pylintrc from here

changed indent from 2 to 4

PS. 谷歌的lint一般建议indent是2,相反百度的lint很多建议indent是4, 个人怀疑这里面有“中文”的问题,也许是因为从小习惯作文空两格?(就是四个英文空格了)

我个人是习惯4个的

just_another_seq2seq's People

Contributors

lvzhetx avatar qhduan 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

just_another_seq2seq's Issues

用其他的数据训练,一个epoch中step数减少太多

您好,
我用其他的数据集训练,该训练集的总条数为1363683,部分数据如下

- E
- M 读大学
- M 没有什么过不去的
- E
- M 我不是你爸
- M 是!否则妈妈怎么老和我说爱他呢,我都吃醋了

在训练的过程中,每个epoch的总数为1085,

而使用您给的数据集的总条数4268087,每个epoch数目可以达到26158,
26158/1085>4268087/1363683, 这个差距比较大,这是什么原因,难道是数据集没有加 “/” 的原因吗

模型启动出错

您好,真的非常感谢您公开自己的源代码
但是我遇到一个很棘手的问题,想请教一下您
我在服务器上训练的模型,服务器上可以跑模型,但是下载到本机之后restore模型报错了。但是本地训练的模型,本地启动模型也是没有问题的,按照道理,保存模型应该和机器没有关系吧
以下是报错的内容:

tensorflow.python.framework.errors_impl.DataLossError: Checksum does not match: stored 942326751 vs. calculated on the restored bytes 3112943709
[[Node: save/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ..., DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_INT32], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save/Const_0_0, save/RestoreV2/tensor_names, save/RestoreV2/shape_and_slices)]]

ValueError: Sample larger than population or is negative

您好,我用自己的数据跑程序,训练到中途出现下面的问题,该如何解决呢,谢谢

  • Traceback (most recent call last):
    File "/home/jiang.li/ENTER/envs/pytorch/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
    File "/home/jiang.li/ENTER/envs/pytorch/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
    File "../threadedgenerator.py", line 43, in _run
    for value in self._iterator:
    File "../data_utils.py", line 197, in batch_flow_bucket
    data_batch = random.sample(ind_data[choice_ind], batch_size)
    File "/home/jiang.li/ENTER/envs/pytorch/lib/python3.6/random.py", line 320, in sample
    raise ValueError("Sample larger than population or is negative")
    ValueError: Sample larger than population or is negative

epoch 7 loss=3.628152 lr=0.000777: 14%|█▍ | 4573/32280 [10:52<1:01:13, 7.54it/s]Traceback (most recent call last):
File "train.py", line 163, in
main()
File "train.py", line 159, in main
test(json.load(open('params.json')))
File "train.py", line 74, in test
x, xl, y, yl = next(flow)
File "../threadedgenerator.py", line 75, in next
raise StopIteration()
StopIteration

loss sometimes jumpes to 200

my params are {
"cell_type": "lstm",
"depth": 2,
"attention_type": "Luong",
"bidirectional": true,
"use_residual": true,
"use_dropout": false,
"time_major": true,
"hidden_units": 1024,
"optimizer": "adam",
"learning_rate": 0.001
}
and i am using fasttext pre-trained word2vec as well
batch size is 128 which i haven't changed it.
But the loss is always fluctuating and sometimes jumps to a huge number (200), it usually started as 10-20. It's cuz this version of seq2seq add a part of reinforcement learning? plz tell u how to fix it?

beam search generate the same sentence

设置beam size 为5,生成的5个结果会有一些是重复的,查看了原因是因为beam search没有在停止符的时候停止,导致加上停止符等符号,整个句子是不一样的,去掉停止符后,句子是一样的了。请问beam search生成结果在停止符的时候不停止这个问题怎么解决了?
code 中是调用如下api:
inference_decoder = BeamSearchDecoder(
cell=self.decoder_cell,
embedding=embed_and_input_proj,
start_tokens=start_tokens,
end_token=end_token,
initial_state=self.decoder_initial_state,
beam_width=self.beam_width,
output_layer=self.decoder_output_projection,
)

如何训练自己搜集的语料

本人搜集了一些语料,格式是txt,形式是(问题a回答b问题c回答d......,分行),不知道如何训练?请大神解答

ner/train_crf_loss.py Error

目前program run 在 ubuntu python3.5
在 ner/train_crf_loss.py 142 行的測試的部分,遇到以下的 Error

try load model from ./s2ss_crf.ckpt
0%| | 0/100 [00:00<?, ?it/s]Traceback (most recent call last):

File "train_crf_loss.py", line 179, in <module>
main()
File "train_crf_loss.py", line 175, in main
test(True, 'lstm', 1, False, True, False, 64, 'tanh')
File "train_crf_loss.py", line 142, in test
if rr:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

看了一下 rr 是 numpy,所以直接用 if rr: 好像會出問題
要把他轉成 list 或是用 if not np.isnan(rr).any() ?
因為 code 還沒完全理解,所以不曉得是我的過程有問題還是code問題

线程错误

python3 train.py

2019-01-23 09:16:28.072380: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not comp
iled to use: AVX2 FMA
epoch 1 loss=1.902081 lr=0.001000: 92%|#######################################################################9 | 119/129 [00:47<00:04, 2.48it/s]
Exception in thread <generator object batch_flow_bucket at 0x7fd488384eb8>:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "../threadedgenerator.py", line 43, in _run
for value in self._iterator:
File "../data_utils.py", line 197, in batch_flow_bucket
data_batch = random.sample(ind_data[choice_ind], batch_size)
File "/usr/lib/python3.5/random.py", line 315, in sample
raise ValueError("Sample larger than population")
ValueError: Sample larger than population

epoch 1 loss=1.866029 lr=0.001000: 100%|##############################################################################| 129/129 [00:51<00:00, 2.54it/s]
epoch 2 loss=1.381537 lr=0.001000: 16%|############2 | 20/129 [00:07<00:44, 2.44it/s]
Traceback (most recent call last):
File "train.py", line 163, in
main()
File "train.py", line 159, in main
test(json.load(open('params.json')))
File "train.py", line 74, in test
x, xl, y, yl = next(flow)
File "../threadedgenerator.py", line 75, in next
raise StopIteration()
StopIteration

seq2seq-ner效果询问

hi,
看到您使用 seq2seq model 来做 ner ,请问您最后得出的效果怎么样?相比与 Bi-lstm-crf 之类,
thanks,

训练出来的模型预测的时候只能出两种结果

param_json如下:
{ "bidirectional": true,
"use_residual": false,
"use_dropout": false,
"time_major": false,
"cell_type": "lstm",
"depth": 2,
"attention_type": "Bahdanau",
"hidden_units": 128,
"optimizer": "adam",
"learning_rate": 0.001,
"embedding_size": 300
}

结果:

输入: ['', '好', '你']
x [[ 3 1625 739]]
xl [3]
Greedy_pred [[2253 2599 739 3668 3]]
输出: 我是你的

输入: ['', '哪', '在', '你']
x [[ 3 1291 1485 739]]
xl [4]
Greedy_pred [[2253 2599 739 3668 3]]
输出: 我是你的

输入: ['', '字', '名', '么', '什', '叫', '你']
x [[ 3 1734 1166 604 665 1142 739]]
xl [7]
Greedy_pred [[ 30 372 30 3]]
输出: =。=

输入: ['', '吗', '友', '朋', '个', '交', '能', '们', '我']
x [[ 3 1175 1122 2664 587 650 4306 691 2253]]
xl [9]
Greedy_pred [[ 30 372 30 3]]
输出: =。=

==我尝试过100epoch、5epoch、2epoch 都是这样的结果。咋回事,请大神指教。

pretrained_embedding

你好啊,

认真看了一遍代码,发现chatbot 中pretrained_embedding部分的代码,没有预训练embedding,只是初始化为0了呢,真是麻烦您了

线程问题

我想问一下,我训练完一个epoch之后,开始第二个epoch的时候,多线程那里报错threads can only be started once,有人遇到吗?这个是怎么改的

how about the training time?

It was 10 hours per epoch in my one-GPU workstation(GTX1060). is that normal? or something wrong that slows my training process down? I am just using your offered data-set, thx.

运行 python3 extract_tmx.py 内存不够

大神你好,我16G内存,跑满了,最后写入文件的时候报 memoryerror,第一次用ElementTree,想只取一部分tu标签,如何实现,我看了api只有findall方法。或者有什么缩小内存占用的办法呢?

训练好后测试显示乱码

我是在windows下跑的,跑完后测试时的样例句子显示:
鐣 鍗 鍚 渚

然后我encode为gbk又显示[b'\xe7\x95', b'\xe5\x8d', b'\xe5\x90', b'\xe4\xbe']

最后我在linux环境下测试,同样显示:鐣 鍗 鍚 渚

求问作者的训练环境和测试环境(不会是因为不该在windows下训练吧。。。)

语料没有到百万行也可以训练吗?

各位好
我用自己蒐集的语料 在train.py时会报错
不知道是哪个部分出了问题?
因为没有像大大给的文本到400多万行
是哪边的参数需要修改吗?

谢谢

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.