需要下载keras框架!
大概解释下整体过程:
1.定义下载数据的函数 load_data()
,在网络上进行下载训练集和测试集文件。
2.定义神经网络模型
输入维数 :图片是28 *28,共10000张,则输入维数是10000 * 784
;
输出维数:九种数字类型,那么输出维数是10000 * 10
;
神经网络层数 :根据自己的需要进行设定;
每层神经元的:同上
激活函数:这里用的是sigmoid
函数
损失函数:根据自己许需要进行选择;
学习率:设定合适的学习率,使训练效果尽量好;
3.开始训练神经网络
4.验证神经网络的好坏
下面是在jupyter上运算的主要过程和代码,能比较清晰的看到整个实验过程。最后训练集上精度是0.1018,测试集上精度是0.1094,都不理想,说明这是欠拟合;在训练集上训练效果就不好,测试集自然也不会好。
下面将代码注释一下:
1.引入各种包和框架。
import numpy as np
import keras
from keras. models import Sequential
from keras. layers. core import Dense, Dropout, Activation
from keras. layers import Convolution2D, MaxPooling2D, Flatten
from keras. optimizers import SGD, Adam
from keras. utils import np_utils
from keras. datasets import mnist
2.定义下载数据的函数
def load_data ( ) :
( x_train, y_train) , ( x_test, y_test) = mnist. load_data( 'mnist.npz' )
number = 10000
x_train = x_train[ 0 : number]
y_train = y_train[ 0 : number]
x_train = x_train. reshape( number, 28 * 28 )
x_test = x_test. reshape( x_test. shape[ 0 ] , 28 * 28 )
x_train = x_train. astype( 'float32' )
x_test = x_train. astype( 'float32' )
y_train = np_utils. to_categorical( y_train, 10 )
y_test = np_utils. to_categorical( y_test, 10 )
x_train = x_train
x_test = x_test
x_train = x_train / 255
x_test = x_test / 255
return ( x_train, y_train) , ( x_test, y_test)
( x_train, y_train) , ( x_test, y_test) = load_data( )
3.引入模型 训练并且测试 输出测试结果
model = Sequential( )
model. add( Dense( input_dim = 28 * 28 , units = 633 , activation = 'sigmoid' ) )
model. add( Dense( units = 633 , activation = 'sigmoid' ) )
model. add( Dense( units = 633 , activation = 'sigmoid' ) )
model. add( Dense( units = 10 , activation = 'softmax' ) )
model. compile ( loss = 'mse' , optimizer = SGD( lr = 0.001 ) , metrics = [ 'accuracy' ] )
model. fit( x_train, y_train, batch_size = 100 , epochs = 20 )
score = model. evaluate( x_train, y_train)
print ( '\n Train Acc: ' , score[ 1 ] )
score = model. evaluate( x_test, y_test)
print ( '\n Test Acc: ' , score[ 1 ] )
这就是整个实验过程,参数自己可以调整,自己运行,查看训练结果。