【问题标题】:Keras: What is the correct data format for recurrent networks?Keras:循环网络的正确数据格式是什么?
【发布时间】:2016-11-20 02:17:39
【问题描述】:

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

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

这是数据格式的问题吗?

【问题讨论】:

  • 你使用什么版本的 Keras?在最新版本中 - 此代码适用于我的计算机。
  • Keras v1.0.6; Theano v0.8.2。仍然没有得到这个工作。
  • 尝试在最后一层将 'softmax' 更改为 'sigmoid'。
  • 没有帮助。使用不同的目标损失函数也无济于事(尝试了 MSE)。更改优化器也无济于事(尝试过 sgd)。

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


【解决方案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

【讨论】:

    猜你喜欢
    • 2017-05-08
    • 2020-09-13
    • 2021-09-22
    • 2013-08-04
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 2022-10-29
    • 1970-01-01
    相关资源
    最近更新 更多