keras(lstm) - 使用return_sequences = True时必要的形状

时间:2017-11-12 16:19:25

标签: keras lstm keras-layer

我正在尝试将LSTM网络安装到sin函数中。目前,据我所知,Keras,我的代码只预测下一个值。根据这个链接:Many to one and many to many LSTM examples in Keras它是一个多对一的模型。但是,我的目标是实现多对多模型。基本上,我希望能够在给定时间内预测10个值。当我想要使用时 return_sequences=True(见行model.add(..)),应该是解决方案,发生以下错误:

ValueError: Error when checking target: expected lstm_8 to have 3 dimensions, but got array with shape (689, 1)

不幸的是,我完全不知道为什么会这样。使用return_sequences=True时,输入形状是否需要一般规则?那么到底我需​​要改变什么呢?谢谢你的帮助。

import pandas
import numpy as np
import matplotlib.pylab as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import sklearn

from keras.models import Sequential
from keras.layers import Activation, LSTM
from keras import optimizers
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot

#generate sin function with noise
x = np.arange(0, 100, 0.1)
noise = np.random.uniform(-0.1, 0.1, size=(1000,))
Y = np.sin(x) + noise

# Perform feature scaling
scaler = MinMaxScaler()
Y = scaler.fit_transform(Y.reshape(-1, 1))

# split in train and test
train_size = int(len(Y) * 0.7)
test_size = len(Y) - train_size
train, test = Y[0:train_size,:], Y[train_size:len(Y),:]

def create_dataset(dataset, look_back=1):
    dataX, dataY = [], []
    for i in range(len(dataset)-look_back-1):
         a = dataset[i:(i+look_back), 0]
         dataX.append(a)
         dataY.append(dataset[i + look_back, 0])
    return np.array(dataX), np.array(dataY)

# reshape into X=t and Y=t+1
look_back = 10
X_train, y_train = create_dataset(train, look_back)
X_test, y_test = create_dataset(test, look_back)

# LSTM network expects the input data in form of [samples, time steps, features]
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
np.set_printoptions(threshold=np.inf)

# compile model
model = Sequential()
model.add(LSTM(1, input_shape=(look_back, 1)))#, return_sequences=True))  <== uncomment this
model.compile(loss='mean_squared_error', optimizer='adam')
SVG(model_to_dot(model).create(prog='dot', format='svg'))

model.fit(X_train, y_train, validation_data=(X_test, y_test), 
batch_size=10, epochs=10, verbose=2)
prediction = model.predict(X_test, batch_size=1, verbose=0)
prediction.reshape(-1) 
#Transform back to original representation
Y = scaler.inverse_transform(Y)
prediction = scaler.inverse_transform(prediction)
plt.plot(np.arange(0,Y.shape[0]), Y)
plt.plot(np.arange(Y.shape[0] - X_test.shape[0] , Y.shape[0]), prediction, 'red')
plt.show()
error = mean_squared_error(y_test, prediction)
print(error)

3 个答案:

答案 0 :(得分:2)

问题不是输入,而是输出。 错误说:“检查目标时出错”,target = y_train和y_test。

因为你的lstm返回一个序列(return_sequences = True),输出维数将是:(n_batch,lookback,1)。

您可以使用model.summary()

进行验证
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_1 (LSTM)                (None, 10, 1)             12        
=================================================================
Total params: 12
Trainable params: 12
Non-trainable params: 0
_________________________________________________________________

您需要更改create_dataset函数,以便塑造每个基本事实(回顾,1)。

你可能想做的事情:
对于列车组中的每个序列x,其y将是下一个程序序列 例如,假设我们想要更容易学习,seqeuence将是之前的数字加1 - &gt; 1,2,3,4,5,6,7,8,9,10。 对于loockback = 4:

X_train[0] = 1,2,3,4   
y_train[0] will be: 2,3,4,5  
X_train[1] = 2,3,4,5  
y_train[1] will be: 3,4,5,6  
and so on...

答案 1 :(得分:1)

我已经按照@DvirSamuel的建议模拟了数据,并提供了LSTM和FNN的代码。请注意,对于LSTM,如果在上一层中包含network_lstm.add(layers.Dense(1, activation = None)),则需要return_sequences = True

## Simulate data.

np.random.seed(20180826)

Z = np.random.randint(0, 10, size = (11000, 1))

for i in range(10):

     Z = np.concatenate((Z, (Z[:, -1].reshape(Z.shape[0], 1) + 1)), axis = 1)

X = Z[:, :-1]

Y = Z[:,  1:]

print(X.shape)

print(Y.shape)

## Training and validation data.

split = 10000

X_train = X[:split, :]
X_valid = X[split:, :]

Y_train = Y[:split, :]
Y_valid = Y[split:, :]

print(X_train.shape)
print(Y_train.shape)
print(X_valid.shape)
print(Y_valid.shape)

LSTM模型的代码:

## LSTM model.

X_lstm_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)
X_lstm_valid = X_valid.reshape(X_valid.shape[0], X_valid.shape[1], 1)

Y_lstm_train = Y_train.reshape(Y_train.shape[0], Y_train.shape[1], 1)
Y_lstm_valid = Y_valid.reshape(Y_valid.shape[0], Y_valid.shape[1], 1)

# Define model.

network_lstm = models.Sequential()
network_lstm.add(layers.LSTM(64, activation = 'relu', input_shape = (X_lstm_train.shape[1], 1),
    return_sequences = True))
network_lstm.add(layers.Dense(1, activation = None))

network_lstm.summary()

# Compile model.

network_lstm.compile(optimizer = 'rmsprop', loss = 'mean_squared_error')

# Fit model.

history_lstm = network_lstm.fit(X_lstm_train, Y_lstm_train, epochs = 5, batch_size = 32, verbose = True,
    validation_data = (X_lstm_valid, Y_lstm_valid))

## Extract loss over epochs and predict.

# Extract loss.

loss_lstm = history_lstm.history['loss']
val_loss_lstm = history_lstm.history['val_loss']
epochs_lstm = range(1, len(loss_lstm) + 1)

plt.plot(epochs_lstm, loss_lstm, 'black', label = 'Training Loss')
plt.plot(epochs_lstm, val_loss_lstm, 'red', label = 'Validation Loss')
plt.title('LSTM: Training and Validation Loss')
plt.legend()

plt.title('First in Sequence')

plt.scatter(Y_train[:, 0], network_lstm.predict(X_lstm_train)[:, 0], alpha = 0.1)
plt.xlabel('Actual')
plt.ylabel('Predicted')
plt.show()

plt.scatter(Y_valid[:, 0], network_lstm.predict(X_lstm_valid)[:, 0], alpha = 0.1)
plt.xlabel('Actual')
plt.ylabel('Predicted')
plt.show()

plt.title('Last in Sequence')

plt.scatter(Y_train[:, -1], network_lstm.predict(X_lstm_train)[:, -1], alpha = 0.1)
plt.xlabel('Actual')
plt.ylabel('Predicted')
plt.show()

plt.scatter(Y_valid[:, -1], network_lstm.predict(X_lstm_valid)[:, -1], alpha = 0.1)
plt.xlabel('Actual')
plt.ylabel('Predicted')
plt.show()

FNN模型的代码:

## FNN model.

# Define model.

network_fnn = models.Sequential()
network_fnn.add(layers.Dense(64, activation = 'relu', input_shape = (X_train.shape[1],)))
network_fnn.add(Dense(10, activation = None))

network_fnn.summary()

# Compile model.

network_fnn.compile(optimizer = 'rmsprop', loss = 'mean_squared_error')

# Fit model.

history_fnn = network_fnn.fit(X_train, Y_train, epochs = 5, batch_size = 32, verbose = True,
    validation_data = (X_valid, Y_valid))

## Extract loss over epochs.

# Extract loss.

loss_fnn = history_fnn.history['loss']
val_loss_fnn = history_fnn.history['val_loss']
epochs_fnn = range(1, len(loss_fnn) + 1)

plt.plot(epochs_fnn, loss_fnn, 'black', label = 'Training Loss')
plt.plot(epochs_fnn, val_loss_fnn, 'red', label = 'Validation Loss')
plt.title('FNN: Training and Validation Loss')
plt.legend()

plt.title('First in Sequence')

plt.scatter(Y_train[:, 1], network_fnn.predict(X_train)[:, 1], alpha = 0.1)
plt.xlabel('Actual')
plt.ylabel('Predicted')
plt.show()

plt.scatter(Y_valid[:, 1], network_fnn.predict(X_valid)[:, 1], alpha = 0.1)
plt.xlabel('Actual')
plt.ylabel('Predicted')
plt.show()

plt.title('Last in Sequence')

plt.scatter(Y_train[:, -1], network_fnn.predict(X_train)[:, -1], alpha = 0.1)
plt.xlabel('Actual')
plt.ylabel('Predicted')
plt.show()

plt.scatter(Y_valid[:, -1], network_fnn.predict(X_valid)[:, -1], alpha = 0.1)
plt.xlabel('Actual')
plt.ylabel('Predicted')
plt.show()

答案 2 :(得分:0)

不是吗?

  

X_train = np.reshape(X_train,(X_train.shape [0],X_train.shape [1],1))

     

X_test = np.reshape(X_test,(X_test.shape [0],X_test.shape [1],1))

像这样:

  

X_train = np.reshape((X_train.shape [0],X_train.shape [1],1))

     

X_test = np.reshape((X_test.shape [0],X_test.shape [1],1))

这是您的问题吗? (xD后1年)