Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

80 Zeilen
3.0 KiB

vor 4 Jahren
  1. import tensorflow as tf
  2. try:
  3. import tensorflow.python.keras as keras
  4. from tensorflow.python.keras import layers
  5. from tensorflow.python.keras import backend as K
  6. except:
  7. import tensorflow.keras as keras
  8. from tensorflow.keras import layers
  9. from tensorflow.keras import backend as K
  10. class Attention_layer(layers.Layer):
  11. def __init__(self, **kwargs):
  12. super(Attention_layer, self).__init__(**kwargs)
  13. def build(self, inputs_shape):
  14. assert isinstance(inputs_shape, list)
  15. # inputs: --> size
  16. # [(None,max_contents,code_vector_size),(None,max_contents)]
  17. # the second input is optional
  18. if (len(inputs_shape) < 1 or len(inputs_shape) > 2):
  19. raise ValueError("AttentionLayer expect one or two inputs.")
  20. # (None,max_contents,code_vector_size)
  21. input_shape = inputs_shape[0]
  22. if (len(input_shape) != 3):
  23. raise ValueError("Input shape for AttentionLayer shoud be of 3 dimensions.")
  24. self.input_length = int(input_shape[1])
  25. self.input_dim = int(input_shape[2])
  26. attention_param_shape = (self.input_dim, 1)
  27. self.attention_param = self.add_weight(
  28. name='attention_param',
  29. shape=attention_param_shape,
  30. initializer='uniform',
  31. trainable=True,
  32. dtype=tf.float32
  33. )
  34. super(Attention_layer, self).build(input_shape)
  35. def call(self, inputs, **kwargs):
  36. assert isinstance(inputs, list)
  37. # inputs:
  38. # [(None,max_contents,code_vector_size),(None,max_contents)]
  39. # the second input is optional
  40. if (len(inputs) < 1 or len(inputs) > 2):
  41. raise ValueError("AttentionLayer expect one or two inputs.")
  42. actual_input = inputs[0]
  43. mask = inputs[1] if (len(inputs) > 1) else None
  44. if mask is not None and not (((len(mask.shape) == 3 and mask.shape[2] == 1) or (len(mask.shape) == 2)) and (
  45. mask.shape[1] == self.input_length)):
  46. raise ValueError(
  47. "`mask` shoud be of shape (batch, input_length) or (batch, input_length, 1) when calling AttentionLayer.")
  48. assert actual_input.shape[-1] == self.attention_param.shape[0]
  49. # (batch, input_length, input_dim) * (input_dim, 1) ==> (batch, input_length, 1)
  50. attention_weights = K.dot(actual_input, self.attention_param)
  51. if mask is not None:
  52. if (len(mask.shape) == 2):
  53. mask = K.expand_dims(mask, axis=2) # (batch, input_dim, 1)
  54. mask = K.log(mask) # e.g. K.exp(K.log(0.)) = 0 K.exp(K.log(1.)) =1
  55. attention_weights += mask
  56. attention_weights = K.softmax(attention_weights, axis=1)
  57. result = K.sum(actual_input * attention_weights, axis=1)
  58. return result, attention_weights
  59. def compute_output_shape(self, input_shape):
  60. return input_shape[0], input_shape[2] # (batch,input_length,input_dim) --> (batch,input_dim)