keras--minist分类

 

from keras.datasets import mnist
import numpy as np
from keras.utils import np_utils
from keras.layers import Dense,Activation
from keras.models import Sequential
from keras.optimizers import RMSprop

(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0],-1)/255 # normalization
x_test = x_test.reshape(x_test.shape[0],-1)/255

y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)

# built model
model = Sequential([
    Dense(32,input_dim=784),
    Activation('relu'),
    Dense(10),
    Activation('softmax')
])
# define optimizer
rmsprop = RMSprop(lr=0.001,rho=0.9,epsilon=1e-08,decay=0)
model.compile(loss='categorical_crossentropy',optimizer=rmsprop,metrics=['accuracy'])

# training
print('training...')
model.fit(x_train,y_train,epochs=2,batch_size=32)
# testing
print('testing...')
cost,accuracy = model.evaluate(x_test,y_test)
print('cost = ',cost)
print('accuracy = ',accuracy)

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值