Keras实现带attention的seq2seq预测模型

参考:https://machinelearningmastery.com/encoder-decoder-attention-sequence-to-sequence-prediction-keras/

转自:https://blog.csdn.net/baidu_33718858/article/details/85988935

目录

带有注意力机制的编码器-解码器(Encoder-Decoder with Attention)

一个测试注意力机制的问题(Test Problem for Attention)

没有注意力机制的编码-解码(Encoder-Decoder Without Attention)

自定义Keras中的Attention层(Custom Keras Attention Layer)

带有注意力机制的编码器-解码器(Encoder-Decoder With Attention)

模型比较(Comparison of Models)


事实证明,递归神经网络的编码器-解码器体系结构在自然语言处理领域的一系列序列到序列预测问题上非常强大。
注意是一种机制,它解决了编码器-解码器体系结构在长序列上的局限性,并且通常加快了学习速度,提高了序列到序列预测问题模型的技能。

在本教程中,您将了解如何在Keras中实现一个带有注意力机制的编码器-解码器卷积神经网络。

完成本教程后,您将知道:

  • 如何设计一个小而可配置的问题来评估有无注意机制的编码器-解码器循环神经网络。
  • 针对序列预测问题,如何设计和评价一个有无注意机制的编码器-解码器网络。
  • 如何在有无注意机制的情况下,鲁棒比较编码器-解码器网络的性能。

带有注意力机制的编码器-解码器(Encoder-Decoder with Attention)

循环神经网络中的编码器-解码器模型是一种序列到序列预测问题的体系结构。
它由两个子模型组成,顾名思义:

  • 编码器:编码器负责单步执行输入时间步骤,并将整个序列编码为一个固定长度的向量,称为上下文向量(context vector)。
  • 解码器:解码器负责逐步执行输出时间步骤,并从上下文向量读取信息。

该体系结构的一个问题是,长输入或输出序列的性能较差。原因被认为是编码器使用了固定尺寸的上下文向量进行内部表示。
注意力机制是对解决这个限制的体系结构的扩展。它首先提供从编码器到解码器的更丰富的上下文,以及一种学习机制,在该机制中,解码器可以在预测输出序列中的每个时间步的输出时,在更丰富的编码中学习应注意的位置。
有关编码器-解码器体系结构的更多注意事项,请参阅以下文章:

  • Attention in Long Short-Term Memory Recurrent Neural Networks
  • How Does Attention Work in Encoder-Decoder Recurrent Neural Networks

一个测试注意力机制的问题(Test Problem for Attention)

在我们开发带有注意力机制的模型之前,我们首先定义一个人为的可伸缩的测试问题,我们可以用它来确定注意力机制是否带来了任何好处。
在这个问题中,我们将生成随机整数序列作为输入并匹配输出序列,输出序列由输入序列中整数的子集组成。
例如,输入序列可以是[1,6,2,7,3],预期的输出序列可以是序列[1,6]中的前两个随机整数。
我们将定义问题,使输入和输出序列的长度相同,并根据需要用“0”值填充输出序列。
首先,我们需要一个函数来生成随机整数序列。我们将使用python的randint()函数生成介于0和最大值之间的随机整数,并将此范围用作问题的基数(例如,特征数)。

下面的generate_sequence()函数将生成一个具有固定长度和指定基数的随机整数序列。


   
   
   
   
  1. from random import randint
  2. # generate a sequence of random integers
  3. def generate_sequence(length, n_unique):
  4. return [randint( 0, n_unique -1) for _ in range(length)]
  5. # generate random sequence
  6. sequence = generate_sequence( 5, 50)
  7. print(sequence)

运行此示例将生成一个具有5个时间步的序列,其中序列中的每个值都是0到49之间的随机整数。

[43, 3, 28, 34, 33]
   
   
   
   

接下来,我们需要一个函数将离散整数值独热编码为二进制向量。
如果使用的基数为50,则每个整数将由0和指定整数值索引处的1表示的50-元素的向量。
下面的one_hot_encode()函数将对给定的整数序列进行一次独热编码。


   
   
   
   
  1. # one hot encode sequence
  2. def one_hot_encode(sequence, n_unique):
  3. encoding = list()
  4. for value in sequence:
  5. vector = [ 0 for _ in range(n_unique)]
  6. vector[value] = 1
  7. encoding.append(vector)
  8. return array(encoding)

我们还需要能够解码独热编码后的序列。这将需要把从模型的预测或独热编码的预期序列转换回我们可以读取和计算的整数序列。

下面的one_hot_decode()函数将把一个独热编码序列解码回一个整数序列。


   
   
   
   
  1. # decode a one hot encoded string
  2. def one_hot_decode(encoded_seq):
  3. return [argmax(vector) for vector in encoded_seq]

我们可以在下面的示例中测试这些操作。


   
   
   
   
  1. from random import randint
  2. from numpy import array
  3. from numpy import argmax
  4. # generate a sequence of random integers
  5. def generate_sequence(length, n_unique):
  6. return [randint( 0, n_unique -1) for _ in range(length)]
  7. # one hot encode sequence
  8. def one_hot_encode(sequence, n_unique):
  9. encoding = list()
  10. for value in sequence:
  11. vector = [ 0 for _ in range(n_unique)]
  12. vector[value] = 1
  13. encoding.append(vector)
  14. return array(encoding)
  15. # decode a one hot encoded string
  16. def one_hot_decode(encoded_seq):
  17. return [argmax(vector) for vector in encoded_seq]
  18. # argmax()为返回沿轴axis最大值的索引,也就是独热编码后1所在的索引,即原整数数值
  19. # generate random sequence
  20. sequence = generate_sequence( 5, 50)
  21. print(sequence)
  22. # one hot encode
  23. encoded = one_hot_encode(sequence, 50)
  24. print(encoded)
  25. # decode
  26. decoded = one_hot_decode(encoded)
  27. print(decoded)

运行这个示例首先打印一个随机生成的序列,然后打印一个独热编码的版本,最后再次打印解码的序列。


   
   
   
   
  1. [ 3, 18, 32, 11, 36]
  2. [[ 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  3. [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  4. [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  5. [ 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  6. [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0]]
  7. [ 3, 18, 32, 11, 36]

最后,我们需要一个可以创建输入和输出序列对的函数来训练和评估模型。
下面名为get_pair()的函数将返回一个给定了输入长度、输出长度和基数的,输入-输出序列对。输入序列和输出序列的长度相同,即等于输入序列的长度,但输出序列将表示为输入序列的前N个字符,并用零值填充到所需的长度。
然后对整数序列进行独热编码,然后将其reshape为循环神经网络所需的三维格式,其尺寸为:样本、时间步和特征。在这种情况下,样本总是1,因为我们只生成一个输入-输出对,时间步数是输入序列长度,特征是每个时间步数的基数。


   
   
   
   
  1. # prepare data for the LSTM
  2. def get_pair(n_in, n_out, n_unique):
  3. # generate random sequence
  4. sequence_in = generate_sequence(n_in, n_unique)
  5. sequence_out = sequence_in[:n_out] + [ 0 for _ in range(n_in-n_out)]
  6. # one hot encode
  7. X = one_hot_encode(sequence_in, n_unique)
  8. y = one_hot_encode(sequence_out, n_unique)
  9. # reshape as 3D
  10. X = X.reshape(( 1, X.shape[ 0], X.shape[ 1]))
  11. y = y.reshape(( 1, y.shape[ 0], y.shape[ 1]))
  12. return X,y

我们可以把这些放在一起并演示数据准备部分的代码。


   
   
   
   
  1. from random import randint
  2. from numpy import array
  3. from numpy import argmax
  4. # generate a sequence of random integers
  5. def generate_sequence(length, n_unique):
  6. return [randint( 0, n_unique -1) for _ in range(length)]
  7. # one hot encode sequence
  8. def one_hot_encode(sequence, n_unique):
  9. encoding = list()
  10. for value in sequence:
  11. vector = [ 0 for _ in range(n_unique)]
  12. vector[value] = 1
  13. encoding.append(vector)
  14. return array(encoding)
  15. # decode a one hot encoded string
  16. def one_hot_decode(encoded_seq):
  17. return [argmax(vector) for vector in encoded_seq]
  18. # prepare data for the LSTM
  19. def get_pair(n_in, n_out, n_unique):
  20. # generate random sequence
  21. sequence_in = generate_sequence(n_in, n_unique)
  22. sequence_out = sequence_in[:n_out] + [ 0 for _ in range(n_in-n_out)]
  23. # one hot encode
  24. X = one_hot_encode(sequence_in, n_unique)
  25. y = one_hot_encode(sequence_out, n_unique)
  26. # reshape as 3D
  27. X = X.reshape(( 1, X.shape[ 0], X.shape[ 1]))
  28. y = y.reshape(( 1, y.shape[ 0], y.shape[ 1]))
  29. return X,y
  30. # generate random sequence
  31. X, y = get_pair( 5, 2, 50)
  32. print(X.shape, y.shape)
  33. print( 'X=%s, y=%s' % (one_hot_decode(X[ 0]), one_hot_decode(y[ 0])))

运行该示例将生成一个输入输出对,并打印两个数组的形状。
然后以解码的形式打印生成的对,我们可以看到序列的前两个整数在输出序列中被复制,后面是零值的填充。


   
   
   
   
  1. ( 1, 5, 50) ( 1, 5, 50)
  2. X=[ 12, 20, 36, 40, 12], y=[ 12, 20, 0, 0, 0]

没有注意力机制的编码-解码(Encoder-Decoder Without Attention)

在这一部分中,我们将开发一个解决上述问题的编码器-解码器模型作为评估的基准。
首先,我们将重复问题的定义:5个时间步长的输入和输出序列,输出序列中的值为输入序列的前2个元素,以50为基数(特征的个数)。


   
   
   
   
  1. # configure problem
  2. n_features = 50
  3. n_timesteps_in = 5
  4. n_timesteps_out = 2

我们可以通过从编码器lstm模型中获取输出编码向量,并按照输出序列中的时间步数重复输入编码向量n次,然后使用解码器预测输出序列,从而在keras中开发一个简单的编码器-解码器模型。
有关如何在Keras中定义编码器-解码器体系结构的详细信息,请参阅以下文章:

  • Encoder-Decoder Long Short-Term Memory Networks

我们将使用相同数量的单元(units,代表循环神经网络中隐藏层的单元个数,也代表了循环神经网络的输出维度)配置编码器和解码器,在本例中为150。我们将利用Adam有效实现梯度下降,并优化分类交叉熵损失函数,前提是该问题在技术上是一个多分类问题。
模型的配置是经过一点尝试和错误后发现的,决不是最优化的。
下面列出了keras中编码器-解码器体系结构的代码。


   
   
   
   
  1. # define model
  2. model = Sequential()
  3. model.add(LSTM( 150, input_shape=(n_timesteps_in, n_features)))
  4. model.add(RepeatVector(n_timesteps_in))
  5. model.add(LSTM( 150, return_sequences= True))
  6. model.add(TimeDistributed(Dense(n_features, activation= 'softmax')))
  7. model.compile(loss= 'categorical_crossentropy', optimizer= 'adam', metrics=[ 'acc'])

我们将在5000个整数序列的随机输入输出对上训练模型。


   
   
   
   
  1. # train LSTM
  2. for epoch in range( 5000):
  3. # generate new random sequence
  4. X,y = get_pair(n_timesteps_in, n_timesteps_out, n_features)
  5. # fit model for one epoch on this sequence
  6. model.fit(X, y, epochs= 1, verbose= 2)

经过训练后,我们将在100个新的随机生成的整数序列上评估模型,并且仅在整个输出序列与预期值匹配时标记预测正确。


   
   
   
   
  1. # evaluate LSTM
  2. total, correct = 100, 0
  3. for _ in range(total):
  4. X,y = get_pair(n_timesteps_in, n_timesteps_out, n_features)
  5. yhat = model.predict(X, verbose= 0)
  6. if array_equal(one_hot_decode(y[ 0]), one_hot_decode(yhat[ 0])):
  7. correct += 1
  8. print( 'Accuracy: %.2f%%' % (float(correct)/float(total)* 100.0))

最后,我们将打印10个期望输出序列和模型预测序列的例子。
将所有这些放在一起,下面列出了完整的示例。


   
   
   
   
  1. from random import randint
  2. from numpy import array
  3. from numpy import argmax
  4. from numpy import array_equal
  5. from keras.models import Sequential
  6. from keras.layers import LSTM
  7. from keras.layers import Dense
  8. from keras.layers import TimeDistributed
  9. from keras.layers import RepeatVector
  10. # generate a sequence of random integers
  11. def generate_sequence(length, n_unique):
  12. return [randint( 0, n_unique -1) for _ in range(length)]
  13. # one hot encode sequence
  14. def one_hot_encode(sequence, n_unique):
  15. encoding = list()
  16. for value in sequence:
  17. vector = [ 0 for _ in range(n_unique)]
  18. vector[value] = 1
  19. encoding.append(vector)
  20. return array(encoding)
  21. # decode a one hot encoded string
  22. def one_hot_decode(encoded_seq):
  23. return [argmax(vector) for vector in encoded_seq]
  24. # prepare data for the LSTM
  25. def get_pair(n_in, n_out, cardinality):
  26. # generate random sequence
  27. sequence_in = generate_sequence(n_in, cardinality)
  28. sequence_out = sequence_in[:n_out] + [ 0 for _ in range(n_in-n_out)]
  29. # one hot encode
  30. X = one_hot_encode(sequence_in, cardinality)
  31. y = one_hot_encode(sequence_out, cardinality)
  32. # reshape as 3D
  33. X = X.reshape(( 1, X.shape[ 0], X.shape[ 1]))
  34. y = y.reshape(( 1, y.shape[ 0], y.shape[ 1]))
  35. return X,y
  36. # configure problem
  37. n_features = 50
  38. n_timesteps_in = 5
  39. n_timesteps_out = 2
  40. # define model
  41. model = Sequential()
  42. model.add(LSTM( 150, input_shape=(n_timesteps_in, n_features)))
  43. model.add(RepeatVector(n_timesteps_in))
  44. model.add(LSTM( 150, return_sequences= True))
  45. model.add(TimeDistributed(Dense(n_features, activation= 'softmax')))
  46. model.compile(loss= 'categorical_crossentropy', optimizer= 'adam', metrics=[ 'acc'])
  47. # train LSTM
  48. for epoch in range( 5000):
  49. # generate new random sequence
  50. X,y = get_pair(n_timesteps_in, n_timesteps_out, n_features)
  51. # fit model for one epoch on this sequence
  52. model.fit(X, y, epochs= 1, verbose= 2)
  53. # evaluate LSTM
  54. total, correct = 100, 0
  55. for _ in range(total):
  56. X,y = get_pair(n_timesteps_in, n_timesteps_out, n_features)
  57. yhat = model.predict(X, verbose= 0)
  58. if array_equal(one_hot_decode(y[ 0]), one_hot_decode(yhat[ 0])):
  59. correct += 1
  60. print( 'Accuracy: %.2f%%' % (float(correct)/float(total)* 100.0))
  61. # spot check some examples
  62. for _ in range( 10):
  63. X,y = get_pair(n_timesteps_in, n_timesteps_out, n_features)
  64. yhat = model.predict(X, verbose= 0)
  65. print( 'Expected:', one_hot_decode(y[ 0]), 'Predicted', one_hot_decode(yhat[ 0]))

在CPU上运行这个例子不会花费很长时间,可能几分钟,不需要GPU。
根据实验该模型的准确度略低于20%。考虑到神经网络的随机性,您的结果会有所不同;可以运行示例几次并取平均值。

Accuracy: 19.00%
   
   
   
   

我们可以从样本输出中看到,模型在输出序列中得到的第一个数字对于大多数或所有情况都是正确的,并且只与第二个数字发生冲突。所有零填充值都是正确预测的。


   
   
   
   
  1. Expected: [ 47, 0, 0, 0, 0] Predicted [ 47, 47, 0, 0, 0]
  2. Expected: [ 43, 31, 0, 0, 0] Predicted [ 43, 31, 0, 0, 0]
  3. Expected: [ 14, 22, 0, 0, 0] Predicted [ 14, 14, 0, 0, 0]
  4. Expected: [ 39, 31, 0, 0, 0] Predicted [ 39, 39, 0, 0, 0]
  5. Expected: [ 6, 4, 0, 0, 0] Predicted [ 6, 4, 0, 0, 0]
  6. Expected: [ 47, 0, 0, 0, 0] Predicted [ 47, 47, 0, 0, 0]
  7. Expected: [ 39, 33, 0, 0, 0] Predicted [ 39, 39, 0, 0, 0]
  8. Expected: [ 23, 2, 0, 0, 0] Predicted [ 23, 23, 0, 0, 0]
  9. Expected: [ 19, 28, 0, 0, 0] Predicted [ 19, 3, 0, 0, 0]
  10. Expected: [ 32, 33, 0, 0, 0] Predicted [ 32, 32, 0, 0, 0]

自定义Keras中的Attention层(Custom Keras Attention Layer)

现在我们需要将注意力机制引入编码器-解码器模型。
在Keras发布正式的注意力机制之前(查Keras的官网发现了一段模糊的话,没看到专门的介绍attention的文档,如果已经有官方实现了,欢迎留言告诉我),我们可以开发自己的实施方案,也可以使用现有的第三方实施方案。


为了加快速度,让我们使用现有的第三方实现。
Zafarali Ahmed ,一位在数据日志网站上的实习生,在2017年发表的题为“How to Visualize Your Recurrent Neural Network with Attention in Keras”的文章和名为“Keras attention”的Github项目中,为Keras开发了一个定制层,为attention提供支持。
自定义关注层称为AttentionDecoder,可在github项目中的custom_recurrents.py文件中找到。我们可以在项目的GNU affero通用公共许可v3.0许可证下重用此代码。
为了完整起见,下面列出了自定义层的副本。复制并粘贴到当前工作目录中名为“attention_decoder.py”的新的单独文件中。


   
   
   
   
  1. import tensorflow as tf
  2. from keras import backend as K
  3. from keras import regularizers, constraints, initializers, activations
  4. from keras.layers.recurrent import Recurrent, _time_distributed_dense
  5. from keras.engine import InputSpec
  6. tfPrint = lambda d, T: tf.Print(input_=T, data=[T, tf.shape(T)], message=d)
  7. class AttentionDecoder(Recurrent):
  8. def __init__(self, units, output_dim,
  9. activation='tanh',
  10. return_probabilities=False,
  11. name='AttentionDecoder',
  12. kernel_initializer='glorot_uniform',
  13. recurrent_initializer='orthogonal',
  14. bias_initializer='zeros',
  15. kernel_regularizer=None,
  16. bias_regularizer=None,
  17. activity_regularizer=None,
  18. kernel_constraint=None,
  19. bias_constraint=None,
  20. **kwargs):
  21. """
  22. Implements an AttentionDecoder that takes in a sequence encoded by an
  23. encoder and outputs the decoded states
  24. :param units: dimension of the hidden state and the attention matrices
  25. :param output_dim: the number of labels in the output space
  26. references:
  27. Bahdanau, Dzmitry, Kyunghyun Cho, and Yoshua Bengio.
  28. "Neural machine translation by jointly learning to align and translate."
  29. arXiv preprint arXiv:1409.0473 (2014).
  30. """
  31. self.units = units
  32. self.output_dim = output_dim
  33. self.return_probabilities = return_probabilities
  34. self.activation = activations.get(activation)
  35. self.kernel_initializer = initializers.get(kernel_initializer)
  36. self.recurrent_initializer = initializers.get(recurrent_initializer)
  37. self.bias_initializer = initializers.get(bias_initializer)
  38. self.kernel_regularizer = regularizers.get(kernel_regularizer)
  39. self.recurrent_regularizer = regularizers.get(kernel_regularizer)
  40. self.bias_regularizer = regularizers.get(bias_regularizer)
  41. self.activity_regularizer = regularizers.get(activity_regularizer)
  42. self.kernel_constraint = constraints.get(kernel_constraint)
  43. self.recurrent_constraint = constraints.get(kernel_constraint)
  44. self.bias_constraint = constraints.get(bias_constraint)
  45. super(AttentionDecoder, self).__init__(**kwargs)
  46. self.name = name
  47. self.return_sequences = True # must return sequences
  48. def build(self, input_shape):
  49. """
  50. See Appendix 2 of Bahdanau 2014, arXiv:1409.0473
  51. for model details that correspond to the matrices here.
  52. """
  53. self.batch_size, self.timesteps, self.input_dim = input_shape
  54. if self.stateful:
  55. super(AttentionDecoder, self).reset_states()
  56. self.states = [ None, None] # y, s
  57. """
  58. Matrices for creating the context vector
  59. """
  60. self.V_a = self.add_weight(shape=(self.units,),
  61. name= 'V_a',
  62. initializer=self.kernel_initializer,
  63. regularizer=self.kernel_regularizer,
  64. constraint=self.kernel_constraint)
  65. self.W_a = self.add_weight(shape=(self.units, self.units),
  66. name= 'W_a',
  67. initializer=self.kernel_initializer,
  68. regularizer=self.kernel_regularizer,
  69. constraint=self.kernel_constraint)
  70. self.U_a = self.add_weight(shape=(self.input_dim, self.units),
  71. name= 'U_a',
  72. initializer=self.kernel_initializer,
  73. regularizer=self.kernel_regularizer,
  74. constraint=self.kernel_constraint)
  75. self.b_a = self.add_weight(shape=(self.units,),
  76. name= 'b_a',
  77. initializer=self.bias_initializer,
  78. regularizer=self.bias_regularizer,
  79. constraint=self.bias_constraint)
  80. """
  81. Matrices for the r (reset) gate
  82. """
  83. self.C_r = self.add_weight(shape=(self.input_dim, self.units),
  84. name= 'C_r',
  85. initializer=self.recurrent_initializer,
  86. regularizer=self.recurrent_regularizer,
  87. constraint=self.recurrent_constraint)
  88. self.U_r = self.add_weight(shape=(self.units, self.units),
  89. name= 'U_r',
  90. initializer=self.recurrent_initializer,
  91. regularizer=self.recurrent_regularizer,
  92. constraint=self.recurrent_constraint)
  93. self.W_r = self.add_weight(shape=(self.output_dim, self.units),
  94. name= 'W_r',
  95. initializer=self.recurrent_initializer,
  96. regularizer=self.recurrent_regularizer,
  97. constraint=self.recurrent_constraint)
  98. self.b_r = self.add_weight(shape=(self.units, ),
  99. name= 'b_r',
  100. initializer=self.bias_initializer,
  101. regularizer=self.bias_regularizer,
  102. constraint=self.bias_constraint)
  103. """
  104. Matrices for the z (update) gate
  105. """
  106. self.C_z = self.add_weight(shape=(self.input_dim, self.units),
  107. name= 'C_z',
  108. initializer=self.recurrent_initializer,
  109. regularizer=self.recurrent_regularizer,
  110. constraint=self.recurrent_constraint)
  111. self.U_z = self.add_weight(shape=(self.units, self.units),
  112. name= 'U_z',
  113. initializer=self.recurrent_initializer,
  114. regularizer=self.recurrent_regularizer,
  115. constraint=self.recurrent_constraint)
  116. self.W_z = self.add_weight(shape=(self.output_dim, self.units),
  117. name= 'W_z',
  118. initializer=self.recurrent_initializer,
  119. regularizer=self.recurrent_regularizer,
  120. constraint=self.recurrent_constraint)
  121. self.b_z = self.add_weight(shape=(self.units, ),
  122. name= 'b_z',
  123. initializer=self.bias_initializer,
  124. regularizer=self.bias_regularizer,
  125. constraint=self.bias_constraint)
  126. """
  127. Matrices for the proposal
  128. """
  129. self.C_p = self.add_weight(shape=(self.input_dim, self.units),
  130. name= 'C_p',
  131. initializer=self.recurrent_initializer,
  132. regularizer=self.recurrent_regularizer,
  133. constraint=self.recurrent_constraint)
  134. self.U_p = self.add_weight(shape=(self.units, self.units),
  135. name= 'U_p',
  136. initializer=self.recurrent_initializer,
  137. regularizer=self.recurrent_regularizer,
  138. constraint=self.recurrent_constraint)
  139. self.W_p = self.add_weight(shape=(self.output_dim, self.units),
  140. name= 'W_p',
  141. initializer=self.recurrent_initializer,
  142. regularizer=self.recurrent_regularizer,
  143. constraint=self.recurrent_constraint)
  144. self.b_p = self.add_weight(shape=(self.units, ),
  145. name= 'b_p',
  146. initializer=self.bias_initializer,
  147. regularizer=self.bias_regularizer,
  148. constraint=self.bias_constraint)
  149. """
  150. Matrices for making the final prediction vector
  151. """
  152. self.C_o = self.add_weight(shape=(self.input_dim, self.output_dim),
  153. name= 'C_o',
  154. initializer=self.recurrent_initializer,
  155. regularizer=self.recurrent_regularizer,
  156. constraint=self.recurrent_constraint)
  157. self.U_o = self.add_weight(shape=(self.units, self.output_dim),
  158. name= 'U_o',
  159. initializer=self.recurrent_initializer,
  160. regularizer=self.recurrent_regularizer,
  161. constraint=self.recurrent_constraint)
  162. self.W_o = self.add_weight(shape=(self.output_dim, self.output_dim),
  163. name= 'W_o',
  164. initializer=self.recurrent_initializer,
  165. regularizer=self.recurrent_regularizer,
  166. constraint=self.recurrent_constraint)
  167. self.b_o = self.add_weight(shape=(self.output_dim, ),
  168. name= 'b_o',
  169. initializer=self.bias_initializer,
  170. regularizer=self.bias_regularizer,
  171. constraint=self.bias_constraint)
  172. # For creating the initial state:
  173. self.W_s = self.add_weight(shape=(self.input_dim, self.units),
  174. name= 'W_s',
  175. initializer=self.recurrent_initializer,
  176. regularizer=self.recurrent_regularizer,
  177. constraint=self.recurrent_constraint)
  178. self.input_spec = [
  179. InputSpec(shape=(self.batch_size, self.timesteps, self.input_dim))]
  180. self.built = True
  181. def call(self, x):
  182. # store the whole sequence so we can "attend" to it at each timestep
  183. self.x_seq = x
  184. # apply the a dense layer over the time dimension of the sequence
  185. # do it here because it doesn't depend on any previous steps
  186. # thefore we can save computation time:
  187. self._uxpb = _time_distributed_dense(self.x_seq, self.U_a, b=self.b_a,
  188. input_dim=self.input_dim,
  189. timesteps=self.timesteps,
  190. output_dim=self.units)
  191. return super(AttentionDecoder, self).call(x)
  192. def get_initial_state(self, inputs):
  193. # apply the matrix on the first time step to get the initial s0.
  194. s0 = activations.tanh(K.dot(inputs[:, 0], self.W_s))
  195. # from keras.layers.recurrent to initialize a vector of (batchsize,
  196. # output_dim)
  197. y0 = K.zeros_like(inputs) # (samples, timesteps, input_dims)
  198. y0 = K.sum(y0, axis=( 1, 2)) # (samples, )
  199. y0 = K.expand_dims(y0) # (samples, 1)
  200. y0 = K.tile(y0, [ 1, self.output_dim])
  201. return [y0, s0]
  202. def step(self, x, states):
  203. ytm, stm = states
  204. # repeat the hidden state to the length of the sequence
  205. _stm = K.repeat(stm, self.timesteps)
  206. # now multiplty the weight matrix with the repeated hidden state
  207. _Wxstm = K.dot(_stm, self.W_a)
  208. # calculate the attention probabilities
  209. # this relates how much other timesteps contributed to this one.
  210. et = K.dot(activations.tanh(_Wxstm + self._uxpb),
  211. K.expand_dims(self.V_a))
  212. at = K.exp(et)
  213. at_sum = K.sum(at, axis= 1)
  214. at_sum_repeated = K.repeat(at_sum, self.timesteps)
  215. at /= at_sum_repeated # vector of size (batchsize, timesteps, 1)
  216. # calculate the context vector
  217. context = K.squeeze(K.batch_dot(at, self.x_seq, axes= 1), axis= 1)
  218. # ~~~> calculate new hidden state
  219. # first calculate the "r" gate:
  220. rt = activations.sigmoid(
  221. K.dot(ytm, self.W_r)
  222. + K.dot(stm, self.U_r)
  223. + K.dot(context, self.C_r)
  224. + self.b_r)
  225. # now calculate the "z" gate
  226. zt = activations.sigmoid(
  227. K.dot(ytm, self.W_z)
  228. + K.dot(stm, self.U_z)
  229. + K.dot(context, self.C_z)
  230. + self.b_z)
  231. # calculate the proposal hidden state:
  232. s_tp = activations.tanh(
  233. K.dot(ytm, self.W_p)
  234. + K.dot((rt * stm), self.U_p)
  235. + K.dot(context, self.C_p)
  236. + self.b_p)
  237. # new hidden state:
  238. st = ( 1-zt)*stm + zt * s_tp
  239. yt = activations.softmax(
  240. K.dot(ytm, self.W_o)
  241. + K.dot(stm, self.U_o)
  242. + K.dot(context, self.C_o)
  243. + self.b_o)
  244. if self.return_probabilities:
  245. return at, [yt, st]
  246. else:
  247. return yt, [yt, st]
  248. def compute_output_shape(self, input_shape):
  249. """
  250. For Keras internal compatability checking
  251. """
  252. if self.return_probabilities:
  253. return ( None, self.timesteps, self.timesteps)
  254. else:
  255. return ( None, self.timesteps, self.output_dim)
  256. def get_config(self):
  257. """
  258. For rebuilding models on load time.
  259. """
  260. config = {
  261. 'output_dim': self.output_dim,
  262. 'units': self.units,
  263. 'return_probabilities': self.return_probabilities
  264. }
  265. base_config = super(AttentionDecoder, self).get_config()
  266. return dict(list(base_config.items()) + list(config.items()))

我们可以在项目中使用这个自定义层,通过用如下的方式导入它:

from attention_decoder import AttentionDecoder
   
   
   
   

该层实现了Bahdanau等人在他们的“Neural Machine Translation by Jointly Learning to Align and Translate.”论文中所描述的注意力机制。
这段代码在文章中解释得很好,并且与LSTM和注意力方程式都有关联。
这种实现的一个限制是,它必须输出与输入序列长度相同的序列,而这是编码器-解码器体系结构设计要克服的特定限制。(确实是这样,如果输入和输出序列长度不一样,可以采用补0措施,将短的序列补为和长的一样的;或者在得到这层输出之后,再通过一层GRU改变序列长度)
重要的是,该层不但避免了第二个LSTM层作为解码器的代码重复,还在这个编码器-解码器模型中通过不加注意机制的Dense层得到SoftMax后的输出。这大大简化了模型的代码。
需要注意的是,自定义层是建立在Keras中的循环层(继承了Recurrent)之上的,在编写时,它被标记为遗留代码,并且可能会在某个时刻从项目中删除。


带有注意力机制的编码器-解码器(Encoder-Decoder With Attention)

既然我们已经有了可以使用的注意力实现,那么我们就可以为我们设计的序列预测问题开发一个有注意力的编码器-解码器模型。
下面定义了具有注意层的模型。我们可以看到,该层能处理编码器-解码器模型本身的一些机制,使得定义模型更简单。


   
   
   
   
  1. # define model
  2. model = Sequential()
  3. model.add(LSTM( 150, input_shape=(n_timesteps_in, n_features), return_sequences= True))
  4. model.add(AttentionDecoder( 150, n_features))
  5. model.compile(loss= 'categorical_crossentropy', optimizer= 'adam', metrics=[ 'acc'])

就是这样。其余的例子是相同的。
下面列出了完整的示例。


   
   
   
   
  1. from random import randint
  2. from numpy import array
  3. from numpy import argmax
  4. from numpy import array_equal
  5. from keras.models import Sequential
  6. from keras.layers import LSTM
  7. from attention_decoder import AttentionDecoder
  8. # generate a sequence of random integers
  9. def generate_sequence(length, n_unique):
  10. return [randint( 0, n_unique -1) for _ in range(length)]
  11. # one hot encode sequence
  12. def one_hot_encode(sequence, n_unique):
  13. encoding = list()
  14. for value in sequence:
  15. vector = [ 0 for _ in range(n_unique)]
  16. vector[value] = 1
  17. encoding.append(vector)
  18. return array(encoding)
  19. # decode a one hot encoded string
  20. def one_hot_decode(encoded_seq):
  21. return [argmax(vector) for vector in encoded_seq]
  22. # prepare data for the LSTM
  23. def get_pair(n_in, n_out, cardinality):
  24. # generate random sequence
  25. sequence_in = generate_sequence(n_in, cardinality)
  26. sequence_out = sequence_in[:n_out] + [ 0 for _ in range(n_in-n_out)]
  27. # one hot encode
  28. X = one_hot_encode(sequence_in, cardinality)
  29. y = one_hot_encode(sequence_out, cardinality)
  30. # reshape as 3D
  31. X = X.reshape(( 1, X.shape[ 0], X.shape[ 1]))
  32. y = y.reshape(( 1, y.shape[ 0], y.shape[ 1]))
  33. return X,y
  34. # configure problem
  35. n_features = 50
  36. n_timesteps_in = 5
  37. n_timesteps_out = 2
  38. # define model
  39. model = Sequential()
  40. model.add(LSTM( 150, input_shape=(n_timesteps_in, n_features), return_sequences= True))
  41. model.add(AttentionDecoder( 150, n_features))
  42. model.compile(loss= 'categorical_crossentropy', optimizer= 'adam', metrics=[ 'acc'])
  43. # train LSTM
  44. for epoch in range( 5000):
  45. # generate new random sequence
  46. X,y = get_pair(n_timesteps_in, n_timesteps_out, n_features)
  47. # fit model for one epoch on this sequence
  48. model.fit(X, y, epochs= 1, verbose= 2)
  49. # evaluate LSTM
  50. total, correct = 100, 0
  51. for _ in range(total):
  52. X,y = get_pair(n_timesteps_in, n_timesteps_out, n_features)
  53. yhat = model.predict(X, verbose= 0)
  54. if array_equal(one_hot_decode(y[ 0]), one_hot_decode(yhat[ 0])):
  55. correct += 1
  56. print( 'Accuracy: %.2f%%' % (float(correct)/float(total)* 100.0))
  57. # spot check some examples
  58. for _ in range( 10):
  59. X,y = get_pair(n_timesteps_in, n_timesteps_out, n_features)
  60. yhat = model.predict(X, verbose= 0)
  61. print( 'Expected:', one_hot_decode(y[ 0]), 'Predicted', one_hot_decode(yhat[ 0]))

运行这个示例可以在100个随机生成的输入输出对进行训练来评估模型的性能。在相同的资源和相同的训练量下,具有注意力的模型表现得更好。
考虑到神经网络的随机性,你的结果可能会有所不同。尝试运行该示例几次。

Accuracy: 95.00%
   
   
   
   

抽查一些样本输出和预测序列,我们可以看到即使在前两个元素中有零值的情况下,都很少有错误。


   
   
   
   
  1. Expected: [ 48, 47, 0, 0, 0] Predicted [ 48, 47, 0, 0, 0]
  2. Expected: [ 7, 46, 0, 0, 0] Predicted [ 7, 46, 0, 0, 0]
  3. Expected: [ 32, 30, 0, 0, 0] Predicted [ 32, 2, 0, 0, 0]
  4. Expected: [ 3, 25, 0, 0, 0] Predicted [ 3, 25, 0, 0, 0]
  5. Expected: [ 45, 4, 0, 0, 0] Predicted [ 45, 4, 0, 0, 0]
  6. Expected: [ 49, 9, 0, 0, 0] Predicted [ 49, 9, 0, 0, 0]
  7. Expected: [ 22, 23, 0, 0, 0] Predicted [ 22, 23, 0, 0, 0]
  8. Expected: [ 29, 36, 0, 0, 0] Predicted [ 29, 36, 0, 0, 0]
  9. Expected: [ 0, 29, 0, 0, 0] Predicted [ 0, 29, 0, 0, 0]
  10. Expected: [ 11, 26, 0, 0, 0] Predicted [ 11, 26, 0, 0, 0]

模型比较(Comparison of Models)

尽管我们在有注意力机制的情况下从模型中得到了更好的结果,但是那只是从模型的一次运行中得到的结果。
在这种情况下,我们通过多次重复对每个模型的评估,并报告这些运行的平均性能,来寻求更可靠的模型评估结果。有关评估神经网络模型的这种稳健方法的更多信息,请参阅以下文章:

  • How to Evaluate the Skill of Deep Learning Models

我们可以定义一个函数来创建每种类型的模型,如下所示。


   
   
   
   
  1. # define the encoder-decoder model
  2. def baseline_model(n_timesteps_in, n_features):
  3. model = Sequential()
  4. model.add(LSTM( 150, input_shape=(n_timesteps_in, n_features)))
  5. model.add(RepeatVector(n_timesteps_in))
  6. model.add(LSTM( 150, return_sequences= True))
  7. model.add(TimeDistributed(Dense(n_features, activation= 'softmax')))
  8. model.compile(loss= 'categorical_crossentropy', optimizer= 'adam', metrics=[ 'acc'])
  9. return model
  10. # define the encoder-decoder with attention model
  11. def attention_model(n_timesteps_in, n_features):
  12. model = Sequential()
  13. model.add(LSTM( 150, input_shape=(n_timesteps_in, n_features), return_sequences= True))
  14. model.add(AttentionDecoder( 150, n_features))
  15. model.compile(loss= 'categorical_crossentropy', optimizer= 'adam', metrics=[ 'acc'])
  16. return model

然后我们可以定义一个函数来拟合和评估模型的精度,并返回精度分数。


   
   
   
   
  1. # train and evaluate a model, return accuracy
  2. def train_evaluate_model(model, n_timesteps_in, n_timesteps_out, n_features):
  3. # train LSTM
  4. for epoch in range( 5000):
  5. # generate new random sequence
  6. X,y = get_pair(n_timesteps_in, n_timesteps_out, n_features)
  7. # fit model for one epoch on this sequence
  8. model.fit(X, y, epochs= 1, verbose= 0)
  9. # evaluate LSTM
  10. total, correct = 100, 0
  11. for _ in range(total):
  12. X,y = get_pair(n_timesteps_in, n_timesteps_out, n_features)
  13. yhat = model.predict(X, verbose= 0)
  14. if array_equal(one_hot_decode(y[ 0]), one_hot_decode(yhat[ 0])):
  15. correct += 1
  16. return float(correct)/float(total)* 100.0

综合起来,我们可以多次重复创建、训练和评估每种模型的过程,并报告多次评估的平均准确性。为了减少运行时间,我们将重复评估每个模型10次,如果您有资源,您可以将其增加到30或100次。
下面列出了完整的示例。


   
   
   
   
  1. from random import randint
  2. from numpy import array
  3. from numpy import argmax
  4. from numpy import array_equal
  5. from keras.models import Sequential
  6. from keras.layers import LSTM
  7. from keras.layers import Dense
  8. from keras.layers import TimeDistributed
  9. from keras.layers import RepeatVector
  10. from attention_decoder import AttentionDecoder
  11. # generate a sequence of random integers
  12. def generate_sequence(length, n_unique):
  13. return [randint( 0, n_unique -1) for _ in range(length)]
  14. # one hot encode sequence
  15. def one_hot_encode(sequence, n_unique):
  16. encoding = list()
  17. for value in sequence:
  18. vector = [ 0 for _ in range(n_unique)]
  19. vector[value] = 1
  20. encoding.append(vector)
  21. return array(encoding)
  22. # decode a one hot encoded string
  23. def one_hot_decode(encoded_seq):
  24. return [argmax(vector) for vector in encoded_seq]
  25. # prepare data for the LSTM
  26. def get_pair(n_in, n_out, cardinality):
  27. # generate random sequence
  28. sequence_in = generate_sequence(n_in, cardinality)
  29. sequence_out = sequence_in[:n_out] + [ 0 for _ in range(n_in-n_out)]
  30. # one hot encode
  31. X = one_hot_encode(sequence_in, cardinality)
  32. y = one_hot_encode(sequence_out, cardinality)
  33. # reshape as 3D
  34. X = X.reshape(( 1, X.shape[ 0], X.shape[ 1]))
  35. y = y.reshape(( 1, y.shape[ 0], y.shape[ 1]))
  36. return X,y
  37. # define the encoder-decoder model
  38. def baseline_model(n_timesteps_in, n_features):
  39. model = Sequential()
  40. model.add(LSTM( 150, input_shape=(n_timesteps_in, n_features)))
  41. model.add(RepeatVector(n_timesteps_in))
  42. model.add(LSTM( 150, return_sequences= True))
  43. model.add(TimeDistributed(Dense(n_features, activation= 'softmax')))
  44. model.compile(loss= 'categorical_crossentropy', optimizer= 'adam', metrics=[ 'acc'])
  45. return model
  46. # define the encoder-decoder with attention model
  47. def attention_model(n_timesteps_in, n_features):
  48. model = Sequential()
  49. model.add(LSTM( 150, input_shape=(n_timesteps_in, n_features), return_sequences= True))
  50. model.add(AttentionDecoder( 150, n_features))
  51. model.compile(loss= 'categorical_crossentropy', optimizer= 'adam', metrics=[ 'acc'])
  52. return model
  53. # train and evaluate a model, return accuracy
  54. def train_evaluate_model(model, n_timesteps_in, n_timesteps_out, n_features):
  55. # train LSTM
  56. for epoch in range( 5000):
  57. # generate new random sequence
  58. X,y = get_pair(n_timesteps_in, n_timesteps_out, n_features)
  59. # fit model for one epoch on this sequence
  60. model.fit(X, y, epochs= 1, verbose= 0)
  61. # evaluate LSTM
  62. total, correct = 100, 0
  63. for _ in range(total):
  64. X,y = get_pair(n_timesteps_in, n_timesteps_out, n_features)
  65. yhat = model.predict(X, verbose= 0)
  66. if array_equal(one_hot_decode(y[ 0]), one_hot_decode(yhat[ 0])):
  67. correct += 1
  68. return float(correct)/float(total)* 100.0
  69. # configure problem
  70. n_features = 50
  71. n_timesteps_in = 5
  72. n_timesteps_out = 2
  73. n_repeats = 10
  74. # evaluate encoder-decoder model
  75. print( 'Encoder-Decoder Model')
  76. results = list()
  77. for _ in range(n_repeats):
  78. model = baseline_model(n_timesteps_in, n_features)
  79. accuracy = train_evaluate_model(model, n_timesteps_in, n_timesteps_out, n_features)
  80. results.append(accuracy)
  81. print(accuracy)
  82. print( 'Mean Accuracy: %.2f%%' % (sum(results)/float(n_repeats)))
  83. # evaluate encoder-decoder with attention model
  84. print( 'Encoder-Decoder With Attention Model')
  85. results = list()
  86. for _ in range(n_repeats):
  87. model = attention_model(n_timesteps_in, n_features)
  88. accuracy = train_evaluate_model(model, n_timesteps_in, n_timesteps_out, n_features)
  89. results.append(accuracy)
  90. print(accuracy)
  91. print( 'Mean Accuracy: %.2f%%' % (sum(results)/float(n_repeats)))

运行这个示例可以打印每个模型重复的精度,从而让您了解运行的进度。


   
   
   
   
  1. Encoder-Decoder Model
  2. 20.0
  3. 23.0
  4. 23.0
  5. 18.0
  6. 28.000000000000004
  7. 28.999999999999996
  8. 23.0
  9. 26.0
  10. 21.0
  11. 20.0
  12. Mean Accuracy: 23.10%
  13. Encoder-Decoder With Attention Model
  14. 98.0
  15. 91.0
  16. 94.0
  17. 93.0
  18. 96.0
  19. 99.0
  20. 97.0
  21. 94.0
  22. 99.0
  23. 96.0
  24. Mean Accuracy: 95.70%

我们可以看到,即使运行10次,注意力模型仍然比没有注意力的编码器-解码器模型表现出更好的性能。
对这种评估的一个很好的扩展是捕获每个模型的每个epoch的模型损失,取平均值,并比较有或无注意力机制的模型中损失随时间的变化情况。我希望这个跟踪会显示出注意力比非注意力模型更快地获得更好的技能,进一步突出了该方法的好处。

 

你可能感兴趣的:(深度学习,Keras)