neural network hyperparameter tuning

# random search cross validation in neural network model

from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.wrappers.scikit_learn import KerasClassifier

def report(results, n_top=3):
    for i in range(1, n_top + 1):
        candidates = numpy.flatnonzero(results['rank_test_score'] == i)
        for candidate in candidates:
            print("Model with rank: {0}".format(i))
            print("Mean validation score: {0:.3f} (std: {1:.3f})"
                  .format(results['mean_test_score'][candidate],
                          results['std_test_score'][candidate]))
            print("Parameters: {0}".format(results['params'][candidate]))
            print("")

def nn_model(activation = 'relu', neurons = 32, optimizer = 'Adam',dropout = 0.1, init_mode = 'uniform'):
    model = Sequential()
    model.add(Dense(32, input_dim = 32, kernel_initializer = init_mode, activation= activation))
    model.add(Dense((neurons*2)//3, kernel_initializer = init_mode,activation= activation))
    model.add(Dense((neurons*4)//9,kernel_initializer = init_mode,  activation = activation))
    model.add(Dropout(dropout))
    model.add(Dense(1, kernel_initializer = init_mode, activation='sigmoid'))
    model.compile(loss='binary_crossentropy', optimizer= optimizer, metrics=['accuracy'])
    return model

# Defining grid parameters
activation = ['softmax', 'softplus', 'softsign', 'relu', 'selu', 'elu', 'tanh','sigmoid', 'linear']
neurons = range(31,39)
dropout = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]
init_mode = ['uniform', 'lecun_uniform', 'normal', 'zero', 'glorot_normal', 'glorot_uniform', 'he_normal', 'he_uniform']
optimizer = ['SGD', 'Adam', 'Adamax','RMSprop','Adagrad','Adadelta','Nadam','Ftrl']
batch_size = range(10,101,10)
param_grid = dict(activation = activation, neurons = neurons, optimizer = optimizer, dropout = dropout, init_mode = init_mode, batch_size = batch_size)

clf = KerasClassifier(build_fn= nn_model, epochs= 10, verbose= 1)

model = RandomizedSearchCV(estimator= clf, param_distributions = param_grid, n_jobs=-1,verbose = 3)
model.fit(stan_x_train,stan_y_train)

report(model.cv_results_)

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
scikit learn neural network hyperparameter tuning hyperparameter tuning machine learning hyperparameter optimization neural network in keras hyperparameter tuning neural network keras supervised machine learning with hyperparameter tuning hyperparameters for neural network what are hyperparameter tuning in machine learning hyperparameters tuning in machine learning neural network hyperparameter optimization python how important is hyperparameter tuning in machine learning what are hyperparameters in neural networks hyperparameter in a neural network hyperparameters in neural networks The hyperparameters in a neural network are ml model hyperparameters tuning hyperparameter neural network hyperparameters in neural network how to choose hyperparameters for neural network hyperparameters of neural network Fine tuning hyperparameter of the ML model hyperparameter tuning for machine learning hyperparameter tuning for deep learning neural network hyperparameters what algorithms used for hyperparameters tuning neural network Hyperparameter tuning with mlr what are hyperparameters of a neural network what are hyperparameters in neural network hyperparameter tuning neual network machine learning mastery hyperparameter tuning deep learning neural networks hyperparameters hyperparameter tuning in deep learning hyperparameter of a neural network cnn hyperparameter tuning kaggle hyperparameter tuning in cnn example ml hyperparameters tuning package hyperparameter tuning artificial neural network classification code machine learning hyperparameter tuning hyperparameter tuning in machine learning hyperparameter tuning using random search in neural network hyperparameter tuning function for neural network python neural network hyperparameter tuning
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source