Manuel Almeida
Manuel Almeida

Reputation: 183

It´s possible to apply cross_val_score() form sklearn to neupy NN that has an addon like Weigth Elimination?

I´m trying to apply cross_val_score() to the following algorithm:

cgnet = algorithms.LevenbergMarquardt(
    connection=[
        layers.Input(XTrain.shape[1]),
        layers.Linear(6),
        layers.Linear(1)],
        mu_update_factor=2,
        mu=0.1,
        shuffle_data=True,
        verbose=True,
        decay_rate=0.1,
        addons=[algorithms.WeightElimination])

kfold = KFold(n_splits=5, shuffle=True, random_state=7)
scores=cross_val_score(cgnet, XTrainScaled,yTrainScaled,scoring='neg_mean_absolute_error',cv=kfold,verbose=10)
print scores
print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))

And this is the error message I get:

TypeError: Cannot create a consistent method resolution
order (MRO) for bases LevenbergMarquardtWeightElimination, WeightElimination

Without WeightElimination or any other addon, cross_val_score(), works fine...Is there another way to do this? Thank you

Upvotes: 0

Views: 157

Answers (1)

itdxer
itdxer

Reputation: 1256

It looks like function cross_val_score won't work in neupy, but you can run the same code in slightly different way.

import numpy as np
from neupy import algorithms, layers
from sklearn.model_selection import *
from sklearn import metrics

XTrainScaled = XTrain = np.random.random((10, 2))
yTrainScaled = np.random.random((10, 1))

kfold = KFold(n_splits=5, shuffle=True, random_state=7)
scores = []

for train, test in kfold.split(XTrainScaled):
    x_train, x_test = XTrainScaled[train], XTrainScaled[test]
    y_train, y_test = yTrainScaled[train], yTrainScaled[test]

    cgnet = algorithms.LevenbergMarquardt(
        connection=[
            layers.Input(XTrain.shape[1]),
            layers.Linear(6),
            layers.Linear(1)
        ],
        mu_update_factor=2,
        mu=0.1,
        shuffle_data=True,
        verbose=True,
        decay_rate=0.1,
        addons=[algorithms.WeightElimination]
    )

    cgnet.train(x_train, y_train, epochs=5)
    y_predicted = cgnet.predict(x_test)

    score = metrics.mean_absolute_error(y_test, y_predicted)
    scores.append(score)

print(scores)
scores = np.array(scores)
print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))

Upvotes: 2

Related Questions