Tensorflow --BeamSearch
github:https://github.com/zle1992/Seq2Seq-Chatbot
1、 注意在infer阶段,需要需要reuse,
2、If you are using the BeamSearchDecoder
with a cell wrapped in AttentionWrapper
, then you must ensure that:
- The encoder output has been tiled to
beam_width
viatf.contrib.seq2seq.tile_batch
(NOTtf.tile
). - The
batch_size
argument passed to thezero_state
method of this wrapper is equal totrue_batch_size * beam_width
. - The initial state created with
zero_state
above contains acell_state
value containing properly tiled final state from the encoder.
import tensorflow as tf
from tensorflow.python.layers.core import Dense BEAM_WIDTH = 5
BATCH_SIZE = 128 # INPUTS
X = tf.placeholder(tf.int32, [BATCH_SIZE, None])
Y = tf.placeholder(tf.int32, [BATCH_SIZE, None])
X_seq_len = tf.placeholder(tf.int32, [BATCH_SIZE])
Y_seq_len = tf.placeholder(tf.int32, [BATCH_SIZE]) # ENCODER
encoder_out, encoder_state = tf.nn.dynamic_rnn(
cell = tf.nn.rnn_cell.BasicLSTMCell(128),
inputs = tf.contrib.layers.embed_sequence(X, 10000, 128),
sequence_length = X_seq_len,
dtype = tf.float32) # DECODER COMPONENTS
Y_vocab_size = 10000
decoder_embedding = tf.Variable(tf.random_uniform([Y_vocab_size, 128], -1.0, 1.0))
projection_layer = Dense(Y_vocab_size) # ATTENTION (TRAINING)
with tf.variable_scope('shared_attention_mechanism'):
attention_mechanism = tf.contrib.seq2seq.LuongAttention(
num_units = 128,
memory = encoder_out,
memory_sequence_length = X_seq_len) decoder_cell = tf.contrib.seq2seq.AttentionWrapper(
cell = tf.nn.rnn_cell.BasicLSTMCell(128),
attention_mechanism = attention_mechanism,
attention_layer_size = 128) # DECODER (TRAINING)
training_helper = tf.contrib.seq2seq.TrainingHelper(
inputs = tf.nn.embedding_lookup(decoder_embedding, Y),
sequence_length = Y_seq_len,
time_major = False)
training_decoder = tf.contrib.seq2seq.BasicDecoder(
cell = decoder_cell,
helper = training_helper,
initial_state = decoder_cell.zero_state(BATCH_SIZE,tf.float32).clone(cell_state=encoder_state),
output_layer = projection_layer)
with tf.variable_scope('decode_with_shared_attention'):
training_decoder_output, _, _ = tf.contrib.seq2seq.dynamic_decode(
decoder = training_decoder,
impute_finished = True,
maximum_iterations = tf.reduce_max(Y_seq_len))
training_logits = training_decoder_output.rnn_output # BEAM SEARCH TILE
encoder_out = tf.contrib.seq2seq.tile_batch(encoder_out, multiplier=BEAM_WIDTH)
X_seq_len = tf.contrib.seq2seq.tile_batch(X_seq_len, multiplier=BEAM_WIDTH)
encoder_state = tf.contrib.seq2seq.tile_batch(encoder_state, multiplier=BEAM_WIDTH) # ATTENTION (PREDICTING)
with tf.variable_scope('shared_attention_mechanism', reuse=True):
attention_mechanism = tf.contrib.seq2seq.LuongAttention(
num_units = 128,
memory = encoder_out,
memory_sequence_length = X_seq_len) decoder_cell = tf.contrib.seq2seq.AttentionWrapper(
cell = tf.nn.rnn_cell.BasicLSTMCell(128),
attention_mechanism = attention_mechanism,
attention_layer_size = 128) # DECODER (PREDICTING)
predicting_decoder = tf.contrib.seq2seq.BeamSearchDecoder(
cell = decoder_cell,
embedding = decoder_embedding,
start_tokens = tf.tile(tf.constant([1], dtype=tf.int32), [BATCH_SIZE]),
end_token = 2,
initial_state = decoder_cell.zero_state(BATCH_SIZE * BEAM_WIDTH,tf.float32).clone(cell_state=encoder_state),
beam_width = BEAM_WIDTH,
output_layer = projection_layer,
length_penalty_weight = 0.0)
with tf.variable_scope('decode_with_shared_attention', reuse=True):
predicting_decoder_output, _, _ = tf.contrib.seq2seq.dynamic_decode(
decoder = predicting_decoder,
impute_finished = False,
maximum_iterations = 2 * tf.reduce_max(Y_seq_len))
predicting_logits = predicting_decoder_output.predicted_ids[:, :, 0] print('successful')
参考:
https://gist.github.com/higepon/eb81ba0f6663a57ff1908442ce753084
https://www.tensorflow.org/api_docs/python/tf/contrib/seq2seq/BeamSearchDecoder
https://github.com/tensorflow/nmt#beam-search
Tensorflow --BeamSearch的更多相关文章
- tensorflow 笔记13:了解机器翻译,google NMT,Attention
一.关于Attention,关于NMT 未完待续... 以google 的 nmt 代码引入 探讨下端到端: 项目地址:https://github.com/tensorflow/nmt 机器翻译算是 ...
- Effective Tensorflow[转]
Effective TensorFlow Table of Contents TensorFlow Basics Understanding static and dynamic shapes Sco ...
- Tensorflow 官方版教程中文版
2015年11月9日,Google发布人工智能系统TensorFlow并宣布开源,同日,极客学院组织在线TensorFlow中文文档翻译.一个月后,30章文档全部翻译校对完成,上线并提供电子书下载,该 ...
- tensorflow学习笔记二:入门基础
TensorFlow用张量这种数据结构来表示所有的数据.用一阶张量来表示向量,如:v = [1.2, 2.3, 3.5] ,如二阶张量表示矩阵,如:m = [[1, 2, 3], [4, 5, 6], ...
- 用Tensorflow让神经网络自动创造音乐
#————————————————————————本文禁止转载,禁止用于各类讲座及ppt中,违者必究————————————————————————# 前几天看到一个有意思的分享,大意是讲如何用Ten ...
- tensorflow 一些好的blog链接和tensorflow gpu版本安装
pading :SAME,VALID 区别 http://blog.csdn.net/mao_xiao_feng/article/details/53444333 tensorflow实现的各种算法 ...
- tensorflow中的基本概念
本文是在阅读官方文档后的一些个人理解. 官方文档地址:https://www.tensorflow.org/versions/r0.12/get_started/basic_usage.html#ba ...
- kubernetes&tensorflow
谷歌内部--Borg Google Brain跑在数十万台机器上 谷歌电商商品分类深度学习模型跑在1000+台机器上 谷歌外部--Kubernetes(https://github.com/kuber ...
- tensorflow学习
tensorflow安装时遇到gcc: error trying to exec 'as': execvp: No such file or directory. 截止到2016年11月13号,源码编 ...
随机推荐
- Auth模块、Forms组件
Auth模块 auth模块是Django自带的用户认证模块: 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这 ...
- python学习:条件语句if、else
条件语句: 1.if...else...; 2.if...elif...esle 举例: 1.if...else... “age_of_princal = 56 guess_age = int(i ...
- angular.isDefined()
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Node.js_express_route 路由
route 路由 (kiss my ass ヾ(゚∀゚ゞ) 请求方式 get / post / put / delete____查 / 增 / 改 / 删 路由路径 ...
- mvc文件下载
public ActionResult xiazai(int id) { DataTable dt = bll.chaxun(id); //获取文件名字 var filename = dt.Rows[ ...
- java学习之路--I/O流
java基础学习总结——流 一.JAVA流式输入/输出原理
- 107个JS常用方法(持续更新中)
1.输出语句:document.write(""); 2.JS中的注释为//3.传统的HTML文档顺序是:document->html->(head,body)4.一个 ...
- 一次php访问sql server 2008的API接口的采坑
2018年6月21日17:17:09,注意:不是详细文档,新手可能会看不懂 windows下安装 项目是sql server 2008的k3,php连接数据库写的API,因为是买的时候是别人的程序,测 ...
- jenkins安装与配置---windows系统
记录安装过程中的步骤及遇到的坑,以做借鉴 服务器主机系统: windows9 ; 已安装开发环境: jdk8 ; 我采用的是war包直接运行的方式: 1.下载最新的版本(一个 WAR 文件).Jen ...
- 《Mysql 锁》
一:什么是锁? - 锁是计算机协调多个进程或纯线程并发访问某一资源的机制. - 通俗的来说,锁是一种对资源的保护形式. 二:锁分类 - 表级锁 - 开销小,加锁快,没有死锁,锁定粒度大,发生锁冲突的概 ...