rnn_train.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/python3
  2. from __future__ import print_function
  3. from keras.models import Sequential
  4. from keras.models import Model
  5. from keras.layers import Input
  6. from keras.layers import Dense
  7. from keras.layers import LSTM
  8. from keras.layers import GRU
  9. from keras.layers import CuDNNGRU
  10. from keras.layers import SimpleRNN
  11. from keras.layers import Dropout
  12. from keras import losses
  13. import h5py
  14. from keras.optimizers import Adam
  15. from keras.constraints import Constraint
  16. from keras import backend as K
  17. import numpy as np
  18. import tensorflow as tf
  19. from keras.backend.tensorflow_backend import set_session
  20. config = tf.ConfigProto()
  21. config.gpu_options.per_process_gpu_memory_fraction = 0.44
  22. set_session(tf.Session(config=config))
  23. def binary_crossentrop2(y_true, y_pred):
  24. return K.mean(2*K.abs(y_true-0.5) * K.binary_crossentropy(y_true, y_pred), axis=-1)
  25. def binary_accuracy2(y_true, y_pred):
  26. return K.mean(K.cast(K.equal(y_true, K.round(y_pred)), 'float32') + K.cast(K.equal(y_true, 0.5), 'float32'), axis=-1)
  27. def quant_model(model):
  28. weights = model.get_weights()
  29. for k in range(len(weights)):
  30. weights[k] = np.maximum(-128, np.minimum(127, np.round(128*weights[k])*0.0078125))
  31. model.set_weights(weights)
  32. class WeightClip(Constraint):
  33. '''Clips the weights incident to each hidden unit to be inside a range
  34. '''
  35. def __init__(self, c=2):
  36. self.c = c
  37. def __call__(self, p):
  38. return K.clip(p, -self.c, self.c)
  39. def get_config(self):
  40. return {'name': self.__class__.__name__,
  41. 'c': self.c}
  42. reg = 0.000001
  43. constraint = WeightClip(.998)
  44. print('Build model...')
  45. main_input = Input(shape=(None, 25), name='main_input')
  46. x = Dense(32, activation='tanh', kernel_constraint=constraint, bias_constraint=constraint)(main_input)
  47. #x = CuDNNGRU(24, return_sequences=True, kernel_constraint=constraint, recurrent_constraint=constraint, bias_constraint=constraint)(x)
  48. x = GRU(24, recurrent_activation='sigmoid', activation='tanh', return_sequences=True, kernel_constraint=constraint, recurrent_constraint=constraint, bias_constraint=constraint)(x)
  49. x = Dense(2, activation='sigmoid', kernel_constraint=constraint, bias_constraint=constraint)(x)
  50. model = Model(inputs=main_input, outputs=x)
  51. batch_size = 2048
  52. print('Loading data...')
  53. with h5py.File('features10b.h5', 'r') as hf:
  54. all_data = hf['data'][:]
  55. print('done.')
  56. window_size = 1500
  57. nb_sequences = len(all_data)//window_size
  58. print(nb_sequences, ' sequences')
  59. x_train = all_data[:nb_sequences*window_size, :-2]
  60. x_train = np.reshape(x_train, (nb_sequences, window_size, 25))
  61. y_train = np.copy(all_data[:nb_sequences*window_size, -2:])
  62. y_train = np.reshape(y_train, (nb_sequences, window_size, 2))
  63. print("Marking ignores")
  64. for s in y_train:
  65. for e in s:
  66. if (e[1] >= 1):
  67. break
  68. e[0] = 0.5
  69. all_data = 0;
  70. x_train = x_train.astype('float32')
  71. y_train = y_train.astype('float32')
  72. print(len(x_train), 'train sequences. x shape =', x_train.shape, 'y shape = ', y_train.shape)
  73. model.load_weights('newweights10a1b_ep206.hdf5')
  74. #weights = model.get_weights()
  75. #for k in range(len(weights)):
  76. # weights[k] = np.round(128*weights[k])*0.0078125
  77. #model.set_weights(weights)
  78. # try using different optimizers and different optimizer configs
  79. model.compile(loss=binary_crossentrop2,
  80. optimizer=Adam(0.0001),
  81. metrics=[binary_accuracy2])
  82. print('Train...')
  83. quant_model(model)
  84. model.fit(x_train, y_train,
  85. batch_size=batch_size,
  86. epochs=10, validation_data=(x_train, y_train))
  87. model.save("newweights10a1c_ep10.hdf5")
  88. quant_model(model)
  89. model.fit(x_train, y_train,
  90. batch_size=batch_size,
  91. epochs=50, initial_epoch=10)
  92. model.save("newweights10a1c_ep50.hdf5")
  93. model.compile(loss=binary_crossentrop2,
  94. optimizer=Adam(0.0001),
  95. metrics=[binary_accuracy2])
  96. quant_model(model)
  97. model.fit(x_train, y_train,
  98. batch_size=batch_size,
  99. epochs=100, initial_epoch=50)
  100. model.save("newweights10a1c_ep100.hdf5")
  101. quant_model(model)
  102. model.fit(x_train, y_train,
  103. batch_size=batch_size,
  104. epochs=150, initial_epoch=100)
  105. model.save("newweights10a1c_ep150.hdf5")
  106. quant_model(model)
  107. model.fit(x_train, y_train,
  108. batch_size=batch_size,
  109. epochs=200, initial_epoch=150)
  110. model.save("newweights10a1c_ep200.hdf5")
  111. quant_model(model)
  112. model.fit(x_train, y_train,
  113. batch_size=batch_size,
  114. epochs=201, initial_epoch=200)
  115. model.save("newweights10a1c_ep201.hdf5")
  116. quant_model(model)
  117. model.fit(x_train, y_train,
  118. batch_size=batch_size,
  119. epochs=202, initial_epoch=201, validation_data=(x_train, y_train))
  120. model.save("newweights10a1c_ep202.hdf5")
  121. quant_model(model)
  122. model.fit(x_train, y_train,
  123. batch_size=batch_size,
  124. epochs=203, initial_epoch=202, validation_data=(x_train, y_train))
  125. model.save("newweights10a1c_ep203.hdf5")
  126. quant_model(model)
  127. model.fit(x_train, y_train,
  128. batch_size=batch_size,
  129. epochs=204, initial_epoch=203, validation_data=(x_train, y_train))
  130. model.save("newweights10a1c_ep204.hdf5")
  131. quant_model(model)
  132. model.fit(x_train, y_train,
  133. batch_size=batch_size,
  134. epochs=205, initial_epoch=204, validation_data=(x_train, y_train))
  135. model.save("newweights10a1c_ep205.hdf5")
  136. quant_model(model)
  137. model.fit(x_train, y_train,
  138. batch_size=batch_size,
  139. epochs=206, initial_epoch=205, validation_data=(x_train, y_train))
  140. model.save("newweights10a1c_ep206.hdf5")