-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
311 additions
and
221 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import nn | ||
import nn_with_modes | ||
import nn_combined | ||
import functional_nn |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from keras.models import Sequential | ||
from keras.layers.core import Activation, Dense, Dropout | ||
from keras.layers import Masking, GRU, Merge, Input, merge, Lambda | ||
from keras.callbacks import EarlyStopping, ModelCheckpoint | ||
import numpy as np | ||
import matplotlib | ||
import matplotlib.pyplot as plt | ||
import datetime | ||
import time | ||
import logging | ||
import os | ||
|
||
def NN_train(data, model_name, mode): | ||
''' | ||
Args: | ||
data: dictionary containing relevant data | ||
Returns: | ||
recurrent neural network: A combined recurrent neural network trained on the different classes of the data | ||
''' | ||
|
||
#defines training sets of different classes | ||
X_jets_train = data['X_jet_train'] | ||
X_photons_train = data['X_photon_train'] | ||
X_event_train = data['X_event_train'] | ||
y_train = data['y_train'] | ||
X_muons_train=data['X_muon_train'] | ||
X_electrons_train=data['X_electron_train'] | ||
|
||
#set up sequential neural networks for the jet and photon classes | ||
jet_channel = Sequential() | ||
photon_channel = Sequential() | ||
event_level = Sequential() | ||
muon_channel=Sequential() | ||
electron_channel=Sequential() | ||
|
||
#declaring the shape of the first row of each class matrix | ||
JET_SHAPE = X_jets_train.shape[1:] | ||
PHOTON_SHAPE = X_photons_train.shape[1:] | ||
EVENT_SHAPE = X_event_train.shape[1] | ||
MUON_SHAPE = X_muons_train.shape[1:] | ||
ELECTRON_SHAPE = X_electrons_train.shape[1:] | ||
|
||
#adding layers to the jet and photon class neural networks | ||
jet_channel.add(Masking(mask_value=-999, input_shape=JET_SHAPE, name='jet_masking')) | ||
jet_channel.add(GRU(25, name='jet_gru')) | ||
jet_channel.add(Dropout(0.3, name='jet_dropout')) | ||
|
||
photon_channel.add(Masking(mask_value=-999, input_shape=PHOTON_SHAPE, name='photon_masking')) | ||
photon_channel.add(GRU(10, name='photon_gru')) | ||
photon_channel.add(Dropout(0.3, name='photon_dropout')) | ||
|
||
event_level.add(Lambda(lambda x: x, input_shape=(EVENT_SHAPE, ))) | ||
|
||
muon_channel.add(Masking(mask_value=-999, input_shape=MUON_SHAPE, name='muon_masking')) | ||
muon_channel.add(GRU(10, name='muon_gru')) | ||
muon_channel.add(Dropout(0.3, name='muon_dropout')) | ||
|
||
electron_channel.add(Masking(mask_value=-999, input_shape=ELECTRON_SHAPE, name='electron_masking')) | ||
electron_channel.add(GRU(10, name='electron_gru')) | ||
electron_channel.add(Dropout(0.3, name='electron_dropout')) | ||
|
||
|
||
#combining the jet and photon classes to make a combined recurrent neural network | ||
combined_rnn = Sequential() | ||
combined_rnn.add(Merge([jet_channel, photon_channel, event_level, muon_channel, electron_channel], mode='concat')) | ||
combined_rnn.add(Dense(36, activation='relu')) | ||
combined_rnn.add(Dropout(0.3)) | ||
combined_rnn.add(Dense(24, activation='relu')) | ||
combined_rnn.add(Dropout(0.3)) | ||
combined_rnn.add(Dense(12, activation='relu')) | ||
combined_rnn.add(Dropout(0.3)) | ||
if mode == 'classification': | ||
combined_rnn.add(Dense(6, activation='softmax')) | ||
combined_rnn.compile('adam', 'sparse_categorical_crossentropy') | ||
|
||
elif mode == 'regression': | ||
combined_rnn.add(Dense(1)) | ||
combined_rnn.compile('adam', 'mae') | ||
|
||
try: | ||
weights_path = os.path.join('weights', 'combinedrnn-progress'+model_name+mode+'.h5') | ||
combined_rnn.load_weights(weights_path) | ||
print "Loaded Pre-trained Weights" | ||
except IOError: | ||
print 'Pre-trained weights not found' | ||
|
||
logger = logging.getLogger('Train') | ||
logger.info('Compiling the net') | ||
try: | ||
combined_rnn.fit([X_jets_train, X_photons_train, X_event_train, X_muons_train, X_electrons_train], | ||
y_train, batch_size=16, class_weight={ | ||
k : (float(len(y_train)) / float(len(np.unique(y_train)) * (len(y_train[y_train == k])))) for k in np.unique(y_train) | ||
}, | ||
callbacks = [ | ||
EarlyStopping(verbose=True, patience=20, monitor='val_loss'), | ||
ModelCheckpoint(weights_path, | ||
monitor='val_loss', verbose=True, save_best_only=True) | ||
], | ||
nb_epoch=1, validation_split = 0.2) | ||
|
||
except KeyboardInterrupt: | ||
print 'Training ended early.' | ||
|
||
#saving the combined recurrent neural network | ||
combined_rnn.load_weights(weights_path) | ||
combined_rnn_json=combined_rnn.to_json() | ||
open('TestModel'+model_name+'.json','w').write(combined_rnn_json) | ||
|
||
return combined_rnn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.