Search code examples
neural-networktheanodeep-learningkerasrecurrent-neural-network

Keras: What is the correct data format for recurrent networks?


I am trying to build a recurrent network which classifies sequences (multidimensional data streams). I must be missing something, since while running my code:

from keras.models import Sequential
from keras.layers import LSTM, Dropout, Activation
import numpy as np

ils = 10            # input layer size
ilt = 11            # input layer time steps
hls = 12            # hidden layer size
nhl = 2             # number of hidden layers
ols = 1             # output layer size
p = 0.2             # dropout probability
f_a = 'relu'        # activation function
opt = 'rmsprop'     # optimizing function

#
# Building the model
#
model = Sequential()

# The input layer
model.add(LSTM(hls, input_shape=(ilt, ils), return_sequences=True))
model.add(Activation(f_a))
model.add(Dropout(p))

# Hidden layers
for i in range(nhl - 1):
    model.add(LSTM(hls, return_sequences=True))
    model.add(Activation(f_a))
    model.add(Dropout(p))

# Output layer
model.add(LSTM(ols, return_sequences=False))
model.add(Activation('softmax'))

model.compile(optimizer=opt, loss='binary_crossentropy')

#
# Making test data and fitting the model
#

m_train, n_class = 1000, 2
data = np.array(np.random.random((m_train, ilt, ils)))
labels = np.random.randint(n_class, size=(m_train, 1))


model.fit(data, labels, nb_epoch=10, batch_size=32)

I get output (truncated):

    Using Theano backend.
 line 611, in __call__
        node = self.make_node(*inputs, **kwargs)
      File "/home/koala/.local/lib/python2.7/site-packages/theano/scan_module/scan_op.py", line 430, in make_node
        new_inputs.append(format(outer_seq, as_var=inner_seq))
      File "/home/koala/.local/lib/python2.7/site-packages/theano/scan_module/scan_op.py", line 422, in format
        rval = tmp.filter_variable(rval)
      File "/home/koala/.local/lib/python2.7/site-packages/theano/tensor/type.py", line 233, in filter_variable
        self=self))
    TypeError: Cannot convert Type TensorType(float32, 3D) (of Variable Subtensor{:int64:}.0) into Type TensorType(float32, (False, False, True)). You can try to manually convert Subtensor{:int64:}.0 into a TensorType(float32, (False, False, True)).

Is this a problem with the data format at all.


Solution

  • For me the problem was fixed when I went and tried it on my real dataset. The difference being that in the real dataset I have more than 1 label. So an example of dataset on which this code works is:

    (...)
    ols = 2  # Output layer size
    (...)
    
    m_train, n_class = 1000, ols
    data = np.array(np.random.random((m_train, ilt, ils)))
    labels = np.random.randint(n_class, size=(m_train, 1))
    # Make labels onehot
    onehot_labels = np.zeros(shape=(labels.shape[0], ols))
    onehot_labels[np.arange(labels.shape[0]), labels.astype(np.int)] = 1