Prueba de red neuronal recurrente (RNN)


In [8]:
import numpy as np
import tensorflow as tf
from tensorflow.contrib import rnn

class SeriesPredictor:
    def __init__(self, input_dim, seq_size, hidden_dim=10):
        
        self.input_dim = input_dim
        self.seq_size = seq_size
        self.hidden_dim = hidden_dim
        
        self.W_out = tf.Variable(tf.random_normal([hidden_dim,1]), name='W_out')
        self.b_out = tf.Variable(tf.random_normal([1]), name='b_out')
        self.x = tf.placeholder(tf.float32, [None, seq_size, input_dim])
        self.y = tf.placeholder(tf.float32, [None, seq_size])
        
        self.cost = tf.reduce_mean(tf.square(self.model() - self.y))
        self.train_op = tf.train.AdamOptimizer().minimize(self.cost)
        
        self.saver = tf.train.Saver()
        
    def model(self):
        cell = rnn.BasicLSTMCell(self.hidden_dim)
        outputs, states = tf.nn.dynamic_rnn(cell, self.x, dtype=tf.float32)
        num_examples = tf.shape(self.x)[0]
        W_repeated = tf.tile(tf.expand_dims(self.W_out, 0), [num_examples, 1, 1])
        
        out = tf.matmul(outputs, W_repeated) + self.b_out
        out = tf.squeeze(out)
        return out
    
    def train(self, train_x, train_y, epochs=1000):
        with tf.Session() as sess:
            tf.get_variable_scope().reuse_variables()
            sess.run(tf.global_variables_initializer())
            for i in range(epochs):
                _, mse = sess.run([self.train_op, self.cost], feed_dict={self.x: train_x, self.y: train_y})
                if i % 100 == 0:
                    print(i,mse)
            save_path = self.saver.save(sess, './checkpoints/rnn_model.ckpt')
            print('Model saved to {}'.format(save_path))
    
    def test(self, test_x):
        with tf.Session() as sess:
            tf.get_variable_scope().reuse_variables()
            self.saver.restore(sess, './checkpoints/rnn_model.ckpt')
            output = sess.run(self.model(), feed_dict={self.x: test_x})
            print(output)

El siguiente codigo no corre en Jupyter notebooks por cuestiones de como maneja el scope de las variables. Pero para probarlo, se incluye en esta misma carpeta el archivo tf_RNN_basic.py con exactamente el mismo codigo


In [10]:
if __name__ == '__main__':
    predictor = SeriesPredictor(input_dim=1, seq_size=4, hidden_dim=10)

    train_x = [[[1],[2],[5],[6]],
               [[5],[7],[7],[8]],
               [[3],[4],[5],[7]]]

    train_y = [[1,3,7,11],
               [5,12,14,15],
               [3,7,9,12]]

    predictor.train(train_x, train_y)

    test_x = [[[1],[2],[3],[4]],
              [[4],[5],[6],[7]]]

    predictor.test(test_x)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-e8fd01801d58> in <module>()
      1 if __name__ == '__main__':
----> 2     predictor = SeriesPredictor(input_dim=1, seq_size=4, hidden_dim=10)
      3 
      4     train_x = [[[1],[2],[5],[6]],
      5                [[5],[7],[7],[8]],

<ipython-input-8-1363adc140f6> in __init__(self, input_dim, seq_size, hidden_dim)
     15         self.y = tf.placeholder(tf.float32, [None, seq_size])
     16 
---> 17         self.cost = tf.reduce_mean(tf.square(self.model() - self.y))
     18         self.train_op = tf.train.AdamOptimizer().minimize(self.cost)
     19 

<ipython-input-8-1363adc140f6> in model(self)
     22     def model(self):
     23         cell = rnn.BasicLSTMCell(self.hidden_dim)
---> 24         outputs, states = tf.nn.dynamic_rnn(cell, self.x, dtype=tf.float32)
     25         num_examples = tf.shape(self.x)[0]
     26         W_repeated = tf.tile(tf.expand_dims(self.W_out, 0), [num_examples, 1, 1])

/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py in dynamic_rnn(cell, inputs, sequence_length, initial_state, dtype, parallel_iterations, swap_memory, time_major, scope)
    543         swap_memory=swap_memory,
    544         sequence_length=sequence_length,
--> 545         dtype=dtype)
    546 
    547     # Outputs of _dynamic_rnn_loop are always shaped [time, batch, depth].

/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py in _dynamic_rnn_loop(cell, inputs, initial_state, parallel_iterations, swap_memory, sequence_length, dtype)
    710       loop_vars=(time, output_ta, state),
    711       parallel_iterations=parallel_iterations,
--> 712       swap_memory=swap_memory)
    713 
    714   # Unpack final output if not using output tuples.

/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, name)
   2624     context = WhileContext(parallel_iterations, back_prop, swap_memory, name)
   2625     ops.add_to_collection(ops.GraphKeys.WHILE_CONTEXT, context)
-> 2626     result = context.BuildLoop(cond, body, loop_vars, shape_invariants)
   2627     return result
   2628 

/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py in BuildLoop(self, pred, body, loop_vars, shape_invariants)
   2457       self.Enter()
   2458       original_body_result, exit_vars = self._BuildLoop(
-> 2459           pred, body, original_loop_vars, loop_vars, shape_invariants)
   2460     finally:
   2461       self.Exit()

/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py in _BuildLoop(self, pred, body, original_loop_vars, loop_vars, shape_invariants)
   2407         structure=original_loop_vars,
   2408         flat_sequence=vars_for_body_with_tensor_arrays)
-> 2409     body_result = body(*packed_vars_for_body)
   2410     if not nest.is_sequence(body_result):
   2411       body_result = [body_result]

/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py in _time_step(time, output_ta_t, state)
    695           skip_conditionals=True)
    696     else:
--> 697       (output, new_state) = call_cell()
    698 
    699     # Pack state if using state tuples

/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py in <lambda>()
    681 
    682     input_t = nest.pack_sequence_as(structure=inputs, flat_sequence=input_t)
--> 683     call_cell = lambda: cell(input_t, state)
    684 
    685     if sequence_length is not None:

/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.py in __call__(self, inputs, state, scope)
    177       else:
    178         c, h = array_ops.split(value=state, num_or_size_splits=2, axis=1)
--> 179       concat = _linear([inputs, h], 4 * self._num_units, True, scope=scope)
    180 
    181       # i = input_gate, j = new_input, f = forget_gate, o = output_gate

/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.py in _linear(args, output_size, bias, bias_start, scope)
    745   with vs.variable_scope(scope) as outer_scope:
    746     weights = vs.get_variable(
--> 747         "weights", [total_arg_size, output_size], dtype=dtype)
    748     if len(args) == 1:
    749       res = math_ops.matmul(args[0], weights)

/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in get_variable(name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape, custom_getter)
    986       collections=collections, caching_device=caching_device,
    987       partitioner=partitioner, validate_shape=validate_shape,
--> 988       custom_getter=custom_getter)
    989 get_variable_or_local_docstring = (
    990     """%s

/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in get_variable(self, var_store, name, shape, dtype, initializer, regularizer, trainable, collections, caching_device, partitioner, validate_shape, custom_getter)
    888           collections=collections, caching_device=caching_device,
    889           partitioner=partitioner, validate_shape=validate_shape,
--> 890           custom_getter=custom_getter)
    891 
    892   def _get_partitioned_variable(self,

/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in get_variable(self, name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape, custom_getter)
    346           reuse=reuse, trainable=trainable, collections=collections,
    347           caching_device=caching_device, partitioner=partitioner,
--> 348           validate_shape=validate_shape)
    349 
    350   def _get_partitioned_variable(

/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in _true_getter(name, shape, dtype, initializer, regularizer, reuse, trainable, collections, caching_device, partitioner, validate_shape)
    331           initializer=initializer, regularizer=regularizer, reuse=reuse,
    332           trainable=trainable, collections=collections,
--> 333           caching_device=caching_device, validate_shape=validate_shape)
    334 
    335     if custom_getter is not None:

/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/python/ops/variable_scope.py in _get_single_variable(self, name, shape, dtype, initializer, regularizer, partition_info, reuse, trainable, collections, caching_device, validate_shape)
    637                          " Did you mean to set reuse=True in VarScope? "
    638                          "Originally defined at:\n\n%s" % (
--> 639                              name, "".join(traceback.format_list(tb))))
    640       found_var = self._vars[name]
    641       if not shape.is_compatible_with(found_var.get_shape()):

ValueError: Variable rnn/basic_lstm_cell/weights already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

  File "/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.py", line 747, in _linear
    "weights", [total_arg_size, output_size], dtype=dtype)
  File "/Users/daza/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.py", line 179, in __call__
    concat = _linear([inputs, h], 4 * self._num_units, True, scope=scope)
  File "<ipython-input-1-08b83ab1467c>", line 24, in model
    outputs, states = tf.nn.dynamic_rnn(cell, self.x, dtype=tf.float32)