如何为人类活动识别时间序列分类开发 RNN 模型
人类活动识别是将由专用线束或智能电话记录的加速度计数据序列分类为已知的明确定义的运动的问题。
该问题的经典方法涉及基于固定大小的窗口和训练机器学习模型(例如决策树的集合)的时间序列数据中的手工制作特征。困难在于此功能工程需要该领域的强大专业知识。
最近,诸如 LSTM 之类的循环神经网络和利用一维卷积神经网络或 CNN 的变化等深度学习方法已经被证明可以在很少或没有数据的情况下提供具有挑战性的活动识别任务的最新结果特征工程,而不是使用原始数据的特征学习。
在本教程中,您将发现三种循环神经网络架构,用于对活动识别时间序列分类问题进行建模。
完成本教程后,您将了解:
- 如何开发一种用于人类活动识别的长短期记忆循环神经网络。
- 如何开发一维卷积神经网络 LSTM 或 CNN-LSTM 模型。
- 如何针对同一问题开发一维卷积 LSTM 或 ConvLSTM 模型。
让我们开始吧。
如何开发用于人类活动识别的 RNN 模型时间序列分类
照片由 Bonnie Moreland ,保留一些权利。
教程概述
本教程分为四个部分;他们是:
- 使用智能手机数据集进行活动识别
- 开发 LSTM 网络模型
- 开发 CNN-LSTM 网络模型
- 开发 ConvLSTM 网络模型
使用智能手机数据集进行活动识别
人类活动识别,或简称为 HAR,是基于使用传感器的移动痕迹来预测人正在做什么的问题。
标准的人类活动识别数据集是 2012 年推出的“使用智能手机数据集的活动识别”。
它由 Davide Anguita 等人准备并提供。来自意大利热那亚大学的 2013 年论文“使用智能手机进行人类活动识别的公共领域数据集”中对该数据集进行了全面描述。该数据集在他们的 2012 年论文中用机器学习算法建模,标题为“使用多类硬件友好支持向量机在智能手机上进行人类活动识别。“
数据集可用,可以从 UCI 机器学习库免费下载:
该数据来自 30 名年龄在 19 至 48 岁之间的受试者,其执行六项标准活动中的一项,同时佩戴记录运动数据的腰部智能手机。记录执行活动的每个受试者的视频,并从这些视频手动标记移动数据。
以下是在记录其移动数据的同时执行活动的主体的示例视频。
<iframe allow=“accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture” allowfullscreen=“” frameborder=“0” height=“375” src=“https://www.youtube.com/embed/XOEN9W05_4A?feature=oembed” width=“500”></iframe>
进行的六项活动如下:
- 步行
- 走上楼
- 走楼下
- 坐在
- 常设
- 铺设
记录的运动数据是来自智能手机的 x,y 和 z 加速度计数据(线性加速度)和陀螺仪数据(角速度),特别是三星 Galaxy S II。以 50Hz(即每秒 50 个数据点)记录观察结果。每个受试者进行两次活动;一旦设备在左侧,一次设备在右侧。
原始数据不可用。相反,可以使用预处理版本的数据集。预处理步骤包括:
- 使用噪声滤波器预处理加速度计和陀螺仪。
- 将数据拆分为 2.56 秒(128 个数据点)的固定窗口,重叠率为 50%。将加速度计数据分割为重力(总)和身体运动分量。
特征工程应用于窗口数据,并且提供具有这些工程特征的数据的副本。
从每个窗口提取在人类活动识别领域中常用的许多时间和频率特征。结果是 561 元素的特征向量。
根据受试者的数据,将数据集分成训练(70%)和测试(30%)组。训练 21 个,测试 9 个。
使用旨在用于智能手机的支持向量机(例如定点算术)的实验结果导致测试数据集的预测准确度为 89%,实现与未修改的 SVM 实现类似的结果。
该数据集是免费提供的,可以从 UCI 机器学习库下载。
数据以单个 zip 文件的形式提供,大小约为 58 兆字节。此下载的直接链接如下:
下载数据集并将所有文件解压缩到当前工作目录中名为“HARDataset”的新目录中。
开发 LSTM 网络模型
在本节中,我们将为人类活动识别数据集开发长期短期记忆网络模型(LSTM)。
LSTM 网络模型是一种循环神经网络,能够学习和记忆长输入数据序列。它们适用于由长序列数据组成的数据,最多 200 到 400 个时间步长。它们可能非常适合这个问题。
该模型可以支持多个并行的输入数据序列,例如加速度计的每个轴和陀螺仪数据。该模型学习从观察序列中提取特征以及如何将内部特征映射到不同的活动类型。
使用 LSTM 进行序列分类的好处是,他们可以直接从原始时间序列数据中学习,反过来不需要领域专业知识来手动设计输入功能。该模型可以学习时间序列数据的内部表示,并且理想地实现与适合具有工程特征的数据集版本的模型相当的表现。
本节分为四个部分;他们是:
- 加载数据
- 拟合和评估模型
- 总结结果
- 完整的例子
加载数据
第一步是将原始数据集加载到内存中。
原始数据中有三种主要信号类型:总加速度,车身加速度和车身陀螺仪。每个都有 3 个数据轴。这意味着每个时间步长总共有九个变量。
此外,每个数据系列已被划分为 2.56 秒数据或 128 个时间步长的重叠窗口。这些数据窗口对应于上一节中工程特征(行)的窗口。
这意味着一行数据具有(128 * 9)或 1,152 个元素。这比前一节中 561 个元素向量的大小小一倍,并且可能存在一些冗余数据。
信号存储在 train 和 test 子目录下的/ Inertial Signals /目录中。每个信号的每个轴都存储在一个单独的文件中,这意味着每个训练和测试数据集都有九个要加载的输入文件和一个要加载的输出文件。在给定一致的目录结构和文件命名约定的情况下,我们可以批量加载这些文件。
输入数据采用 CSV 格式,其中列由空格分隔。这些文件中的每一个都可以作为 NumPy 数组加载。下面的load_file()
函数在给定文件填充路径的情况下加载数据集,并将加载的数据作为 NumPy 数组返回。
# load a single file as a numpy array
def load_file(filepath):
dataframe = read_csv(filepath, header=None, delim_whitespace=True)
return dataframe.values
然后,我们可以将给定组(训练或测试)的所有数据加载到单个三维 NumPy 数组中,其中数组的尺寸为[_ 样本,时间步长,特征 _]。
为了更清楚,有 128 个时间步和 9 个特征,其中样本数是任何给定原始信号数据文件中的行数。
下面的load_group()
函数实现了这种行为。 dstack()NumPy 函数允许我们将每个加载的 3D 数组堆叠成单个 3D 数组,其中变量在第三维(特征)上分开。
# load a list of files into a 3D array of [samples, timesteps, features]
def load_group(filenames, prefix=''):
loaded = list()
for name in filenames:
data = load_file(prefix + name)
loaded.append(data)
# stack group so that features are the 3rd dimension
loaded = dstack(loaded)
return loaded
我们可以使用此功能加载给定组的所有输入信号数据,例如训练或测试。
下面的load_dataset_group()
函数使用目录之间的一致命名约定加载单个组的所有输入信号数据和输出数据。
# load a dataset group, such as train or test
def load_dataset_group(group, prefix=''):
filepath = prefix + group + '/Inertial Signals/'
# load all 9 files as a single array
filenames = list()
# total acceleration
filenames += ['total_acc_x_'+group+'.txt', 'total_acc_y_'+group+'.txt', 'total_acc_z_'+group+'.txt']
# body acceleration
filenames += ['body_acc_x_'+group+'.txt', 'body_acc_y_'+group+'.txt', 'body_acc_z_'+group+'.txt']
# body gyroscope
filenames += ['body_gyro_x_'+group+'.txt', 'body_gyro_y_'+group+'.txt', 'body_gyro_z_'+group+'.txt']
# load input data
X = load_group(filenames, filepath)
# load class output
y = load_file(prefix + group + '/y_'+group+'.txt')
return X, y
最后,我们可以加载每个训练和测试数据集。
输出数据定义为类号的整数。我们必须对这些类整数进行热编码,以使数据适合于拟合神经网络多分类模型。我们可以通过调用 to_categorical()Keras 函数来实现。
下面的load_dataset()
函数实现了这种行为,并返回训练并测试 X 和 y 元素,以便拟合和评估定义的模型。
# load the dataset, returns train and test X and y elements
def load_dataset(prefix=''):
# load all train
trainX, trainy = load_dataset_group('train', prefix + 'HARDataset/')
print(trainX.shape, trainy.shape)
# load all test
testX, testy = load_dataset_group('test', prefix + 'HARDataset/')
print(testX.shape, testy.shape)
# zero-offset class values
trainy = trainy - 1
testy = testy - 1
# one hot encode y
trainy = to_categorical(trainy)
testy = to_categorical(testy)
print(trainX.shape, trainy.shape, testX.shape, testy.shape)
return trainX, trainy, testX, testy
拟合和评估模型
现在我们已将数据加载到内存中以便进行建模,我们可以定义,拟合和评估 LSTM 模型。
我们可以定义一个名为evaluate_model()
的函数,它接受训练和测试数据集,拟合训练数据集上的模型,在测试数据集上对其进行评估,并返回模型表现的估计值。
首先,我们必须使用 Keras 深度学习库来定义 LSTM 模型。该模型需要使用[_ 样本,时间步长,特征 _]进行三维输入。
这正是我们加载数据的方式,其中一个样本是时间序列数据的一个窗口,每个窗口有 128 个时间步长,时间步长有九个变量或特征。
模型的输出将是一个六元素向量,包含属于六种活动类型中每种活动类型的给定窗口的概率。
在拟合模型时需要输入和输出尺寸,我们可以从提供的训练数据集中提取它们。
n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
为简单起见,该模型被定义为顺序 Keras 模型。
我们将模型定义为具有单个 LSTM 隐藏层。接下来是一个脱落层,旨在减少模型过拟合到训练数据。最后,在使用最终输出层做出预测之前,使用密集的完全连接层来解释由 LSTM 隐藏层提取的特征。
随机梯度下降的有效 Adam 版本将用于优化网络,并且鉴于我们正在学习多类别分类问题,将使用分类交叉熵损失函数。
下面列出了该模型的定义。
model = Sequential()
model.add(LSTM(100, input_shape=(n_timesteps,n_features)))
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
该模型适用于固定数量的时期,在这种情况下为 15,并且将使用 64 个样本的批量大小,其中在更新模型的权重之前将 64 个数据窗口暴露给模型。
模型拟合后,将在测试数据集上进行评估,并返回测试数据集上拟合模型的精度。
注意,在拟合 LSTM 时,通常不对值序列数据进行混洗。这里我们在训练期间对输入数据的窗口进行随机播放(默认)。在这个问题中,我们感兴趣的是利用 LSTM 的能力来学习和提取窗口中时间步长的功能,而不是跨窗口。
下面列出了完整的evaluate_model()
函数。
# fit and evaluate a model
def evaluate_model(trainX, trainy, testX, testy):
verbose, epochs, batch_size = 0, 15, 64
n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
model = Sequential()
model.add(LSTM(100, input_shape=(n_timesteps,n_features)))
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
# evaluate model
_, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
return accuracy
网络结构或选择的超参数没有什么特别之处,它们只是这个问题的起点。
总结结果
我们无法从单一评估中判断模型的技能。
其原因是神经网络是随机的,这意味着当在相同数据上训练相同的模型配置时将产生不同的特定模型。
这是网络的一个特征,它为模型提供了自适应能力,但需要对模型进行稍微复杂的评估。
我们将多次重复对模型的评估,然后在每次运行中总结模型的表现。例如,我们可以调用evaluate_model()
共 10 次。这将导致必须总结的模型评估分数。
# repeat experiment
scores = list()
for r in range(repeats):
score = evaluate_model(trainX, trainy, testX, testy)
score = score * 100.0
print('>#%d: %.3f' % (r+1, score))
scores.append(score)
我们可以通过计算和报告绩效的均值和标准差来总结得分样本。均值给出了数据集上模型的平均精度,而标准差给出了精度与平均值的平均方差。
下面的函数summarize_results()
总结了运行的结果。
# summarize scores
def summarize_results(scores):
print(scores)
m, s = mean(scores), std(scores)
print('Accuracy: %.3f%% (+/-%.3f)' % (m, s))
我们可以将重复评估,结果收集和结果汇总捆绑到实验的主要功能中,称为 run_experiment(),如下所示。
默认情况下,在报告模型表现之前,会对模型进行 10 次评估。
# run an experiment
def run_experiment(repeats=10):
# load data
trainX, trainy, testX, testy = load_dataset()
# repeat experiment
scores = list()
for r in range(repeats):
score = evaluate_model(trainX, trainy, testX, testy)
score = score * 100.0
print('>#%d: %.3f' % (r+1, score))
scores.append(score)
# summarize results
summarize_results(scores)
完整的例子
现在我们已经拥有了所有的部分,我们可以将它们组合成一个有效的例子。
完整的代码清单如下。
# lstm model
from numpy import mean
from numpy import std
from numpy import dstack
from pandas import read_csv
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import Dropout
from keras.layers import LSTM
from keras.utils import to_categorical
from matplotlib import pyplot
# load a single file as a numpy array
def load_file(filepath):
dataframe = read_csv(filepath, header=None, delim_whitespace=True)
return dataframe.values
# load a list of files and return as a 3d numpy array
def load_group(filenames, prefix=''):
loaded = list()
for name in filenames:
data = load_file(prefix + name)
loaded.append(data)
# stack group so that features are the 3rd dimension
loaded = dstack(loaded)
return loaded
# load a dataset group, such as train or test
def load_dataset_group(group, prefix=''):
filepath = prefix + group + '/Inertial Signals/'
# load all 9 files as a single array
filenames = list()
# total acceleration
filenames += ['total_acc_x_'+group+'.txt', 'total_acc_y_'+group+'.txt', 'total_acc_z_'+group+'.txt']
# body acceleration
filenames += ['body_acc_x_'+group+'.txt', 'body_acc_y_'+group+'.txt', 'body_acc_z_'+group+'.txt']
# body gyroscope
filenames += ['body_gyro_x_'+group+'.txt', 'body_gyro_y_'+group+'.txt', 'body_gyro_z_'+group+'.txt']
# load input data
X = load_group(filenames, filepath)
# load class output
y = load_file(prefix + group + '/y_'+group+'.txt')
return X, y
# load the dataset, returns train and test X and y elements
def load_dataset(prefix=''):
# load all train
trainX, trainy = load_dataset_group('train', prefix + 'HARDataset/')
print(trainX.shape, trainy.shape)
# load all test
testX, testy = load_dataset_group('test', prefix + 'HARDataset/')
print(testX.shape, testy.shape)
# zero-offset class values
trainy = trainy - 1
testy = testy - 1
# one hot encode y
trainy = to_categorical(trainy)
testy = to_categorical(testy)
print(trainX.shape, trainy.shape, testX.shape, testy.shape)
return trainX, trainy, testX, testy
# fit and evaluate a model
def evaluate_model(trainX, trainy, testX, testy):
verbose, epochs, batch_size = 0, 15, 64
n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
model = Sequential()
model.add(LSTM(100, input_shape=(n_timesteps,n_features)))
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
# evaluate model
_, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
return accuracy
# summarize scores
def summarize_results(scores):
print(scores)
m, s = mean(scores), std(scores)
print('Accuracy: %.3f%% (+/-%.3f)' % (m, s))
# run an experiment
def run_experiment(repeats=10):
# load data
trainX, trainy, testX, testy = load_dataset()
# repeat experiment
scores = list()
for r in range(repeats):
score = evaluate_model(trainX, trainy, testX, testy)
score = score * 100.0
print('>#%d: %.3f' % (r+1, score))
scores.append(score)
# summarize results
summarize_results(scores)
# run the experiment
run_experiment()
运行该示例首先打印已加载数据集的形状,然后打印训练和测试集的形状以及输入和输出元素。这确认了样本数,时间步长和变量,以及类的数量。
接下来,创建和评估模型,并为每个模型打印调试消息。
最后,打印分数样本,然后是平均值和标准差。我们可以看到该模型表现良好,在原始数据集上实现了约 89.7%的分类准确度,标准偏差约为 1.3。
这是一个很好的结果,考虑到原始论文发表了 89%的结果,在具有重域特定特征工程的数据集上进行了训练,而不是原始数据集。
注意:鉴于算法的随机性,您的具体结果可能会有所不同。如果是这样,请尝试运行几次代码。
(7352, 128, 9) (7352, 1)
(2947, 128, 9) (2947, 1)
(7352, 128, 9) (7352, 6) (2947, 128, 9) (2947, 6)
>#1: 90.058
>#2: 85.918
>#3: 90.974
>#4: 89.515
>#5: 90.159
>#6: 91.110
>#7: 89.718
>#8: 90.295
>#9: 89.447
>#10: 90.024
[90.05768578215134, 85.91788259246692, 90.97387173396675, 89.51476077366813, 90.15948422124194, 91.10960298608755, 89.71835765184933, 90.29521547336275, 89.44689514760775, 90.02375296912113]
Accuracy: 89.722% (+/-1.371)
现在我们已经了解了如何开发用于时间序列分类的 LSTM 模型,让我们看看如何开发更复杂的 CNN LSTM 模型。
开发 CNN-LSTM 网络模型
CNN LSTM 架构涉及使用卷积神经网络(CNN)层对输入数据进行特征提取以及 LSTM 以支持序列预测。
CNN LSTM 是针对视觉时间序列预测问题以及从图像序列(例如视频)生成文本描述的应用而开发的。具体来说,问题是:
- 活动识别:生成在一系列图像中演示的活动的文本描述。
- 图像说明:生成单个图像的文本描述。
- 视频说明:生成图像序列的文本描述。
您可以在帖子中了解有关 CNN LSTM 架构的更多信息:
要了解有关组合这些模型的后果的更多信息,请参阅论文:
- 卷积,长短期记忆,完全连接的深度神经网络,2015。
CNN LSTM 模型将以块为单位读取主序列的子序列,从每个块中提取特征,然后允许 LSTM 解释从每个块提取的特征。
实现此模型的一种方法是将 128 个时间步的每个窗口拆分为 CNN 模型要处理的子序列。例如,每个窗口中的 128 个时间步长可以分成 32 个时间步长的四个子序列。
# reshape data into time steps of sub-sequences
n_steps, n_length = 4, 32
trainX = trainX.reshape((trainX.shape[0], n_steps, n_length, n_features))
testX = testX.reshape((testX.shape[0], n_steps, n_length, n_features))
然后我们可以定义一个 CNN 模型,该模型期望以 32 个时间步长和 9 个特征的长度读取序列。
整个 CNN 模型可以包裹在 TimeDistributed 层中,以允许相同的 CNN 模型在窗口的四个子序列中的每一个中读取。然后将提取的特征展平并提供给 LSTM 模型以进行读取,在最终映射到活动之前提取其自身的特征。
# define model
model = Sequential()
model.add(TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu'), input_shape=(None,n_length,n_features)))
model.add(TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu')))
model.add(TimeDistributed(Dropout(0.5)))
model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(100))
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
通常使用两个连续的 CNN 层,然后是丢失和最大池层,这是 CNN LSTM 模型中使用的简单结构。
下面列出了更新的 evaluate_model()。
# fit and evaluate a model
def evaluate_model(trainX, trainy, testX, testy):
# define model
verbose, epochs, batch_size = 0, 25, 64
n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
# reshape data into time steps of sub-sequences
n_steps, n_length = 4, 32
trainX = trainX.reshape((trainX.shape[0], n_steps, n_length, n_features))
testX = testX.reshape((testX.shape[0], n_steps, n_length, n_features))
# define model
model = Sequential()
model.add(TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu'), input_shape=(None,n_length,n_features)))
model.add(TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu')))
model.add(TimeDistributed(Dropout(0.5)))
model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(100))
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
# evaluate model
_, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
return accuracy
我们可以像上一节中的直线 LSTM 模型一样评估此模型。
完整的代码清单如下。
# cnn lstm model
from numpy import mean
from numpy import std
from numpy import dstack
from pandas import read_csv
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import Dropout
from keras.layers import LSTM
from keras.layers import TimeDistributed
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
from keras.utils import to_categorical
from matplotlib import pyplot
# load a single file as a numpy array
def load_file(filepath):
dataframe = read_csv(filepath, header=None, delim_whitespace=True)
return dataframe.values
# load a list of files and return as a 3d numpy array
def load_group(filenames, prefix=''):
loaded = list()
for name in filenames:
data = load_file(prefix + name)
loaded.append(data)
# stack group so that features are the 3rd dimension
loaded = dstack(loaded)
return loaded
# load a dataset group, such as train or test
def load_dataset_group(group, prefix=''):
filepath = prefix + group + '/Inertial Signals/'
# load all 9 files as a single array
filenames = list()
# total acceleration
filenames += ['total_acc_x_'+group+'.txt', 'total_acc_y_'+group+'.txt', 'total_acc_z_'+group+'.txt']
# body acceleration
filenames += ['body_acc_x_'+group+'.txt', 'body_acc_y_'+group+'.txt', 'body_acc_z_'+group+'.txt']
# body gyroscope
filenames += ['body_gyro_x_'+group+'.txt', 'body_gyro_y_'+group+'.txt', 'body_gyro_z_'+group+'.txt']
# load input data
X = load_group(filenames, filepath)
# load class output
y = load_file(prefix + group + '/y_'+group+'.txt')
return X, y
# load the dataset, returns train and test X and y elements
def load_dataset(prefix=''):
# load all train
trainX, trainy = load_dataset_group('train', prefix + 'HARDataset/')
print(trainX.shape, trainy.shape)
# load all test
testX, testy = load_dataset_group('test', prefix + 'HARDataset/')
print(testX.shape, testy.shape)
# zero-offset class values
trainy = trainy - 1
testy = testy - 1
# one hot encode y
trainy = to_categorical(trainy)
testy = to_categorical(testy)
print(trainX.shape, trainy.shape, testX.shape, testy.shape)
return trainX, trainy, testX, testy
# fit and evaluate a model
def evaluate_model(trainX, trainy, testX, testy):
# define model
verbose, epochs, batch_size = 0, 25, 64
n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
# reshape data into time steps of sub-sequences
n_steps, n_length = 4, 32
trainX = trainX.reshape((trainX.shape[0], n_steps, n_length, n_features))
testX = testX.reshape((testX.shape[0], n_steps, n_length, n_features))
# define model
model = Sequential()
model.add(TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu'), input_shape=(None,n_length,n_features)))
model.add(TimeDistributed(Conv1D(filters=64, kernel_size=3, activation='relu')))
model.add(TimeDistributed(Dropout(0.5)))
model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(100))
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
# evaluate model
_, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
return accuracy
# summarize scores
def summarize_results(scores):
print(scores)
m, s = mean(scores), std(scores)
print('Accuracy: %.3f%% (+/-%.3f)' % (m, s))
# run an experiment
def run_experiment(repeats=10):
# load data
trainX, trainy, testX, testy = load_dataset()
# repeat experiment
scores = list()
for r in range(repeats):
score = evaluate_model(trainX, trainy, testX, testy)
score = score * 100.0
print('>#%d: %.3f' % (r+1, score))
scores.append(score)
# summarize results
summarize_results(scores)
# run the experiment
run_experiment()
运行该示例总结了 10 个运行中每个运行的模型表现,然后报告了测试集上模型表现的最终摘要。
我们可以看到该模型的表现约为 90.6%,标准偏差约为 1%。
注意:鉴于算法的随机性,您的具体结果可能会有所不同。如果是这样,请尝试运行几次代码。
>#1: 91.517
>#2: 91.042
>#3: 90.804
>#4: 92.263
>#5: 89.684
>#6: 88.666
>#7: 91.381
>#8: 90.804
>#9: 89.379
>#10: 91.347
[91.51679674244994, 91.04173736002714, 90.80420766881574, 92.26331862911435, 89.68442483881914, 88.66644044791313, 91.38106549032915, 90.80420766881574, 89.37902952154734, 91.34713267729894]
Accuracy: 90.689% (+/-1.051)
开发 ConvLSTM 网络模型
CNN LSTM 想法的进一步扩展是执行 CNN 的卷积(例如 CNN 如何读取输入序列数据)作为 LSTM 的一部分。
这种组合称为卷积 LSTM,简称 ConvLSTM,与 CNN LSTM 一样,也用于时空数据。
与直接读取数据以计算内部状态和状态转换的 LSTM 不同,并且与解释 CNN 模型的输出的 CNN LSTM 不同,ConvLSTM 直接使用卷积作为读取 LSTM 单元本身的输入的一部分。
有关如何在 LSTM 单元内计算 ConvLSTM 方程的更多信息,请参阅文章:
- 卷积 LSTM 网络:用于降水预报的机器学习方法,2015。
Keras 库提供 ConvLSTM2D 类,支持用于 2D 数据的 ConvLSTM 模型。它可以配置为 1D 多变量时间序列分类。
默认情况下,ConvLSTM2D 类要求输入数据具有以下形状:
(samples, time, rows, cols, channels)
其中每个时间步数据被定义为(行*列)数据点的图像。
在上一节中,我们将给定的数据窗口(128 个时间步长)划分为 32 个时间步长的四个子序列。我们可以在定义 ConvLSTM2D 输入时使用相同的子序列方法,其中时间步数是窗口中子序列的数量,当我们处理一维数据时行数是 1,列数代表子序列中的时间步长数,在本例中为 32。
对于这个选择的问题框架,ConvLSTM2D 的输入因此是:
- 样本:n,表示数据集中的窗口数。
- 时间:4,对于我们将 128 个时间步长的窗口分成四个子序列。
- 行:1,用于每个子序列的一维形状。
- 列:32,表示输入子序列中的 32 个时间步长。
- 频道:9,为九个输入变量。
我们现在可以为 ConvLSTM2D 模型准备数据。
n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
# reshape into subsequences (samples, time steps, rows, cols, channels)
n_steps, n_length = 4, 32
trainX = trainX.reshape((trainX.shape[0], n_steps, 1, n_length, n_features))
testX = testX.reshape((testX.shape[0], n_steps, 1, n_length, n_features))
ConvLSTM2D 类需要根据 CNN 和 LSTM 进行配置。这包括指定滤波器的数量(例如 64),二维内核大小,在这种情况下(子序列时间步长的 1 行和 3 列),以及激活函数,在这种情况下是整流的线性。
与 CNN 或 LSTM 模型一样,输出必须展平为一个长向量,然后才能通过密集层进行解释。
# define model
model = Sequential()
model.add(ConvLSTM2D(filters=64, kernel_size=(1,3), activation='relu', input_shape=(n_steps, 1, n_length, n_features)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
然后我们可以在之前对 LSTM 和 CNN LSTM 模型进行评估。
下面列出了完整的示例。
# convlstm model
from numpy import mean
from numpy import std
from numpy import dstack
from pandas import read_csv
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import Dropout
from keras.layers import LSTM
from keras.layers import TimeDistributed
from keras.layers import ConvLSTM2D
from keras.utils import to_categorical
from matplotlib import pyplot
# load a single file as a numpy array
def load_file(filepath):
dataframe = read_csv(filepath, header=None, delim_whitespace=True)
return dataframe.values
# load a list of files and return as a 3d numpy array
def load_group(filenames, prefix=''):
loaded = list()
for name in filenames:
data = load_file(prefix + name)
loaded.append(data)
# stack group so that features are the 3rd dimension
loaded = dstack(loaded)
return loaded
# load a dataset group, such as train or test
def load_dataset_group(group, prefix=''):
filepath = prefix + group + '/Inertial Signals/'
# load all 9 files as a single array
filenames = list()
# total acceleration
filenames += ['total_acc_x_'+group+'.txt', 'total_acc_y_'+group+'.txt', 'total_acc_z_'+group+'.txt']
# body acceleration
filenames += ['body_acc_x_'+group+'.txt', 'body_acc_y_'+group+'.txt', 'body_acc_z_'+group+'.txt']
# body gyroscope
filenames += ['body_gyro_x_'+group+'.txt', 'body_gyro_y_'+group+'.txt', 'body_gyro_z_'+group+'.txt']
# load input data
X = load_group(filenames, filepath)
# load class output
y = load_file(prefix + group + '/y_'+group+'.txt')
return X, y
# load the dataset, returns train and test X and y elements
def load_dataset(prefix=''):
# load all train
trainX, trainy = load_dataset_group('train', prefix + 'HARDataset/')
print(trainX.shape, trainy.shape)
# load all test
testX, testy = load_dataset_group('test', prefix + 'HARDataset/')
print(testX.shape, testy.shape)
# zero-offset class values
trainy = trainy - 1
testy = testy - 1
# one hot encode y
trainy = to_categorical(trainy)
testy = to_categorical(testy)
print(trainX.shape, trainy.shape, testX.shape, testy.shape)
return trainX, trainy, testX, testy
# fit and evaluate a model
def evaluate_model(trainX, trainy, testX, testy):
# define model
verbose, epochs, batch_size = 0, 25, 64
n_timesteps, n_features, n_outputs = trainX.shape[1], trainX.shape[2], trainy.shape[1]
# reshape into subsequences (samples, time steps, rows, cols, channels)
n_steps, n_length = 4, 32
trainX = trainX.reshape((trainX.shape[0], n_steps, 1, n_length, n_features))
testX = testX.reshape((testX.shape[0], n_steps, 1, n_length, n_features))
# define model
model = Sequential()
model.add(ConvLSTM2D(filters=64, kernel_size=(1,3), activation='relu', input_shape=(n_steps, 1, n_length, n_features)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit network
model.fit(trainX, trainy, epochs=epochs, batch_size=batch_size, verbose=verbose)
# evaluate model
_, accuracy = model.evaluate(testX, testy, batch_size=batch_size, verbose=0)
return accuracy
# summarize scores
def summarize_results(scores):
print(scores)
m, s = mean(scores), std(scores)
print('Accuracy: %.3f%% (+/-%.3f)' % (m, s))
# run an experiment
def run_experiment(repeats=10):
# load data
trainX, trainy, testX, testy = load_dataset()
# repeat experiment
scores = list()
for r in range(repeats):
score = evaluate_model(trainX, trainy, testX, testy)
score = score * 100.0
print('>#%d: %.3f' % (r+1, score))
scores.append(score)
# summarize results
summarize_results(scores)
# run the experiment
run_experiment()
与之前的实验一样,运行模型会在每次拟合和评估时打印模型的表现。最终模型表现的摘要在运行结束时给出。
我们可以看到,该模型在实现约 90%的准确度的问题上始终表现良好,可能比较大的 CNN LSTM 模型具有更少的资源。
注意:鉴于算法的随机性,您的具体结果可能会有所不同。如果是这样,请尝试运行几次代码。
>#1: 90.092
>#2: 91.619
>#3: 92.128
>#4: 90.533
>#5: 89.243
>#6: 90.940
>#7: 92.026
>#8: 91.008
>#9: 90.499
>#10: 89.922
[90.09161859518154, 91.61859518154056, 92.12758737699356, 90.53274516457415, 89.24329826942655, 90.93993892093654, 92.02578893790296, 91.00780454699695, 90.49881235154395, 89.92195453003053]
Accuracy: 90.801% (+/-0.886)
扩展
本节列出了一些扩展您可能希望探索的教程的想法。
- 数据准备。考虑探索简单的数据扩展方案是否可以进一步提升模型表现,例如标准化,标准化和电源转换。
- LSTM 变化。 LSTM 架构的变体可以在此问题上实现更好的表现,例如栈式 LSTM 和双向 LSTM。
- 超参数调整。考虑探索模型超参数的调整,例如单位数,训练时期,批量大小等。
如果你探索任何这些扩展,我很想知道。
进一步阅读
如果您希望深入了解,本节将提供有关该主题的更多资源。
文件
- 使用智能手机进行人类活动识别的公共领域数据集,2013 年。
- 智能手机上的人类活动识别使用多类硬件友好支持向量机,2012。
- 卷积,长短期记忆,完全连接的深度神经网络,2015。
- 卷积 LSTM 网络:用于降水预报的机器学习方法,2015。
用品
摘要
在本教程中,您发现了三种循环神经网络架构,用于对活动识别时间序列分类问题进行建模。
具体来说,你学到了:
- 如何开发一种用于人类活动识别的长短期记忆循环神经网络。
- 如何开发一维卷积神经网络 LSTM 或 CNN LSTM 模型。
- 如何针对同一问题开发一维卷积 LSTM 或 ConvLSTM 模型。
你有任何问题吗?
在下面的评论中提出您的问题,我会尽力回答。
如何开始用于时间序列预测的深度学习(7 天迷你课程)
时间序列预测速成课程的深度学习。
在 7 天内为您的时间序列项目带来深度学习方法。
时间序列预测具有挑战性,尤其是在处理长序列,噪声数据,多步预测和多个输入和输出变量时。
深度学习方法为时间序列预测提供了许多希望,例如时间依赖的自动学习和趋势和季节性等时间结构的自动处理。
在本速成课程中,您将了解如何开始并自信地开发深度学习模型,以便在 7 天内使用 Python 进行时间序列预测问题。
这是一个重要且重要的帖子。您可能想要将其加入书签。
让我们开始吧。
如何开始深度学习时间序列预测(7 天迷你课程)
摄影: Brian Richardson ,保留一些权利。
谁是这个速成课?
在我们开始之前,让我们确保您在正确的位置。
以下列表提供了有关本课程设计对象的一般指导原则。
你得知道:
- 您需要了解时间序列预测的基础知识。
- 你需要了解基本的 Python,NumPy 和 Keras 的深度学习方法。
你不需要知道:
- 你不需要成为一个数学家!
- 你不需要成为一名深度学习专家!
- 你不需要成为时间序列专家!
这个速成课程将带您从了解一点机器学习的开发人员到可以为您自己的时间序列预测项目带来深度学习方法的开发人员。
注意:这个速成课程假设你有一个有效的 Python 2 或 3 SciPy 环境,至少安装了 NumPy 和 Keras 2。如果您需要有关环境的帮助,可以按照此处的分步教程进行操作:
速成课程概述
这个速成课程分为 7 节课。
您可以每天完成一节课(推荐)或在一天内完成所有课程(硬核)。这取决于你有空的时间和你的热情程度。
以下 7 个课程将通过深入学习 Python 中的时间序列预测来帮助您开始并提高工作效率:
- 第 01 课:深度学习的承诺
- 第 02 课:如何转换时间序列数据
- 第 03 课:时间序列预测的 MLP
- 第 04 课:时间序列预测的 CNN
- 第 05 课:时间序列预测的 LSTM
- 第 06 课: CNN-LSTM 用于时间序列预测
- 第 07 课:编解码器 LSTM 多步预测
每节课可能需要 60 秒或 30 分钟。花点时间,按照自己的进度完成课程。在下面的评论中提出问题甚至发布结果。
课程期望你去学习如何做事。我将给你提示,但每节课的部分内容是强迫你学习去哪里寻求帮助,以及深入学习,时间序列预测和 Python 中最好的工具(提示, _ 我直接在这个博客上找到了所有答案,使用搜索框 _)。
我确实以相关帖子的链接形式提供了更多帮助,因为我希望你建立一些信心和惯性。
在评论中发布您的结果,我会为你欢呼!
挂在那里,不要放弃。
注:这只是一个速成课程。有关更多详细信息和 25 个充实教程,请参阅我的书,主题为“深度学习时间序列预测”。
第一课:深度学习的承诺
在本课程中,您将发现时间序列预测的深度学习方法的前景。
通常,像 Multilayer Perceptrons 或 MLP 这样的神经网络提供的功能很少,例如:
- 强健噪音。神经网络对输入数据和映射函数中的噪声具有鲁棒性,甚至可以在存在缺失值的情况下支持学习和预测。
- 非线性。神经网络不会对映射函数做出强有力的假设,并且很容易学习线性和非线性关系。
- 多变量输入。可以指定任意数量的输入要素,为多变量预测提供直接支持。
- 多步骤预测。可以指定任意数量的输出值,为多步骤甚至多变量预测提供
直接支持。
仅就这些功能而言,前馈神经网络可用于时间序列预测。
你的任务
在本课程中,您必须提出卷积神经网络和循环神经网络的一种功能,这些功能可能有助于建模时间序列预测问题。
在下面的评论中发表您的答案。我很乐意看到你发现了什么。
更多信息
在下一课中,您将了解如何转换时间序列预测的时间序列数据。
课程 02:如何转换时间序列的数据
在本课程中,您将了解如何将时间序列数据转换为监督学习格式。
大多数实际机器学习使用监督学习。
监督学习是输入变量(X)和输出变量(y)的地方,您可以使用算法来学习从输入到输出的映射函数。目标是近似真实的底层映射,以便在有新输入数据时,可以预测该数据的输出变量。
时间序列数据可以表达为监督学习。
给定时间序列数据集的数字序列,我们可以将数据重组为看起来像监督学习问题。我们可以使用前面的时间步长作为输入变量,并使用下一个时间步作为输出变量。
例如,系列:
1, 2, 3, 4, 5, ...
可以转换为具有输入和输出组件的样本,这些组件可以用作训练集的一部分,以训练监督学习模型,如深度学习神经网络。
X, y
[1, 2, 3] 4
[2, 3, 4] 5
...
这称为滑动窗口转换,因为它就像在先前观察中滑动窗口一样,用作模型的输入以预测序列中的下一个值。在这种情况下,窗口宽度是 3 个时间步长。
你的任务
在本课程中,您必须开发 Python 代码,将每日女性分娩数据集转换为具有一定数量输入和一个输出的监督学习格式。
你可以从这里下载数据集: daily-total-female-births.csv
在下面的评论中发表您的答案。我很乐意看到你发现了什么。
更多信息
在下一课中,您将了解如何开发用于预测单变量时间序列的多层感知机深度学习模型。
第 03 课:时间序列预测的 MLP
在本课程中,您将了解如何为单变量时间序列预测开发多层感知机模型或 MLP。
我们可以将一个简单的单变量问题定义为整数序列,使模型适合该序列,并让模型预测序列中的下一个值。我们将问题框架为 3 输入和 1 输出,例如:[10,20,30]作为输入,[40]作为输出。
首先,我们可以定义模型。我们将通过第一个隐藏层上的input_dim
参数将输入时间步数定义为 3。在这种情况下,我们将使用随机梯度下降的有效 Adam 版本并优化均方误差(‘mse
’)损失函数。
一旦定义了模型,它就可以适合训练数据,并且拟合模型可以用于做出预测。
下面列出了完整的示例。
# univariate mlp example
from numpy import array
from keras.models import Sequential
from keras.layers import Dense
# define dataset
X = array([[10, 20, 30], [20, 30, 40], [30, 40, 50], [40, 50, 60]])
y = array([40, 50, 60, 70])
# define model
model = Sequential()
model.add(Dense(100, activation='relu', input_dim=3))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X, y, epochs=2000, verbose=0)
# demonstrate prediction
x_input = array([50, 60, 70])
x_input = x_input.reshape((1, 3))
yhat = model.predict(x_input, verbose=0)
print(yhat)
运行该示例将使模型适合数据,然后预测下一个样本外的值。
给定[50,60,70]作为输入,模型正确地预测 80 作为序列中的下一个值。
你的任务
在本课程中,您必须下载每日女性分娩数据集,将其分为训练集和测试集,并开发一个可以对测试集进行合理准确预测的模型。
你可以从这里下载数据集: daily-total-female-births.csv
在下面的评论中发表您的答案。我很乐意看到你发现了什么。
更多信息
在下一课中,您将了解如何开发用于预测单变量时间序列的卷积神经网络模型。
第 04 课:CNN 进行时间序列预测
在本课程中,您将了解如何开发用于单变量时间序列预测的卷积神经网络模型或 CNN。
我们可以将一个简单的单变量问题定义为整数序列,使模型适合该序列,并让模型预测序列中的下一个值。我们将问题框架为 3 输入和 1 输出,例如:[10,20,30]作为输入,[40]作为输出。
与 MLP 模型的一个重要区别是 CNN 模型需要具有[_ 样本,时间步长,特征 ]形状的三维输入。我们将以[ 样本,时间步长 _]的形式定义数据并相应地重新整形。
我们将通过第一个隐藏层上的input_shape
参数将输入时间步数定义为 3,将要素数定义为 1。
我们将使用一个卷积隐藏层,后跟最大池池。然后,在由 Dense 层解释并输出预测之前,将滤镜图展平。该模型使用随机梯度下降的有效 Adam 模型,并优化均方误差(‘mse
’)损失函数。
一旦定义了模型,它就可以适合训练数据,并且拟合模型可以用于做出预测。
下面列出了完整的示例。
# univariate cnn example
from numpy import array
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
# define dataset
X = array([[10, 20, 30], [20, 30, 40], [30, 40, 50], [40, 50, 60]])
y = array([40, 50, 60, 70])
# reshape from [samples, timesteps] into [samples, timesteps, features]
X = X.reshape((X.shape[0], X.shape[1], 1))
# define model
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=2, activation='relu', input_shape=(3, 1)))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(50, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X, y, epochs=1000, verbose=0)
# demonstrate prediction
x_input = array([50, 60, 70])
x_input = x_input.reshape((1, 3, 1))
yhat = model.predict(x_input, verbose=0)
print(yhat)
运行该示例将使模型适合数据,然后预测下一个样本外的值。
给定[50,60,70]作为输入,模型正确地预测 80 作为序列中的下一个值。
你的任务
在本课程中,您必须下载每日女性分娩数据集,将其分为训练集和测试集,并开发一个可以对测试集进行合理准确预测的模型。
你可以从这里下载数据集: daily-total-female-births.csv
在下面的评论中发表您的答案。我很乐意看到你发现了什么。
更多信息
在下一课中,您将了解如何开发长短期记忆网络模型以预测单变量时间序列。
课 05:时间序列预测的 LSTM
在本课程中,您将了解如何开发长期短期记忆神经网络模型或 LSTM 以进行单变量时间序列预测。
我们可以将一个简单的单变量问题定义为整数序列,使模型适合该序列,并让模型预测序列中的下一个值。我们将问题框架为 3 输入和 1 输出,例如:[10,20,30]作为输入,[40]作为输出。
与 MLP 模型的重要区别在于,与 CNN 模型一样,LSTM 模型需要具有形状[_ 样本,时间步长,特征 ]的三维输入。我们将以[ 样本,时间步长 _]的形式定义数据并相应地重新整形。
我们将通过第一个隐藏层上的input_shape
参数将输入时间步数定义为 3,将要素数定义为 1。
我们将使用一个 LSTM 层来处理 3 个时间步的每个输入子序列,然后使用 Dense 层来解释输入序列的摘要。该模型使用随机梯度下降的有效 Adam 模型,并优化均方误差(‘mse
’)损失函数。
一旦定义了模型,它就可以适合训练数据,并且拟合模型可以用于做出预测。
下面列出了完整的示例。
# univariate lstm example
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
# define dataset
X = array([[10, 20, 30], [20, 30, 40], [30, 40, 50], [40, 50, 60]])
y = array([40, 50, 60, 70])
# reshape from [samples, timesteps] into [samples, timesteps, features]
X = X.reshape((X.shape[0], X.shape[1], 1))
# define model
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(3, 1)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X, y, epochs=1000, verbose=0)
# demonstrate prediction
x_input = array([50, 60, 70])
x_input = x_input.reshape((1, 3, 1))
yhat = model.predict(x_input, verbose=0)
print(yhat)
运行该示例将使模型适合数据,然后预测下一个样本外的值。
给定[50,60,70]作为输入,模型正确地预测 80 作为序列中的下一个值。
你的任务
在本课程中,您必须下载每日女性分娩数据集,将其分为训练集和测试集,并开发一个可以对测试集进行合理准确预测的模型。
你可以从这里下载数据集: daily-total-female-births.csv
在下面的评论中发表您的答案。我很乐意看到你发现了什么。
更多信息
在下一课中,您将了解如何针对单变量时间序列预测问题开发混合 CNN-LSTM 模型。
第 06 课:CNN-LSTM 用于时间序列预测
在本课程中,您将了解如何开发用于单变量时间序列预测的混合 CNN-LSTM 模型。
该模型的好处是该模型可以支持非常长的输入序列,可以通过 CNN 模型作为块或子序列读取,然后由 LSTM 模型拼凑在一起。
我们可以将一个简单的单变量问题定义为整数序列,使模型适合该序列,并让模型预测序列中的下一个值。我们将问题框架为 4 输入和 1 输出,例如:[10,20,30,40]作为输入,[50]作为输出。
当使用混合 CNN-LSTM 模型时,我们将进一步将每个样本分成更多的子序列。 CNN 模型将解释每个子序列,并且 LSTM 将来自子序列的解释拼凑在一起。因此,我们将每个样本分成 2 个子序列,每个子序列 2 次。
CNN 将被定义为每个子序列有一个特征需要 2 个时间步长。然后将整个 CNN 模型包装在 TimeDistributed 包装层中,以便可以将其应用于样本中的每个子序列。然后在模型输出预测之前由 LSTM 层解释结果。
该模型使用随机梯度下降的有效 Adam 模型,并优化均方误差(‘mse’)损失函数。
一旦定义了模型,它就可以适合训练数据,并且拟合模型可以用于做出预测。
下面列出了完整的示例。
# univariate cnn-lstm example
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import TimeDistributed
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
# define dataset
X = array([[10, 20, 30, 40], [20, 30, 40, 50], [30, 40, 50, 60], [40, 50, 60, 70]])
y = array([50, 60, 70, 80])
# reshape from [samples, timesteps] into [samples, subsequences, timesteps, features]
X = X.reshape((X.shape[0], 2, 2, 1))
# define model
model = Sequential()
model.add(TimeDistributed(Conv1D(filters=64, kernel_size=1, activation='relu'), input_shape=(None, 2, 1)))
model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(50, activation='relu'))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X, y, epochs=500, verbose=0)
# demonstrate prediction
x_input = array([50, 60, 70, 80])
x_input = x_input.reshape((1, 2, 2, 1))
yhat = model.predict(x_input, verbose=0)
print(yhat)
运行该示例将使模型适合数据,然后预测下一个样本外的值。
给定[50,60,70,80]作为输入,模型正确地预测 90 作为序列中的下一个值。
你的任务
在本课程中,您必须下载每日女性分娩数据集,将其分为训练集和测试集,并开发一个可以对测试集进行合理准确预测的模型。
你可以从这里下载数据集: daily-total-female-births.csv
在下面的评论中发表您的答案。我很乐意看到你发现了什么。
更多信息
- CNN 长短期记忆网络
- 如何在 Python 中为长期短期记忆网络使用时间分布层
在下一课中,您将了解如何开发用于多步时间序列预测的编解码器 LSTM 网络模型。
课程 07:编解码器 LSTM 多步预测
在本课程中,您将了解如何为多步时间序列预测开发编解码器 LSTM 网络模型。
我们可以将一个简单的单变量问题定义为整数序列,使模型适合该序列,并让模型预测序列中的下两个值。我们将问题框架为 3 输入和 2 输出,例如:[10,20,30]作为输入,[40,50]作为输出。
LSTM 模型需要具有[_ 样本,时间步长,特征 ]形状的三维输入。我们将以[ 样本,时间步长 _]的形式定义数据并相应地重新整形。使用编解码器模型时,输出也必须以这种方式成形。
我们将通过第一个隐藏层上的input_shape
参数将输入时间步数定义为 3,将要素数定义为 1。
我们将定义一个 LSTM 编码器来读取和编码 3 个时间步的输入序列。对于使用 RepeatVector 层的模型所需的两个输出时间步长,模型将重复编码序列 2 次。在使用包含在 TimeDistributed 层中的 Dense 输出层之前,这些将被馈送到解码器 LSTM 层,该层将为输出序列中的每个步骤产生一个输出。
该模型使用随机梯度下降的有效 Adam 模型,并优化均方误差(‘mse
’)损失函数。
一旦定义了模型,它就可以适合训练数据,并且拟合模型可以用于做出预测。
下面列出了完整的示例。
# multi-step encoder-decoder lstm example
from numpy import array
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import RepeatVector
from keras.layers import TimeDistributed
# define dataset
X = array([[10, 20, 30], [20, 30, 40], [30, 40, 50], [40, 50, 60]])
y = array([[40,50],[50,60],[60,70],[70,80]])
# reshape from [samples, timesteps] into [samples, timesteps, features]
X = X.reshape((X.shape[0], X.shape[1], 1))
y = y.reshape((y.shape[0], y.shape[1], 1))
# define model
model = Sequential()
model.add(LSTM(100, activation='relu', input_shape=(3, 1)))
model.add(RepeatVector(2))
model.add(LSTM(100, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.compile(optimizer='adam', loss='mse')
# fit model
model.fit(X, y, epochs=100, verbose=0)
# demonstrate prediction
x_input = array([50, 60, 70])
x_input = x_input.reshape((1, 3, 1))
yhat = model.predict(x_input, verbose=0)
print(yhat)
运行该示例将使模型适合数据,然后预测接下来的两个样本外值。
给定[50,60,70]作为输入,模型正确地预测[80,90]作为序列中的下两个值。
你的任务
在本课程中,您必须下载每日女性分娩数据集,将其分为训练集和测试集,并开发一个可以对测试集进行合理准确预测的模型。
你可以从这里下载数据集: daily-total-female-births.csv
在下面的评论中发表您的答案。我很乐意看到你发现了什么。
更多信息
结束!
(_ 看你有多远 _)
你做到了。做得好!
花点时间回顾一下你到底有多远。
你发现:
- 深度学习神经网络对时间序列预测问题的承诺。
- 如何将时间序列数据集转换为监督学习问题。
- 如何为单变量时间序列预测问题开发多层感知机模型。
- 如何建立一个单变量时间序列预测问题的卷积神经网络模型。
- 如何为单变量时间序列预测问题开发长短期记忆网络模型。
- 如何为单变量时间序列预测问题开发混合 CNN-LSTM 模型。
- 如何为多步时间序列预测问题开发编解码器 LSTM 模型。
这只是您深入学习时间序列预测的旅程的开始。继续练习和发展你的技能。
下一步,查看我的书深度学习时间序列。
摘要
你是如何使用迷你课程的?
你喜欢这个速成课吗?
你有什么问题吗?有没有任何问题?
让我知道。在下面发表评论。
如何为时间序列预测网格搜索深度学习模型
原文:
machinelearningmastery.com/how-to-grid-search-deep-learning-models-for-time-series-forecasting/
网格搜索通常不是我们可以使用深度学习方法执行的操作。
这是因为深度学习方法通常需要大量数据和大型模型,因此需要花费数小时,数天或数周才能训练的模型。
在数据集较小的情况下,例如单变量时间序列,可以使用网格搜索来调整深度学习模型的超参数。
在本教程中,您将了解如何为深度学习模型开发网格搜索超参数框架。
完成本教程后,您将了解:
- 如何开发用于调整模型超参数的通用网格搜索框架。
- 如何在航空公司乘客单变量时间序列预测问题上对多层感知机模型进行网格搜索超参数。
- 如何使框架适应卷积和长期短期记忆神经网络的网格搜索超参数。
让我们开始吧。
如何网格搜索时间序列预测的深度学习模型
照片由 Hannes Flo ,保留一些权利。
教程概述
本教程分为五个部分;他们是:
- 时间序列问题
- 网格搜索框架
- 网格搜索多层感知机
- 网格搜索卷积神经网络
- 网格搜索长短期记忆网络
时间序列问题
'_ 月度航空公司乘客 _'数据集总结了 1949 年至 1960 年期间航空公司每月数千人的国际旅客总数。
直接从这里下载数据集:
在当前工作目录中使用文件名“ monthly-airline-passengers.csv ”保存文件。
我们可以使用函数read_csv()
将此数据集作为 Pandas 系列加载。
# load
series = read_csv('monthly-airline-passengers.csv', header=0, index_col=0)
加载后,我们可以总结数据集的形状,以确定观察的数量。
# summarize shape
print(series.shape)
然后我们可以创建该系列的线图,以了解该系列的结构。
# plot
pyplot.plot(series)
pyplot.show()
我们可以将所有这些结合在一起;下面列出了完整的示例。
# load and plot dataset
from pandas import read_csv
from matplotlib import pyplot
# load
series = read_csv('monthly-airline-passengers.csv', header=0, index_col=0)
# summarize shape
print(series.shape)
# plot
pyplot.plot(series)
pyplot.show()
首先运行该示例将打印数据集的形状。
(144, 1)
该数据集是每月一次,有 12 年或 144 次观测。在我们的测试中,我们将使用去年或 12 个观测值作为测试集。
创建线图。数据集具有明显的趋势和季节性成分。季节性成分的期限为 12 个月。
每月国际航空公司乘客的线路情节
在本教程中,我们将介绍用于网格搜索的工具,但我们不会针对此问题优化模型超参数。相反,我们将演示如何通常网格搜索深度学习模型超参数,并找到与朴素模型相比具有一定技巧的模型。
从之前的实验中,一个朴素的模型可以通过持续 12 个月前的值(相对指数-12)来实现 50.70 的均方根误差或 RMSE(记住单位是数千名乘客)。
这个朴素模型的表现提供了一个被认为适合这个问题的模型的约束。任何在过去 12 个月内达到低于 50.70 的预测表现的模型都具有技巧。
应该注意的是,调谐的 ETS 模型可以实现 17.09 的 RMSE,并且调谐的 SARIMA 可以实现 13.89 的 RMSE。这些为这个问题提供了一个调整良好的深度学习模型的预期的下限。
现在我们已经定义了模型技能的问题和期望,我们可以看看定义网格搜索测试工具。
网格搜索框架
在本节中,我们将开发一个网格搜索测试工具,可用于评估不同神经网络模型的一系列超参数,例如 MLP,CNN 和 LSTM。
本节分为以下几部分:
- 训练 - 测试分裂
- 系列作为监督学习
- 前瞻性验证
- 重复评估
- 总结表现
- 工作示例
训练 - 测试分裂
第一步是将加载的系列分成训练和测试集。
我们将使用前 11 年(132 个观测值)进行训练,最后 12 个用于测试集。
下面的train_test_split()
函数将拆分系列,将原始观察值和在测试集中使用的观察数作为参数。
# split a univariate dataset into train/test sets
def train_test_split(data, n_test):
return data[:-n_test], data[-n_test:]
系列作为监督学习
接下来,我们需要能够将单变量观测系列框架化为监督学习问题,以便我们可以训练神经网络模型。
系列的监督学习框架意味着数据需要分成模型从中学习和概括的多个示例。
每个样本必须同时具有输入组件和输出组件。
输入组件将是一些先前的观察,例如三年或 36 个时间步骤。
输出组件将是下个月的总销售额,因为我们有兴趣开发一个模型来进行一步预测。
我们可以使用 pandas DataFrame 上的 shift()函数来实现它。它允许我们向下移动一列(向前移动)或向后移动(向后移动)。我们可以将该系列作为一列数据,然后创建列的多个副本,向前或向后移动,以便使用我们需要的输入和输出元素创建样本。
当一个系列向下移动时,会引入 NaN 值,因为我们没有超出系列开头的值。
例如,系列定义为列:
(t)
1
2
3
4
此列可以预先移位并作为列插入:
(t-1), (t)
Nan, 1
1, 2
2, 3
3, 4
4 NaN
我们可以看到,在第二行,值 1 作为输入提供,作为前一时间步的观察,2 是系列中可以预测的下一个值,或者当 1 是预测模型时要学习的值作为输入呈现。
可以删除具有 NaN 值的行。
下面的series_to_supervised()
函数实现了这种行为,允许您指定输入中使用的滞后观察数和每个样本的输出中使用的数。它还将删除具有 NaN 值的行,因为它们不能用于训练或测试模型。
# transform list into supervised learning format
def series_to_supervised(data, n_in=1, n_out=1):
df = DataFrame(data)
cols = list()
# input sequence (t-n, ... t-1)
for i in range(n_in, 0, -1):
cols.append(df.shift(i))
# forecast sequence (t, t+1, ... t+n)
for i in range(0, n_out):
cols.append(df.shift(-i))
# put it all together
agg = concat(cols, axis=1)
# drop rows with NaN values
agg.dropna(inplace=True)
return agg.values
前瞻性验证
可以使用前向验证在测试集上评估时间序列预测模型。
前瞻性验证是一种方法,其中模型一次一个地对测试数据集中的每个观察做出预测。在对测试数据集中的时间步长进行每个预测之后,将预测的真实观察结果添加到测试数据集并使其可用于模型。
在进行后续预测之前,可以使用观察结果更简单的模型。考虑到更高的计算成本,更复杂的模型,例如神经网络,不会被改装。
然而,时间步骤的真实观察可以用作输入的一部分,用于在下一个时间步骤上做出预测。
首先,数据集分为训练集和测试集。我们将调用train_test_split()
函数来执行此拆分并传入预先指定数量的观察值以用作测试数据。
对于给定配置,模型将适合训练数据集一次。
我们将定义一个通用的model_fit()
函数来执行此操作,可以为我们稍后可能感兴趣的给定类型的神经网络填充该操作。该函数获取训练数据集和模型配置,并返回准备好做出预测的拟合模型。
# fit a model
def model_fit(train, config):
return None
枚举测试数据集的每个时间步。使用拟合模型做出预测。
同样,我们将定义一个名为model_predict()
的通用函数,它采用拟合模型,历史和模型配置,并进行单个一步预测。
# forecast with a pre-fit model
def model_predict(model, history, config):
return 0.0
将预测添加到预测列表中,并将来自测试集的真实观察结果添加到用训练数据集中的所有观察结果播种的观察列表中。此列表在前向验证的每个步骤中构建,允许模型使用最新历史记录进行一步预测。
然后可以将所有预测与测试集中的真实值进行比较,并计算误差测量值。
我们将计算预测和真实值之间的均方根误差或 RMSE。
RMSE 计算为预测值与实际值之间的平方差的平均值的平方根。measure_rmse()
使用 mean_squared_error()scikit-learn 函数在计算平方根之前首先计算均方误差或 MSE。
# root mean squared error or rmse
def measure_rmse(actual, predicted):
return sqrt(mean_squared_error(actual, predicted))
下面列出了将所有这些联系在一起的完整walk_forward_validation()
函数。
它采用数据集,用作测试集的观察数量以及模型的配置,并返回测试集上模型表现的 RMSE。
# walk-forward validation for univariate data
def walk_forward_validation(data, n_test, cfg):
predictions = list()
# split dataset
train, test = train_test_split(data, n_test)
# fit model
model = model_fit(train, cfg)
# seed history with training dataset
history = [x for x in train]
# step over each time-step in the test set
for i in range(len(test)):
# fit model and make forecast for history
yhat = model_predict(model, history, cfg)
# store forecast in list of predictions
predictions.append(yhat)
# add actual observation to history for the next loop
history.append(test[i])
# estimate prediction error
error = measure_rmse(test, predictions)
print(' > %.3f' % error)
return error
重复评估
神经网络模型是随机的。
这意味着,在给定相同的模型配置和相同的训练数据集的情况下,每次训练模型时将产生不同的内部权重集,这反过来将具有不同的表现。
这是一个好处,允许模型自适应并找到复杂问题的高表现配置。
在评估模型的表现和选择用于做出预测的最终模型时,这也是一个问题。
为了解决模型评估问题,我们将通过前进验证多次评估模型配置,并将错误报告为每次评估的平均误差。
对于大型神经网络而言,这并不总是可行的,并且可能仅适用于能够在几分钟或几小时内完成的小型网络。
下面的repeat_evaluate()
函数实现了这一点,并允许将重复次数指定为默认为 10 的可选参数,并返回所有重复的平均 RMSE 分数。
# score a model, return None on failure
def repeat_evaluate(data, config, n_test, n_repeats=10):
# convert config to a key
key = str(config)
# fit and evaluate the model n times
scores = [walk_forward_validation(data, n_test, config) for _ in range(n_repeats)]
# summarize score
result = mean(scores)
print('> Model[%s] %.3f' % (key, result))
return (key, result)
网格搜索
我们现在拥有框架的所有部分。
剩下的就是驱动搜索的功能。我们可以定义grid_search()
函数,该函数获取数据集,要搜索的配置列表以及用作测试集的观察数量并执行搜索。
一旦为每个配置计算平均分数,配置列表将按升序排序,以便首先列出最佳分数。
完整的功能如下所列。
# grid search configs
def grid_search(data, cfg_list, n_test):
# evaluate configs
scores = scores = [score_model(data, n_test, cfg) for cfg in cfg_list]
# sort configs by error, asc
scores.sort(key=lambda tup: tup[1])
return scores
工作示例
现在我们已经定义了测试工具的元素,我们可以将它们绑定在一起并定义一个简单的持久性模型。
我们不需要拟合模型,因此model_fit()
函数将被实现为简单地返回 None。
# fit a model
def model_fit(train, config):
return None
我们将使用配置来定义先前观察中的索引偏移列表,该列表相对于将被用作预测的预测时间。例如,12 将使用 12 个月前(-12)相对于预测时间的观察。
# define config
cfg_list = [1, 6, 12, 24, 36]
可以实现model_predict()
函数以使用此配置将值保持在负相对偏移处。
# forecast with a pre-fit model
def model_predict(model, history, offset):
history[-offset]
下面列出了使用简单持久性模型使用框架的完整示例。
# grid search persistence models for airline passengers
from math import sqrt
from numpy import mean
from pandas import read_csv
from sklearn.metrics import mean_squared_error
# split a univariate dataset into train/test sets
def train_test_split(data, n_test):
return data[:-n_test], data[-n_test:]
# root mean squared error or rmse
def measure_rmse(actual, predicted):
return sqrt(mean_squared_error(actual, predicted))
# fit a model
def model_fit(train, config):
return None
# forecast with a pre-fit model
def model_predict(model, history, offset):
return history[-offset]
# walk-forward validation for univariate data
def walk_forward_validation(data, n_test, cfg):
predictions = list()
# split dataset
train, test = train_test_split(data, n_test)
# fit model
model = model_fit(train, cfg)
# seed history with training dataset
history = [x for x in train]
# step over each time-step in the test set
for i in range(len(test)):
# fit model and make forecast for history
yhat = model_predict(model, history, cfg)
# store forecast in list of predictions
predictions.append(yhat)
# add actual observation to history for the next loop
history.append(test[i])
# estimate prediction error
error = measure_rmse(test, predictions)
print(' > %.3f' % error)
return error
# score a model, return None on failure
def repeat_evaluate(data, config, n_test, n_repeats=10):
# convert config to a key
key = str(config)
# fit and evaluate the model n times
scores = [walk_forward_validation(data, n_test, config) for _ in range(n_repeats)]
# summarize score
result = mean(scores)
print('> Model[%s] %.3f' % (key, result))
return (key, result)
# grid search configs
def grid_search(data, cfg_list, n_test):
# evaluate configs
scores = scores = [repeat_evaluate(data, cfg, n_test) for cfg in cfg_list]
# sort configs by error, asc
scores.sort(key=lambda tup: tup[1])
return scores
# define dataset
series = read_csv('monthly-airline-passengers.csv', header=0, index_col=0)
data = series.values
# data split
n_test = 12
# model configs
cfg_list = [1, 6, 12, 24, 36]
# grid search
scores = grid_search(data, cfg_list, n_test)
print('done')
# list top 10 configs
for cfg, error in scores[:10]:
print(cfg, error)
运行该示例将打印在最近 12 个月的数据中使用前向验证评估的模型的 RMSE。
每个模型配置被评估 10 次,但是,因为模型没有随机元素,所以每次得分都相同。
在运行结束时,将报告前三个执行模型配置的配置和 RMSE。
我们可以看到,正如我们可能预期的那样,持续一年前的值(相对偏移-12)导致持久性模型的最佳表现。
...
> 110.274
> 110.274
> 110.274
> Model[36] 110.274
done
12 50.708316214732804
1 53.1515129919491
24 97.10990337413241
36 110.27352356753639
6 126.73495965991387
现在我们有一个强大的网格搜索模型超参数测试工具,我们可以用它来评估一套神经网络模型。
网格搜索多层感知机
我们可能希望调整 MLP 的许多方面。
我们将定义一个非常简单的模型,其中包含一个隐藏层,并定义五个超参数进行调整。他们是:
- n_input :用作模型输入的先前输入数(例如 12 个月)。
- n_nodes :隐藏层中使用的节点数(例如 50)。
- n_epochs :训练时期的数量(例如 1000)。
- n_batch :每个小批量中包含的样本数(例如 32)。
- n_diff :差分顺序(例如 0 或 12)。
现代神经网络可以通过很少的预处理来处理原始数据,例如缩放和差分。然而,当涉及时间序列数据时,有时差异系列可以使问题更容易建模。
回想一下,差分是数据的变换,使得从当前观察中减去先前观察的值,去除趋势或季节性结构。
我们将为网格搜索测试工具添加差异支持,以防它为您的特定问题增加价值。它确实为内部航空公司乘客数据集增加了价值。
下面的 _ 差异()_ 函数将计算数据集的给定顺序的差异。
# difference dataset
def difference(data, order):
return [data[i] - data[i - order] for i in range(order, len(data))]
差异将是可选的,其中 0 的顺序表示没有差异,而 1 阶或 12 阶将要求在拟合模型之前差异数据并且模型的预测需要在返回预测之前反转差分。
我们现在可以定义在测试工具中安装 MLP 模型所需的元素。
首先,我们必须解压缩超参数列表。
# unpack config
n_input, n_nodes, n_epochs, n_batch, n_diff = config
接下来,我们必须准备数据,包括差分,将数据转换为监督格式,并分离出数据样本的输入和输出方面。
# prepare data
if n_diff > 0:
train = difference(train, n_diff)
# transform series into supervised format
data = series_to_supervised(train, n_in=n_input)
# separate inputs and outputs
train_x, train_y = data[:, :-1], data[:, -1]
我们现在可以使用提供的配置定义和拟合模型。
# define model
model = Sequential()
model.add(Dense(n_nodes, activation='relu', input_dim=n_input))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
# fit model
model.fit(train_x, train_y, epochs=n_epochs, batch_size=n_batch, verbose=0)
下面列出了model_fit()
函数的完整实现。
# fit a model
def model_fit(train, config):
# unpack config
n_input, n_nodes, n_epochs, n_batch, n_diff = config
# prepare data
if n_diff > 0:
train = difference(train, n_diff)
# transform series into supervised format
data = series_to_supervised(train, n_in=n_input)
# separate inputs and outputs
train_x, train_y = data[:, :-1], data[:, -1]
# define model
model = Sequential()
model.add(Dense(n_nodes, activation='relu', input_dim=n_input))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
# fit model
model.fit(train_x, train_y, epochs=n_epochs, batch_size=n_batch, verbose=0)
return model
五个选择的超参数绝不是要调整的模型的唯一或最佳超参数。您可以修改该功能以调整其他参数,例如更多隐藏层的添加和大小等等。
一旦模型适合,我们就可以使用它来做出预测。
如果数据差异,则必须反转差异以预测模型。这涉及将历史的相对偏移处的值添加回模型预测的值。
# invert difference
correction = 0.0
if n_diff > 0:
correction = history[-n_diff]
...
# correct forecast if it was differenced
return correction + yhat[0]
这也意味着必须区分历史记录,以便用于做出预测的输入数据具有预期的形式。
# calculate difference
history = difference(history, n_diff)
准备好之后,我们可以使用历史数据创建单个样本作为模型的输入,以进行一步预测。
一个样本的形状必须是[1,n_input],其中n_input
是要使用的滞后观察数的选定数量。
# shape input for model
x_input = array(history[-n_input:]).reshape((1, n_input))
最后,可以做出预测。
# make forecast
yhat = model.predict(x_input, verbose=0)
下面列出了model_predict()
函数的完整实现。
接下来,我们必须定义要为每个超参数尝试的值范围。
我们可以定义model_configs()
函数,该函数创建要尝试的不同参数组合的列表。
我们将定义一小部分配置作为示例,包括 12 个月的差异,我们预计这将是必需的。建议您尝试使用独立模型,查看学习曲线诊断图,并使用有关域的信息来设置超参数值到网格搜索的范围。
我们还鼓励您重复网格搜索以缩小显示更好表现的值范围。
下面列出了model_configs()
函数的实现。
# create a list of configs to try
def model_configs():
# define scope of configs
n_input = [12]
n_nodes = [50, 100]
n_epochs = [100]
n_batch = [1, 150]
n_diff = [0, 12]
# create configs
configs = list()
for i in n_input:
for j in n_nodes:
for k in n_epochs:
for l in n_batch:
for m in n_diff:
cfg = [i, j, k, l, m]
configs.append(cfg)
print('Total configs: %d' % len(configs))
return configs
我们现在拥有网格搜索 MLP 模型所需的所有部分,用于单变量时间序列预测问题。
下面列出了完整的示例。
# grid search mlps for airline passengers
from math import sqrt
from numpy import array
from numpy import mean
from pandas import DataFrame
from pandas import concat
from pandas import read_csv
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense
# split a univariate dataset into train/test sets
def train_test_split(data, n_test):
return data[:-n_test], data[-n_test:]
# transform list into supervised learning format
def series_to_supervised(data, n_in=1, n_out=1):
df = DataFrame(data)
cols = list()
# input sequence (t-n, ... t-1)
for i in range(n_in, 0, -1):
cols.append(df.shift(i))
# forecast sequence (t, t+1, ... t+n)
for i in range(0, n_out):
cols.append(df.shift(-i))
# put it all together
agg = concat(cols, axis=1)
# drop rows with NaN values
agg.dropna(inplace=True)
return agg.values
# root mean squared error or rmse
def measure_rmse(actual, predicted):
return sqrt(mean_squared_error(actual, predicted))
# difference dataset
def difference(data, order):
return [data[i] - data[i - order] for i in range(order, len(data))]
# fit a model
def model_fit(train, config):
# unpack config
n_input, n_nodes, n_epochs, n_batch, n_diff = config
# prepare data
if n_diff > 0:
train = difference(train, n_diff)
# transform series into supervised format
data = series_to_supervised(train, n_in=n_input)
# separate inputs and outputs
train_x, train_y = data[:, :-1], data[:, -1]
# define model
model = Sequential()
model.add(Dense(n_nodes, activation='relu', input_dim=n_input))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
# fit model
model.fit(train_x, train_y, epochs=n_epochs, batch_size=n_batch, verbose=0)
return model
# forecast with the fit model
def model_predict(model, history, config):
# unpack config
n_input, _, _, _, n_diff = config
# prepare data
correction = 0.0
if n_diff > 0:
correction = history[-n_diff]
history = difference(history, n_diff)
# shape input for model
x_input = array(history[-n_input:]).reshape((1, n_input))
# make forecast
yhat = model.predict(x_input, verbose=0)
# correct forecast if it was differenced
return correction + yhat[0]
# walk-forward validation for univariate data
def walk_forward_validation(data, n_test, cfg):
predictions = list()
# split dataset
train, test = train_test_split(data, n_test)
# fit model
model = model_fit(train, cfg)
# seed history with training dataset
history = [x for x in train]
# step over each time-step in the test set
for i in range(len(test)):
# fit model and make forecast for history
yhat = model_predict(model, history, cfg)
# store forecast in list of predictions
predictions.append(yhat)
# add actual observation to history for the next loop
history.append(test[i])
# estimate prediction error
error = measure_rmse(test, predictions)
print(' > %.3f' % error)
return error
# score a model, return None on failure
def repeat_evaluate(data, config, n_test, n_repeats=10):
# convert config to a key
key = str(config)
# fit and evaluate the model n times
scores = [walk_forward_validation(data, n_test, config) for _ in range(n_repeats)]
# summarize score
result = mean(scores)
print('> Model[%s] %.3f' % (key, result))
return (key, result)
# grid search configs
def grid_search(data, cfg_list, n_test):
# evaluate configs
scores = scores = [repeat_evaluate(data, cfg, n_test) for cfg in cfg_list]
# sort configs by error, asc
scores.sort(key=lambda tup: tup[1])
return scores
# create a list of configs to try
def model_configs():
# define scope of configs
n_input = [12]
n_nodes = [50, 100]
n_epochs = [100]
n_batch = [1, 150]
n_diff = [0, 12]
# create configs
configs = list()
for i in n_input:
for j in n_nodes:
for k in n_epochs:
for l in n_batch:
for m in n_diff:
cfg = [i, j, k, l, m]
configs.append(cfg)
print('Total configs: %d' % len(configs))
return configs
# define dataset
series = read_csv('monthly-airline-passengers.csv', header=0, index_col=0)
data = series.values
# data split
n_test = 12
# model configs
cfg_list = model_configs()
# grid search
scores = grid_search(data, cfg_list, n_test)
print('done')
# list top 3 configs
for cfg, error in scores[:3]:
print(cfg, error)
运行该示例,我们可以看到框架总共要评估八种配置。
每个配置将被评估 10 次;这意味着将使用前向验证创建和评估 10 个模型,以在报告这 10 个分数的平均值并用于对配置进行评分之前计算 RMSE 分数。
然后对得分进行排序,最后报告具有最低 RMSE 的前 3 个配置。与报告 RMSE 为 50.70 的幼稚模型相比,发现了一种熟练的模型配置。
我们可以看到 18.98 的最佳 RMSE 是通过[12,100,100,1,12]的配置实现的,我们知道可以解释为:
- n_input :12
- n_nodes :100
- n_epochs :100
- n_batch :1
- n_diff :12
下面列出了网格搜索的截断示例输出。
鉴于算法的随机性,您的具体分数可能会有所不同。
Total configs: 8
> 20.707
> 29.111
> 17.499
> 18.918
> 28.817
...
> 21.015
> 20.208
> 18.503
> Model[[12, 100, 100, 150, 12]] 19.674
done
[12, 100, 100, 1, 12] 18.982720013625606
[12, 50, 100, 150, 12] 19.33004059448595
[12, 100, 100, 1, 0] 19.5389405532858
网格搜索卷积神经网络
我们现在可以将框架调整为网格搜索 CNN 模型。
可以像使用 MLP 模型一样搜索大量相同的超参数集,除了隐藏层中的节点数可以由卷积层中的滤波器映射数和内核大小替换。
在 CNN 模型中选择的网格搜索超参数集如下:
- n_input :用作模型输入的先前输入数(例如 12 个月)。
- n_filters :卷积层中的滤波器映射的数量(例如 32)。
- n_kernel :卷积层中的内核大小(例如 3)。
- n_epochs :训练时期的数量(例如 1000)。
- n_batch :每个小批量中包含的样本数(例如 32)。
- n_diff :差分顺序(例如 0 或 12)。
您可能希望研究的一些额外的超参数是在池化层之前使用两个卷积层,重复卷积和池化层模式,使用丢失等等。
我们将定义一个非常简单的 CNN 模型,其中包含一个卷积层和一个最大池池。
# define model
model = Sequential()
model.add(Conv1D(filters=n_filters, kernel_size=n_kernel, activation='relu', input_shape=(n_input, n_features)))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
必须以与 MLP 相同的方式准备数据。
与期望输入数据具有[样本,特征]形状的 MLP 不同,1D CNN 模型期望数据具有[_ 样本,时间步长,特征 _]的形状,其中特征映射到通道上并且在此案例 1 是我们每个月测量的一个变量。
# reshape input data into [samples, timesteps, features]
n_features = 1
train_x = train_x.reshape((train_x.shape[0], train_x.shape[1], n_features))
下面列出了model_fit()
函数的完整实现。
# fit a model
def model_fit(train, config):
# unpack config
n_input, n_filters, n_kernel, n_epochs, n_batch, n_diff = config
# prepare data
if n_diff > 0:
train = difference(train, n_diff)
# transform series into supervised format
data = series_to_supervised(train, n_in=n_input)
# separate inputs and outputs
train_x, train_y = data[:, :-1], data[:, -1]
# reshape input data into [samples, timesteps, features]
n_features = 1
train_x = train_x.reshape((train_x.shape[0], train_x.shape[1], n_features))
# define model
model = Sequential()
model.add(Conv1D(filters=n_filters, kernel_size=n_kernel, activation='relu', input_shape=(n_input, n_features)))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
# fit
model.fit(train_x, train_y, epochs=n_epochs, batch_size=n_batch, verbose=0)
return model
使用拟合 CNN 模型做出预测非常类似于使用拟合 MLP 做出预测。
同样,唯一的区别是一个样本值的输入数据必须具有三维形状。
x_input = array(history[-n_input:]).reshape((1, n_input, 1))
下面列出了model_predict()
函数的完整实现。
# forecast with the fit model
def model_predict(model, history, config):
# unpack config
n_input, _, _, _, _, n_diff = config
# prepare data
correction = 0.0
if n_diff > 0:
correction = history[-n_diff]
history = difference(history, n_diff)
x_input = array(history[-n_input:]).reshape((1, n_input, 1))
# forecast
yhat = model.predict(x_input, verbose=0)
return correction + yhat[0]
最后,我们可以定义要评估的模型的配置列表。和以前一样,我们可以通过定义超参数值列表来尝试将它们组合成一个列表。我们将尝试少量配置以确保示例在合理的时间内执行。
完整的model_configs()
功能如下所示。
# create a list of configs to try
def model_configs():
# define scope of configs
n_input = [12]
n_filters = [64]
n_kernels = [3, 5]
n_epochs = [100]
n_batch = [1, 150]
n_diff = [0, 12]
# create configs
configs = list()
for a in n_input:
for b in n_filters:
for c in n_kernels:
for d in n_epochs:
for e in n_batch:
for f in n_diff:
cfg = [a,b,c,d,e,f]
configs.append(cfg)
print('Total configs: %d' % len(configs))
return configs
我们现在拥有网格搜索卷积神经网络的超参数所需的所有元素,用于单变量时间序列预测。
下面列出了完整的示例。
# grid search cnn for airline passengers
from math import sqrt
from numpy import array
from numpy import mean
from pandas import DataFrame
from pandas import concat
from pandas import read_csv
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
# split a univariate dataset into train/test sets
def train_test_split(data, n_test):
return data[:-n_test], data[-n_test:]
# transform list into supervised learning format
def series_to_supervised(data, n_in=1, n_out=1):
df = DataFrame(data)
cols = list()
# input sequence (t-n, ... t-1)
for i in range(n_in, 0, -1):
cols.append(df.shift(i))
# forecast sequence (t, t+1, ... t+n)
for i in range(0, n_out):
cols.append(df.shift(-i))
# put it all together
agg = concat(cols, axis=1)
# drop rows with NaN values
agg.dropna(inplace=True)
return agg.values
# root mean squared error or rmse
def measure_rmse(actual, predicted):
return sqrt(mean_squared_error(actual, predicted))
# difference dataset
def difference(data, order):
return [data[i] - data[i - order] for i in range(order, len(data))]
# fit a model
def model_fit(train, config):
# unpack config
n_input, n_filters, n_kernel, n_epochs, n_batch, n_diff = config
# prepare data
if n_diff > 0:
train = difference(train, n_diff)
# transform series into supervised format
data = series_to_supervised(train, n_in=n_input)
# separate inputs and outputs
train_x, train_y = data[:, :-1], data[:, -1]
# reshape input data into [samples, timesteps, features]
n_features = 1
train_x = train_x.reshape((train_x.shape[0], train_x.shape[1], n_features))
# define model
model = Sequential()
model.add(Conv1D(filters=n_filters, kernel_size=n_kernel, activation='relu', input_shape=(n_input, n_features)))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
# fit
model.fit(train_x, train_y, epochs=n_epochs, batch_size=n_batch, verbose=0)
return model
# forecast with the fit model
def model_predict(model, history, config):
# unpack config
n_input, _, _, _, _, n_diff = config
# prepare data
correction = 0.0
if n_diff > 0:
correction = history[-n_diff]
history = difference(history, n_diff)
x_input = array(history[-n_input:]).reshape((1, n_input, 1))
# forecast
yhat = model.predict(x_input, verbose=0)
return correction + yhat[0]
# walk-forward validation for univariate data
def walk_forward_validation(data, n_test, cfg):
predictions = list()
# split dataset
train, test = train_test_split(data, n_test)
# fit model
model = model_fit(train, cfg)
# seed history with training dataset
history = [x for x in train]
# step over each time-step in the test set
for i in range(len(test)):
# fit model and make forecast for history
yhat = model_predict(model, history, cfg)
# store forecast in list of predictions
predictions.append(yhat)
# add actual observation to history for the next loop
history.append(test[i])
# estimate prediction error
error = measure_rmse(test, predictions)
print(' > %.3f' % error)
return error
# score a model, return None on failure
def repeat_evaluate(data, config, n_test, n_repeats=10):
# convert config to a key
key = str(config)
# fit and evaluate the model n times
scores = [walk_forward_validation(data, n_test, config) for _ in range(n_repeats)]
# summarize score
result = mean(scores)
print('> Model[%s] %.3f' % (key, result))
return (key, result)
# grid search configs
def grid_search(data, cfg_list, n_test):
# evaluate configs
scores = scores = [repeat_evaluate(data, cfg, n_test) for cfg in cfg_list]
# sort configs by error, asc
scores.sort(key=lambda tup: tup[1])
return scores
# create a list of configs to try
def model_configs():
# define scope of configs
n_input = [12]
n_filters = [64]
n_kernels = [3, 5]
n_epochs = [100]
n_batch = [1, 150]
n_diff = [0, 12]
# create configs
configs = list()
for a in n_input:
for b in n_filters:
for c in n_kernels:
for d in n_epochs:
for e in n_batch:
for f in n_diff:
cfg = [a,b,c,d,e,f]
configs.append(cfg)
print('Total configs: %d' % len(configs))
return configs
# define dataset
series = read_csv('monthly-airline-passengers.csv', header=0, index_col=0)
data = series.values
# data split
n_test = 12
# model configs
cfg_list = model_configs()
# grid search
scores = grid_search(data, cfg_list, n_test)
print('done')
# list top 10 configs
for cfg, error in scores[:3]:
print(cfg, error)
运行该示例,我们可以看到只评估了八种不同的配置。
我们可以看到[12,64,5,100,1,12]的配置实现了 18.89 的 RMSE,与实现 50.70 的朴素预测模型相比,这是巧妙的。
我们可以将此配置解压缩为:
- n_input :12
- n_filters :64
- n_kernel :5
- n_epochs :100
- n_batch :1
- n_diff :12
下面列出了网格搜索的截断示例输出。
鉴于算法的随机性,您的具体分数可能会有所不同。
Total configs: 8
> 23.372
> 28.317
> 31.070
...
> 20.923
> 18.700
> 18.210
> Model[[12, 64, 5, 100, 150, 12]] 19.152
done
[12, 64, 5, 100, 1, 12] 18.89593462072732
[12, 64, 5, 100, 150, 12] 19.152486150334234
[12, 64, 3, 100, 150, 12] 19.44680151564605
网格搜索长短期记忆网络
我们现在可以采用网格搜索 LSTM 模型的超参数。
LSTM 模型的超参数将与 MLP 相同;他们是:
- n_input :用作模型输入的先前输入数(例如 12 个月)。
- n_nodes :隐藏层中使用的节点数(例如 50)。
- n_epochs :训练时期的数量(例如 1000)。
- n_batch :每个小批量中包含的样本数(例如 32)。
- n_diff :差分顺序(例如 0 或 12)。
我们将定义一个简单的 LSTM 模型,该模型具有单个隐藏的 LSTM 层和指定该层中单元数的节点数。
# define model
model = Sequential()
model.add(LSTM(n_nodes, activation='relu', input_shape=(n_input, n_features)))
model.add(Dense(n_nodes, activation='relu'))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
# fit model
model.fit(train_x, train_y, epochs=n_epochs, batch_size=n_batch, verbose=0)
探索调整其他配置(例如使用双向输入层,栈式 LSTM 层,甚至是具有 CNN 或 ConvLSTM 输入模型的混合模型)可能会很有趣。
与 CNN 模型一样,LSTM 模型期望输入数据具有样本,时间步长和特征的三维形状。
# reshape input data into [samples, timesteps, features]
n_features = 1
train_x = train_x.reshape((train_x.shape[0], train_x.shape[1], n_features))
下面列出了model_fit()
函数的完整实现。
# fit a model
def model_fit(train, config):
# unpack config
n_input, n_nodes, n_epochs, n_batch, n_diff = config
# prepare data
if n_diff > 0:
train = difference(train, n_diff)
# transform series into supervised format
data = series_to_supervised(train, n_in=n_input)
# separate inputs and outputs
train_x, train_y = data[:, :-1], data[:, -1]
# reshape input data into [samples, timesteps, features]
n_features = 1
train_x = train_x.reshape((train_x.shape[0], train_x.shape[1], n_features))
# define model
model = Sequential()
model.add(LSTM(n_nodes, activation='relu', input_shape=(n_input, n_features)))
model.add(Dense(n_nodes, activation='relu'))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
# fit model
model.fit(train_x, train_y, epochs=n_epochs, batch_size=n_batch, verbose=0)
return model
与 CNN 一样,用于做出预测的单个输入样本也必须重新形成预期的三维结构。
# reshape sample into [samples, timesteps, features]
x_input = array(history[-n_input:]).reshape((1, n_input, 1))
完整的model_predict()
功能如下所示。
# forecast with the fit model
def model_predict(model, history, config):
# unpack config
n_input, _, _, _, n_diff = config
# prepare data
correction = 0.0
if n_diff > 0:
correction = history[-n_diff]
history = difference(history, n_diff)
# reshape sample into [samples, timesteps, features]
x_input = array(history[-n_input:]).reshape((1, n_input, 1))
# forecast
yhat = model.predict(x_input, verbose=0)
return correction + yhat[0]
我们现在可以定义用于创建要评估的模型配置列表的函数。
训练的 LSTM 模型比 MLP 和 CNN 模型慢得多;因此,您可能希望评估每次运行的配置更少。
我们将定义一组非常简单的两种配置来探索:随机和批量梯度下降。
# create a list of configs to try
def model_configs():
# define scope of configs
n_input = [12]
n_nodes = [100]
n_epochs = [50]
n_batch = [1, 150]
n_diff = [12]
# create configs
configs = list()
for i in n_input:
for j in n_nodes:
for k in n_epochs:
for l in n_batch:
for m in n_diff:
cfg = [i, j, k, l, m]
configs.append(cfg)
print('Total configs: %d' % len(configs))
return configs
我们现在拥有了针对 LSTM 模型的网格搜索超参数所需的一切,用于单变量时间序列预测。
下面列出了完整的示例。
# grid search lstm for airline passengers
from math import sqrt
from numpy import array
from numpy import mean
from pandas import DataFrame
from pandas import concat
from pandas import read_csv
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
# split a univariate dataset into train/test sets
def train_test_split(data, n_test):
return data[:-n_test], data[-n_test:]
# transform list into supervised learning format
def series_to_supervised(data, n_in=1, n_out=1):
df = DataFrame(data)
cols = list()
# input sequence (t-n, ... t-1)
for i in range(n_in, 0, -1):
cols.append(df.shift(i))
# forecast sequence (t, t+1, ... t+n)
for i in range(0, n_out):
cols.append(df.shift(-i))
# put it all together
agg = concat(cols, axis=1)
# drop rows with NaN values
agg.dropna(inplace=True)
return agg.values
# root mean squared error or rmse
def measure_rmse(actual, predicted):
return sqrt(mean_squared_error(actual, predicted))
# difference dataset
def difference(data, order):
return [data[i] - data[i - order] for i in range(order, len(data))]
# fit a model
def model_fit(train, config):
# unpack config
n_input, n_nodes, n_epochs, n_batch, n_diff = config
# prepare data
if n_diff > 0:
train = difference(train, n_diff)
# transform series into supervised format
data = series_to_supervised(train, n_in=n_input)
# separate inputs and outputs
train_x, train_y = data[:, :-1], data[:, -1]
# reshape input data into [samples, timesteps, features]
n_features = 1
train_x = train_x.reshape((train_x.shape[0], train_x.shape[1], n_features))
# define model
model = Sequential()
model.add(LSTM(n_nodes, activation='relu', input_shape=(n_input, n_features)))
model.add(Dense(n_nodes, activation='relu'))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
# fit model
model.fit(train_x, train_y, epochs=n_epochs, batch_size=n_batch, verbose=0)
return model
# forecast with the fit model
def model_predict(model, history, config):
# unpack config
n_input, _, _, _, n_diff = config
# prepare data
correction = 0.0
if n_diff > 0:
correction = history[-n_diff]
history = difference(history, n_diff)
# reshape sample into [samples, timesteps, features]
x_input = array(history[-n_input:]).reshape((1, n_input, 1))
# forecast
yhat = model.predict(x_input, verbose=0)
return correction + yhat[0]
# walk-forward validation for univariate data
def walk_forward_validation(data, n_test, cfg):
predictions = list()
# split dataset
train, test = train_test_split(data, n_test)
# fit model
model = model_fit(train, cfg)
# seed history with training dataset
history = [x for x in train]
# step over each time-step in the test set
for i in range(len(test)):
# fit model and make forecast for history
yhat = model_predict(model, history, cfg)
# store forecast in list of predictions
predictions.append(yhat)
# add actual observation to history for the next loop
history.append(test[i])
# estimate prediction error
error = measure_rmse(test, predictions)
print(' > %.3f' % error)
return error
# score a model, return None on failure
def repeat_evaluate(data, config, n_test, n_repeats=10):
# convert config to a key
key = str(config)
# fit and evaluate the model n times
scores = [walk_forward_validation(data, n_test, config) for _ in range(n_repeats)]
# summarize score
result = mean(scores)
print('> Model[%s] %.3f' % (key, result))
return (key, result)
# grid search configs
def grid_search(data, cfg_list, n_test):
# evaluate configs
scores = scores = [repeat_evaluate(data, cfg, n_test) for cfg in cfg_list]
# sort configs by error, asc
scores.sort(key=lambda tup: tup[1])
return scores
# create a list of configs to try
def model_configs():
# define scope of configs
n_input = [12]
n_nodes = [100]
n_epochs = [50]
n_batch = [1, 150]
n_diff = [12]
# create configs
configs = list()
for i in n_input:
for j in n_nodes:
for k in n_epochs:
for l in n_batch:
for m in n_diff:
cfg = [i, j, k, l, m]
configs.append(cfg)
print('Total configs: %d' % len(configs))
return configs
# define dataset
series = read_csv('monthly-airline-passengers.csv', header=0, index_col=0)
data = series.values
# data split
n_test = 12
# model configs
cfg_list = model_configs()
# grid search
scores = grid_search(data, cfg_list, n_test)
print('done')
# list top 10 configs
for cfg, error in scores[:3]:
print(cfg, error)
运行该示例,我们可以看到只评估了两个不同的配置。
我们可以看到[12,100,50,1,12]的配置实现了 21.24 的 RMSE,与实现 50.70 的朴素预测模型相比,这是巧妙的。
该模型需要更多的调整,并且可以使用混合配置做得更好,例如将 CNN 模型作为输入。
我们可以将此配置解压缩为:
- n_input :12
- n_nodes :100
- n_epochs :50
- n_batch :1
- n_diff :12
下面列出了网格搜索的截断示例输出。
鉴于算法的随机性,您的具体分数可能会有所不同。
Total configs: 2
> 20.488
> 17.718
> 21.213
...
> 22.300
> 20.311
> 21.322
> Model[[12, 100, 50, 150, 12]] 21.260
done
[12, 100, 50, 1, 12] 21.243775750634093
[12, 100, 50, 150, 12] 21.259553398553606
扩展
本节列出了一些扩展您可能希望探索的教程的想法。
- 更多配置。探索其中一个模型的大型配置套件,看看您是否能找到能够提高表现的配置。
- 数据缩放。更新网格搜索框架以在拟合模型和反转变换以做出预测之前还支持数据的缩放(标准化和/或标准化)。
- 网络架构。探索网格搜索给定模型的更大架构更改,例如添加更多隐藏层。
- 新数据集。在新的单变量时间序列数据集中探索给定模型的网格搜索。
- 多变量。更新网格搜索框架以支持小的多变量时间序列数据集,例如具有多个输入变量的数据集。
如果你探索任何这些扩展,我很想知道。
进一步阅读
如果您希望深入了解,本节将提供有关该主题的更多资源。
摘要
在本教程中,您了解了如何为深度学习模型开发网格搜索超参数框架。
具体来说,你学到了:
- 如何开发用于调整模型超参数的通用网格搜索框架。
- 如何在航空公司乘客单变量时间序列预测问题上对多层感知机模型进行网格搜索超参数。
- 如何使框架适应卷积和长期短期记忆神经网络的网格搜索超参数。
你有任何问题吗?
在下面的评论中提出您的问题,我会尽力回答。