model.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. '''
  2. Created on 2019年3月26日
  3. @author: User
  4. '''
  5. import os
  6. import sys
  7. sys.path.append(os.path.abspath("../.."))
  8. from BiddingKG.dl.common.Utils import *
  9. from keras import layers
  10. from keras import models
  11. from keras import losses,optimizers
  12. import tensorflow as tf
  13. from BiddingKG.dl.common.models import transformer_model
  14. def getBiLSTMModel():
  15. input = layers.Input(shape=(100,))
  16. lstm = layers.Bidirectional(layers.LSTM(128,return_sequences=False))(input)
  17. matrix = layers.Dense(48,activation="relu")(lstm)
  18. out = layers.Dense(2,activation="softmax")(matrix)
  19. model = models.Model(inputs=input,outputs=out)
  20. model.compile(optimizer=optimizers.Adam(lr=0.01), loss=losses.categorical_crossentropy,metrics=[precision,recall,f1_score])
  21. model.summary()
  22. return model
  23. def getTextCNNModel():
  24. input = layers.Input(shape=(100,60))
  25. list_pool = []
  26. list_size = [10,15,20]
  27. for size in list_size:
  28. c = layers.Conv1D(filters=4,kernel_size=size,strides=1,activation="relu")(input)
  29. p = layers.AvgPool1D(pool_size=int(c.shape[1]))(c)
  30. list_pool.append(p)
  31. concat = layers.merge(list_pool,mode="concat")
  32. flatten = layers.Flatten()(concat)
  33. matrix = layers.Dense(12,activation="relu")(flatten)
  34. out = layers.Dense(2,activation="softmax")(matrix)
  35. model = models.Model(inputs=input,outputs=out)
  36. model.compile(optimizer=optimizers.Adadelta(),loss=losses.categorical_crossentropy,metrics=[precision,recall,f1_score])
  37. model.summary()
  38. return model
  39. def get_context_form_model(vocab_len,char_dim,lstm_dim,context_dim,res_dim,residual_stacks):
  40. input = tf.placeholder(shape=[None,9,30],dtype=tf.int32)
  41. label = tf.placeholder(shape=[None,2],dtype=tf.int32)
  42. # input_center = tf.slice(input,[0,4,0],[-1,1,-1])
  43. with tf.variable_scope("embedding"):
  44. embedding = tf.get_variable("char_embedding",shape=[vocab_len,char_dim])
  45. embedding_input = tf.nn.embedding_lookup(embedding,input)
  46. with tf.variable_scope("center"):
  47. center_embedding = tf.slice(embedding_input,[0,4,0,0],[-1,1,-1,-1])
  48. with tf.variable_scope("bert"):
  49. bert_input = tf.reshape(center_embedding,[-1,30,char_dim])
  50. bert_output = transformer_model(input_tensor=bert_input,name="bert")
  51. forward_cell = tf.contrib.rnn.BasicLSTMCell(lstm_dim, state_is_tuple=True)
  52. backward_cell = tf.contrib.rnn.BasicLSTMCell(lstm_dim,state_is_tuple=True)
  53. outputs, final_out = tf.nn.bidirectional_dynamic_rnn(forward_cell,backward_cell,bert_output, dtype=tf.float32)
  54. bi_output = tf.concat(final_out,-1) #[-1,lstm_dim*2]
  55. bi_output = tf.reshape(tf.slice(bi_output,[1,0,0],[1,-1,-1]),[-1,lstm_dim*2])
  56. center_output = tf.nn.relu(tf.matmul(bi_output,tf.get_variable("center_v",shape=[lstm_dim*2,res_dim])))
  57. center_output = tf.nn.dropout(center_output,1)
  58. with tf.variable_scope("context"):
  59. with tf.variable_scope("bi_rnn"):
  60. resize_input = tf.reshape(embedding_input,[-1,30,char_dim])
  61. forward_cell = tf.contrib.rnn.BasicLSTMCell(lstm_dim, state_is_tuple=True)
  62. backward_cell = tf.contrib.rnn.BasicLSTMCell(lstm_dim,state_is_tuple=True)
  63. outputs, final_out = tf.nn.bidirectional_dynamic_rnn(forward_cell,backward_cell,resize_input, dtype=tf.float32)
  64. bi_output = tf.concat(final_out,-1) #[-1,lstm_dim*2]
  65. bi_output = tf.slice(bi_output,[1,0,0],[1,-1,-1])
  66. context_input = tf.reshape(bi_output,[-1,lstm_dim*2])
  67. context_v = tf.get_variable("context_v",shape=[lstm_dim*2,context_dim])
  68. context_emb = tf.nn.relu(tf.matmul(context_input,context_v))
  69. context_output = tf.reshape(context_emb,[-1,9*context_dim])
  70. context_output = tf.nn.relu(tf.matmul(context_output,tf.get_variable("context_output_v",shape=[9*context_dim,res_dim])))
  71. context_output = tf.nn.dropout(context_output,1)
  72. with tf.variable_scope("residual"):
  73. input_x = tf.concat([context_output,center_output],axis=-1)
  74. for stack in range(residual_stacks):
  75. stack_w = tf.get_variable("stack_w_%d"%stack,shape=[res_dim*2,res_dim*2])
  76. stack_b = tf.get_variable("stack_b_%d"%stack,shape=[1,res_dim*2])
  77. stack_out = tf.matmul(input_x,stack_w)+stack_b
  78. input_x = tf.nn.relu(stack_out+input_x)
  79. with tf.variable_scope("softmax"):
  80. softmax_input = tf.identity(input_x)
  81. softmax_w = tf.get_variable("softmax_w",shape=[res_dim*2,2])
  82. softmax_output = tf.nn.softmax(tf.matmul(softmax_input,softmax_w))
  83. # with tf.variable_scope("softmax"):
  84. # softmax_input = tf.reshape(bi_output,[-1,lstm_dim*2])
  85. # softmax_w = tf.get_variable("softmax_w",shape=[lstm_dim*2,2])
  86. # softmax_output = tf.nn.softmax(tf.matmul(softmax_input,softmax_w))
  87. _p = precision(tf.cast(label,tf.float32),softmax_output)
  88. _r = recall(tf.cast(label,tf.float32),softmax_output)
  89. loss = -tf.reduce_mean(tf.cast(label,tf.float32)*tf.log(softmax_output))
  90. optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
  91. global_step = tf.Variable(0, trainable=False)
  92. grads_vars = optimizer.compute_gradients(loss)
  93. capped_grads_vars = [[tf.clip_by_value(g, -5, 5), v] for g, v in grads_vars]
  94. train = optimizer.apply_gradients(capped_grads_vars, global_step)
  95. return {"input":input,"output":softmax_output,"label":label,"train":train,"embedding":embedding,"loss":loss,"precision":_p,"recall":_r}
  96. if __name__=="__main__":
  97. #getBiLSTMModel()
  98. getTextCNNModel()