farizrahman4u / recurrentshop

Framework for building complex recurrent neural networks with Keras
MIT License
765 stars 218 forks source link

Error with stateful RecurrentModel #93

Open manuelknott opened 6 years ago

manuelknott commented 6 years ago

Hello,

I am trying to build a recurrentshop.RecurrentModel() as follows. It builds the graph if stateful=False but raises an error when stateful=True. Ì worked with stateful RNNs in Keras before without any problems. Is there anything special i need to think of when building stateful RNNs with recurrentshop?

Thank you very much.

Code:

def pos_recurrent_integrated():
    def recurrent_unit():
        a_t = Input(shape=(1,), dtype='float32', name='a_i')
        ssd_tm1 = Input(shape=(1,), dtype='float32', name='ssd_i-1')
        v_tm1 = Input(shape=(1,), dtype='float32', name='v_i-1')
        x_tm1 = Input(shape=(1,), dtype='float32', name='x_i-1')

        v_t = add([Dense(1, use_bias=True)(a_t), v_tm1])

        ssd = Dense(1, activation='sigmoid')(concatenate([Lambda(lambda x: abs(x))(a_t), ssd_tm1]))
        v_t = multiply([v_t, Lambda(lambda x: K.round(x))(ssd)])

        x_t = add([Dense(1, use_bias=True)(v_t), x_tm1])

        rnn = RecurrentModel(
            input=a_t,
            output=x_t,
            initial_states=[v_tm1, x_tm1, ssd_tm1],
            final_states=[v_t, x_t, ssd],
            return_sequences=True,
            stateful=True,
            state_initializer=initializers.zeros
        )
        return rnn

    main_input = Input(shape=(1, 1), dtype='float32')
    main_output = recurrent_unit()(main_input)
    model = Model(inputs=[main_input], outputs=[main_output], name='vel_pos_model')
    model.compile(optimizer='sgd', loss='mean_squared_error')
    return model

Error:

C:\Users\Manuel\AppData\Roaming\Python\Python36\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
C:\Users\Manuel\AppData\Roaming\Python\Python36\site-packages\keras\engine\topology.py:1523: UserWarning: The list of outputs passed to the model is redundant. All outputs should only appear once. Found: [<tf.Tensor 'add_2/add:0' shape=(?, 1) dtype=float32>, <tf.Tensor 'multiply_1/mul:0' shape=(?, 1) dtype=float32>, <tf.Tensor 'add_2/add:0' shape=(?, 1) dtype=float32>, <tf.Tensor 'dense_2/Sigmoid:0' shape=(?, 1) dtype=float32>]
  ' Found: ' + str(self.outputs))
Traceback (most recent call last):
  File "C:/Users/Manuel/Dropbox/Masterarbeit/DCPS_BSD_RMS_MONITORING/dcps_bsd_rms_monitoring/velocity_estimation/keras_anns.py", line 213, in <module>
    model = pos_recurrent_integrated()
  File "C:/Users/Manuel/Dropbox/Masterarbeit/DCPS_BSD_RMS_MONITORING/dcps_bsd_rms_monitoring/velocity_estimation/keras_anns.py", line 199, in pos_recurrent_integrated
    main_output = recurrent_unit()(main_input)
  File "C:\Users\Manuel\AppData\Local\Programs\Python\Python36\lib\site-packages\recurrentshop-1.0.0-py3.6.egg\recurrentshop\engine.py", line 476, in __call__
  File "C:\Users\Manuel\AppData\Local\Programs\Python\Python36\lib\site-packages\recurrentshop-1.0.0-py3.6.egg\recurrentshop\engine.py", line 353, in build
  File "C:\Users\Manuel\AppData\Local\Programs\Python\Python36\lib\site-packages\recurrentshop-1.0.0-py3.6.egg\recurrentshop\engine.py", line 421, in reset_states
  File "C:\Users\Manuel\AppData\Roaming\Python\Python36\site-packages\keras\backend\tensorflow_backend.py", line 680, in zeros
    return variable(tf.constant_initializer(0., dtype=tf_dtype)(shape),
  File "C:\Users\Manuel\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\init_ops.py", line 200, in __call__
    self.value, dtype=dtype, shape=shape, verify_shape=verify_shape)
  File "C:\Users\Manuel\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\constant_op.py", line 208, in constant
    value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "C:\Users\Manuel\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 380, in make_tensor_proto
    if shape is not None and np.prod(shape, dtype=np.int64) == 0:
  File "C:\Users\Manuel\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\fromnumeric.py", line 2566, in prod
    out=out, **kwargs)
  File "C:\Users\Manuel\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\_methods.py", line 35, in _prod
    return umr_prod(a, axis, dtype, out, keepdims)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
MikeShlis commented 6 years ago

I got it to work by adjusting some attributes: 1) Moved all your shapes into batch_shape=(batch_size,)+( _rest_ofshape) 2) This could be due to version we have, so this may not be a required adjustment for you, but I had to change the initializer to be same size as hidden state list --> So i changed it to ['zeros', 'zeros', 'zeros']

Doing both of these it ran just fine. Couple of Notes: 1) stateful with time-series length of 1 wont even adjust the recurrent weights because it cant propogate through time 2) All stateful models in keras require shape to be altered to batch_shape