0

我在 tensorflow 上运行 keras,试图实现一个多维 LSTM 网络来预测线性连续目标变量,每个示例的单个值(return_sequences = False)。我的序列长度为 10,特征数(暗淡)为 11。这就是我运行的:

import pprint, pickle
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers import LSTM

# Input sequence
wholeSequence = [[0,0,0,0,0,0,0,0,0,2,1],
                 [0,0,0,0,0,0,0,0,2,1,0],
                 [0,0,0,0,0,0,0,2,1,0,0],
                 [0,0,0,0,0,0,2,1,0,0,0],
                 [0,0,0,0,0,2,1,0,0,0,0],
                 [0,0,0,0,2,1,0,0,0,0,0],
                 [0,0,0,2,1,0,0,0,0,0,0],
                 [0,0,2,1,0,0,0,0,0,0,0],
                 [0,2,1,0,0,0,0,0,0,0,0],
                 [2,1,0,0,0,0,0,0,0,0,0]]

# Preprocess Data:
wholeSequence = np.array(wholeSequence, dtype=float) # Convert to NP array.
data = wholeSequence
target = np.array([20])

# Reshape training data for Keras LSTM model
data = data.reshape(1, 10, 11)
target = target.reshape(1, 1, 1)

# Build Model
model = Sequential()
model.add(LSTM(11, input_shape=(10, 11), unroll=True, return_sequences=False))
model.add(Dense(11))
model.add(Activation('linear'))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(data, target, nb_epoch=1, batch_size=1, verbose=2)

并得到错误 ValueError: Error when checks target: expected activation_1 to have 2 dimensions, but got array with shape (1, 1, 1) Not sure what should the activation layer should get (shape wise) 感谢任何帮助

4

1 回答 1

0

如果您只想拥有一个线性输出神经元,您可以简单地使用具有一个隐藏单元的密集层并在那里提供激活。然后,您的输出可以是一个没有重塑的向量 - 我调整了您给定的示例代码以使其工作:

wholeSequence = np.array(wholeSequence, dtype=float) # Convert to NP array.
data = wholeSequence
target = np.array([20])

# Reshape training data for Keras LSTM model
data = data.reshape(1, 10, 11)

# Build Model
model = Sequential()
model.add(LSTM(11, input_shape=(10, 11), unroll=True, return_sequences=False))
model.add(Dense(1, activation='linear'))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(data, target, nb_epoch=1, batch_size=1, verbose=2)
于 2017-06-25T19:56:41.603 回答