01.神经网络、优化方法与正则化
激活函数
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10,10,1000)
y = tf.nn.sigmoid(x)
plt.plot(x,y)
plt.grid()
plt.show()
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10,10,1000)
y = tf.nn.tanh(x)
plt.plot(x,y)
plt.grid()
plt.show()
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10,10,1000)
y = tf.nn.relu(x)
plt.plot(x,y)
plt.grid()
plt.show()
参数初始化
神经网络的搭建
import tensorflow as tf
import tensorflow.keras as keras
import tensorflow.keras.layers as layers
model = keras.Sequential([
#第一个隐层
layers.Dense(3,activation="relu",kernel_initializer="he_normal",name="layer1",input_shape=(3,)),
#第二个隐层
layers.Dense(2,activation="relu",kernel_initializer="he_normal",name="layer2"),
#输出层
layers.Dense(2,activation="relu",kernel_initializer="he_normal",name="layer3")
])
model.summary()
利用function API构建神经网络
import tensorflow as tf
import tensorflow.keras as keras
import tensorflow.keras.layers as layers
#定义模型的输入
inputs = keras.Input(shape=(3,),name='input')
#第一个隐层
x = layers.Dense(3,activation="relu",name="layer1")(inputs)
#第二个隐层
x = layers.Dense(2,activation="relu",name="layer2")(x)
#输出层
outputs = layers.Dense(2,activation="sigmoid",name="layer3")(x)
#创建模型
model = keras.Model(inputs=inputs,outputs=outputs,name="Functional API Model")
model.summary()
通过model的子类构建神经网络
import tensorflow as tf
import tensorflow.keras as keras
import tensorflow.keras.layers as layers
#定义一个model的子类
class MyModel(keras.Model):
#定义网络的层结构
def __init__(self):
super(MyModel,self).__init__()
#第一个隐层
self.layer1 = layers.Dense(3,activation="relu",name="layer1")
#第二个隐层
self.layer2 = layers.Dense(2,activation="relu",name="layer2")
#输出层
self.layer3 = layers.Dense(2,activation="sigmoid",name="layer3")
#定义网络的前向传播
def call(self,inputs):
x = self.layer1(inputs)
x = self.layer2(x)
outputs = self.layer3(x)
return outputs
#实例化模型
model = MyModel()
#设置输入
x = tf.ones((1,3))
y = model(x)
model.summary()
神经网络的优缺点
常见的损失函数
交叉熵损失函数
import tensorflow as tf
#设置真实值和预测值
y_true = [[0,1,0],[0,0,1]]
y_pre = [[0.05,0.9,0.05],[0.05,0.05,0.9]]
#实例化交叉熵损失
cce=tf.keras.losses.CategoricalCrossentropy()
#计算损失结果
print(cce(y_true,y_pre))
二分类的交叉熵损失函数
import tensorflow as tf
#设置真实值和预测值
y_true = [[0],[1]]
y_pre = [[0.1],[0.9]]
#实例化交叉熵损失
bce=tf.keras.losses.BinaryCrossentropy()
#计算损失结果
print(bce(y_true,y_pre))
MAE损失(L1 LOSS)
import tensorflow as tf
#设置真实值和预测值
y_true = [[0],[1]]
y_pre = [[1.],[0.]]
#实例化MAE损失
mae = tf.keras.losses.MeanAbsoluteError()
mae(y_true,y_pre)
MSE损失(L2 LOSS)
smoth L1损失
梯度下降算法
反向传播算法(BP算法)
动量梯度下降算法
AdaGrad
RMSprop
Adam算法
学习率退火
深度学习正则化
L1、L2正则化
Dropout正则化
提前停止