Keras:循环网络的正确数据格式是什么?

时间:2016-07-17 10:42:24

标签: neural-network theano deep-learning keras recurrent-neural-network

我正在尝试构建一个对序列(多维数据流)进行分类的循环网络。我必须遗漏一些东西,因为在运行我的代码时:

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)

我得到输出(截断):

    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)).

这完全是数据格式的问题。

1 个答案:

答案 0 :(得分:0)

对我来说,当我去真实数据集上尝试时,问题就解决了。不同之处在于,在真实数据集中,我有超过1个标签。因此,此代码的数据集示例如下:

(...)
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