简体   繁体   English

时间序列数据中 LSTM 训练测试拆分中的问题

[英]Problem in LSTM train-test split in time series data

I am trying to make a train set and test set with my csv file to train an LSTM.我正在尝试使用我的 csv 文件制作训练集和测试集来训练 LSTM。 The csv file looks like this: csv 文件如下所示:

        datetime      invno          inkw             outkw    Total    wind_spd        temp    pres             ts
2021-12-01  00:00:00    1       0.0                    0.0      0.0         4.6         -0.9    1007.7      1638284400.0
2021-12-01  00:00:00    4       0.0                    0.0      0.0,        4.6         -0.9    1007.7      1638284400.0
2021-12-01  00:00:00    2       0.0                    0.0      0.0,        4.6         -0.9    1007.7      1638284400.0
2021-12-01  00:00:00    3       0.0                    0.0      0.0,        4.6         -0.9    1007.7      1638284400.0
2021-12-01  00:00:00    5       0.0                    0.0      0.0,        4.6         -0.9    1007.7      1638284400.0
2021-12-01  01:00:00    1       0.0                    0.0      0.0,        9.8         -1.3    1007.7      1638288000.0
2021-12-01  01:00:00    4       0.0                    0.0      0.0,        9.8         -1.3    1007.7      1638288000.0
..........  ........    .       ...                    ....     ...         ...         ....    ...         ......
..........  ........    .       ...                    ....     ...         ...         ....    ...         ......
2021-12-10  17:00:00    2       0.06735057830810548    0.087    23.9        2.3         -1.2    1007.6      163828800.0
2021-12-10  17:00:00    3       0.03403729248046875    0.091    24.1        2.3         -1.2    1007.6      163828800.0
2021-12-10  17:00:00    4       0.08401119232177734    0.09     24.3        2.3         -1.2    1007.6      163828800.0
2021-12-10  17:00:00    5       0.08356260681152344    0.087    24.6        2.3         -1.2    1007.6      163828800.0

Dataset shape after I make train and test set:制作训练集和测试集后的数据集形状:

(1170, 9)
Training shape: (930, 30, 8)
Testing shape: (185, 30, 8)

This is my code:这是我的代码:

import os
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
#from sklearn.externals import joblib
import joblib
import seaborn as sns
sns.set(color_codes=True)
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from numpy.random import seed
#from tensorflow import set_random_seed
import tensorflow
tensorflow.random.set_seed

import tensorflow as tf
#tf.logging.set_verbosity(tf.logging.ERROR)

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Dropout, Dense, LSTM, TimeDistributed, RepeatVector
from tensorflow.keras.models import Model
from tensorflow.keras import regularizers
import plotly.graph_objects as go

dataset = pd.read_csv('./data/combined.csv')
print(dataset.shape)
dataset.fillna(0, inplace=True)
dataset = dataset.set_index('datetime')

train = dataset[:'2021-12-08 23:00:00']
test = dataset['2021-12-08 23:00:00':]

scaler = StandardScaler()
scaler = scaler.fit(train)

train = scaler.transform(train)
test = scaler.transform(test)

TIME_STEPS=30

def create_sequences(X, y, time_steps=TIME_STEPS):
    Xs, ys = [], []
    for i in range(len(X)-time_steps):
        Xs.append(X.iloc[i:(i+time_steps)].values)
        ys.append(y.iloc[i+time_steps])
    
    return np.array(Xs), np.array(ys)

X_train, y_train = create_sequences(train, train)
X_test, y_test = create_sequences(test, test)

print(f'Training shape: {X_train.shape}')
print(f'Testing shape: {X_test.shape}')

model = Sequential()
model.add(LSTM(128, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Dropout(rate=0.2))
model.add(RepeatVector(X_train.shape[1]))
model.add(LSTM(128, return_sequences=True))
model.add(Dropout(rate=0.2))
model.add(TimeDistributed(Dense(X_train.shape[2])))
model.compile(optimizer='adam', loss='mae')
model.summary()

history = model.fit(X_train, y_train, epochs=100, batch_size=16, validation_split=0.1 , shuffle=False)

Whenever I run this code I get following error:每当我运行此代码时,我都会收到以下错误:

Traceback (most recent call last):
  File "/Users/sudip/Desktop/workspace/local_work/LSTM_api/test-1.py", line 58, in <module>
    X_train, y_train = create_sequences(train, train)
  File "/Users/sudip/Desktop/workspace/local_work/LSTM_api/test-1.py", line 53, in create_sequences
    Xs.append(X.iloc[i:(i+time_steps)].values)
AttributeError: 'numpy.ndarray' object has no attribute 'iloc'

After removing iloc and values I get following error:删除ilocvalues后,我收到以下错误:

Epoch 1/100
Traceback (most recent call last):
  File "/Users/sudip/Desktop/workspace/local_work/LSTM_api/test-1.py", line 77, in <module>
    history = model.fit(X_train, y_train, epochs=100, batch_size=16, validation_split=0.1 , shuffle=False)
  File "/Users/sudip/Desktop/workspace/env/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/Users/sudip/Desktop/workspace/env/lib/python3.9/site-packages/tensorflow/python/eager/execute.py", line 58, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError:  Incompatible shapes: [16,30,8] vs. [16,8]
         [[node gradient_tape/mean_absolute_error/sub/BroadcastGradientArgs
 (defined at /Users/sudip/Desktop/workspace/env/lib/python3.9/site-packages/keras/optimizer_v2/optimizer_v2.py:464)
]] [Op:__inference_train_function_5593]

Errors may have originated from an input operation.
Input Source operations connected to node gradient_tape/mean_absolute_error/sub/BroadcastGradientArgs:

I think errors are from input shapes.我认为错误来自输入形状。 Can I get some help to fix this issue?我可以得到一些帮助来解决这个问题吗?

How can I split train and test from timeseries data based on date and time?如何根据日期和时间从时间序列数据中拆分训练和测试?

There is a problem with the data shape.数据形状有问题。 The input shape and the output shape of your network are the same, but the shapes of X_train and y_train are not.您的网络的输入形状和 output 形状相同,但 X_train 和 y_train 的形状不同。
A simple model that would do the job:一个简单的 model 可以完成这项工作:

model = Sequential()
model.add(LSTM(128, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Dense(y_train.shape[1]))
model.compile(optimizer='adam', loss='mae')
model.summary()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM