第三章 搭建网络八股
六步法
六步之第三步
Model = tf.keras.models.Sequential([网络结构])
描述各层网络,可以将sequential看成一个容器,这个容器里封装一个神经网络结构,在sequential中要描述从输入层到输出层的每一层网络结构。
举例:
拉直层:tf.keras.layers.Flatten()
ps:不含计算,只把输入特征拉直为一维数组
全连接层:tf.keras.layers.Dense(神经元个数,activation=”激活函数”,kernel_regularizer=那种正则化方法)
activation可选:
- relu
- softmax
- sigmoid
- tanh
Kernel_regularizer可选:
- tf.keras.regularizers.l1()
- tf.keras.regularizers.l2()
卷积层:tf.keras.layers.Conv2D(filter=卷积核个数,kernel_size=卷积核尺寸,strides=卷积步长,padding=”valid”|”same”)
LSTM层:tf.keras.layers_LSTM()
六步之第四步
Model.compile(optimizer=优化器,loss=损失函数,metrics=[“准确率”])
Optimizer可选:
- ‘sgd’ | tf.keras.optimizers.SGD(lr=学习率,momentum=动量参数)
- ‘adagrad’ | tf.keras.optimizers.Adagrad(lr=学习率)
- ‘adadelta’ | tf.keras.optimizers.Adadelta(lr=学习率)
- ‘adam’ | tf.keras.optimizers.Adam(lr=学习率,beta_1=0.9,beta_2=0.999)
Loss可选:
- ‘mse’ | tf.keras.losses.MeanSquaredError()
- ‘sparse_categorical_crossentropy’ | tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)
Metrics可选:
- ‘accuracy’:y_,y都是数值,如y_=[1] y=[1]
- ‘categorical_accuracy’:y_,y都是独热码(概率分布),如y_=[0,1,0] y=[0.256,0.695,0.048]
- ‘sparse_categorical_accuracy’:y_是数值,y是独热码(概率分布),如y_=[1] y=[0.256,0.695,0.048]
六步之第五步
model.fit(训练集得输入特征,训练集的标签,batch_size=,epochs=,
validation_data=(测试集输入特征,测试集标签),
validation_split=从训练集划分多少比例给测试集,
validation_freq=多少次epoch测试一次)
六步之第六步
model.summary()
打印输出训练数、精度等相关信息
实例1:识别鸢尾花
import numpy as np
# load_iris 为sklearn内嵌函数(读取数据)
from sklearn.datasets import load_iris
import tensorflow as tf
# 读取花的数据
x_data = load_iris().data
# 读取数据结果
y_data = load_iris().target
# 数据乱序,打乱数据
np.random.seed(116)
np.random.shuffle(x_data)
np.random.seed(116)
np.random.shuffle(y_data)
np.random.seed(116)
# 搭建神经网络结构,model容器,softmax激活函数,L2正则化
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(3,activation='softmax',kernel_regularizer=tf.keras.regularizers.l2())
])
# SGD优化函数,交叉熵代价函数,y_为数值,y为one-hot
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
# 训练model,batch_size为32,迭代次数为500,测试集占比20%,测试频率为20次epoch测一次
model.fit(x_data,y_data,batch_size=32,epochs=500,validation_split=0.2,validation_freq=20)
model.summary() # 打印输出结果
实例2:MNIST数据集
提供6w张28*28像素点的0-9手写数字图片和标签,用于训练
提供1w张28*28像素点的0-9手写数字图片和标签,用于测试
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_train,x_test = x_train/255.0,x_test/255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128,activation='relu'),
tf.keras.layers.Dense(10,activation='softmax')
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
model.fit(x_train,y_train,batch_size=32,epochs=5,validation_data=(x_test,y_test),validation_freq=1)
model.summary()
实例3:Fashion数据集
提供6w张28*28像素点的衣裤等图片和标签,用于训练
提供1w张28*28像素点的衣裤等图片和标签,用于测试
import tensorflow as tf
(x_train,y_train),(x_test,y_test) = tf.keras.datasets.fashion_mnist.load_data()
x_train,x_test = x_train/255.0, x_test/255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128,activation='relu',
kernel_regularizer=tf.keras.regularizers.l2()),
tf.keras.layers.Dense(10,activation='softmax',
kernel_regularizer=tf.keras.regularizers.l2())
])
model.compile(optimizer=tf.keras.optimizers.Adam(beta_1=0.1,beta_2=0.99),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
model.fit(x_train,y_train,validation_data=(x_test,y_test),validation_freq=2,epochs=5)
model.summary()
解决部分跳连的网络结构
跳连网络结构:神经元的输出是下面几个神经元的输入,比如resNet残差网络
采用类的方法:
实例3:Fashion数据集_class实现
import tensorflow as tf
from tensorflow.keras import Model
(x_train,y_train),(x_test,y_test) = tf.keras.datasets.fashion_mnist.load_data()
x_train,x_test = x_train/255.0, x_test/255.0
class fashionModel(Model):
def __init__(self):
super(fashionModel, self).__init__()
self.d1 = tf.keras.layers.Flatten()
self.d2 = tf.keras.layers.Dense(128,activation='relu',kernel_regularizer=tf.keras.regularizers.l2())
self.d3 = tf.keras.layers.Dense(10,activation='softmax',kernel_regularizer=tf.keras.regularizers.l2())
def call(self, x):
y1 = self.d1(x)
y2 = self.d2(y1)
y3 = self.d3(y2)
return y3
model = fashionModel()
model.compile(optimizer=tf.keras.optimizers.Adam(beta_1=0.1,beta_2=0.99),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
model.fit(x_train,y_train,validation_data=(x_test,y_test),validation_freq=2,epochs=5)
model.summary()