【发布时间】:2018-03-21 16:09:23
【问题描述】:
我正在尝试训练一个 LSTM 循环神经网络,用于序列分类。
我的数据格式如下:
Input: [1,5,2,3,6,2, ...] -> Output: 1
Input: [2,10,4,6,12,4, ...] -> Output: 1
Input: [4,1,7,1,9,2, ...] -> Output: 2
Input: [1,3,5,9,10,20, ...] -> Output: 3
.
.
.
所以基本上我想提供一个序列作为输入并获得一个整数作为输出。
每个输入序列的长度 = 2000 个浮点数,我有大约 1485 个样本用于训练
输出只是一个从 1 到 10 的整数
这是我尝试做的:
# Get the training numpy 2D array for the input (1485X 2000).
# Each element is an input sequence of length 2000
# eg: [ [1,2,3...], [4,5,6...], ... ]
x_train = get_training_x()
# Get the training numpy 2D array for the outputs (1485 X 1).
# Each element is an integer output for the corresponding input from x_train
# eg: [ 1, 2, 3, ...]
y_train = get_training_y()
# Create the model
model = Sequential()
model.add(LSTM(100, input_shape=(x_train.shape)))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(x_train, y_train, nb_epoch=3, batch_size=64)
我收到以下错误:
Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (1485, 2000)
我尝试改用这个:
model.add(LSTM(100, input_shape=(1485, 1, 2000)))
但这次又报错了:
ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4
谁能解释我的输入形状是什么?我做错了什么?
谢谢
【问题讨论】:
标签: python neural-network keras lstm rnn