Jim*_*Zer 17 deep-learning tensorflow recurrent-neural-network
我正在使用TensorFlow v:1.1,我想使用tf.contrib.seq2seq api 实现序列模型.但是我很难理解如何使用提供的所有函数(BasicDecoder,Dynamic_decode,Helper,Training Helper ...)来构建我的模型.
这是我的设置:我想将一系列特征向量"(翻译"):( batch_size,encoder_max_seq_len,feature_dim)转换为不同长度的序列(batch_size,decoder_max_len,1).
我已经有一个带有LSTM单元的RNN 编码器,我得到了它想要作为初始输入提供给解码器的最终状态.我已经有了解码器的单元,MultiRNNCell LSM.你能帮助我使用tf.contrib.seq2seq2和dynamic_decode 的功能构建最后一部分(会非常感谢示例代码或解释)吗?
这是我的代码:
import tensorflow as tf
from tensorflow.contrib import seq2seq
from tensorflow.contrib import rnn
import math
from data import gen_sum_2b2
class Seq2SeqModel:
def __init__(self,
in_size,
out_size,
embed_size,
n_symbols,
cell_type,
n_units,
n_layers):
self.in_size = in_size
self.out_size = out_size
self.embed_size = embed_size
self.n_symbols = n_symbols
self.cell_type = cell_type
self.n_units = n_units
self.n_layers = n_layers
self.build_graph()
def build_graph(self):
self.init_placeholders()
self.init_cells()
self.encoder()
self.decoder_train()
self.loss()
self.training()
def init_placeholders(self):
with tf.name_scope('Placeholders'):
self.encoder_inputs = tf.placeholder(shape=(None, None, self.in_size),
dtype=tf.float32, name='encoder_inputs')
self.decoder_targets = tf.placeholder(shape=(None, None),
dtype=tf.int32, name='decoder_targets')
self.seqs_len = tf.placeholder(dtype=tf.int32)
self.batch_size = tf.placeholder(tf.int32, name='dynamic_batch_size')
self.max_len = tf.placeholder(tf.int32, name='dynamic_seq_len')
decoder_inputs = tf.reshape(self.decoder_targets, shape=(self.batch_size,
self.max_len, self.out_size))
self.decoder_inputs = tf.cast(decoder_inputs, tf.float32)
self.eos_step = tf.ones([self.batch_size, 1], dtype=tf.float32, name='EOS')
self.pad_step = tf.zeros([self.batch_size, 1], dtype=tf.float32, name='PAD')
def RNNCell(self):
c = self.cell_type(self.n_units, reuse=None)
c = rnn.MultiRNNCell([self.cell_type(self.n_units) for i in range(self.n_layers)])
return c
def init_cells(self):
with tf.variable_scope('RNN_enc_cell'):
self.encoder_cell = self.RNNCell()
with tf.variable_scope('RNN_dec_cell'):
self.decoder_cell = rnn.OutputProjectionWrapper(self.RNNCell(), self.n_symbols)
def encoder(self):
with tf.variable_scope('Encoder'):
self.init_state = self.encoder_cell.zero_state(self.batch_size, tf.float32)
_, self.encoder_final_state = tf.nn.dynamic_rnn(self.encoder_cell, self.encoder_inputs,
initial_state=self.init_state)
Run Code Online (Sandbox Code Playgroud)
vij*_*y m 19
解码层:
解码包括的,因为他们之间的分歧两个部分中training和inference:
在特定时间步长的解码器输入总是来自前一时间步的输出.但是在训练期间,输出固定 为实际目标(实际目标作为输入反馈),这表明可以提高性能.
这两个都是使用来自的方法处理的tf.contrib.seq2seq.
主要功能decoder是:seq2seq.dynamic decoder()执行动态解码:
tf.contrib.seq2seq.dynamic_decode(decoder,maximum_iterations)
这需要一个Decoder实例和maximum_iterations=maximum seq length输入.
1.1 Decoder实例来自:
seq2seq.BasicDecoder(cell, helper, initial_state,output_layer)
输入是:( cell一个RNNCell实例), helper(辅助实例),initial_state(解码器的初始状态应该是编码器的输出状态)和output_layer(可选的密集层作为输出以进行预测)
1.2 RNNCell实例可以是a rnn.MultiRNNCell().
1.3 helper实例是,在不同的一个training和inference.期间training,我们希望将输入馈送到解码器,而在此期间inference,我们希望解码器的输出time-step (t)作为输入传递给解码器time step (t+1).
对于培训:我们使用辅助函数:
seq2seq.TrainingHelper(inputs, sequence_length),它只读取输入.
对于推理:我们称之为辅助函数:
seq2seq.GreedyEmbeddingHelper() or seqseq.SampleEmbeddingHelper()不同之处在于它是否使用argmax() or sampling(from a distribution)输出并通过嵌入层传递结果以获得下一个输入.
放在一起:Seq2Seq模型
encoder layer并将其作为a传递initial_state给解码器.decoder train和decoder inference使用的输出seq2seq.dynamic_decoder().当您调用这两种方法时,请确保共享权重.(variable_scope用于重复使用权重)seq2seq.sequence_loss.| 归档时间: |
|
| 查看次数: |
3307 次 |
| 最近记录: |