Guest User

Untitled

a guest
Feb 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. import os
  2.  
  3. import keras
  4. from keras import Input
  5. from keras.layers import BatchNormalization, Dense, LSTM, concatenate, Dropout, TimeDistributed
  6. from keras.optimizers import RMSprop
  7. from keras.utils import plot_model
  8.  
  9. from metrics import in_top5, top5_accuracy
  10.  
  11.  
  12. def gen_nn_model(hyperparameters, target_symbols):
  13. # - Input definitions:
  14.  
  15. batch_size = hyperparameters['batch_size']
  16. n_cols = hyperparameters['n_cols']
  17. n_history = hyperparameters['n_history']
  18. # -- Daily Input
  19. daily_input = Input(shape = (n_history, n_cols), batch_shape = (batch_size, n_history, n_cols), dtype = 'float32',
  20. name = 'daily_input')
  21.  
  22. normalized_daily_input = BatchNormalization()(daily_input)
  23.  
  24. # normalized_daily_input = TimeDistributed(Dense(n_cols, activation='linear'))(normalized_daily_input)
  25.  
  26. normalized_daily_input = TimeDistributed(Dense(n_cols, activation = 'relu'))(normalized_daily_input)
  27. normalized_daily_input = TimeDistributed(Dense(n_cols, activation = 'relu'))(normalized_daily_input)
  28. normalized_daily_input = TimeDistributed(Dense(n_cols, activation = 'relu'))(normalized_daily_input)
  29. normalized_daily_input = Dropout(0.2)(normalized_daily_input)
  30.  
  31. # Deep LSTM Layers
  32.  
  33. x = LSTM(n_cols // 7,
  34. return_state = False,
  35. return_sequences = True,
  36. stateful = True)(normalized_daily_input)
  37.  
  38. x = LSTM(n_cols // 7,
  39. stateful = False)(x)
  40.  
  41. r = Dense(n_cols - 1, activation = 'tanh')(x)
  42. r = Dense(n_cols - 1, activation = 'tanh')(r)
  43. r = Dense(len(target_symbols), activation = 'linear', name = 'r')(r)
  44.  
  45. joint_flow = concatenate([x, r])
  46.  
  47. p = Dense(n_cols - 1, activation = 'sigmoid')(joint_flow)
  48. p = Dense(n_cols - 1, activation = 'sigmoid')(p)
  49. # p = keras.layers.core.Lambda(lambda x: x *5)(p)
  50. p = Dense(len(target_symbols), activation = 'softmax', name = 'p')(p)
  51.  
  52. # l = keras.layers.core.Lambda(lambda x: negative(x))(p)
  53. l = Dense(n_cols - 1, activation = 'sigmoid')(joint_flow)
  54. l = Dense(n_cols - 1, activation = 'sigmoid')(l)
  55. # l = keras.layers.core.Lambda(lambda x: x / 2)(l)
  56. l = Dense(len(target_symbols), activation = 'softmax', name = 'l')(l)
  57.  
  58. # -- Model creation
  59. inputs = [daily_input]
  60. outputs = [r, p, l]
  61.  
  62. model = keras.models.Model(inputs = inputs, outputs = outputs)
  63. return batch_size, model
  64.  
  65.  
  66. def compile_model(model):
  67. lr = 0.1
  68. loss = { 'r': 'mean_absolute_error',
  69. 'p': 'kullback_leibler_divergence',
  70. 'l': 'kullback_leibler_divergence'
  71. }
  72.  
  73. loss_weights = { 'r': .20,
  74. 'p': .40,
  75. 'l': .40,
  76. }
  77. opt = RMSprop(lr = lr)
  78. # opt = 'adam'
  79.  
  80. metrics = { 'p': [in_top5, top5_accuracy],
  81. 'l': [in_top5, top5_accuracy] }
  82.  
  83. model.compile(optimizer = opt,
  84. loss = loss,
  85. loss_weights = loss_weights,
  86. metrics = metrics
  87. )
  88. return model
  89.  
  90.  
  91. def show_model_details(EXP_NAME, EXP_PATH, model):
  92. print(model.summary())
  93. str_image_file_name = os.path.join(EXP_PATH, '{0}.png'.format(EXP_NAME))
  94. plot_model(model, to_file = str_image_file_name,
  95. show_shapes = True,
  96. show_layer_names = True,
  97. rankdir = 'TD')
Add Comment
Please, Sign In to add comment