深度学习实验--初步探索数据增强、优化器对模型的影响

  • 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
  • 🍖 原作者:K同学啊

前言

  • 这次主要是探究,优化器、数据增强对模型训练的影响;
  • 基础篇还剩下几个, 后面的难度会逐步提升;
  • 越学,越觉得这个东西很玄学,没有过硬实力真的很难把控;
  • 欢迎收藏 + 关注, 本人会持续更新.

文章目录

  • 1、实验
    • 优化器对比实验
    • 优化器结合数据增强
      • 分别进行与不进行数据增强,跑20轮
      • 分别进行与不进行数据增强,跑50轮
  • 2、代码实现
    • 1、数据处理
      • 1、导入库
      • 2、数据导入与划分数据集
      • 3、展示一部分数据
      • 4、数据归一化和内存加速
      • 5、数据增强
      • 6、数据整合
    • 2、模型构建和实验
    • 3、模型训练与实验验证
    • 4、结果检验

1、实验

💁‍♂ 提示:本次实验并不一定具有广泛意义,具体也是需要更具不同场景进行不同分析,这里仅仅作为简单的对比,初步了解不同优化器的区别。

优化器对比实验

  • 目的:对比AdamSGD优化器
  • 条件:用优化版VGG16(详情请看代码实现)对人脸进行识别分类,训练50轮,效果如下:

在这里插入图片描述

⛹️‍♀️ 分析

  • Adam 每一次训练的时候是结合动量和自适应学习方法,可以自动调整学习率;
  • SGD是每一次训练一批数据的时候,是用一个一个样本进行训练的,比较简单;
  • 效果分析Adam比较快提高准确率,在训练初期的时候效果好,但是在后期的时候SGD综合效果更好一点点。

优化器结合数据增强

分别进行与不进行数据增强,跑20轮

效果如图

  • 不进行

在这里插入图片描述

  • 进行

在这里插入图片描述

初步分析

  • 从准确率和损失率来看,发现数据增强后效果较好,但是都存在过拟合的情况,下面加大轮次进行训练。

分别进行与不进行数据增强,跑50轮

效果如图

  • 不进行

在这里插入图片描述

  • 进行

在这里插入图片描述

初步分析

  • 从准确率和损失率来看,发现在加大训练轮次后,进行数据增强效果更好,因为解决了过拟合的情况,不进行数据增强依然存在过拟合情况
  • 效果:从模型效果来看,想要继续优化,最好的办法就是换更好的模型(本文用的是VGG16),后面可以用Resnet这些模型进行再一次优化。

总结

  • 数据、优化器均对效果有不同影响,我感觉这也是深度学习很难得地方,很难把控,解决方法,我感觉只有多实践,多看论文,多复现论文积累经验。

2、代码实现

这里完整代码是50轮数据增强代码,不数据增强,只需要不允许

1、数据处理

1、导入库

import tensorflow as tf
import numpy as np 
from tensorflow.keras import models, datasets, layersgpu = tf.config.list_physical_devices("GPU")print(gpu)
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

2、数据导入与划分数据集

这里查看图像数据分类信息

import os, pathlibdata_dir = './data/'
data_dir = pathlib.Path(data_dir)personClassNames = os.listdir(data_dir)personClassNames
['Angelina Jolie','Brad Pitt','Denzel Washington','Hugh Jackman','Jennifer Lawrence','Johnny Depp','Kate Winslet','Leonardo DiCaprio','Megan Fox','Natalie Portman','Nicole Kidman','Robert Downey Jr','Sandra Bullock','Scarlett Johansson','Tom Cruise','Tom Hanks','Will Smith']

划分: 训练集 :验证集 = 8 : 2

batch_size = 16
image_width, image_height = 336, 336train_ds = tf.keras.preprocessing.image_dataset_from_directory('./data/',subset='training',validation_split=0.2,batch_size=batch_size,image_size=(image_width, image_height),seed=42,shuffle=True
)val_ds = tf.keras.preprocessing.image_dataset_from_directory('./data/',subset='validation',validation_split=0.2,batch_size=batch_size,image_size=(image_width, image_height),seed=42,shuffle=True
)
Found 1800 files belonging to 17 classes.
Using 1440 files for training.
Found 1800 files belonging to 17 classes.
Using 360 files for validation.

查看数据划分后的格式

one_batch_data = next(iter(train_ds))one_batch_data_images, one_batcg_data_labels = one_batch_dataprint("images [N, W, H, C]: ",one_batch_data_images.shape)
print("labels: ", one_batcg_data_labels)
images [N, W, H, C]:  (16, 336, 336, 3)
labels:  tf.Tensor([13 11 15  4  5  3 13  5 15  7  3  5  9 16 13  1], shape=(16,), dtype=int32)

3、展示一部分数据

import matplotlib.pyplot as plt plt.figure(figsize=(20, 10))for i in range(10):plt.subplot(5, 10, i + 1)plt.imshow(one_batch_data_images[i].numpy().astype('uint8'))plt.title(personClassNames[one_batcg_data_labels[i]])plt.axis('off')plt.show()


在这里插入图片描述

4、数据归一化和内存加速

AUTOTUNE = tf.data.AUTOTUNEdef train_preprocessing(image,label):return (image/255.0,label)train_ds = (train_ds.cache().shuffle(1000).map(train_preprocessing)    .prefetch(buffer_size=AUTOTUNE)
)val_ds = (val_ds.cache().shuffle(1000).map(train_preprocessing)    .prefetch(buffer_size=AUTOTUNE)
)

5、数据增强

这里对图像进行旋转

data_augmental = tf.keras.Sequential([tf.keras.layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical"),tf.keras.layers.experimental.preprocessing.RandomRotation(0.2)
])

随机取一张图片看效果

# 取归一化后数据temp_data = next(iter(train_ds))temp_data_images, _ = temp_datatest_image = tf.expand_dims(temp_data_images[i], 0)# 随机旋转9张
plt.figure(figsize=(8, 8))
for i in range(9):data_strength = data_augmental(test_image)plt.subplot(3, 3, i + 1)plt.imshow(data_strength[0])plt.axis("off")


在这里插入图片描述

6、数据整合

对训练集进行数据增强

AUTOTUNE = tf.data.AUTOTUNEdef prepare(ds):ds = ds.map(lambda x, y : (data_augmental(x), y), num_parallel_calls=AUTOTUNE)return ds train_ds = prepare(train_ds)

2、模型构建和实验

这里采用VGG16模型,优化器默认使用“adam”,本次实验主要对比“adam”和“sgd", 池化层:采用平均池化
修改: 全连接层大幅度降低计算量

from tensorflow.keras.applications import VGG16
from tensorflow.keras.layers import Dense, Flatten, Dropout, BatchNormalization
from tensorflow.keras.models import Modeldef create_model(optimizer='adam'):# VGG16模型vgg16 = VGG16(include_top=False, weights='imagenet', input_shape=(image_width, image_height, 3), pooling='avg')# 冻结权重for layer in vgg16.layers:layer.trainable = False# 获取卷积输出x = vgg16.output# 展开x = Flatten()(x)x = Dense(170, activation='relu')(x)  # 修改全连接层x = BatchNormalization()(x)x = Dropout(0.5)(x)# 分类输出output = Dense(len(personClassNames), activation='softmax')(x)# 模型整合vgg16_model = Model(inputs=vgg16.input, outputs=output)# 超参数设计vgg16_model.compile(optimizer=optimizer,loss='sparse_categorical_crossentropy',metrics=['accuracy'])return vgg16_model

实验:分别检验SGD和Aadm优化器的效果

model1 = create_model(optimizer=tf.keras.optimizers.Adam())
model2 = create_model(optimizer=tf.keras.optimizers.SGD())# 输出一个结构展示
model1.summary()
Model: "model"
_________________________________________________________________Layer (type)                Output Shape              Param #   
=================================================================input_1 (InputLayer)        [(None, 336, 336, 3)]     0         block1_conv1 (Conv2D)       (None, 336, 336, 64)      1792      block1_conv2 (Conv2D)       (None, 336, 336, 64)      36928     block1_pool (MaxPooling2D)  (None, 168, 168, 64)      0         block2_conv1 (Conv2D)       (None, 168, 168, 128)     73856     block2_conv2 (Conv2D)       (None, 168, 168, 128)     147584    block2_pool (MaxPooling2D)  (None, 84, 84, 128)       0         block3_conv1 (Conv2D)       (None, 84, 84, 256)       295168    block3_conv2 (Conv2D)       (None, 84, 84, 256)       590080    block3_conv3 (Conv2D)       (None, 84, 84, 256)       590080    block3_pool (MaxPooling2D)  (None, 42, 42, 256)       0         block4_conv1 (Conv2D)       (None, 42, 42, 512)       1180160   block4_conv2 (Conv2D)       (None, 42, 42, 512)       2359808   block4_conv3 (Conv2D)       (None, 42, 42, 512)       2359808   block4_pool (MaxPooling2D)  (None, 21, 21, 512)       0         block5_conv1 (Conv2D)       (None, 21, 21, 512)       2359808   block5_conv2 (Conv2D)       (None, 21, 21, 512)       2359808   block5_conv3 (Conv2D)       (None, 21, 21, 512)       2359808   block5_pool (MaxPooling2D)  (None, 10, 10, 512)       0         global_average_pooling2d (G  (None, 512)              0         lobalAveragePooling2D)                                          flatten (Flatten)           (None, 512)               0         dense (Dense)               (None, 170)               87210     batch_normalization (BatchN  (None, 170)              680       ormalization)                                                   dropout (Dropout)           (None, 170)               0         dense_1 (Dense)             (None, 17)                2907      =================================================================
Total params: 14,805,485
Trainable params: 90,457
Non-trainable params: 14,715,028
_________________________________________________________________

3、模型训练与实验验证

epochs = 50history_model1 = model1.fit(train_ds,validation_data=val_ds,verbose=1,epochs=epochs
)history_model2 = model2.fit(train_ds,validation_data=val_ds,verbose=1,epochs=epochs
)
Epoch 1/50
2024-11-29 20:04:09.371628: I tensorflow/stream_executor/cuda/cuda_dnn.cc:384] Loaded cuDNN version 8101
 5/90 [>.............................] - ETA: 3s - loss: 3.6247 - accuracy: 0.0500    2024-11-29 20:04:11.269447: I tensorflow/stream_executor/cuda/cuda_blas.cc:1786] TensorFloat-32 will be used for the matrix multiplication. This will only be logged once.
90/90 [==============================] - 11s 65ms/step - loss: 2.9974 - accuracy: 0.1243 - val_loss: 2.7417 - val_accuracy: 0.0889
Epoch 2/50
90/90 [==============================] - 7s 65ms/step - loss: 2.4747 - accuracy: 0.2313 - val_loss: 2.5382 - val_accuracy: 0.1611
Epoch 3/50
90/90 [==============================] - 8s 68ms/step - loss: 2.1940 - accuracy: 0.2771 - val_loss: 2.3752 - val_accuracy: 0.2222
Epoch 4/50
90/90 [==============================] - 9s 62ms/step - loss: 2.0498 - accuracy: 0.3292 - val_loss: 2.1286 - val_accuracy: 0.3194
Epoch 5/50
90/90 [==============================] - 7s 61ms/step - loss: 1.9032 - accuracy: 0.3688 - val_loss: 2.0959 - val_accuracy: 0.2500
Epoch 6/50
90/90 [==============================] - 7s 64ms/step - loss: 1.8406 - accuracy: 0.3958 - val_loss: 1.8965 - val_accuracy: 0.3361
Epoch 7/50
90/90 [==============================] - 7s 60ms/step - loss: 1.7796 - accuracy: 0.4160 - val_loss: 1.8721 - val_accuracy: 0.3444
Epoch 8/50
90/90 [==============================] - 7s 67ms/step - loss: 1.6878 - accuracy: 0.4417 - val_loss: 1.8793 - val_accuracy: 0.3694
Epoch 9/50
90/90 [==============================] - 7s 67ms/step - loss: 1.5962 - accuracy: 0.4806 - val_loss: 2.1096 - val_accuracy: 0.3778
Epoch 10/50
90/90 [==============================] - 8s 65ms/step - loss: 1.5721 - accuracy: 0.4736 - val_loss: 1.8630 - val_accuracy: 0.3639
Epoch 11/50
90/90 [==============================] - 7s 67ms/step - loss: 1.5559 - accuracy: 0.4771 - val_loss: 1.8641 - val_accuracy: 0.3667
Epoch 12/50
90/90 [==============================] - 8s 59ms/step - loss: 1.5054 - accuracy: 0.4993 - val_loss: 1.6805 - val_accuracy: 0.4222
Epoch 13/50
90/90 [==============================] - 7s 62ms/step - loss: 1.4850 - accuracy: 0.5153 - val_loss: 1.7722 - val_accuracy: 0.4139
Epoch 14/50
90/90 [==============================] - 7s 64ms/step - loss: 1.4857 - accuracy: 0.5014 - val_loss: 1.9245 - val_accuracy: 0.4000
Epoch 15/50
90/90 [==============================] - 8s 63ms/step - loss: 1.4274 - accuracy: 0.5299 - val_loss: 1.6763 - val_accuracy: 0.4306
Epoch 16/50
90/90 [==============================] - 7s 58ms/step - loss: 1.4398 - accuracy: 0.5347 - val_loss: 1.8342 - val_accuracy: 0.4139
Epoch 17/50
90/90 [==============================] - 7s 68ms/step - loss: 1.3925 - accuracy: 0.5514 - val_loss: 1.9606 - val_accuracy: 0.3722
Epoch 18/50
90/90 [==============================] - 7s 64ms/step - loss: 1.3788 - accuracy: 0.5576 - val_loss: 1.9656 - val_accuracy: 0.4056
Epoch 19/50
90/90 [==============================] - 7s 66ms/step - loss: 1.3547 - accuracy: 0.5507 - val_loss: 1.7001 - val_accuracy: 0.4472
Epoch 20/50
90/90 [==============================] - 7s 63ms/step - loss: 1.3617 - accuracy: 0.5299 - val_loss: 1.7058 - val_accuracy: 0.4139
Epoch 21/50
90/90 [==============================] - 8s 61ms/step - loss: 1.3571 - accuracy: 0.5500 - val_loss: 1.7327 - val_accuracy: 0.4111
Epoch 22/50
90/90 [==============================] - 7s 64ms/step - loss: 1.3489 - accuracy: 0.5562 - val_loss: 1.8420 - val_accuracy: 0.4139
Epoch 23/50
90/90 [==============================] - 7s 71ms/step - loss: 1.3550 - accuracy: 0.5444 - val_loss: 1.5965 - val_accuracy: 0.4944
Epoch 24/50
90/90 [==============================] - 7s 63ms/step - loss: 1.3284 - accuracy: 0.5597 - val_loss: 1.7336 - val_accuracy: 0.3944
Epoch 25/50
90/90 [==============================] - 7s 66ms/step - loss: 1.3051 - accuracy: 0.5604 - val_loss: 2.1655 - val_accuracy: 0.3333
Epoch 26/50
90/90 [==============================] - 7s 66ms/step - loss: 1.2735 - accuracy: 0.5813 - val_loss: 1.8967 - val_accuracy: 0.3972
Epoch 27/50
90/90 [==============================] - 8s 58ms/step - loss: 1.2642 - accuracy: 0.5813 - val_loss: 1.8246 - val_accuracy: 0.4556
Epoch 28/50
90/90 [==============================] - 7s 63ms/step - loss: 1.2473 - accuracy: 0.5778 - val_loss: 1.8763 - val_accuracy: 0.4167
Epoch 29/50
90/90 [==============================] - 7s 61ms/step - loss: 1.2544 - accuracy: 0.5868 - val_loss: 1.8783 - val_accuracy: 0.4278
Epoch 30/50
90/90 [==============================] - 7s 62ms/step - loss: 1.2677 - accuracy: 0.5958 - val_loss: 1.9555 - val_accuracy: 0.3667
Epoch 31/50
90/90 [==============================] - 7s 63ms/step - loss: 1.2191 - accuracy: 0.5847 - val_loss: 2.1074 - val_accuracy: 0.3333
Epoch 32/50
90/90 [==============================] - 7s 64ms/step - loss: 1.1922 - accuracy: 0.5903 - val_loss: 1.8136 - val_accuracy: 0.4111
Epoch 33/50
90/90 [==============================] - 7s 71ms/step - loss: 1.2122 - accuracy: 0.5826 - val_loss: 1.8247 - val_accuracy: 0.4417
Epoch 34/50
90/90 [==============================] - 7s 65ms/step - loss: 1.1953 - accuracy: 0.5944 - val_loss: 1.7219 - val_accuracy: 0.4861
Epoch 35/50
90/90 [==============================] - 7s 59ms/step - loss: 1.2409 - accuracy: 0.5750 - val_loss: 2.0469 - val_accuracy: 0.4028
Epoch 36/50
90/90 [==============================] - 7s 64ms/step - loss: 1.1802 - accuracy: 0.5972 - val_loss: 1.7781 - val_accuracy: 0.4000
Epoch 37/50
90/90 [==============================] - 7s 65ms/step - loss: 1.2651 - accuracy: 0.5715 - val_loss: 1.7563 - val_accuracy: 0.4278
Epoch 38/50
90/90 [==============================] - 7s 62ms/step - loss: 1.1964 - accuracy: 0.5806 - val_loss: 1.7468 - val_accuracy: 0.4778
Epoch 39/50
90/90 [==============================] - 7s 61ms/step - loss: 1.1818 - accuracy: 0.6118 - val_loss: 1.5965 - val_accuracy: 0.4917
Epoch 40/50
90/90 [==============================] - 7s 69ms/step - loss: 1.2117 - accuracy: 0.5868 - val_loss: 2.2473 - val_accuracy: 0.3306
Epoch 41/50
90/90 [==============================] - 7s 66ms/step - loss: 1.1878 - accuracy: 0.6014 - val_loss: 1.6355 - val_accuracy: 0.4944
Epoch 42/50
90/90 [==============================] - 7s 60ms/step - loss: 1.1425 - accuracy: 0.6104 - val_loss: 2.2436 - val_accuracy: 0.3472
Epoch 43/50
90/90 [==============================] - 7s 65ms/step - loss: 1.1815 - accuracy: 0.5979 - val_loss: 1.7965 - val_accuracy: 0.4667
Epoch 44/50
90/90 [==============================] - 7s 63ms/step - loss: 1.1452 - accuracy: 0.6104 - val_loss: 1.8330 - val_accuracy: 0.4500
Epoch 45/50
90/90 [==============================] - 7s 63ms/step - loss: 1.1664 - accuracy: 0.5972 - val_loss: 1.8414 - val_accuracy: 0.4306
Epoch 46/50
90/90 [==============================] - 7s 59ms/step - loss: 1.1557 - accuracy: 0.6153 - val_loss: 2.9897 - val_accuracy: 0.3333
Epoch 47/50
90/90 [==============================] - 7s 72ms/step - loss: 1.1534 - accuracy: 0.6167 - val_loss: 1.9379 - val_accuracy: 0.4222
Epoch 48/50
90/90 [==============================] - 7s 61ms/step - loss: 1.1549 - accuracy: 0.6229 - val_loss: 1.6648 - val_accuracy: 0.4667
Epoch 49/50
90/90 [==============================] - 7s 59ms/step - loss: 1.1369 - accuracy: 0.6319 - val_loss: 1.7084 - val_accuracy: 0.4833
Epoch 50/50
90/90 [==============================] - 7s 60ms/step - loss: 1.1110 - accuracy: 0.6313 - val_loss: 1.7481 - val_accuracy: 0.4639
Epoch 1/50
90/90 [==============================] - 8s 60ms/step - loss: 3.1046 - accuracy: 0.0993 - val_loss: 2.8330 - val_accuracy: 0.0444
Epoch 2/50
90/90 [==============================] - 7s 61ms/step - loss: 2.7295 - accuracy: 0.1694 - val_loss: 2.6644 - val_accuracy: 0.1694
Epoch 3/50
90/90 [==============================] - 7s 62ms/step - loss: 2.5302 - accuracy: 0.2174 - val_loss: 2.5356 - val_accuracy: 0.1944
Epoch 4/50
90/90 [==============================] - 7s 62ms/step - loss: 2.3906 - accuracy: 0.2333 - val_loss: 2.3604 - val_accuracy: 0.2333
Epoch 5/50
90/90 [==============================] - 7s 68ms/step - loss: 2.2756 - accuracy: 0.2632 - val_loss: 2.3378 - val_accuracy: 0.2167
Epoch 6/50
90/90 [==============================] - 7s 60ms/step - loss: 2.1989 - accuracy: 0.2861 - val_loss: 2.2364 - val_accuracy: 0.2639
Epoch 7/50
90/90 [==============================] - 7s 63ms/step - loss: 2.1255 - accuracy: 0.3187 - val_loss: 2.1736 - val_accuracy: 0.3111
Epoch 8/50
90/90 [==============================] - 7s 64ms/step - loss: 2.1089 - accuracy: 0.3076 - val_loss: 1.9729 - val_accuracy: 0.3583
Epoch 9/50
90/90 [==============================] - 7s 69ms/step - loss: 2.0349 - accuracy: 0.3271 - val_loss: 1.9062 - val_accuracy: 0.3833
Epoch 10/50
90/90 [==============================] - 7s 66ms/step - loss: 1.9601 - accuracy: 0.3479 - val_loss: 1.8394 - val_accuracy: 0.4056
Epoch 11/50
90/90 [==============================] - 7s 65ms/step - loss: 1.9124 - accuracy: 0.3646 - val_loss: 1.8802 - val_accuracy: 0.3833
Epoch 12/50
90/90 [==============================] - 7s 66ms/step - loss: 1.8987 - accuracy: 0.3757 - val_loss: 1.8779 - val_accuracy: 0.4083
Epoch 13/50
90/90 [==============================] - 7s 61ms/step - loss: 1.8571 - accuracy: 0.3993 - val_loss: 1.7853 - val_accuracy: 0.4000
Epoch 14/50
90/90 [==============================] - 7s 63ms/step - loss: 1.7928 - accuracy: 0.4174 - val_loss: 1.8882 - val_accuracy: 0.4000
Epoch 15/50
90/90 [==============================] - 7s 63ms/step - loss: 1.7943 - accuracy: 0.4167 - val_loss: 1.8541 - val_accuracy: 0.3917
Epoch 16/50
90/90 [==============================] - 7s 59ms/step - loss: 1.7808 - accuracy: 0.4062 - val_loss: 1.8767 - val_accuracy: 0.3722
Epoch 17/50
90/90 [==============================] - 7s 71ms/step - loss: 1.7786 - accuracy: 0.4174 - val_loss: 1.7747 - val_accuracy: 0.3944
Epoch 18/50
90/90 [==============================] - 7s 67ms/step - loss: 1.7188 - accuracy: 0.4264 - val_loss: 1.7444 - val_accuracy: 0.4306
Epoch 19/50
90/90 [==============================] - 7s 63ms/step - loss: 1.7334 - accuracy: 0.4264 - val_loss: 1.9065 - val_accuracy: 0.3806
Epoch 20/50
90/90 [==============================] - 7s 67ms/step - loss: 1.6817 - accuracy: 0.4583 - val_loss: 1.7351 - val_accuracy: 0.4028
Epoch 21/50
90/90 [==============================] - 7s 68ms/step - loss: 1.6734 - accuracy: 0.4493 - val_loss: 1.7435 - val_accuracy: 0.4250
Epoch 22/50
90/90 [==============================] - 7s 67ms/step - loss: 1.6751 - accuracy: 0.4313 - val_loss: 1.8199 - val_accuracy: 0.3667
Epoch 23/50
90/90 [==============================] - 7s 59ms/step - loss: 1.6424 - accuracy: 0.4431 - val_loss: 1.7815 - val_accuracy: 0.3833
Epoch 24/50
90/90 [==============================] - 7s 64ms/step - loss: 1.6310 - accuracy: 0.4611 - val_loss: 2.1978 - val_accuracy: 0.3194
Epoch 25/50
90/90 [==============================] - 7s 58ms/step - loss: 1.6379 - accuracy: 0.4500 - val_loss: 1.7472 - val_accuracy: 0.4111
Epoch 26/50
90/90 [==============================] - 7s 66ms/step - loss: 1.5982 - accuracy: 0.4549 - val_loss: 1.7060 - val_accuracy: 0.4250
Epoch 27/50
90/90 [==============================] - 7s 64ms/step - loss: 1.6151 - accuracy: 0.4479 - val_loss: 1.7618 - val_accuracy: 0.3833
Epoch 28/50
90/90 [==============================] - 7s 67ms/step - loss: 1.5639 - accuracy: 0.4667 - val_loss: 1.7085 - val_accuracy: 0.4278
Epoch 29/50
90/90 [==============================] - 7s 68ms/step - loss: 1.5825 - accuracy: 0.4660 - val_loss: 1.7090 - val_accuracy: 0.4417
Epoch 30/50
90/90 [==============================] - 7s 59ms/step - loss: 1.5632 - accuracy: 0.4660 - val_loss: 1.6777 - val_accuracy: 0.4278
Epoch 31/50
90/90 [==============================] - 7s 59ms/step - loss: 1.5528 - accuracy: 0.4840 - val_loss: 1.7472 - val_accuracy: 0.4250
Epoch 32/50
90/90 [==============================] - 7s 59ms/step - loss: 1.5206 - accuracy: 0.4826 - val_loss: 1.6585 - val_accuracy: 0.4361
Epoch 33/50
90/90 [==============================] - 7s 64ms/step - loss: 1.5087 - accuracy: 0.5069 - val_loss: 1.8085 - val_accuracy: 0.3861
Epoch 34/50
90/90 [==============================] - 7s 62ms/step - loss: 1.4967 - accuracy: 0.4917 - val_loss: 1.7083 - val_accuracy: 0.4222
Epoch 35/50
90/90 [==============================] - 7s 64ms/step - loss: 1.5370 - accuracy: 0.4736 - val_loss: 1.8167 - val_accuracy: 0.3806
Epoch 36/50
90/90 [==============================] - 7s 63ms/step - loss: 1.5184 - accuracy: 0.4951 - val_loss: 1.7889 - val_accuracy: 0.4139
Epoch 37/50
90/90 [==============================] - 7s 61ms/step - loss: 1.4898 - accuracy: 0.5069 - val_loss: 1.7182 - val_accuracy: 0.4111
Epoch 38/50
90/90 [==============================] - 7s 59ms/step - loss: 1.4586 - accuracy: 0.5139 - val_loss: 1.7317 - val_accuracy: 0.4083
Epoch 39/50
90/90 [==============================] - 7s 66ms/step - loss: 1.4991 - accuracy: 0.4986 - val_loss: 1.8129 - val_accuracy: 0.3917
Epoch 40/50
90/90 [==============================] - 7s 62ms/step - loss: 1.4643 - accuracy: 0.5146 - val_loss: 1.7422 - val_accuracy: 0.4194
Epoch 41/50
90/90 [==============================] - 7s 61ms/step - loss: 1.4585 - accuracy: 0.5132 - val_loss: 1.6986 - val_accuracy: 0.4389
Epoch 42/50
90/90 [==============================] - 7s 63ms/step - loss: 1.4478 - accuracy: 0.5174 - val_loss: 1.6919 - val_accuracy: 0.4556
Epoch 43/50
90/90 [==============================] - 7s 58ms/step - loss: 1.4544 - accuracy: 0.5104 - val_loss: 1.6879 - val_accuracy: 0.4472
Epoch 44/50
90/90 [==============================] - 7s 58ms/step - loss: 1.4157 - accuracy: 0.5146 - val_loss: 1.6645 - val_accuracy: 0.4278
Epoch 45/50
90/90 [==============================] - 7s 63ms/step - loss: 1.4005 - accuracy: 0.5292 - val_loss: 1.7376 - val_accuracy: 0.4556
Epoch 46/50
90/90 [==============================] - 7s 67ms/step - loss: 1.4008 - accuracy: 0.5250 - val_loss: 1.7319 - val_accuracy: 0.4222
Epoch 47/50
90/90 [==============================] - 7s 59ms/step - loss: 1.4031 - accuracy: 0.5271 - val_loss: 1.7014 - val_accuracy: 0.4389
Epoch 48/50
90/90 [==============================] - 7s 67ms/step - loss: 1.3762 - accuracy: 0.5347 - val_loss: 1.7294 - val_accuracy: 0.4222
Epoch 49/50
90/90 [==============================] - 7s 60ms/step - loss: 1.4056 - accuracy: 0.5188 - val_loss: 1.7143 - val_accuracy: 0.4417
Epoch 50/50
90/90 [==============================] - 7s 64ms/step - loss: 1.4376 - accuracy: 0.5312 - val_loss: 1.7685 - val_accuracy: 0.4417

4、结果检验

from matplotlib.ticker import MultipleLocator
plt.rcParams['savefig.dpi'] = 300 #图片像素
plt.rcParams['figure.dpi']  = 300 #分辨率acc1     = history_model1.history['accuracy']
acc2     = history_model2.history['accuracy']
val_acc1 = history_model1.history['val_accuracy']
val_acc2 = history_model2.history['val_accuracy']loss1     = history_model1.history['loss']
loss2     = history_model2.history['loss']
val_loss1 = history_model1.history['val_loss']
val_loss2 = history_model2.history['val_loss']epochs_range = range(len(acc1))plt.figure(figsize=(16, 4))
plt.subplot(1, 2, 1)plt.plot(epochs_range, acc1, label='Training Accuracy-Adam')
plt.plot(epochs_range, acc2, label='Training Accuracy-SGD')
plt.plot(epochs_range, val_acc1, label='Validation Accuracy-Adam')
plt.plot(epochs_range, val_acc2, label='Validation Accuracy-SGD')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
# 设置刻度间隔,x轴每1一个刻度
ax = plt.gca()
ax.xaxis.set_major_locator(MultipleLocator(1))plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss1, label='Training Loss-Adam')
plt.plot(epochs_range, loss2, label='Training Loss-SGD')
plt.plot(epochs_range, val_loss1, label='Validation Loss-Adam')
plt.plot(epochs_range, val_loss2, label='Validation Loss-SGD')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')# 设置刻度间隔,x轴每1一个刻度
ax = plt.gca()
ax.xaxis.set_major_locator(MultipleLocator(1))plt.show()


在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/887871.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

新型大语言模型的预训练与后训练范式,谷歌的Gemma 2语言模型

前言:大型语言模型(LLMs)的发展历程可以说是非常长,从早期的GPT模型一路走到了今天这些复杂的、公开权重的大型语言模型。最初,LLM的训练过程只关注预训练,但后来逐步扩展到了包括预训练和后训练在内的完整…

SQL Server管理员sa登录失败原因

文章目录 一、开启混合登录模式二、启用sa三、更改密码四、登录sa一、开启混合登录模式 用Windows身份登录数据库服务。 在连接名上右键→属性。 在安全性选项卡下,选择【SQL Server和Windows身份验证模式】,点击【确定】,提示需要重启服务。 Win+R,输入指令:services.ms…

logminer挖掘日志归档查找问题

--根据发生问题时间点查找归档文件 select first_time,NAME from gv$archived_log where first_time>2016-03-15 17:00:00 and first_time<2016-03-15 21:00:00; 2016-03-15 17:23:55 ARCH/jxdb/archivelog/2016_03_15/thread_1_seq_41588.4060.906577337 2016-03-15 17:…

如何监控Elasticsearch集群状态?

大家好&#xff0c;我是锋哥。今天分享关于【如何监控Elasticsearch集群状态&#xff1f;】面试题。希望对大家有帮助&#xff1b; 如何监控Elasticsearch集群状态&#xff1f; 1000道 互联网大厂Java工程师 精选面试题-Java资源分享网 监控 Elasticsearch 集群的状态对于确保…

C# 集合(Collection)

文章目录 前言一、动态数组&#xff08;ArrayList&#xff09;二、哈希表&#xff08;Hashtable&#xff09;三、排序列表&#xff08;SortedList&#xff09;四、堆栈&#xff08;Stack&#xff09;五、队列&#xff08;Queue&#xff09;六、点阵列&#xff08;BitArray&…

浏览器的数据六种存储方法比较 :LocalStorage vs. IndexedDB vs. Cookies vs. OPFS vs. WASM-SQLite

在构建该 Web 应用程序&#xff0c;并且希望将数据存储在用户浏览器中。也许您只需要存储一些小标志&#xff0c;或者甚至需要一个成熟的数据库。 我们构建的 Web 应用程序类型发生了显着变化。在网络发展的早期&#xff0c;我们提供静态 html 文件。然后我们提供动态渲染的 h…

蓝桥杯备赛笔记(一)

这里的笔记是关于蓝桥杯关键知识点的记录&#xff0c;有别于基础语法&#xff0c;很多内容只要求会用就行&#xff0c;无需深入掌握。 文章目录 前言一、编程基础1.1 C基础格式和版本选择1.2 输入输出cin和cout&#xff1a; 1.3 string以下是字符串的一些简介&#xff1a;字符串…

【Redis】Redis介绍

目录 1.Redis是什么? 2. Redis特性 2.1 速度快 2.2 基于键值对的数据结构服务器 2.3 丰富的功能 2.4 简单稳定 2.5 客户端语言多 2.6 持久化 2.7 主从复制 2.8 高可用和分布式 3. Redis使用场景 3.1 缓存(Cache) 3.2 排行榜系统 3.3 计数器应用 3.4 社交网络 …

Redis使用场景-缓存-缓存穿透

前言 之前在针对实习面试的博文中讲到Redis在实际开发中的生产问题&#xff0c;其中缓存穿透、击穿、雪崩在面试中问的最频繁&#xff0c;本文加了图解&#xff0c;希望帮助你更直观的了解缓存穿透&#x1f600; &#xff08;放出之前写的针对实习面试的关于Redis生产问题的博…

STM32 ADC --- 多通道序列采样

STM32 ADC — 序列通道采样 文章目录 STM32 ADC --- 序列通道采样CubeMX配置代码编写总结 CubeMX配置 使用cubeMX生成HAL工程 扫描模式在配置多个通道时会自动变为Enable&#xff0c;这里需要注意的是需要将连续转换模式配置为Disable&#xff0c;将结束转换选择选择为每个通道…

C语言数据结构——详细讲解《队列》

C语言数据结构——详细讲解《队列》 前言一、队列的概念二、队列的操作&#xff08;一&#xff09;定义队列结构&#xff08;二&#xff09;初始化队列&#xff08;三&#xff09;入队列操作&#xff08;四&#xff09;出队列操作&#xff08;五&#xff09;获取队头元素&#…

【游资悟道】-作手新一悟道心法

作手新一经典语录节选&#xff1a; 乔帮主传完整版&#xff1a;做股票5年&#xff0c;炼成18式&#xff0c;成为A股低吸大神&#xff01;从小白到大神&#xff0c;散户炒股的六个过程&#xff0c;不看不知道自己水平 围着主线做&#xff0c;多研究龙头&#xff0c;研究涨停&am…

2025年入职/转行网络安全,该如何规划?网络安全职业规划

网络安全是一个日益增长的行业&#xff0c;对于打算进入或转行进入该领域的人来说&#xff0c;制定一个清晰且系统的职业规划非常重要。2025年&#xff0c;网络安全领域将继续发展并面临新的挑战&#xff0c;包括不断变化的技术、法规要求以及日益复杂的威胁环境。以下是一个关…

使用pymupdf提取PDF文档中的文字和其颜色

最近我在捣鼓一个PDF文件&#xff0c;想把它里面的文字和文字颜色给提取出来。后来发现有个叫pymupdf的库能搞定这事儿。操作起来挺简单的&#xff0c;pymupdf的示例文档里就有现成的代码可以参考。 how-to-extract-text-with-color 我本地的测试代码如下&#xff1a; impor…

STM32 + CubeMX + 串口 + IAP升级

这篇文章分享一个简单的串口IAP Demo&#xff0c;实现使用串口更新我们自己的App程序。 目录 一、IAP简介二、Stm32CubeMx配置三、Boot代码及配置1、代码2、配置 四、App代码及配置1、代码2、配置 五、效果展示 一、IAP简介 IAP介绍可以在网上找找&#xff0c;相关资料很多&am…

A051-基于Spring Boot的网络海鲜市场系统的设计与实现

&#x1f64a;作者简介&#xff1a;在校研究生&#xff0c;拥有计算机专业的研究生开发团队&#xff0c;分享技术代码帮助学生学习&#xff0c;独立完成自己的网站项目。 代码可以查看文章末尾⬇️联系方式获取&#xff0c;记得注明来意哦~&#x1f339; 赠送计算机毕业设计600…

《String类》

目录 一、定义与概述 二、创建字符串对象 2.1 直接赋值 2.2 使用构造函数 三、字符串的不可变性 四、常用方法 4.1 String对象的比较 4.1.1 比较是否引用同一个对象 4.1.2 boolean equals(Object anObject)方法&#xff1a;按照字典序比较 4.1.3 int compareTo(Strin…

探讨播客的生态系统

最近对播客发生了兴趣&#xff0c;从而引起了对播客背后的技术&#xff0c;生态的关注。本文谈谈播客背后的技术生态系统。 播客很简单 播客&#xff08;podcast&#xff09;本质上就是以语音的方式发布信息。它和博客非常类似。如果将CSDN 网站上的文字加一个语音播报。CSDN …

@bytemd/vue掘金markdown插件预览内容有误

vue项目使用bytemd/vue 来预览字符串格式的markdown内容&#xff0c;总会多出如图的一段代码&#xff0c; 请问有没有大佬知道为什么&#xff1f; 很急&#xff0c;求教&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;

windows下编译IEC 61850依赖库

windows下编译IEC 61850依赖库 0、引言1、环境准备2、源码下载3、下载WpdPack4、生成vs解决方案5、在VS上对解决方案进行编译 0、引言 最近刚好在学习IEC 61850的一些标准规范&#xff0c;主要包含了两大块协议&#xff1a;MMS和GOOSE。61850是一个非常强大的协议&#xff0c;…