Guest User

Untitled

a guest
Jun 17th, 2017
227
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # MLP
  2. from keras.models import Sequential
  3. from keras.layers import Dense, Dropout
  4. from keras.constraints import maxnorm
  5. from keras.utils import np_utils
  6. from keras import backend as K
  7. from keras.callbacks import TensorBoard
  8. from keras.wrappers.scikit_learn import KerasClassifier
  9. from sklearn.model_selection import StratifiedKFold, GridSearchCV
  10. import numpy as np
  11. import matplotlib.pyplot as plt
  12. import os
  13. import time
  14. import gc
  15.  
  16.  
  17. def create_model():
  18.     # create model
  19.     model = Sequential()
  20.  
  21.     # model with dropout and a kernel constraint on the hidden layers
  22.     model.add(Dense(37, input_dim=num_inputs, kernel_initializer='normal', activation='relu'))
  23.     model.add(Dropout(0.2))
  24.     model.add(Dense(20, kernel_initializer='normal', activation='relu'))
  25.     model.add(Dropout(0.2))
  26.     model.add(Dense(num_classes, kernel_initializer='normal', activation='softmax'))
  27.  
  28.     # Compile model
  29.     model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
  30.  
  31.     return model
  32.  
  33. # fix random seed for reproducibility
  34. seed = 42
  35. np.random.seed(seed)
  36.  
  37. X_d = np.loadtxt('../datasets/mixed_extracted/digits_features.csv', delimiter=',')
  38. y_d = np.loadtxt('../datasets/mixed_extracted/digits_target.csv', delimiter=',')
  39.  
  40. # define k-fold cross validation test harness
  41. num_folds = 5
  42. cv = StratifiedKFold(n_splits=num_folds, shuffle=True, random_state=42)
  43.  
  44. # one hot encode outputs
  45. # transforming the vector of class integers into a binary matrix
  46. y_d = np_utils.to_categorical(y_d)
  47.  
  48. # define network parameters
  49. num_epochs = 150
  50. num_inputs = X_d.shape[1]
  51. num_classes = y_d.shape[1]
  52.  
  53. # create model
  54. model = KerasClassifier(build_fn=create_model(), verbose=0)
  55.  
  56. # grid search epochs, batch size, optimizers and kernel_initializer
  57. optimizers = ['rmsprop', 'adam']
  58. init = ['glorot_uniform', 'normal', 'uniform']
  59. epochs = np.array([50, 100, 150])
  60. batches = np.array([5, 10, 20])
  61. param_grid = dict(optimizers=optimizers, epochs=epochs, batch_size=batches, kernel_initializer=init)
  62. grid = GridSearchCV(estimator=model, param_grid=param_grid, cv=cv, refit=True, scoring='accuracy')
  63.  
  64. # Fit the model
  65. grid.fit(X_d, y_d)
  66. print("[INFO] Training ended")
  67.  
  68. # summarize results
  69. print("[INFO]Best parameters set found on development set:")
  70. print()
  71. print(grid.best_params_)
  72. print()
  73. print("[INFO]Grid scores on development set:")
  74. print()
  75. means = grid.cv_results_['mean_test_score']
  76. stds = grid.cv_results_['std_test_score']
  77. for mean, std, params in zip(means, stds, grid.cv_results_['params']):
  78.     print("%0.3f (+/-%0.03f) for %r"
  79.           % (mean, std * 2, params))
  80.  
  81. gc.collect()
  82. K.clear_session()
RAW Paste Data