keras开发者文档 9: 使用RNN

介绍

递归神经网络(RNN)是一类对于建模序列数据(例如时间序列或自然语言)非常有力的神经网络。

从概念上讲,RNN层使用for循环在序列的时间步上进行迭代,同时保持内部状态,该状态对迄今为止已看到的时间步的信息进行编码。

Keras RNN API的设计重点是:

  • 易于使用:内置的keras.layers.RNN,keras.layers.LSTM,keras.layers.GRU图层使您能够快速构建循环模型,而不必进行困难的配置选择。
  • 易于定制:您还可以使用自定义行为定义自己的RNN单元层(for循环的内部),并将其与通用keras.layers.RNN层(for循环本身)一起使用。 这使您能够以最少的代码以灵活的方式快速原型化不同的研究思路。

设置

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

内置RNN层:一个简单的例子

Keras中有三个内置的RNN层:

  • keras.layers.SimpleRNN,一个完全连接的RNN,来自先前时间步的输出将馈送到下一个时间步。

  • keras.layers.GRU,最早在Cho等人于2014年提出。

  • keras.layers.LSTM,于1997年在Hochreiter&Schmidhuber中首次提出。

2015年初,Keras拥有LSTM和GRU的第一个可重用的开源Python实现。

这是一个顺序模型的简单示例,该模型处理整数序列,将每个整数嵌入到64维向量中,然后使用LSTM层处理向量序列。

model = keras.Sequential()
# Add an Embedding layer expecting input vocab of size 1000, and
# output embedding dimension of size 64.
model.add(layers.Embedding(input_dim=1000, output_dim=64))

# Add a LSTM layer with 128 internal units.
model.add(layers.LSTM(128))

# Add a Dense layer with 10 units.
model.add(layers.Dense(10))

model.summary()

内置RNN支持许多有用的功能:

  • 通过dropout和recurrent_dropout参数进行Recurrent dropout
  • 可以通过go_backwards参数反向处理输入序列
  • 通过unroll参数进行循环展开(在CPU上处理短序列时可能导致较大的加速)
  • …和更多。

输出和状态

默认情况下,RNN层的输出每个样本包含一个矢量。 该向量是与最后一个时间步相对应的RNN单元输出,其中包含有关整个输入序列的信息。 此输出的形状为(batch_size,units),其中unit对应于传递给图层构造函数的units参数。

如果您设置return_sequences = True,则RNN层还可以返回每个样本的完整输出序列(每个样本每个时间步一个向量)。 此输出的形状是(batch_size, timesteps, units)。

model = keras.Sequential()
model.add(layers.Embedding(input_dim=1000, output_dim=64))

# The output of GRU will be a 3D tensor of shape (batch_size, timesteps, 256)
model.add(layers.GRU(256, return_sequences=True))

# The output of SimpleRNN will be a 2D tensor of shape (batch_size, 128)
model.add(layers.SimpleRNN(128))

model.add(layers.Dense(10))

model.summary()

另外,RNN层可以返回其最终内部状态。 返回的状态可用于稍后恢复RNN执行to initialize another RNN。 此设置通常用于编码器-解码器序列到序列模型,其中编码器的最终状态用作解码器的初始状态。

要将RNN图层配置为返回其内部状态,请在创建图层时将return_state参数设置为True。 请注意,LSTM具有2个状态张量,而GRU仅具有1个。

要配置图层的初始状态,只需使用其他关键字参数initial_state调用图层即可。 请注意,state的形状需要匹配图层的单位大小,如以下示例所示。

encoder_vocab = 1000
decoder_vocab = 2000

encoder_input = layers.Input(shape=(None,))
encoder_embedded = layers.Embedding(input_dim=encoder_vocab, output_dim=64)(
    encoder_input
)

# Return states in addition to output
output, state_h, state_c = layers.LSTM(64, return_state=True, name="encoder")(
    encoder_embedded
)
encoder_state = [state_h, state_c]

decoder_input = layers.Input(shape=(None,))
decoder_embedded = layers.Embedding(input_dim=decoder_vocab, output_dim=64)(
    decoder_input
)

# Pass the 2 states to a new LSTM layer, as initial state
decoder_output = layers.LSTM(64, name="decoder")(
    decoder_embedded, initial_state=encoder_state
)
output = layers.Dense(10)(decoder_output)

model = keras.Model([encoder_input, decoder_input], output)
model.summary()

RNN层和RNN单元

除了内置的RNN层之外,RNN API还提供了cell级API。 与处理整批输入序列的RNN层不同,RNN单元仅处理单个时间步。

该cell是RNN层的内部for循环。 将cell包裹在keras.layers.RNN层中,可以为您提供能够处理一系列序列的层,例如 RNN(LSTMCell(10))。

在数学上,RNN(LSTMCell(10))产生与LSTM(10)相同的结果。 实际上,在TF v1.x中该层的实现只是创建相应的RNN单元并将其包装在RNN层中。 但是,使用内置的GRU和LSTM层可以使用CuDNN,您可能会看到更好的性能。

内置三个RNN cells,每个cell对应于匹配的RNN层:

  • keras.layers.SimpleRNNCell 对应于 SimpleRNN 层.
  • keras.layers.GRUCell 对应于 GRU 层.
  • keras.layers.LSTMCell 对应于 LSTM 层.
    Cell抽象以及通用的keras.layers.RNN类使为研究实现自定义RNN体系结构变得非常容易。

跨批状态

在处理很长的序列(可能是无限的)时,您可能需要使用跨批状态在处理很长的序列(可能是无限的)时,您可能需要使用跨批状态处理模式。

通常,每次看到新批次时,都会重置RNN层的内部状态(即,假定该层看到的每个样本都独立于过去)。 该层将仅在处理给定样本时保持状态。

如果您有很长的序列,则将它们分成较短的序列,然后将这些较短的序列依次馈入RNN层而不重置该层的状态很有用。 这样,即使一次仅看到一个子序列,该层也可以保留有关整个序列的信息。

您可以通过在构造函数中设置stateful = True来实现。

如果序列s = [t0,t1,… t1546,t1547],则将其拆分为例如模式。

通常,每次看到新批次时,都会重置RNN层的内部状态(即,假定该层看到的每个样本都独立于过去)。 该层将仅在处理给定样本时保持状态。

如果您有很长的序列,则将它们分成较短的序列,然后将这些较短的序列依次馈入RNN层而不重置该层的状态很有用。 这样,即使一次仅看到一个子序列,该层也可以保留有关整个序列的信息。

您可以通过在构造函数中设置stateful = True来实现。

如果序列s = [t0,t1,… t1546,t1547],则将其拆分为例如

s1 = [t0, t1, ... t100]
s2 = [t101, ... t201]
...
s16 = [t1501, ... t1547]

然后,您可以通过以下方式进行处理:

lstm_layer = layers.LSTM(64, stateful=True)
for s in sub_sequences:
  output = lstm_layer(s)

当您想清除状态时,可以使用layer.reset_states()

注意:在此设置中,假定给定批次中的样本i是上一个批次中样本i的延续。 这意味着所有批次应包含相同数量的样本(批次大小)。 例如。 如果一个批次包含[sequence_A_from_t0_to_t100,sequence_B_from_t0_to_t100],则下一个批次应包含[sequence_A_from_t101_to_t200,sequence_B_from_t101_to_t200]。

完整的例子:

paragraph1 = np.random.random((20, 10, 50)).astype(np.float32)
paragraph2 = np.random.random((20, 10, 50)).astype(np.float32)
paragraph3 = np.random.random((20, 10, 50)).astype(np.float32)

lstm_layer = layers.LSTM(64, stateful=True)
output = lstm_layer(paragraph1)
output = lstm_layer(paragraph2)
output = lstm_layer(paragraph3)

# reset_states() will reset the cached state to the original initial_state.
# If no initial_state was provided, zero-states will be used by default.
lstm_layer.reset_states()

RNN状态重用

RNN层的记录状态不包含在layer.weights()中。 如果您想重用RNN层中的状态,则可以通过new_layer(inputs,initial_state = layer.states)的Keras功能API逐层检索states值并将其用作新层的初始状态 ,或模型子类别。

另请注意,在这种情况下可能不使用顺序模型,因为它仅支持具有单个输入和输出的图层,初始状态的额外输入使其无法在此处使用。

paragraph1 = np.random.random((20, 10, 50)).astype(np.float32)
paragraph2 = np.random.random((20, 10, 50)).astype(np.float32)
paragraph3 = np.random.random((20, 10, 50)).astype(np.float32)

lstm_layer = layers.LSTM(64, stateful=True)
output = lstm_layer(paragraph1)
output = lstm_layer(paragraph2)

existing_state = lstm_layer.states

new_lstm_layer = layers.LSTM(64)
new_output = new_lstm_layer(paragraph3, initial_state=existing_state)

Bidirectional RNNs

对于时间序列以外的序列(例如文本),通常RNN模型不仅可以从头到尾处理序列,而且可以向后处理序列,因此可以表现更好。 例如,要预测句子中的下一个单词,通常使单词周围具有上下文,这不仅对单词之前的单词有用。

Keras提供了一个简单的API,供您构建此类双向RNN:keras.layers.Bidirectional包装器。

model = keras.Sequential()

model.add(
    layers.Bidirectional(layers.LSTM(64, return_sequences=True), input_shape=(5, 10))
)
model.add(layers.Bidirectional(layers.LSTM(32)))
model.add(layers.Dense(10))

model.summary()

在后台,双向将复制传入的RNN层,并翻转新复制的层的go_backwards字段,以便它将以相反的顺序处理输入。

默认情况下,双向RNN的输出将是前向图层输出和后向图层输出的总和。 如果您需要其他合并行为,例如 串联,请在“双向包装器”构造函数中更改merge_mode参数。 有关双向的更多详细信息,请查看API文档。

性能优化和CuDNN内核

在TensorFlow 2.0中,内置的LSTM和GRU层已更新为在GPU可用时默认使用CuDNN内核。 通过此更改,先前的keras.layers.CuDNNLSTM / CuDNNGRU层已被弃用,您可以构建模型而不必担心它将运行的硬件。

由于CuDNN内核是根据某些假设构建的,因此这意味着如果您更改内置LSTM或GRU层的默认设置,则该层将无法使用CuDNN内核。 例如。:

  • 将激活功能从tanh更改为其他功能。
  • 将recurrent_activation函数从Sigmoid更改为其他内容。
  • 使用recurrent_dropout> 0。
  • 将unroll设置为True,这将强制LSTM / GRU将内部tf.while_loop分解为unrolled for循环。
  • 将use_bias设置为False。
  • 当输入数据未严格右填充时使用屏蔽(如果掩码对应于严格右填充数据,则仍可以使用CuDNN。这是最常见的情况)。

在可用时使用CuDNN内核

让我们建立一个简单的LSTM模型来演示性能差异。

我们将MNIST数字的行序列(作为时间步长处理每一行像素)用作输入序列,并预测该数字的标签。

batch_size = 64
# Each MNIST image batch is a tensor of shape (batch_size, 28, 28).
# Each input sequence will be of size (28, 28) (height is treated like time).
input_dim = 28

units = 64
output_size = 10  # labels are from 0 to 9

# Build the RNN model
def build_model(allow_cudnn_kernel=True):
    # CuDNN is only available at the layer level, and not at the cell level.
    # This means `LSTM(units)` will use the CuDNN kernel,
    # while RNN(LSTMCell(units)) will run on non-CuDNN kernel.
    if allow_cudnn_kernel:
        # The LSTM layer with default options uses CuDNN.
        lstm_layer = keras.layers.LSTM(units, input_shape=(None, input_dim))
    else:
        # Wrapping a LSTMCell in a RNN layer will not use CuDNN.
        lstm_layer = keras.layers.RNN(
            keras.layers.LSTMCell(units), input_shape=(None, input_dim)
        )
    model = keras.models.Sequential(
        [
            lstm_layer,
            keras.layers.BatchNormalization(),
            keras.layers.Dense(output_size),
        ]
    )
    return model

让我们加载MNIST数据集:

mnist = keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
sample, sample_label = x_train[0], y_train[0]

让我们创建一个模型实例并对其进行训练。

我们选择sparse_categorical_crossentropy作为模型的损失函数。 模型的输出形状为[batch_size,10]。 该模型的目标是一个整数向量,每个整数都在0到9的范围内。

model = build_model(allow_cudnn_kernel=True)

model.compile(
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    optimizer="sgd",
    metrics=["accuracy"],
)


model.fit(
    x_train, y_train, validation_data=(x_test, y_test), batch_size=batch_size, epochs=1
)

现在,让我们与不使用CuDNN内核的模型进行比较:

noncudnn_model = build_model(allow_cudnn_kernel=False)
noncudnn_model.set_weights(model.get_weights())
noncudnn_model.compile(
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    optimizer="sgd",
    metrics=["accuracy"],
)
noncudnn_model.fit(
    x_train, y_train, validation_data=(x_test, y_test), batch_size=batch_size, epochs=1
)

在安装了NVIDIA GPU和CuDNN的计算机上运行时,与使用常规TensorFlow内核的模型相比,使用CuDNN构建的模型的训练速度要快得多。

相同的启用了CuDNN的模型也可以用于在仅CPU的环境中运行推理。 下面的tf.device注释只是强制放置设备。 如果没有可用的GPU,默认情况下该模型将在CPU上运行。

您完全不必担心正在运行的硬件。 那不是很酷吗?

import matplotlib.pyplot as plt

with tf.device("CPU:0"):
    cpu_model = build_model(allow_cudnn_kernel=True)
    cpu_model.set_weights(model.get_weights())
    result = tf.argmax(cpu_model.predict_on_batch(tf.expand_dims(sample, 0)), axis=1)
    print(
        "Predicted result is: %s, target result is: %s" % (result.numpy(), sample_label)
    )
    plt.imshow(sample, cmap=plt.get_cmap("gray"))

具有列表/字典输入或嵌套输入的RNN

嵌套结构允许实施者在单个时间步之内包括更多信息。 例如,一个视频帧可以同时具有音频和视频输入。 在这种情况下,数据形状可能是:

[batch, timestep, {“video”: [height, width, channel], “audio”: [frequency]}]

在另一个示例中,笔迹数据可以具有笔的当前位置的坐标x和y以及压力信息。 因此数据表示可以是:

[batch, timestep, {“location”: [x, y], “pressure”: [force]}]

以下代码提供了一个示例,说明如何构建接受此类结构化输入的自定义RNN单元。

定义一个支持嵌套输入/输出的自定义单元格

class NestedCell(keras.layers.Layer):
    def __init__(self, unit_1, unit_2, unit_3, **kwargs):
        self.unit_1 = unit_1
        self.unit_2 = unit_2
        self.unit_3 = unit_3
        self.state_size = [tf.TensorShape([unit_1]), tf.TensorShape([unit_2, unit_3])]
        self.output_size = [tf.TensorShape([unit_1]), tf.TensorShape([unit_2, unit_3])]
        super(NestedCell, self).__init__(**kwargs)

    def build(self, input_shapes):
        # expect input_shape to contain 2 items, [(batch, i1), (batch, i2, i3)]
        i1 = input_shapes[0][1]
        i2 = input_shapes[1][1]
        i3 = input_shapes[1][2]

        self.kernel_1 = self.add_weight(
            shape=(i1, self.unit_1), initializer="uniform", name="kernel_1"
        )
        self.kernel_2_3 = self.add_weight(
            shape=(i2, i3, self.unit_2, self.unit_3),
            initializer="uniform",
            name="kernel_2_3",
        )

    def call(self, inputs, states):
        # inputs should be in [(batch, input_1), (batch, input_2, input_3)]
        # state should be in shape [(batch, unit_1), (batch, unit_2, unit_3)]
        input_1, input_2 = tf.nest.flatten(inputs)
        s1, s2 = states

        output_1 = tf.matmul(input_1, self.kernel_1)
        output_2_3 = tf.einsum("bij,ijkl->bkl", input_2, self.kernel_2_3)
        state_1 = s1 + output_1
        state_2_3 = s2 + output_2_3

        output = (output_1, output_2_3)
        new_states = (state_1, state_2_3)

        return output, new_states

    def get_config(self):
        return {"unit_1": self.unit_1, "unit_2": unit_2, "unit_3": self.unit_3}

使用嵌套的输入/输出构建RNN模型

让我们构建一个使用keras.layers.RNN图层和我们刚刚定义的自定义单元的Keras模型。

unit_1 = 10
unit_2 = 20
unit_3 = 30

i1 = 32
i2 = 64
i3 = 32
batch_size = 64
num_batches = 10
timestep = 50

cell = NestedCell(unit_1, unit_2, unit_3)
rnn = keras.layers.RNN(cell)

input_1 = keras.Input((None, i1))
input_2 = keras.Input((None, i2, i3))

outputs = rnn((input_1, input_2))

model = keras.models.Model([input_1, input_2], outputs)

model.compile(optimizer="adam", loss="mse", metrics=["accuracy"])

使用随机生成的数据训练模型

由于此模型没有很好的候选数据集,因此我们使用随机的Numpy数据进行演示

input_1_data = np.random.random((batch_size * num_batches, timestep, i1))
input_2_data = np.random.random((batch_size * num_batches, timestep, i2, i3))
target_1_data = np.random.random((batch_size * num_batches, unit_1))
target_2_data = np.random.random((batch_size * num_batches, unit_2, unit_3))
input_data = [input_1_data, input_2_data]
target_data = [target_1_data, target_2_data]

model.fit(input_data, target_data, batch_size=batch_size)

使用Keras keras.layers.RNN层,只需要为序列中的单个步骤定义数学逻辑,而keras.layers.RNN层将为您处理序列迭代。 这是一种快速开发新型RNN(例如LSTM变体)的强大方法。

有关更多详细信息,请访问API文档。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值