Search code examples
pythonfunctionmachine-learningdeep-learningcoding-style

How to convert raw code into function(s) example


I have just started learning how to code in Python and would appreciate if anyone could give me a brief explanation/hint on how to convert raw code into function(s).

Example machine learning code:

# create model
model = Sequential()
model.add(Dense(neurons, input_dim=8, kernel_initializer='uniform', activation='linear', kernel_constraint=maxnorm(4)))
model.add(Dropout(0.2))
model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = KerasClassifier(build_fn=model, epochs=100, batch_size=10, verbose=0)
# define the grid search parameters
neurons = [1, 5]
param_grid = dict(neurons=neurons)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=3)
grid_result = grid.fit(X, Y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
    print("%f (%f) with: %r" % (mean, stdev, param))

How should I start with this example if I want to make it in 1 or 2 functions?

EDIT:

In the code above, I have created a function for < # create model > :

def create_model(neurons=1):
    # create model
    model = Sequential()
    model.add(Dense(neurons, input_dim=8, kernel_initializer='uniform', activation='linear', kernel_constraint=maxnorm(4)))
    model.add(Dropout(0.2))
    model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))
    # Compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
return model

Then, I will have to pass create_model() into < KerasClassifier(build_fn=create_model etc...) >

Is it right if I create another function like this below:

def keras_classifier(model):
    # split into input (X) and output (Y) variables
    X = dataset[:,0:8]
    Y = dataset[:,8]
    model = KerasClassifier(build_fn=model, epochs=100, batch_size=10, verbose=0)
    # define the grid search parameters
    neurons = [1, 5]
    param_grid = dict(neurons=neurons)
    grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=3)
    grid_result = grid.fit(X, Y)
    # summarize results
    print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
    means = grid_result.cv_results_['mean_test_score']
    stds = grid_result.cv_results_['std_test_score']
    params = grid_result.cv_results_['params']
    for mean, stdev, param in zip(means, stds, params):
         print("%f (%f) with: %r" % (mean, stdev, param))

Is it correct/can be a function called in another function?

Because if I call the two functions:

create_model(neurons)
keras_classifier(model)

I get the error NameError: name 'model' is not defined

Could anyone help please?


Solution

  • There is an issue with your function def I believe:

    def create_model(neurons):
        ....
    return model
    

    needs to be

    def create_model(neurons):
        ....
        return model
    

    indentations are very important in python, they form part of the syntax. don't write ugly code thanks :)

    And yes you can pass in the model into a function that then passes it to the build_fn= named variable of the keras classifier. the thing that you put in to the classifier call must itself be a model object, so do this:

    model = KerasClassifier(build_fn=create_model(), epochs=100, batch_size=10, verbose=0)
    

    using different names for models created by your functions or passing to functions can help keep track of them.