Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 名称错误:名称';分类器&x27;没有定义_Python 3.x_Tensorflow_Machine Learning_Keras_Prediction - Fatal编程技术网

Python 3.x 名称错误:名称';分类器&x27;没有定义

Python 3.x 名称错误:名称';分类器&x27;没有定义,python-3.x,tensorflow,machine-learning,keras,prediction,Python 3.x,Tensorflow,Machine Learning,Keras,Prediction,我是机器学习新手。我试图在数据集上进行预测,但当我运行该程序时,它给了我以下错误: NameError: name 'classifier' is not defined 这是我的密码: 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

我是机器学习新手。我试图在数据集上进行预测,但当我运行该程序时,它给了我以下错误:

NameError: name 'classifier' is not defined 
这是我的密码:

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'

您正在使用
分类器
进行预测。但是没有定义
分类器。这就是错误所在

要解决此问题,您必须拥有已保存的keras模型,该模型针对您的特定问题进行了培训。如果你有,你可以加载它并做出预测

下面的代码显示了如何加载模型

from keras.models import load_model

classifier = load_model('path_to_your_model')
加载模型后,您可以使用该模型进行类似的预测

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'

在开始向模型中添加图层之前,必须指定“空”版本

from keras.models import load_model

classifier = load_model('path_to_your_model')
您只需在代码上方添加以下行即可修复此错误:

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

#empty model
classifier = Sequential()
然后继续指定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))

这是意料之中的。您尚未定义分类器,因此必须加载已保存的分类器。我有try.py文件trained@ShubhamAher如果答案解决了你的问题,请通过点击答案旁边的复选框来接受它(如果你有帮助的话,你也可以考虑一个赞成票)。