Skip to content

This MLPClassifier instance is not fitted yet #35

@Gbouna

Description

@Gbouna

Hello,
I am trying to use the package with imagedata generator, but i am getting error. Below is my code

from __future__ import print_function, absolute_import, division, unicode_literals, with_statement
from hypopt import GridSearch
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler 
from sklearn.model_selection import train_test_split

# Neural Network imports (simple sklearn Neural Network)
from sklearn.neural_network import MLPClassifier
# Silence neural network SGD convergence warnings.
from sklearn.exceptions import ConvergenceWarning
import warnings
warnings.filterwarnings('ignore', category=ConvergenceWarning)


param_grid = {
    'dropout_rate': [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
    'hidden_layer_sizes': [(10,20), (12,30)],
    'batch_size': [16, 32, 64],
    'optimizer':  ['SGD', 'RMSprop', 'Adagrad', 'Adadelta', 'Adam', 'Adamax', 'Nadam'],
    'learn_rate':  [0.00001, 0.0001, 0.001, 0.01],
    'init_mode':  ['uniform', 'lecun_uniform', 'normal', 'zero', 'glorot_normal', 'glorot_uniform', 'he_normal', 'he_uniform'],
    'activation':  ['softmax', 'softplus', 'softsign', 'relu', 'tanh', 'sigmoid', 'hard_sigmoid', 'linear'],
    'activation': ['relu'],
}


from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1.0/255.0)
test_datagen = ImageDataGenerator(rescale=1.0/255.0)
# prepare iterators
#folder_count = glob('Data/Test')
train = train_datagen.flow_from_directory('train_dir', 
                                             class_mode='categorical', target_size=(224, 224))
val = test_datagen.flow_from_directory('val_dir',
                                           class_mode='categorical',  target_size=(224, 224))
test = test_datagen.flow_from_directory('test_dir',
                                            class_mode='categorical', target_size=(224, 224))

y_train = train.class_indices
y_test = test.class_indices
y_val = val.class_indices
print(y_train)
print(y_val)
print(y_test)


gs_val = GridSearch(model = MLPClassifier(max_iter=6, random_state=0), param_grid=param_grid,\
    parallelize=False)
print("Grid-search using a validation set.\n","-"*79)
%time gs_val.fit(train, val)
test_score = round(gs_val.score(test, y_test), 4)
val_score = round(gs_val.score(val, y_val), 4)
print('\nTEST SCORE (hyper-parameter optimization with validation set):', test_score)
print('VALIDATION SCORE (hyper-parameter optimization with validation set):', val_score)

I added the labels (y) value and use it in the code because it was initially giving the error that

TypeError: score() missing 1 required positional argument: 'y

Although i beleive it should have been fine without it since the imagedata generator generates the lables alongside.

However, after that, i am still getting the error

NotFittedError: This MLPClassifier instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator

I will appreciate your response.
Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions