第10章深入探讨了时间序列数据的深度学习应用,涵盖了从预测到分类、事件检测和异常检测等多种任务。本章通过温度预测示例,详细介绍了如何使用循环神经网络(RNN)及其变体(如LSTM和GRU)来处理时间序列数据。通过本章,读者将掌握如何使用深度学习解决时间序列问题,并理解RNN的工作原理。
时间序列任务的类型
温度预测示例
循环神经网络(RNN)
高级RNN用法
import numpy as np
from tensorflow import keras
# 数据归一化
mean = raw_data[:num_train_samples].mean(axis=0)
raw_data -= mean
std = raw_data[:num_train_samples].std(axis=0)
raw_data /= std
# 使用timeseries_dataset_from_array生成数据集
sampling_rate = 6
sequence_length = 120
delay = sampling_rate * (sequence_length + 24 - 1)
batch_size = 256
train_dataset = keras.utils.timeseries_dataset_from_array(
raw_data[:-delay],
targets=temperature[delay:],
sampling_rate=sampling_rate,
sequence_length=sequence_length,
shuffle=True,
batch_size=batch_size,
start_index=0,
end_index=num_train_samples
)
def evaluate_naive_method(dataset):
total_abs_err = 0.
samples_seen = 0
for samples, targets in dataset:
preds = samples[:, -1, 1] * std[1] + mean[1]
total_abs_err += np.sum(np.abs(preds - targets))
samples_seen += samples.shape[0]
return total_abs_err / samples_seen
print(f"Validation MAE: {evaluate_naive_method(val_dataset):.2f}")
print(f"Test MAE: {evaluate_naive_method(test_dataset):.2f}")
inputs = keras.Input(shape=(sequence_length, raw_data.shape[-1]))
x = layers.Conv1D(8, 24, activation="relu")(inputs)
x = layers.MaxPooling1D(2)(x)
x = layers.Conv1D(8, 12, activation="relu")(x)
x = layers.MaxPooling1D(2)(x)
x = layers.Conv1D(8, 6, activation="relu")(x)
x = layers.GlobalAveragePooling1D()(x)
outputs = layers.Dense(1)(x)
model = keras.Model(inputs, outputs)
model.compile(optimizer="rmsprop", loss="mse", metrics=["mae"])
inputs = keras.Input(shape=(sequence_length, raw_data.shape[-1]))
x = layers.LSTM(16)(inputs)
outputs = layers.Dense(1)(x)
model = keras.Model(inputs, outputs)
model.compile(optimizer="rmsprop", loss="mse", metrics=["mae"])
inputs = keras.Input(shape=(sequence_length, raw_data.shape[-1]))
x = layers.LSTM(32, recurrent_dropout=0.25)(inputs)
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(1)(x)
model = keras.Model(inputs, outputs)
model.compile(optimizer="rmsprop", loss="mse", metrics=["mae"])
inputs = keras.Input(shape=(sequence_length, raw_data.shape[-1]))
x = layers.GRU(32, recurrent_dropout=0.5, return_sequences=True)(inputs)
x = layers.GRU(32, recurrent_dropout=0.5)(x)
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(1)(x)
model = keras.Model(inputs, outputs)
model.compile(optimizer="rmsprop", loss="mse", metrics=["mae"])
inputs = keras.Input(shape=(sequence_length, raw_data.shape[-1]))
x = layers.Bidirectional(layers.LSTM(16))(inputs)
outputs = layers.Dense(1)(x)
model = keras.Model(inputs, outputs)
model.compile(optimizer="rmsprop", loss="mse", metrics=["mae"])
中文:时间序列数据的核心在于其动态特性。
英文原文:Working with timeseries involves understanding the dynamics of a system—its periodic cycles, how it trends over time, its regular regime and its sudden spikes.
解释:这句话强调了时间序列数据的动态特性和周期性。
中文:RNN通过维护内部状态来处理序列数据。
英文原文:A recurrent neural network (RNN) adopts the same principle: it processes sequences by iterating through the sequence elements and maintaining a state that contains information relative to what it has seen so far.
解释:这句话介绍了RNN的基本工作原理。
中文:LSTM通过引入门控机制解决了梯度消失问题。
英文原文:The LSTM layer adds a way to carry information across many timesteps.
解释:这句话总结了LSTM的核心优势。
中文:循环dropout是防止RNN过拟合的有效方法。
英文原文:Recurrent dropout is a variant of dropout, used to fight overfitting in recurrent layers.
解释:这句话介绍了循环dropout的作用。
中文:双向RNN通过同时处理正向和反向序列信息来提高性能。
英文原文:A bidirectional RNN exploits the order sensitivity of RNNs: it uses two regular RNNs, each of which processes the input sequence in one direction (chronologically and antichronologically), and then merges their representations.
解释:这句话解释了双向RNN的工作原理。
通过本章的学习,读者将掌握时间序列数据的深度学习处理方法,包括如何使用RNN及其变体进行预测、分类和异常检测。这些知识将为解决实际问题提供强大的工具。