机器学习实操 第15章 使用循环神经网络(RNNs)和卷积神经网络(CNNs)处理序列

内容概要

第15章探讨了循环神经网络(RNNs)及其在序列数据处理中的应用。RNNs能够分析时间序列数据,例如网站的每日活跃用户数、城市每小时气温、家庭每日电力消耗等,并利用过去的模式预测未来。RNNs适用于处理任意长度的序列数据,广泛应用于自然语言处理、语音识别、时间序列预测等领域。
在这里插入图片描述

主要内容

  1. 循环神经元和层

    • RNN通过循环连接捕捉序列数据的时间依赖性。每个时间步的输出不仅取决于当前输入,还取决于前一时间步的输出。
    • RNN的基本单元是循环神经元,它在每个时间步接收输入并向自身发送输出,形成时间上的连接。
  2. RNN架构

    • 简单RNN:最基本的RNN架构,适用于短序列数据。
    • 深度RNN:通过堆叠多个RNN层,提高模型的表达能力。
    • LSTM和GRU单元:解决简单RNN在处理长序列时的梯度消失和短期记忆问题。LSTM通过引入门控机制(输入门、遗忘门、输出门)控制信息的流动,而GRU简化了LSTM的结构,合并了遗忘门和输入门,并去掉了输出门。
  3. 序列到序列模型

    • 序列到序列(seq2seq)模型:输入和输出均为序列,适用于机器翻译、文本生成等任务。
    • 序列到向量(seq2vec)模型:输入为序列,输出为单个值,适用于情感分析等任务。
    • 向量到序列(vec2seq)模型:输入为单个值,输出为序列,适用于图像描述生成等任务。
    • 编码器-解码器模型:结合序列到向量和向量到序列模型,适用于机器翻译等任务。
  4. 训练RNN

    • 使用反向传播通过时间(BPTT)来训练RNN。通过展开网络(unrolling),RNN可以表示为多个时间步的前馈网络,从而应用标准的反向传播算法。
  5. 时间序列预测

    • 使用经典统计模型(如ARMA、ARIMA、SARIMA)作为基线,与RNN模型进行比较。
    • 使用Keras的timeseries_dataset_from_array函数准备时间序列数据,构建和训练RNN模型进行预测。
  6. 处理长序列

    • 梯度不稳定问题:通过使用较小的学习率、梯度裁剪、层归一化等技术来解决。
    • 短期记忆问题:使用LSTM和GRU单元扩展RNN的记忆能力。
  7. 1D卷积层和WaveNet

    • 使用1D卷积层处理序列数据,结合RNN或独立使用。
    • WaveNet架构通过逐层增加扩张率,高效处理极长序列,适用于音频和文本生成任务。

精彩语录

  1. 中文:RNNs能够分析时间序列数据,例如网站的每日活跃用户数、城市每小时气温、家庭每日电力消耗等,并利用过去的模式预测未来。
    英文原文:RNNs can analyze time series data, such as the number of daily active users on your website, the hourly temperature in your city, your home’s daily power consumption, the trajectories of nearby cars, and more. Once an RNN learns past patterns in the data, it is able to use its knowledge to forecast the future, assuming of course that past patterns still hold in the future.
    解释:强调了RNN在时间序列预测中的应用。

  2. 中文:通过展开网络(unrolling),RNN可以表示为多个时间步的前馈网络,这使得使用反向传播通过时间(BPTT)进行训练成为可能。
    英文原文:To train an RNN, the trick is to unroll it through time (like we just did) and then use regular backpropagation. This strategy is called backpropagation through time (BPTT).
    解释:介绍了RNN的训练方法。

  3. 中文:LSTM单元通过引入门控机制,能够学习存储、丢弃和读取长期状态,从而有效捕捉长时模式。
    英文原文:The LSTM cell can learn to recognize an important input (that’s the role of the input gate), store it in the long-term state, preserve it for as long as it is needed (that’s the role of the forget gate), and extract it whenever it is needed.
    解释:解释了LSTM单元的工作原理。

  4. 中文:WaveNet通过逐层增加扩张率,使得网络能够同时学习短时和长时模式,适用于处理极长的序列。
    英文原文:WaveNet stacks 1D convolutional layers, doubling the dilation rate at every layer: the first convolutional layer gets a glimpse of just two time steps at a time, while the next one sees four time steps, the next one sees eight time steps, and so on.
    解释:介绍了WaveNet架构的特点。

关键代码

使用Keras实现简单RNN

model = tf.keras.Sequential([
    tf.keras.layers.SimpleRNN(32, input_shape=[None, 1]),
    tf.keras.layers.Dense(1)
])

model.compile(loss="mse", optimizer="adam", metrics=["mae"])
history = model.fit(train_ds, validation_data=valid_ds, epochs=100)

使用LSTM单元

model = tf.keras.Sequential([
    tf.keras.layers.LSTM(32, return_sequences=True, input_shape=[None, 5]),
    tf.keras.layers.Dense(14)
])

model.compile(loss="mse", optimizer="adam", metrics=["mae"])
history = model.fit(train_ds, validation_data=valid_ds, epochs=100)

使用WaveNet架构

wavenet_model = tf.keras.Sequential()
wavenet_model.add(tf.keras.layers.Input(shape=[None, 5]))
for rate in (1, 2, 4, 8) * 2:
    wavenet_model.add(tf.keras.layers.Conv1D(
        filters=32, kernel_size=2, padding="causal", activation="relu", dilation_rate=rate))
wavenet_model.add(tf.keras.layers.Conv1D(filters=14, kernel_size=1))

wavenet_model.compile(loss="mse", optimizer="adam", metrics=["mae"])
history = wavenet_model.fit(train_ds, validation_data=valid_ds, epochs=100)

总结

通过本章的学习,读者将掌握循环神经网络(RNNs)的基本原理和实现方法。内容涵盖了RNN的构建块、训练方法、长短期记忆问题的解决方法,以及如何使用Keras实现RNN模型进行时间序列预测。这些知识将帮助读者在处理序列数据时构建高效、准确的模型。

Logo

脑启社区是一个专注类脑智能领域的开发者社区。欢迎加入社区,共建类脑智能生态。社区为开发者提供了丰富的开源类脑工具软件、类脑算法模型及数据集、类脑知识库、类脑技术培训课程以及类脑应用案例等资源。

更多推荐