2026/3/28 20:26:55
网站建设
项目流程
天津网站建设seo优化,山西焦煤集团公司网站,商务网站开发心得,网络营销环境分析包括哪些内容#x1f368; 本文为#x1f517;365天深度学习训练营中的学习记录博客#x1f356; 原作者#xff1a;K同学啊
一、前置知识
1、知识总结 概念 作用 归一化 统一数据范围#xff0c;加速训练 卷积层 提取图像局部特征 池化层 压缩数据#xff0c;增强鲁棒性 全…本文为365天深度学习训练营中的学习记录博客原作者K同学啊一、前置知识1、知识总结概念作用归一化统一数据范围加速训练卷积层提取图像局部特征池化层压缩数据增强鲁棒性全连接层综合特征输出分类激活函数(ReLU)引入非线性2、CNN网络二、代码实现1、准备工作1.1.设置GPUimport tensorflow as tf gpus tf.config.list_physical_devices(GPU) if gpus: gpu0 gpus[0] #如果有多个GPU仅使用第0个GPU tf.config.experimental.set_memory_growth(gpu0, True) #设置GPU显存用量按需使用 tf.config.set_visible_devices([gpu0],GPU) print(gpus)2026-01-08 18:44:48.698143: I tensorflow/core/util/util.cc:169] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS0. [PhysicalDevice(name/physical_device:GPU:0, device_typeGPU)]1.2.导入数据import tensorflow as tf from tensorflow.keras import datasets, layers, models import matplotlib.pyplot as plt # 导入mnist数据依次分别为训练集图片、训练集标签、测试集图片、测试集标签 (train_images, train_labels), (test_images, test_labels) datasets.mnist.load_data()Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz 11490434/11490434 [] - 3s 0us/step1.3.归一化数据归一化作用使不同量纲的特征处于同一数值量级减少方差大的特征的影响使模型更准确。加快学习算法的收敛速度。# 将像素的值标准化至0到1的区间内。(对于灰度图片来说每个像素最大值是255每个像素最小值是0也就是直接除以255就可以完成归一化。) train_images, test_images train_images / 255.0, test_images / 255.0 # 查看数据维数信息 train_images.shape,test_images.shape,train_labels.shape,test_labels.shape((60000, 28, 28), (10000, 28, 28), (60000,), (10000,))1.4.可视化图片# 将数据集前20个图片数据可视化显示 # 进行图像大小为20宽、10长的绘图(单位为英寸inch) plt.figure(figsize(20,10)) # 遍历MNIST数据集下标数值0~49 for i in range(20): # 将整个figure分成2行10列绘制第i1个子图。 plt.subplot(2,10,i1) # 设置不显示x轴刻度 plt.xticks([]) # 设置不显示y轴刻度 plt.yticks([]) # 设置不显示子图网格线 plt.grid(False) # 图像展示cmap为颜色图谱plt.cm.binary为matplotlib.cm中的色表 plt.imshow(train_images[i], cmapplt.cm.binary) # 设置x轴标签显示为图片对应的数字 plt.xlabel(train_labels[i]) # 显示图片 plt.show()1.5.调整图片格式#调整数据到我们需要的格式 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((60000, 28, 28, 1), (10000, 28, 28, 1), (60000,), (10000,))2、训练模型2.1.构建CNN网络# 创建并设置卷积神经网络 # 卷积层通过卷积操作对输入图像进行降维和特征抽取 # 池化层是一种非线性形式的下采样。主要用于特征降维压缩数据和参数的数量减小过拟合同时提高模型的鲁棒性。 # 全连接层在经过几个卷积和池化层之后神经网络中的高级推理通过全连接层来完成。 model models.Sequential([ # 设置二维卷积层1设置32个3*3卷积核activation参数将激活函数设置为ReLu函数input_shape参数将图层的输入形状设置为(28, 28, 1) # ReLu函数作为激活励函数可以增强判定函数和整个神经网络的非线性特性而本身并不会改变卷积层 # 相比其它函数来说ReLU函数更受青睐这是因为它可以将神经网络的训练速度提升数倍而并不会对模型的泛化准确度造成显著影响。 layers.Conv2D(32, (3, 3), activationrelu, input_shape(28, 28, 1)), #池化层12*2采样 layers.MaxPooling2D((2, 2)), # 设置二维卷积层2设置64个3*3卷积核activation参数将激活函数设置为ReLu函数 layers.Conv2D(64, (3, 3), activationrelu), #池化层22*2采样 layers.MaxPooling2D((2, 2)), layers.Flatten(), #Flatten层连接卷积层与全连接层 layers.Dense(64, activationrelu), #全连接层特征进一步提取64为输出空间的维数activation参数将激活函数设置为ReLu函数 layers.Dense(10) #输出层输出预期结果10为输出空间的维数 ]) # 打印网络结构 model.summary()2026-01-08 18:49:53.891561: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F AVX512_VNNI FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2026-01-08 18:49:55.030714: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1532] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 10099 MB memory: - device: 0, name: NVIDIA GeForce RTX 3080 Ti, pci bus id: 0000:5e:00.0, compute capability: 8.6 Model: sequential _________________________________________________________________ Layer (type) Output Shape Param # conv2d (Conv2D) (None, 26, 26, 32) 320 max_pooling2d (MaxPooling2D (None, 13, 13, 32) 0 ) conv2d_1 (Conv2D) (None, 11, 11, 64) 18496 max_pooling2d_1 (MaxPooling (None, 5, 5, 64) 0 2D) flatten (Flatten) (None, 1600) 0 dense (Dense) (None, 64) 102464 dense_1 (Dense) (None, 10) 650 Total params: 121,930 Trainable params: 121,930 Non-trainable params: 0 _________________________________________________________________2.2.编译模型 这里设置优化器、损失函数以及metrics model.compile()方法用于在配置训练方法时告知训练时用的优化器、损失函数和准确率评测标准 model.compile( # 设置优化器为Adam优化器 optimizeradam, # 设置损失函数为交叉熵损失函数tf.keras.losses.SparseCategoricalCrossentropy() # from_logits为True时会将y_pred转化为概率用softmax否则不进行转换通常情况下用True结果更稳定 losstf.keras.losses.SparseCategoricalCrossentropy(from_logitsTrue), # 设置性能指标列表将在模型训练时监控列表中的指标 metrics[accuracy])2.3.训练模型 这里设置输入训练数据集图片及标签、验证数据集图片及标签以及迭代次数epochs 关于model.fit()函数的具体介绍可参考我的博客 https://blog.csdn.net/qq_38251616/article/details/122321757 history model.fit( # 输入训练集图片 train_images, # 输入训练集标签 train_labels, # 设置10个epoch每一个epoch都将会把所有的数据输入模型完成一次训练。 epochs10, # 设置验证集 validation_data(test_images, test_labels))Epoch 1/10 2026-01-08 18:51:21.815488: I tensorflow/stream_executor/cuda/cuda_dnn.cc:384] Loaded cuDNN version 8101 2026-01-08 18:51:24.686085: I tensorflow/stream_executor/cuda/cuda_blas.cc:1786] TensorFloat-32 will be used for the matrix multiplication. This will only be logged once. 1875/1875 [] - 14s 4ms/step - loss: 0.6748 - accuracy: 0.7807 - val_loss: 0.3137 - val_accuracy: 0.9037 Epoch 2/10 1875/1875 [] - 8s 4ms/step - loss: 0.2385 - accuracy: 0.9280 - val_loss: 0.1592 - val_accuracy: 0.9516 Epoch 3/10 1875/1875 [] - 8s 4ms/step - loss: 0.1465 - accuracy: 0.9559 - val_loss: 0.1082 - val_accuracy: 0.9665 Epoch 4/10 1875/1875 [] - 8s 4ms/step - loss: 0.1113 - accuracy: 0.9657 - val_loss: 0.0896 - val_accuracy: 0.9729 Epoch 5/10 1875/1875 [] - 8s 4ms/step - loss: 0.0913 - accuracy: 0.9717 - val_loss: 0.0877 - val_accuracy: 0.9732 Epoch 6/10 1875/1875 [] - 8s 4ms/step - loss: 0.0796 - accuracy: 0.9758 - val_loss: 0.0700 - val_accuracy: 0.9780 Epoch 7/10 1875/1875 [] - 8s 4ms/step - loss: 0.0712 - accuracy: 0.9777 - val_loss: 0.0613 - val_accuracy: 0.9816 Epoch 8/10 1875/1875 [] - 8s 4ms/step - loss: 0.0638 - accuracy: 0.9802 - val_loss: 0.0712 - val_accuracy: 0.9759 Epoch 9/10 1875/1875 [] - 8s 4ms/step - loss: 0.0584 - accuracy: 0.9814 - val_loss: 0.0574 - val_accuracy: 0.9815 Epoch 10/10 1875/1875 [] - 8s 4ms/step - loss: 0.0550 - accuracy: 0.9830 - val_loss: 0.0509 - val_accuracy: 0.98443、模型预测plt.imshow(test_images[1])matplotlib.image.AxesImage at 0x7f917a9d5100pre model.predict(test_images) # 对所有测试图片进行预测 pre[1] # 输出第一张图片的预测结果313/313 [] - 1s 2ms/step array([ 4.0916557, 8.121223 , 17.289515 , -1.078687 , -17.282494 , -4.956851 , -0.79667 , -10.196654 , 4.119256 , -14.890562 ], dtypefloat32)