我遵循了使用mnist和keras的教程。我已经训练了模型并获得了非常好的准确性。现在我如何通过提供新的输入来做出新的预测。 这是代码
import numpy as np
from keras.layers import MaxPool2D,Conv2D,Dense,Flatten,Dropout
from keras.models import Sequential
from keras.utils import np_utils
from keras.layers import activations
from keras.datasets import mnist
np.random.seed(1)
(x_train,y_train),(x_test,y_test)=mnist.load_data()
x_train = x_train.reshape(x_train.shape[0], 28,28,1).astype('float32')
x_test = x_test.reshape(x_test.shape[0], 28, 28,1).astype('float32')
x_train=x_train.astype('float32')
x_tset=x_test.astype('float32')
x_train=x_train/255
x_test=x_test/255
y_train=np_utils.to_categorical(y_train,10)
y_test=np_utils.to_categorical(y_test,10)
model=Sequential()
model.add(Conv2D(32,(3,3),activation='relu',input_shape=(28,28,1)))
model.add(MaxPool2D(pool_size= (2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(32,(3,3), activation='relu',input_shape=(28,28,1)))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(units=128,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=10,activation='softmax'))
model.compile(optimizer='adam',loss='binary_crossentropy',metrics= ['accuracy'])
model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10, batch_size=200, verbose=2)
model.evaluate(x_test,y_test,verbose=0)
model.save('hand_written.h5')
先谢谢!!
答案 0 :(得分:1)
本教程中的这一行代码是采用新的示例来评估您的模型:model.evaluate(x_test,y_test,verbose=0)
您需要做的是在此表单中为evaluate(x_test,y_test,verbose=0)
提供新输入。如果您想了解预测的工作原理,请写下:
prediction = model.evaluate(x_test,y_test,verbose=0)
print('prediction:',prediction)