rnn_train.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/python
  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 SimpleRNN
  10. from keras.layers import Dropout
  11. from keras import losses
  12. import h5py
  13. from keras import backend as K
  14. import numpy as np
  15. def binary_crossentrop2(y_true, y_pred):
  16. return K.mean(2*K.abs(y_true-0.5) * K.binary_crossentropy(y_pred, y_true), axis=-1)
  17. print('Build model...')
  18. #model = Sequential()
  19. #model.add(Dense(16, activation='tanh', input_shape=(None, 25)))
  20. #model.add(GRU(12, dropout=0.0, recurrent_dropout=0.0, activation='tanh', recurrent_activation='sigmoid', return_sequences=True))
  21. #model.add(Dense(2, activation='sigmoid'))
  22. main_input = Input(shape=(None, 25), name='main_input')
  23. x = Dense(16, activation='tanh')(main_input)
  24. x = GRU(12, dropout=0.1, recurrent_dropout=0.1, activation='tanh', recurrent_activation='sigmoid', return_sequences=True)(x)
  25. x = Dense(2, activation='sigmoid')(x)
  26. model = Model(inputs=main_input, outputs=x)
  27. batch_size = 64
  28. print('Loading data...')
  29. with h5py.File('features.h5', 'r') as hf:
  30. all_data = hf['features'][:]
  31. print('done.')
  32. window_size = 1500
  33. nb_sequences = len(all_data)/window_size
  34. print(nb_sequences, ' sequences')
  35. x_train = all_data[:nb_sequences*window_size, :-2]
  36. x_train = np.reshape(x_train, (nb_sequences, window_size, 25))
  37. y_train = np.copy(all_data[:nb_sequences*window_size, -2:])
  38. y_train = np.reshape(y_train, (nb_sequences, window_size, 2))
  39. all_data = 0;
  40. x_train = x_train.astype('float32')
  41. y_train = y_train.astype('float32')
  42. print(len(x_train), 'train sequences. x shape =', x_train.shape, 'y shape = ', y_train.shape)
  43. # try using different optimizers and different optimizer configs
  44. model.compile(loss=binary_crossentrop2,
  45. optimizer='adam',
  46. metrics=['binary_accuracy'])
  47. print('Train...')
  48. model.fit(x_train, y_train,
  49. batch_size=batch_size,
  50. epochs=200,
  51. validation_data=(x_train, y_train))
  52. model.save("newweights.hdf5")