Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. from keras.layers import *
  2. from keras.activations import softmax
  3. from keras.models import Model
  4.  
  5. """
  6.  
  7. References
  8. ----------
  9. [1]. Parikh, Ankur P., et al. "A decomposable attention model for natural language inference." arXiv preprint arXiv:1606.01933 (2016).
  10.  
  11. """
  12.  
  13. def StaticEmbedding(embedding_matrix):
  14. in_dim, out_dim = embedding_matrix.shape
  15. embedding = Embedding(in_dim, out_dim, weights=[embedding_matrix], trainable=False)
  16. return embedding
  17.  
  18. def unchanged_shape(input_shape):
  19. return input_shape
  20.  
  21. def time_distributed(x, layers):
  22. for l in layers:
  23. x = TimeDistributed(l)(x)
  24. return x
  25.  
  26. def align(input_1, input_2):
  27. attention = Dot(axes=-1)([input_1, input_2])
  28. w_att_1 = Lambda(lambda x: softmax(x, axis=1),
  29. output_shape=unchanged_shape)(attention)
  30. w_att_2 = Permute((2,1))(Lambda(lambda x: softmax(x, axis=2),
  31. output_shape=unchanged_shape)(attention))
  32. in1_aligned = Dot(axes=1)([w_att_1, input_1])
  33. in2_aligned = Dot(axes=1)([w_att_2, input_2])
  34. return in1_aligned, in2_aligned
  35.  
  36. def aggregate(x1, x2, num_class, dense_dim=300, dropout_rate=0.2, activation="relu"):
  37. feat1 = concatenate(map(lambda l: l(x1), [GlobalAvgPool1D(), GlobalMaxPool1D()]))
  38. feat2 = concatenate(map(lambda l: l(x2), [GlobalAvgPool1D(), GlobalMaxPool1D()]))
  39. x = Concatenate()([feat1, feat2])
  40. x = BatchNormalization()(x)
  41. x = Dense(dense_dim, activation=activation)(x)
  42. x = Dropout(dropout_rate)(x)
  43. x = BatchNormalization()(x)
  44. x = Dense(dense_dim, activation=activation)(x)
  45. x = Dropout(dropout_rate)(x)
  46. scores = Dense(num_class, activation='sigmoid')(x)
  47. return scores
  48.  
  49. def build_model(embedding_matrix, num_class=1,
  50. projection_dim=300, projection_hidden=0, projection_dropout=0.2,
  51. compare_dim=500, compare_dropout=0.2,
  52. dense_dim=300, dropout_rate=0.2,
  53. lr=1e-3, activation='relu', maxlen=30):
  54. q1 = Input(name='q1',shape=(maxlen,))
  55. q2 = Input(name='q2',shape=(maxlen,))
  56.  
  57. # Embedding
  58. encode = StaticEmbedding(embedding_matrix)
  59. q1_embed = encode(q1)
  60. q2_embed = encode(q2)
  61.  
  62. # Projection
  63. projection_layers = []
  64. if projection_hidden > 0:
  65. projection_layers.extend([
  66. Dense(projection_hidden, activation=activation),
  67. Dropout(rate=projection_dropout),
  68. ])
  69. projection_layers.extend([
  70. Dense(projection_dim, activation=None),
  71. Dropout(rate=projection_dropout),
  72. ])
  73. q1_encoded = time_distributed(q1_embed, projection_layers)
  74. q2_encoded = time_distributed(q2_embed, projection_layers)
  75.  
  76. # Attention
  77. q1_aligned, q2_aligned = align(q1_encoded, q2_encoded)
  78.  
  79. # Compare
  80. q1_combined = concatenate([q1_encoded, q2_aligned])
  81. q2_combined = concatenate([q2_encoded, q1_aligned])
  82. compare_layers = [
  83. Dense(compare_dim, activation=activation),
  84. Dropout(compare_dropout),
  85. Dense(compare_dim, activation=activation),
  86. Dropout(compare_dropout),
  87. ]
  88. q1_compare = time_distributed(q1_combined, compare_layers)
  89. q2_compare = time_distributed(q2_combined, compare_layers)
  90.  
  91. # Aggregate
  92. scores = aggregate(q1_compare, q2_compare, num_class)
  93.  
  94. model = Model(inputs=[q1, q2], outputs=scores)
  95. return model
  96.  
  97. if __name__ == "__main__":
  98. import numpy as np
  99. model = build_model(embedding_matrix=np.zeros((30, 20)), projection_hidden=200)
  100. print model.summary()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement