I'm trying to create an LSTM network in Tensorflow and I'm lost in terminology/basics. I have n time series examples so X=xn, where xi=[[x11x12,x13],...,[xm1xm2,xm3]] and where xii is a float. First of all I want to train a model that given the start of a sequence ([x11x12,x13]) I can predict the rest of the sequence. Then later I hope to include a classifier to predict which binary class each xi belongs to.
So my problem is what do I feed in to the start and pull out the end of my model? So far I have something that looks like the below
class ETLSTM(object):
"""docstring for ETLSTM"""
def __init__(self, isTraining, config):
super(ETLSTM, self).__init__()
# This needs to be tidied
self.batchSize = batchSize = config.batchSize
self.numSteps = numSteps = config.numSteps
self.numInputs = numInputs = config.numInputs
self.numLayers = numLayers = config.numLayers
lstmSize = config.lstm_size
DORate = config.keep_prob
self.input_data = tf.placeholder(tf.float32, [batchSize, numSteps,
numInputs])
self.targets = tf.placeholder(tf.float32, [batchSize, numSteps,
numInputs])
lstmCell = rnn_cell.BasicLSTMCell(lstmSize, forgetbias=0.0)
if(isTraining and DORate < 1):
lstmCell = tf.nn.rnn_cell.DropoutWrapper(lstmCell,
output_keep_prob=DORate)
cell = tf.nn.rnn_cell.MultiRNNCell([lstmCell]*numLayers)
self._initial_state = cell.zero_state(batchSize, tf.float32)
# This won't work with my data, need to find what goes in...
with tf.device("/cpu:0"):
embedding = tf.get_variable("embedding", [vocab_size, size])
inputs = tf.nn.embedding_lookup(embedding, self._input_data)
if(isTraining and DORate < 1):
inputs = tf.nn.dropout(inputs, DORate)
EDIT:
Specifically, how to I finish the __init__
function so that it is compatible with my data?
An RNN predicts the value of N+1 given the values from 1 to N so far. (LSTM is just one way to implement an RNN cell.)
The short answer is:
The longer answer is:
Your example just shows the initialization of the model. You also need to implement a training function to run back propagation as well as a sample function that predicts the results.
The following code snippets are mix & match and are for illustration purposes only...
For training just feed in your complete sequences with start + rest in your data iterator.
For example in the sample code tensorflow/models/rnn/ptb_word_lm.py the training loop computes a cost function for batches of input_data against targets (which are the input_data shifted by one timestep)
Note the data iterator in tensorflow/models/rnn/reader.py returns the input data as 'x' and the targets as 'y' which are just shifted one step forward from x. (You would need to create a data iterator like this that packages your set of training sequences.)
After training, you run the model forward to make predictions for sequences by feeding in the start of your sequence start_x=[X1, X2, X3,...]...this snippets assumes binary values representing classes, you'd have to adjust the sampling function for float values.