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号,源码编 ...
随机推荐
- SQLServer 2014 内存优化表
内存优化表是 SQLServer 2014 的新功能,它是可以将表放在内存中,这会明显提升DML性能.关于内存优化表,更多可参考两位大侠的文章:SQL Server 2014新特性探秘(1)-内存数据 ...
- React Component Lifecycle(生命周期)
生命周期 所谓生命周期,就是一个对象从开始生成到最后消亡所经历的状态,理解生命周期,是合理开发的关键.RN 组件的生命周期整理如下图: 如图,可以把组件生命周期大致分为三个阶段: 第一阶段:是组件第一 ...
- PostgreSQL自学笔记:9 索引
9 索引 9.1 索引简介 索引是对数据库表中一列或多列值进行排序的一种结构,使用 索引可提高数据库中特定数据的查询速度 9.1.1 索引的含义和特点 索引是一种单独的.存储在磁盘上的数据库结构,他们 ...
- markdown改变字体颜色和大小
markdown中改变字体颜色与大小方法同html 先看例子 <font face="黑体">我是黑体字</font> 我是黑体字 <font fac ...
- C++ 控制台推箱子小游戏
// 游戏菜单.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #in ...
- Servlet.service() for servlet [jsp] in context with path [/Healthy_manager] threw exception [Unable to compile class for JSP] with root cause java.lang.IllegalArgumentException: Page directive: inval
严重: Servlet.service() for servlet [jsp] in context with path [/Healthy_manager] threw exception [Una ...
- Selenium 3 学习小结
4个类+常用的46个方法 从以下知识内容对selenium 3自动化框架进行初步学习: 1.安装selenium pip install selenium pip list 2.驱动.关闭浏览器 首先 ...
- vue发送请求----vue-resource
使用插件vue-resource 官方提供的接口,在vue官网找不到 但在github中可以找到 安装:cnpm install vue-resource --save 第一步:注意要加--save, ...
- 使用ajax分页查询
controller: /** * 查询所有用户/查找指定用户 * 分页+搜索 * */@RequestMapping("/findClientBySize")@ResponseB ...
- Mybatis获取传参
取自 https://blog.csdn.net/weixin_38303684/article/details/78886375 mybatis中SQL接受的参数分为:(1)基本类型(2)对象(3 ...