T1打卡——mnist手写数字识别

1.定义GPU

import tensorflow as tf

gpus=tf.config.list_physical_devices("GPU")

if gpus:
    gpu0=gpus[0]
    tf.config.experimental.set_memort_groth(gpu0,True)   #设置GPU现存用量按需使用
    tf.config.experimental.set_visible_devices([gpu0],"GPU")

2.数据预处理

#导入数据
import tensorflow as tf
from tensorflow.keras import datasets,layers,models
import matplotlib.pyplot as plt

(train_images,train_labels),(test_images,test_labels)=datasets.mnist.load_data()

#标准化
train_images,test_images=train_images/255.0,test_images/255.0
#查看维数
train_images.shape,test_images.shape,train_labels.shape,test_labels.shape
#数据可视化
plt.figure(figsize=(20,5))
for i in range(20):
    plt.subplot(2,10,i+1)
    #不显示x轴刻度
    plt.xticks([])
    #不显示y轴刻度
    plt.yticks([])
    #不显示子图网格线
    plt.grid(False)
    #cmap是颜色图谱,plt.cm.binary是色表
    plt.imshow(train_images[i],cmap=plt.cm.binary)
    plt.xlabel(train_labels[i])
plt.show()

#重塑数据维度使其可易于被模型处理
train_images=train_images.reshape((60000,28,28,1))
test_images=test_images.reshape((10000,28,28,1))

train_images.shape,test_images.shape,train_labels.shape,test_labels.shape

55f75a2526564d6b8a3e6401b0fffd84.png3.定义CNN网络模型

#定义cnn模型
model=models.Sequential([
    ## 设置二维卷积层1,设置32个3*3卷积核
    layers.Conv2D(32,(3,3),activation='relu',input_shape=(28,28,1)),
    #池化层1,2*2采样
    layers.MaxPooling2D((2,2)),
     # 设置二维卷积层2,设置64个3*3卷积核,
    layers.Conv2D(64,(3,3),activation='relu'),
    #池化层2,2*2采样
    layers.MaxPooling2D((2,2)),
    #连接卷积层和全连接层
    layers.Flatten(),
    #全连接层,64是输出维度
    layers.Dense(64,activation='relu'),
    #输出层,输出维度是10
    layers.Dense(10)
])
model.summary()
#定义优化器损失函数
model.compile(
    optimizer='adam',
    #交叉熵损失函数(tf.keras.losses.SparseCategoricalCrossentropy(), from_logits为True时,会将y_pred转化为概率(用softmax),否则不进行转换,通常情况下用True结果更稳定
    loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=(['accuracy'])
)

4e3d56a10e5f436c855e795924ee218e.png

4.训练模型

history=model.fit(train_images,train_labels,epochs=10,validation_data=(test_images,test_labels))

36ee9364253e470a81f0462e5dd2fb49.png

5.预测数据

plt.imshow(test_images[1])

pre=model.predict(test_images)
pre[1]

e017f98a7c024cf58b37464c984a25f7.png7d6aa815cfca4a698de75129b32c28ed.png

 

知识点总结:

1.CNN网络

7cf628d80a7e42eb86799850b101a741.png

         卷积层:通过卷积操作对输入图像进行降维和特征抽取
         池化层:是一种非线性形式的下采样。主要用于特征降维,压缩数据和参数的数量,减小过拟合,同时提高模型的鲁棒性。
         全连接层:在经过几个卷积和池化层之后,神经网络中的高级推理通过全连接层来完成。

        作用:         

  • 输入层:用于将数据输入到训练网络
  • 卷积层:使用卷积核提取图片特征
  • 池化层:进行下采样,用更高层的抽象表示图像特征
  • Flatten层:将多维的输入一维化,常用在卷积层到全连接层的过渡
  • 全连接层:起到“特征提取器”的作用
  • 输出层:输出结果

2.定义gpu的方法:

import tensorflow as tf

gpus=tf.config.list_physical_devices("GPU")

if gpus:
    gpu0=gpus[0]
    tf.config.experimental.set_memort_groth(gpu0,True)   #设置GPU现存用量按需使用
    tf.config.experimental.set_visible_devices([gpu0],"GPU")

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值