根据个人经验总结的深度学习入门路线(简单快速)
https://blog.csdn.net/weixin_44414948/article/details/109704871
深度学习入门二阶段demo练习:
https://blog.csdn.net/weixin_44414948/article/details/110673660
Demo任务:
使用深度学习框架(tensorflow2.0及以上、pytorch1.0及以上版本)搭建VGG、inception、resnet网络。
数据集:mnist,准确率高于90%。
追加要求:训练过程loss曲线图(一个画布同时画出3个网络的loss曲线),训练过程acc曲线图(一个画布同时画出3个网络的acc曲线)。
示例代码:
VGG实现部分(history_VGG变量会用于后面的曲线联合绘制)
import tensorflow as tf
from tensorflow.keras import layers
import numpy as np
#加载mnist数据集
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
#预处理
x_train, x_test = x_train.astype(np.float32)/255., x_test.astype(np.float32)/255.
x_train, x_test = np.expand_dims(x_train, axis=3), np.expand_dims(x_test, axis=3)
# 创建训练集50000、验证集10000以及测试集10000
x_val = x_train[-10000:]
y_val = y_train[-10000:]
x_train = x_train[:-10000]
y_train = y_train[:-10000]
#标签转为one-hot格式
y_train = tf.one_hot(y_train, depth=10).numpy()
y_val = tf.one_hot(y_val, depth=10).numpy()
y_test = tf.one_hot(y_test, depth=10).numpy()
# tf.data.Dataset 批处理
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(100).repeat()
val_dataset = tf.data.Dataset.from_tensor_slices((x_val, y_val)).batch(100).repeat()
test_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(100).repeat()
from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics,regularizers
from tensorflow import keras
input_shape = (28, 28, 1)
weight_decay = 0.001
num_classes = 10
model = tf.keras.Sequential()
model.add(layers.Conv2D(64, (3, 3), padding='same',
input_shape=input_shape, kernel_regularizer=regularizers.l2(weight_decay)))
model.add(layers.Activation('relu'))
model.add(layers.BatchNormalization())
model.add(layers.Dropout(0.3))
model.add(layers.Conv2D(64, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
model.add(layers.Activation('relu'))
model.add(layers.BatchNormalization())
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Conv2D(128, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
model.add(layers.Activation('relu'))
model.add(layers.BatchNormalization())
model.add(layers.Dropout(0.4))
model.add(layers.Conv2D(128, (3, 3), padding='same',kernel_regularizer=regularizers.l2(weight_decay)))
model.add(layers.Activation('relu'))
model.add(layers.BatchNormalization())
model.add(layers.MaxPooling2D(pool_size=(2, 2)))
model.add(layers.Conv2D(256, (3, 3)