def RNN(parameters, input, model, initial_state):
# The model is:
# 1. input
# 2. linear layer
# 3 - n. LSTM layers
# n+1. linear layer
# n+1. output
input = tf.verify_tensor_all_finite(input, "Input not finite!")
# input shape: (batch_size, n_steps, n_input)
input = tf.transpose(input, [1, 0, 2]) # permute n_steps and batch_size
input = tf.verify_tensor_all_finite(input, "Input not finite2!")
# Reshape to prepare input to the linear layer
input = tf.reshape(input, [-1, parameters['n_input']]) # (n_steps*batch_size, n_input)
input = tf.verify_tensor_all_finite(input, "Input not finite3!")
# 1. layer, linear activation for each batch and step.
if (model.has_key('input_weights')):
input = tf.matmul(input, model['input_weights']) + model['input_bias']
# input = tf.nn.dropout(input, model['keep_prob'])
# Split data because rnn cell needs a list of inputs for the RNN inner loop,
# that is, a n_steps length list of tensors shaped: (batch_size, n_inputs)
# This is not well documented, but check for yourself here: https://goo.gl/NzA5pX
input = tf.split(0, parameters['n_steps'], input) # n_steps * (batch_size, :)
initial_state = tf.verify_tensor_all_finite(initial_state, "Initial state not finite!")
# Note: States is shaped: batch_size x cell.state_size
outputs, states = rnn.rnn(model['rnn_cell'], input, initial_state=initial_state)
#outputs[-1] = tf.Print(outputs[-1], [outputs[-1]], "LSTM Output: ", summarize = 100)
lastOutput = tf.verify_tensor_all_finite(outputs[-1], "LSTM Outputs not finite!")
#lastOutput = tf.nn.dropout(lastOutput, model['keep_prob'])
# Only the last output is interesting for error back propagation and prediction.
# Note that all batches are handled together here.
raw_output = tf.matmul(lastOutput, model['output_weights']) + model['output_bias']
raw_output = tf.verify_tensor_all_finite(raw_output, "Raw output not finite!")
n_mixtures = parameters['n_mixtures']
batch_size = parameters['batch_size']
# And now, instead of just outputting the expected value, we output mixture distributions.
# The number of mixtures is intuitively the number of possible actions the target can take.
# The output is divided into triplets of n_mixtures mixture parameters for the 2 absolute position coordinates.
output = softmax_mixtures(raw_output, n_mixtures, batch_size)
#output = tf.Print(output, [output], "Output: ", summarize = 100)
output = tf.verify_tensor_all_finite(output, "Final output not finite!")
return (output, states)
# Returns the generative LSTM stack created based on the parameters.
# Processes one input at a time.
# Input shape is: 1 x (parameters['n_input'])
# State shape is: 1 x (parameters['n_input'])
评论列表
文章目录