LSTM 的输入管道与时间序列数据使用在 Tensorflow 中使用具有多个.csv的大型数据集



目前,我可以使用一个基于本教程的 csv 文件训练 LSTM 网络:https://machinelearningmastery.com/how-to-develop-lstm-models-for-time-series-forecasting/

此代码生成滑动窗口,其中保存了最后n_steps的特征以预测实际目标(类似于:Keras LSTM - 使用生成器中的 Tensorflow 数据集 API 馈送序列数据(:

#%% Import
import pandas as pd
import tensorflow as tf
from tensorflow.python.keras.models import Sequential, model_from_json
from tensorflow.python.keras.layers import LSTM
from tensorflow.python.keras.layers import Dense
# for path 
import pathlib
import os
#%% Define functions
# Function to split multivariate input data into samples according to the number of timesteps (n_steps) used for the prediction ("sliding window")
def split_sequences(sequences, n_steps):
X, y = list(), list()
for i in range(len(sequences)):
# find end of this pattern
end_ix = i + n_steps
# check if beyond maximum index of input data
if end_ix > len(sequences):
break
# gather input and output parts of the data in corresponding format (depending on n_steps)
seq_x, seq_y = sequences[i:end_ix, :-1], sequences[end_ix-1, -1]
X.append(seq_x)
y.append(seq_y)
#Append: Adds its argument as a single element to the end of a list. The length of the list increases by one.
return array(X), array(y)
# Set source files
csv_train_path = os.path.join(dir_of_file, 'SimulationData', 'SimulationTrainData', 'SimulationTrainData001.csv')
# Load data
df_train = pd.read_csv(csv_train_path, header=0, parse_dates=[0], index_col=0)

#%% Select features and target
features_targets_considered = ['Fz1', 'Fz2', 'Fz3', 'Fz4', 'Fz5', 'Fz_res']
n_features = len(features_targets_considered)-1 # substract the target 
features_targets_train = df_train[features_targets_considered]
# "Convert" to array
train_values = features_targets_train.values
# Set number of previous timesteps, which are considered to predict 
n_steps = 100
# Convert into input (400x5) and output (1) values 
X, y = split_sequences(train_values, n_steps)
X_test, y_test = split_sequences(test_values, n_steps)

#%% Define model
model = Sequential()
model.add(LSTM(200, activation='relu', return_sequences=True, input_shape=(n_steps, n_features)))
model.add(LSTM(200, activation='relu', return_sequences=True))
model.add(LSTM(200, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
#%% Fit model
history = model.fit(X, y, epochs=200, verbose=1)

我现在想扩展此示例,以使用不同的 csv 文件有效地训练网络。在数据文件夹中,我有文件"SimulationTrainData001.csv","SimulationTrainData002.csv",...,"SimulationTrainData300.csv"(约14 GB(。 为了实现这一点,我尝试采用这个输入管道示例的代码:https://www.tensorflow.org/guide/data#consuming_sets_of_files,它在一定程度上工作。我可以通过此更改显示文件夹中的训练文件:

# Set source folders
csv_train_path = os.path.join(dir_of_file, 'SimulationData', 'SimulationTrainData')
csv_train_path = pathlib.Path(csv_train_path)
#%% Show five example files from training folder
list_ds = tf.data.Dataset.list_files(str(csv_train_path/'*'))
for f in list_ds.take(5):
print(f.numpy())

一个问题是,在示例中,文件是花朵的图片而不是时间序列值,我不知道在什么时候可以使用split_sequences(sequences, n_steps)函数来创建滑动窗口以提供必要的数据格式来训练 LSTM 网络。

此外,据我所知,如果对不同文件的生成窗口进行洗牌,则训练过程会更好。我可以在每个 csv 文件上使用split_sequences(sequences, n_steps)函数(生成X_testy_test(并将结果加入一个大变量或文件中并打乱窗口,但我认为这不是一种有效的方法,如果n_steps将被更改,也必须重做。

如果有人能提出一个(既定的(方法或示例来预处理我的数据,我将不胜感激。

您可以在使用这些文件集后使用 TimeSeriesGenerator。
这是参考链接。

根据文档: ''' 此类采用以相等间隔收集的数据点序列,以及步幅、历史记录长度等时间序列参数,以生成用于训练/验证的批次。 '''

提供了单变量和多变量方案的示例

单变量示例


from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, LSTM 
import numpy as np
import tensorflow as tf
# define dataset
series = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# reshape to [10, 1]
n_features = 1
series = series.reshape((len(series), n_features))
# define generator
n_input = 2
generator = TimeseriesGenerator(series, series, length=n_input, batch_size=8)
# create model
model = Sequential()
model.add(LSTM(100, activation='relu', input_shape=(n_input, n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit_generator(generator, steps_per_epoch=1, epochs=500, verbose=1)
#sample prediction
inputs = np.array([9, 10]).reshape((1, n_input, n_features))
result = model.predict(inputs, verbose=0)
print(result)

多变量示例

from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, LSTM 
import numpy as np
import tensorflow as tf
# define dataset
in_seq1 = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
in_seq2 = np.array([15, 25, 35, 45, 55, 65, 75, 85, 95, 105])
# reshape series
in_seq1 = in_seq1.reshape((len(in_seq1), 1))
in_seq2 = in_seq2.reshape((len(in_seq2), 1))
# horizontally stack columns
dataset = np.hstack((in_seq1, in_seq2))
# define generator
n_features = dataset.shape[1]
n_input = 2
generator = TimeseriesGenerator(dataset, dataset, length=n_input, batch_size=8)
# define model
model = Sequential()
model.add(LSTM(100, activation='relu', input_shape=(n_input, n_features)))
model.add(Dense(2))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit_generator(generator, steps_per_epoch=1, epochs=500, verbose=1)
# make a one step prediction out of sample
inputs = np.array([[90, 95], [100, 105]]).reshape((1, n_input, n_features))
result = model.predict(inputs, verbose=1)
print(result)

注意:所有这些都是使用谷歌合作实验室模拟的

最新更新