Trading Strategy with Cross Validation - Runtime error

I was trying our the code provided on this particular notebook and man you guys skip a LOT of informations. For example the importing a local python script which is designed to make a new Neural Network was skipped, I had to search around a bit to understand why 

from Keras_CV import create_new_model

was causing an error.

Immediately after fixing that issue, I am facing this:

grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=1,verbose=2)

grid_result = grid.fit(X_train, y_train)

RETURNS:
RuntimeError: Cannot clone object <keras.wrappers.scikit_learn.KerasClassifier object at 0x0000014EBE7032E8>, as the constructor either does not set or modifies parameter class_weight

Apparently this error is because my code is trying to clone something.
How do I fix this?
Pretty disappointing that I have to come here and ask for help even for the code which is provided to us as an example.
Now I have to wait for hours for someone to actually reply to this so that I can move ahead with my learning/experimenting because you guys won't provide an actual working strategy.
What a waste of time and money. Thank you Quantra.
 

 

Hi Abinash,



Please replace the following line of code:



model = KerasClassifier(build_fn=create_new_model, epochs=20, batch_size=128, verbose=2, 

                       validation_split=0.2,class_weight=class_weight)



by



model = KerasClassifier(build_fn=create_new_model, epochs=20, batch_size=128, verbose=2, 

                       validation_split=0.2)



by removing class_weight=class_weight from the call. And the code shall work.



Please let us know if you need more help.



Thanks,

Ashish Garg

The above fix worked but that defeats the purpose of balancing the input data based on their weights. Doesn’t that ruin the purpose of pre-processing ?

Plus in the same example of Trading strategy with cross-validation, why the final output from the best_model.predict() is a decimal number instead of 0 or 1 , which it was trained to predict. y_train consists of 0s and 1s

Hi Abhinash, The final output is a probability of the data belonging to a particular class. This is explained in the strategy notebook used in the Section 2 Unit 11 of the course. We are currently working on the runtime error issue that you have posted earlier and will get back to you as soon as we find a solution to this problem.

Hi Abhinash, There is solution to the class_weights problem. You can pass them as parameter when you fit the data to the grid search object. Please check the following code for clarity:

from sklearn.pipeline import Pipeline neurons_params = [ 225,150,175] act_1_params=[‘tanh’,‘sigmoid’,‘relu’] dropout_ratio_params=[0.18,0.30,0.23] pipeline = Pipeline(steps=[(‘clf’, model )]) param_grid = dict(clf__neurons=neurons_params,clf__act_1=act_1_params,clf__dropout_ratio=dropout_ratio_params) grid = GridSearchCV(estimator=pipeline, param_grid=param_grid, n_jobs=1,verbose=2) grid_result = grid.fit(X_train, y_train,clf__class_weight=class_weight)

The class_weights feature is facing some issues in the latest version of Keras wrapper and is a known issue. You can track it on the github: Using callbacks with the SciKit-Learn API? · Issue #4081 · keras-team/keras · GitHub . Meanwhile, we will update the current solution file and downloadable code in the course so that you can try the solution at your end and confirm it.

Hi Abinash, Notebook and downloadables section have been updated. Please take a look.