How to convert raw code into function(s) example

老子叫甜甜 提交于 2020-08-10 03:37:12

问题


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?


回答1:


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.




回答2:


Well, there is no one way to go with that but I'll try to state some base lines for ordering your code.

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.

With that said you need to follow some "rules":

  • Try to divide the code into related chunks, for example: building the NN while allowing the function to receive params.

  • Make sure that functions are single responsibility (check out the single-responsibility for OOP altough I find it very useful when creating functions as well https://en.wikipedia.org/wiki/Single-responsibility_principle)

Now, I'd like to also mention that from your code I can see that your tackling a data problem e.g machine-learning problem.

I find this type of problems abit different from the traditional software-engineering problems since many times you do things one time (could be even hard coded like manipulating some specific data-frame column and filling the nans with some arbitrary value) So it's kinda tough and maybe even unnecessery as a beginner to devide into functions but tackle it from another perspective which i'll explain now.

So even before thinking about functions try to use some sort of jupyter-notebook and split the codes into chunks there, that'll provide you some essence as to how to divide code and wont be to hard like I mentioned above.



来源:https://stackoverflow.com/questions/63119430/how-to-convert-raw-code-into-functions-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!