概述
1 data_pre_procession
2 .model = Sequencial([dense(),relu(),dense(),softmax])
3.可选:自定义优化器,optimizer
4.model.compile(loss='categorical_crossentropy',metrics=['accuracy'],optimizer)
5.model.fit(data_x,data_y,epoch=,metrics=['accuracy'])
6. loss,accuracy = model.evaluate(x_test,y_test)
# 5 - Classifier example
import numpy as np
from keras import optimizers
np.random.seed(1337)
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation
# import numpy as np
# data
# download mnist to the path '~/.keras/datasets/' if it is first time to be called
# X shape (60000,28*28),Y_shape(10000)
#data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
print(X_train.shape) # (60000, 28, 28)
print(X_test.shape) # (10000, 28, 28)
print(y_train.shape) # (60000,)
print(y_test.shape) # (10000,)
#data
#input_reshape
X_train =X_train.reshape(X_train.shape[0],-1) / 255
X_test =X_test.reshape(X_test.shape[0],-1) / 255
#y_data标签预处理
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes = 10)
#add_layers,新建一个序列对象,供下面的编译时候指定
model = Sequential(
[
Dense(32,input_dim=784),
Activation('relu'),
Dense(10),
Activation('softmax'),
]
)
#可以在编译之前自定义optimizer
rm_sprop = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
#compile
model.compile(optimizer=rm_sprop,loss='categorical_crossentropy',metrics=['accuracy'])
#fit
model.fit(X_train,y_train,epochs=5,batch_size=32)#batch
#evaluate
loss,accuracy = model.evaluate(X_test, y_test)
print('test loss:',loss)
print('test accuracy:',accuracy)
最后
以上就是优雅金毛为你收集整理的Keras_Classifier的全部内容,希望文章能够帮你解决Keras_Classifier所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复