model.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. '''
  2. Created on 2019年2月25日
  3. @author: User
  4. '''
  5. import keras
  6. from keras import models
  7. from keras import layers
  8. from keras import optimizers,losses,metrics
  9. from keras.callbacks import ModelCheckpoint
  10. from Utils import *
  11. import keras.backend as K
  12. import tensorflow as tf
  13. class Attention(layers.Layer):
  14. def __init__(self, **kwargs):
  15. super(Attention, self).__init__(**kwargs)
  16. def build(self, input_shape):
  17. # W: (EMBED_SIZE, 1)
  18. # b: (MAX_TIMESTEPS, 1)
  19. # u: (MAX_TIMESTEPS, MAX_TIMESTEPS)
  20. print(input_shape)
  21. self.W = self.add_weight(name="W_{:s}".format(self.name),
  22. shape=(input_shape[-1], 1),
  23. initializer="uniform")
  24. self.b = self.add_weight(name="b_{:s}".format(self.name),
  25. shape=(input_shape[1], 1),
  26. initializer="uniform")
  27. super(Attention, self).build(input_shape)
  28. def call(self, x, mask=None):
  29. # input: (BATCH_SIZE, MAX_TIMESTEPS, EMBED_SIZE)
  30. # et: (BATCH_SIZE, MAX_TIMESTEPS)
  31. et = K.squeeze(K.tanh(K.dot(x, self.W) + self.b), axis=-1)
  32. # at: (BATCH_SIZE, MAX_TIMESTEPS)
  33. print("et",np.shape(et))
  34. #at = K.dot(et, self.u)
  35. #if mask is not None:
  36. #at *= K.cast(mask, K.floatx())
  37. # ot: (BATCH_SIZE, MAX_TIMESTEPS, EMBED_SIZE)
  38. et /= K.cast(K.sum(et, axis=1, keepdims=True) + K.epsilon(), K.floatx())
  39. print(np.shape(et))
  40. # atx = K.expand_dims(at, axis=-1)
  41. # atx1 = K.argmax(at,axis=-1)
  42. # et1 = K.one_hot(atx1,100)
  43. # at1 = (at * (et1 - 1)) * -1
  44. # atx2 = K.argmax(at1,axis=-1)
  45. # et2 = K.one_hot(atx2,100)
  46. # at2 = (at1 * (et2 - 1)) * -1
  47. # atx3 = K.argmax(at2,axis=-1)
  48. # et3 = K.one_hot(atx3,100)
  49. # at3 = (at2 * (et3 - 1)) * -1
  50. # atx4 = K.argmax(at3,axis=-1)
  51. # et4 = K.one_hot(atx4,100)
  52. # at4 = (at3 * (et4 - 1)) * -1
  53. # atx5 = K.argmax(at4,axis=-1)
  54. # et5 = K.one_hot(atx5,100)
  55. # at5 = (at4 * (et5 - 1)) * -1
  56. # atx6 = K.argmax(at5,axis=-1)
  57. # et6 = K.one_hot(atx6,100)
  58. # et = et1 + et2 + et3 + et4 + et5 + et6
  59. # at = at * et
  60. # for i in range(at.shape[0]):
  61. # at[i][atx1[i]] = 0
  62. # atx2 = K.argmax(at,axis=-1)
  63. # for i in range(at.shape[0]):
  64. # at[i][atx2[i]] = 0
  65. # atx3 = K.argmax(at,axis=-1)
  66. # ad = K.zeros([at.shape[0],at.shape[1]])
  67. # at = at * ad
  68. #atx = K.expand_dims(at, axis=-1)
  69. return et
  70. def compute_mask(self, input, input_mask=None):
  71. # do not pass the mask to the next layers
  72. return None
  73. def compute_output_shape(self, input_shape):
  74. # output shape: (BATCH_SIZE, EMBED_SIZE)
  75. return (input_shape[0], input_shape[1])
  76. def get_config(self):
  77. return super(Attention, self).get_config()
  78. def getBiRNNModel(input_shape=[None,36],out_len=2):
  79. '''
  80. @summary:获取模型
  81. '''
  82. input = layers.Input(shape=input_shape,dtype="float32")
  83. mask = layers.Masking(mask_value=0)(input)
  84. '''
  85. whole_lstm = layers.Bidirectional(layers.LSTM(12,return_sequences=False))(mask)
  86. repeat = layers.RepeatVector(input_shape[0])(whole_lstm)
  87. #lstm_0 = layers.Bidirectional(layers.LSTM(12,return_sequences=True))(mask)
  88. #lstm_1 = layers.Bidirectional(layers.LSTM(48,return_sequences=True))(lstm_0)
  89. matrix = layers.Dense(24,activation="relu")(mask)
  90. concat = layers.merge([repeat,matrix],mode="concat")
  91. matrix = layers.Dense(48,activation="relu")(concat)
  92. matrix = layers.Dense(24,activation="relu")(matrix)
  93. #output = layers.Dense(out_len,activation="softmax")(matrix)
  94. output = Attention()(concat)
  95. print("out",np.shape(output))
  96. #layers.RepeatVector(np.shape(matrix)[-2])(whole_lstm)
  97. '''
  98. ''''''
  99. lstm_0 = layers.Bidirectional(layers.LSTM(32,return_sequences=True))(mask)
  100. #matrix = layers.Dense(24,activation="relu")(lstm_0)
  101. lstm_1 = layers.Bidirectional(layers.LSTM(12,return_sequences=True))(lstm_0)
  102. output = layers.Dense(out_len,activation="softmax")(lstm_1)
  103. model = models.Model(inputs=[input],outputs=output)
  104. model.compile(optimizer=optimizers.Adam(lr=0.01),loss=my_loss,metrics=[acc,precision,recall,f1_score])
  105. model.summary()
  106. return model
  107. if __name__=="__main__":
  108. getBiRNNModel()