CNN卷积神经网络搭建训练

import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

tensorflow用于构建训练机器学习的强大框架,功能包括定义神经网络模型,数据预处理,模型评估和训练
numpy:python核心科学计算库提供高效的多维数组以及许多数学、逻辑、数组的操作
panda:用于数据分析和处理,功能包括加载数据、数据清洗、数据分析可视化

(train_images,train_labels),(test_images,test_labels)=tf.keras.datasets.mnist.load_data()
#载入数据
train_images.shape
(60000, 28, 28)
train_images=train_images.reshape(60000,28,28,1)
test_images=test_images.reshape(10000,28,28,1)
#维度变换因为图片和卷积核变换后可能会有不同的层
train_images.shape
(60000, 28, 28, 1)
train_images=train_images/255
test_images=test_images/255
#归一化
train_labels=np.array(pd.get_dummies(train_labels))
test_labels=np.array(pd.get_dummies(test_labels))
train_labels
array([[0, 0, 0, ..., 0, 0, 0],
       [1, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 1, 0]], dtype=uint8)
model=tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(filters=6,kernel_size=(5,5),input_shape=(28,28,1),activation='relu'))
#卷积层的定义包括6个cov kernel,卷积核尺寸5*5
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2,2)))
#默认池化层边长就是步长,因此不相交
model.add(tf.keras.layers.Flatten())#展平层
model.add(tf.keras.layers.Dense(64,activation='sigmoid'))#进入了全连接层后进入隐藏层有64个神经元
model.add(tf.keras.layers.Dense(10,activation='softmax'))#因为是分类问题,进入了有10个的输出层,多分类问题用的是softmax

运行不出来,代码又没有错,可以Kernel-Restart and Clear Outputs of all cells

model.summary()
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d (Conv2D)             (None, 24, 24, 6)         156       
                                                                 
 max_pooling2d (MaxPooling2  (None, 12, 12, 6)         0         
 D)                                                              
                                                                 
 flatten (Flatten)           (None, 864)               0         
                                                                 
 dense (Dense)               (None, 64)                55360     
                                                                 
 dense_1 (Dense)             (None, 10)                650       
                                                                 
=================================================================
Total params: 56166 (219.40 KB)
Trainable params: 56166 (219.40 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

可见卷积层156参数——25*6+6(偏置项),而池化层层不需要参数

model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['acc'])
history=model.fit(train_images,train_labels,epochs=10,validation_data=(test_images,test_labels))
Epoch 1/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.3257 - acc: 0.9167 - val_loss: 0.1322 - val_acc: 0.9621
Epoch 2/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.1086 - acc: 0.9698 - val_loss: 0.0791 - val_acc: 0.9765
Epoch 3/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.0701 - acc: 0.9803 - val_loss: 0.0640 - val_acc: 0.9800
Epoch 4/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.0529 - acc: 0.9849 - val_loss: 0.0529 - val_acc: 0.9829
Epoch 5/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.0402 - acc: 0.9889 - val_loss: 0.0474 - val_acc: 0.9846
Epoch 6/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.0323 - acc: 0.9907 - val_loss: 0.0420 - val_acc: 0.9860
Epoch 7/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.0262 - acc: 0.9929 - val_loss: 0.0436 - val_acc: 0.9866
Epoch 8/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.0220 - acc: 0.9946 - val_loss: 0.0431 - val_acc: 0.9847
Epoch 9/10
1875/1875 [==============================] - 5s 3ms/step - loss: 0.0176 - acc: 0.9955 - val_loss: 0.0433 - val_acc: 0.9868
Epoch 10/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.0145 - acc: 0.9965 - val_loss: 0.0394 - val_acc: 0.9864

调用 model.fit() 方法时,会启动模型的训练过程。这个方法接受训练数据集和训练参数,并返回一个 history 对象。history: 这个对象记录了模型在训练过程中的一些指标和记录,比如每个训练轮次的损失值和准确率,以及验证集上的损失值和准确率等。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值