1.数据集:mnist
2.网络:cnn
3.框架:tensorflow
cnn基础结构为:卷积层-》池化层-》卷积层-》池化层-》拉直-》全连接层-》输出层
其中卷积层、卷积层、全连接层、输出层,这四个层存在激励函数,在这里我选了常用
这6个激励函数’sigmoid’,‘tanh’,‘elu’,‘softmax’,‘relu’,‘linear’。然后通过遍历探究结果
import tensorflow as tfmnist=tf.keras.datasets.mnist# 下面三行是常规导入from tensorflow import kerasfrom tensorflow.keras import layersfrom tensorflow.keras import models#导入mnist数据集,需要保持网络畅通(X_train, Y_train), (X_test, Y_test) = mnist.load_data()var1=[\'sigmoid\',\'tanh\',\'elu\',\'softmax\',\'relu\',\'linear\']var2=[\'sigmoid\',\'tanh\',\'elu\',\'softmax\',\'relu\',\'linear\']var3=[\'sigmoid\',\'tanh\',\'elu\',\'softmax\',\'relu\',\'linear\']var4=[\'sigmoid\',\'tanh\',\'elu\',\'softmax\',\'relu\',\'linear\']img_rows, img_cols = 28, 28 # 图像的尺寸a=var1[5]# 训练数据;X_train是60000张28*28的数据,所以尺寸是60000*28*28,Y_train是对应的数字,尺寸是60000*1,X_test和Y_test同理X_train, X_test = X_train / 255.0, X_test / 255.0 # 将图像像素转化为0-1的实数# 将标准答案通过one-hot编码转化为需要的格式(这两个函数不懂见下个模块的介绍)Y_train = keras.utils.to_categorical(Y_train, num_classes=10)Y_test = keras.utils.to_categorical(Y_test, num_classes=10)# 将训练所用的图像调整尺寸,由于图像是黑白图像,所以最后一维的值是1X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)max=0min=1################################## 使用keras API开始定义模型 #################################for i in range(0,1):for j in range(0,2):for p in range(0,6):for q in range(0,6):model = models.Sequential()# 向模型中添加层model.add(layers.Conv2D(32, kernel_size=(5,5), # 添加卷积层,深度32,过滤器大小5*5activation=var1[i], # 使用relu激活函数input_shape=(img_rows, img_cols, 1))) # 输入的尺寸就是一张图片的尺寸(28,28,1)model.add(layers.MaxPooling2D(pool_size=(2, 2))) # 添加池化层,过滤器大小是2*2model.add(layers.Conv2D(64, (5,5), activation=var2[j])) # 添加卷积层,简单写法model.add(layers.MaxPooling2D(pool_size=(2, 2))) # 添加池化层model.add(layers.Flatten()) # 将池化层的输出拉直,然后作为全连接层的输入model.add(layers.Dense(500, activation=var3)) # 添加有500个结点的全连接层model.add(layers.Dense(10, activation=var4[q])) # 输出最终结果,有10个# 定义损失函数、优化函数、评测方法model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.SGD(),metrics=[\'accuracy\'])# 自动完成模型的训练过程model.fit(X_train, Y_train, # 训练集batch_size=128, # batchsizeepochs=20, # 训练轮数validation_data=(X_test, Y_test)) # 验证集# 打印运行结果,即损失和准确度score = model.evaluate(X_test, Y_test)if max<=score[1]:max=score[1]maxnum=var1[i]+\' \'+var2[j]+\' \'+var3[p]+\' \'+var4[q]if min>=score[1]:min=score[1]minnum=var1[i]+\' \'+var2[j]+\' \'+var3[p]+\' \'+var4[q]print(\'Test loss:\', score[0])print(var1[i]+\' \'+var2[j]+\' \'+var3[p]+\' \'+var4[q]+\'Test accuracy:\', score[1])print(maxnum,max)print(minnum,min)
[p]最终发现如下的组合单次训练后结果比较好:
1.tanh tanh linear softmax 0.9024999737739563
2.tanh elu linear softmax 0.916100025177002
3.tanh relu elu softmax 0.9189000129699707
4.elu tanh linear softmax 0.9142000079154968
5.elu elu tanh softmax 0.9168000221252441
6.elu relu tanh softmax 0.9240000247955322
7.relu tanh linear softmax 0.9164000153541565
8.relu elu linear softmax 0.9193000197410583
9.relu relu linear softmax 0.9235000014305115
10.linear tanh linear softmax 0.9129999876022339
11.linear elu relu softmax 0.9178000092506409
12.linear relu tanh softmax 0.9239000082015991
从中发现训练结果中的输出层采用softmax是最好的