Skip to content Skip to sidebar Skip to footer

Nameerror: Name 'classifier' Is Not Defined

I am new to machine learning. I was trying to predict on a dataset but when I run the program it give me following error: NameError: name 'classifier' is not defined Here is my c

Solution 1:

You are using classifier to make predictions. But the classifier is not defined. That is what the error is.

To solve this, You must have the saved keras model that is trained for your specific problem with it. If you have that, you can load it and make predictions.

Below code shows how you can load the model.

from keras.models importload_modelclassifier= load_model('path_to_your_model')

After the model is loaded you can use that to make predictions like you do.

import numpy as np
from keras.preprocessing import image
test_image = image.load_img('dataset/single_prediction/1.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
  prediction = 'nsfw'else:
  prediction = 'sfw'

Solution 2:

You have to specify an 'empty' version, before you start adding the layers into the model.

You can simply fix this error by adding this line above your code:

import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.models import load_model

#empty model
classifier = Sequential()

Then continue with specifying like:

#add layers, start with hidden layer and first deep layer
classifier.add(Dense(output_dim=15, init="uniform", activation='relu',input_dim = 15))
classifier.add(Dropout(rate=0.1))

Post a Comment for "Nameerror: Name 'classifier' Is Not Defined"