train.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from tensorflow.keras.layers import *
  2. from tensorflow.keras.models import *
  3. from tensorflow.keras.optimizers import *
  4. from tensorflow.keras.losses import *
  5. from BiddingKG.dl.common.Utils import *
  6. import numpy as np
  7. from random import random
  8. import json
  9. def getData(list_data):
  10. # list_data = load("./data/2021-06-25-mergeTrain.pk")
  11. train_x = []
  12. train_y = []
  13. test_x = []
  14. test_y = []
  15. test_index = []
  16. _index = -1
  17. for _data in list_data:
  18. _index += 1
  19. matrix = json.loads(_data["json_matrix"])
  20. # new_matrix = []
  21. # for i in range(len(matrix)):
  22. # if i <56:
  23. # if matrix[i] == -1:
  24. # matrix[i] = 0
  25. # if i%2==1:
  26. # matrix[i] /= 10
  27. # new_matrix.append(matrix[i])
  28. # elif i<63:
  29. # matrix[i] /= 10
  30. # new_matrix.append(matrix[i])
  31. # else:
  32. # new_matrix.append(matrix[i])
  33. matrix = np.array(matrix)
  34. _data["json_matrix"] = matrix
  35. label = [1,0] if _data["prob"] is None else [0,1]
  36. if random()>0.2:
  37. train_x.append(matrix)
  38. train_y.append(label)
  39. else:
  40. test_index.append(_index)
  41. test_x.append(matrix)
  42. test_y.append(label)
  43. return np.array(train_x),np.array(train_y),np.array(test_x),np.array(test_y),list_data,test_index
  44. def getModel():
  45. input = Input(shape=(46,))
  46. # def _f():
  47. # v1 = tf.get_variable("dense_kernel",shape=(46,2),dtype=tf.float32)
  48. # b1 = tf.get_variable("bias_kernel",shape=(2,),dtype=tf.float32)
  49. # Lambda()
  50. b = Dense(2,activation="tanh")(input)
  51. out = Softmax()(b)
  52. model = Model(inputs=input,outputs=out)
  53. optimizer = Adadelta()
  54. _loss = categorical_crossentropy
  55. model.compile(optimizer,_loss,metrics=[precision,recall])
  56. model.summary()
  57. return model
  58. def train():
  59. model = getModel()
  60. for i in range(20):
  61. file1 = "2021-07-15-mergeTrain_isnotnull_part%d.pk"%i
  62. file2 = "2021-07-15-mergeTrain_isnull_part%d.pk"%i
  63. data1 = load(os.path.join("F:\\Workspace2016\\DataMining\\data",file1))
  64. data2 = load(os.path.join("F:\\Workspace2016\\DataMining\\data",file2))
  65. data1.extend(data2)
  66. train_x,train_y,test_x,test_y,list_data,test_index = getData(data1)
  67. model.fit(x=train_x,y=train_y,batch_size=300,epochs=30,validation_data=(test_x,test_y))
  68. predict = model.predict(test_x)
  69. _count = 0
  70. for _p,_l,_index in zip(predict,test_y,test_index):
  71. if np.argmax(_p)!=np.argmax(_l):
  72. _count += 1
  73. print("===================")
  74. print(list_data[_index])
  75. print(_p)
  76. print(_l)
  77. print('diff count:%d'%_count)
  78. model.save("model/merge.h5")
  79. class MergePredictor():
  80. def __init__(self):
  81. self.input_size = 46
  82. self.output_size = 2
  83. self.matrix = np.array([[-5.817399024963379, 3.367797374725342], [-18.3098201751709, 17.649206161499023], [-7.115952014923096, 9.236002922058105], [-5.054129123687744, 1.8316771984100342], [6.391637325286865, -7.57396125793457], [-2.8721542358398438, 6.826520919799805], [-5.426159858703613, 10.235260009765625], [-4.240962982177734, -0.32092899084091187], [-0.6378090381622314, 0.4834124445915222], [-1.7574478387832642, -0.17846578359603882], [4.325063228607178, -2.345501661300659], [0.6086963415145874, 0.8325914740562439], [2.5674285888671875, 1.8432368040084839], [-11.195490837097168, 17.4630184173584], [-11.334247589111328, 10.294097900390625], [2.639320135116577, -8.072785377502441], [-2.2689898014068604, -3.6194612979888916], [-11.129570960998535, 18.907018661499023], [4.526485919952393, 4.57423210144043], [-3.170452356338501, -1.3847776651382446], [-0.03280467540025711, -3.0471489429473877], [-6.601675510406494, -10.05613899230957], [-2.9116673469543457, 4.819308280944824], [1.4398306608200073, -0.6549674272537231], [7.091512203216553, -0.142232745885849], [-0.14478975534439087, 0.06628061085939407], [-6.775437831878662, 9.279582023620605], [-0.006781991105526686, 1.6472798585891724], [3.83730149269104, 1.4072834253311157], [1.2229349613189697, -2.1653425693511963], [1.445560336112976, -0.8397432565689087], [-11.325132369995117, 11.231744766235352], [2.3229124546051025, -4.623719215393066], [0.38562265038490295, -1.2645516395568848], [-1.3670002222061157, 2.4323790073394775], [-3.6994268894195557, 0.7515658736228943], [-0.11617227643728256, -0.820703387260437], [4.089913368225098, -4.693605422973633], [-0.4959050714969635, 1.5272167921066284], [-2.7135870456695557, -0.5120691657066345], [0.573157548904419, -1.9375460147857666], [-4.262857437133789, 0.6375582814216614], [-1.8825865983963013, 2.427532911300659], [-4.565115451812744, 4.0269083976745605], [-4.339804649353027, 6.754288196563721], [-4.31907320022583, 0.28193211555480957]])
  84. self.bias = np.array([16.79706382751465, -13.713337898254395])
  85. # self.model = load_model("model/merge.h5",custom_objects={"precision":precision,"recall":recall,"f1_score":f1_score})
  86. def activation(self,vec,_type):
  87. if _type=="relu":
  88. _vec = np.array(vec)
  89. return _vec*(_vec>0)
  90. if _type=="tanh":
  91. return np.tanh(vec)
  92. if _type=="softmax":
  93. _vec = np.array(vec)
  94. _exp = np.exp(_vec)
  95. return _exp/np.sum(_exp)
  96. def predict(self,input):
  97. _out = self.activation(self.activation(np.matmul(np.array(input).reshape(-1,self.input_size),self.matrix)+self.bias,"tanh"),"softmax")
  98. # print(self.model.predict(np.array(input).reshape(-1,46)))
  99. return _out
  100. import tensorflow as tf
  101. def getVariable():
  102. graph=tf.Graph()
  103. sess = tf.Session(graph=graph)
  104. with graph.as_default():
  105. with sess.as_default():
  106. model = getModel()
  107. model.load_weights("model/merge.h5")
  108. # model = load_model("model/merge.h5",custom_objects={"precision":precision,"recall":recall,"f1_score":f1_score})
  109. model.summary()
  110. # a = Model()
  111. print(model.get_weights())
  112. for _w in model.get_weights():
  113. print(np.array(_w).tolist())
  114. if __name__=="__main__":
  115. # train()
  116. # getVariable()
  117. mp = MergePredictor()
  118. mp.predict([0. , 0. , 0. , 0. , 0. ,
  119. 0. , 0. , 0. , 0. , 0. ,
  120. 0. , 0. , 1. , 0. , 0. ,
  121. 0. , 0. , 0. , 0. , 0. ,
  122. 0. , 0. , 0. , 0. , 0. ,
  123. 0. , 0. , 0. , 0. , 0. ,
  124. 0. , 0. , 0. , 0. , 0. ,
  125. 0. , 0. , 0. , 0.6 , 1. ,
  126. 0.27272727, 1. , 0.6 , 0.6 , 0.2 ,
  127. 1. ])