daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from keras.wrappers.scikit_learn import KerasClassifier
  2.  
  3. # create model function to use with KerasClassifier
  4. def create_model(optimizer='adam', neurons=64, dropout_rate=0.25):
  5.     activation='relu'
  6.     #build layers
  7.     model = Sequential()
  8.     model.add(BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001, center=True, scale=True,
  9.                                  beta_initializer='zeros', gamma_initializer='ones', moving_mean_initializer='zeros'))
  10.     model.add(Dense(neurons, activation='relu'))
  11.     model.add(Dropout(dropout_rate))
  12.     model.add(Dense(neurons, activation='relu'))
  13.     model.add(Dropout(dropout_rate))
  14.     model.add(Dense(neurons, activation='relu'))
  15.     model.add(Dropout(dropout_rate))
  16.     model.add(Dense(2, activation='softmax'))
  17.     #compile model
  18.     model.compile(loss='categorical_crossentropy',
  19.                   optimizer=optimizer,
  20.                   metrics=['accuracy'])
  21.     return model
  22.  
  23. model = KerasClassifier(build_fn=create_model, batch_size=128, epochs=2)
  24.  
  25. from sklearn.model_selection import RandomizedSearchCV
  26.  
  27. # create grid of hyperparameters
  28. params = {'neurons':[256, 512],
  29.           'dropout_rate':[0.25, 0.5, 0.75],
  30.           'optimizer':['adam', 'sgd']}
  31.  
  32. grid = RandomizedSearchCV(estimator=model, param_distributions=params,
  33.                           verbose=2, n_jobs=-1)
  34. grid.fit(X_train, y_train)
  35. grid.best_params_
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top