检查输入时出错:预期lstm_132_input具有3个维度,但数组的形状为(23,1,3,1)

时间:2019-07-01 08:28:45

标签: python-3.x pandas time deep-learning lstm

我有一个包括温度,湿度和风的数据集。在这里,我想预测下一个小时的未来温度值。

我用LSTM预测未来的温度值。 但是当我运行模型时,它显示了此错误Error when checking input: expected lstm_132_input to have 3 dimensions, but got array with shape (23, 1, 3, 1)

有人可以帮我解决这个问题吗?

这是我的代码:

    import datetime
    import time
    from sklearn.metrics import mean_squared_error
    import matplotlib.pyplot as plt 
    from matplotlib.dates import DateFormatter
    import numpy as np
    import pandas as pd 
    from sklearn.preprocessing import MinMaxScaler

    from sklearn import preprocessing
    from keras.layers.core import Dense, Dropout, Activation
    from keras.activations import linear
    from keras.layers.recurrent import LSTM
    from keras.models import Sequential
    from sklearn.preprocessing import MinMaxScaler


    data = pd.read_csv('data6.csv' , sep=',')
    data['date'] = pd.to_datetime(data['date'] + " " + data['time'], format='%m/%d/%Y %H:%M:%S')
    data.set_index('time', inplace=True)
    data = data.values
    data = data.astype('float32')
    # normalize the dataset
    def create_data(train,X,n_out=1):
    #data = np.reshape(train, (train.shape[0], train_shape[1], train_shape[2]))
    x,y=list(),list()
    start =0
    for _ in range(len(data)):
        in_end = start+X
        out_end= in_end + n_out
        if out_end < len(data):
            x_input = data[start:in_end]
            x.append(x_input)
            y.append(data[in_end:out_end,0])
        start +=1
    return np.array(x),np.array(y)
    scaler = MinMaxScaler()
    data = scaler.fit_transform(data)
    # split into train and test sets
    train = int(len(data) * 0.6)
    test = len(data) - train
    train, test = data[0:train,:], data[train:len(data),:]
    X=1
    x_train, y_train = create_data(train,X)
    x_test, y_test = create_data(test,X)
    x_train=x_train.reshape(x_train.shape +(1,))
    x_test=x_test.reshape(x_test.shape + (1,))


    n_timesteps, n_features, n_outputs = x_train.shape[1], x_train.shape[2], x_train.shape[1]


    model = Sequential()
    model.add(LSTM(8, activation='relu', input_shape=(n_timesteps, n_features)))
    model.add(Dense(8,activation='relu'))
    model.add(Dense(n_outputs))
    model.compile(loss='mse', optimizer='adam')
    # fit network
    model.fit(x_train,y_train, epochs=10,batch_size=1, verbose=0)

我的csv文件:

My csv file.

我的错误:

enter image description here

enter image description here

模型摘要:

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要在最后一层添加激活

    model = Sequential()
model.add(LSTM(8, activation='relu', input_shape=(n_timesteps, n_features)))
model.add(Dense(8,activation='relu'))
# here
model.add(Dense(n_outputs,activation='relu'))
model.compile(loss='mse', optimizer='adam')
# fit network
model.fit(x_train,y_train, epochs=10,batch_size=1, verbose=0)