作者:mobiledu2502936307 | 来源:互联网 | 2023-09-16 13:19
深度学习(20)神经网络与全连接层三: 全连接层 1. 单层 2. 三层 3. 多层 4. Here comes Deep Learning! 5. Why Deep Learning? 6. Heroes 7. Fully connected layer(全连接层) 8. net.build() 9. 网络中的维度设置出错会导致报错 10. Multi-Layers——keras.Sequential() 11. 全连接层实战 12. Next
Outline
Matmul Neural Network Deep Learning Multi-Layer Recap
out=f(X@W+b)out=f(X@W+b) o u t = f ( X @ W + b ) →\to → out=relu(X@W+b)out=relu(X@W+b) o u t = r e l u ( X @ W + b ) 1. 单层 X@W+bX@W+b X @ W + b h=relu(X@W+b)h=relu(X@W+b) h = r e l u ( X @ W + b ) [h00h10h01h11]=relu([x00x10x20x10x11x12]@[w00w01w10w11w20w21]+[b0b1])\begin{bmatrix}h_0^0&h_1^0\\h_0^1&h_1^1\end{bmatrix}=relu(\begin{bmatrix}x_0^0&x_1^0&x_2^0\\x_1^0&x_1^1&x_1^2\end{bmatrix}@\begin{bmatrix}w_{00}&w_{01}\\w_{10}&w_{11}\\w_{20}&w_{21}\end{bmatrix}+\begin{bmatrix}b_0&b_1\end{bmatrix}) [ h 0 0 h 0 1 h 1 0 h 1 1 ] = r e l u ( [ x 0 0 x 1 0 x 1 0 x 1 1 x 2 0 x 1 2 ] @ ⎣ ⎡ w 0 0 w 1 0 w 2 0 w 0 1 w 1 1 w 2 1 ⎦ ⎤ + [ b 0 b 1 ] ) 2. 三层 h0=relu(X@W1+b1)h_0=relu(X@W_1+b_1) h 0 = r e l u ( X @ W 1 + b 1 ) h1=relu(h0@W2+b2)h_1=relu(h_0@W_2+b_2) h 1 = r e l u ( h 0 @ W 2 + b 2 ) out=relu(h1@W3+b3)out=relu(h_1@W_3+b_3) o u t = r e l u ( h 1 @ W 3 + b 3 ) 3. 多层 Input Hidden Output 4. Here comes Deep Learning! (1) Neural Network in the 1980s
3∼5layers3\sim5\ layers 3 ∼ 5 l a y e r s (2) Deep Learning now
n≈1200layersn≈1200\ layers n ≈ 1 2 0 0 l a y e r s 5. Why Deep Learning? 486 PC with DSP32C 20Mflops, 4MB RAM Telsa V100 32GB HBM2, 100Tflops 6. Heroes BigDATA ReLU Dropout BatchNorm ResNet Xavier Initialization Caffe/TensorFlow/PyTorch … 7. Fully connected layer(全连接层)
(1) x = tf.random.normal([4, 784])
: 创建一个维度为[4, 784]的Tensor; (2) net = tf.keras.layers.Dense(512)
: 创建一层神经网络,其输出维度为512; (3) tf.keras.layers.Dense()
在运行时会自动根据输入的shape来生成权值,即w和b; (4) out.shape
: 输出out的维度为[4, 512]; (5) net.kernal.shape
: 为自动生成的权值w,其shape为[784, 512]; (6) net.bias.shape
: 为自动生成的权值(偏置项)b,其shape为[512];
8. net.build() (1) net.build(input_shape=(None, 4))
: 可以通过调用net.build()函数创建权重ww w 和bb b ,其中4为下一层的维度; (2) net.build(input_shape=(None, 20))
: 这里可以看出可以通过多次调用net.build()函数来计算权重ww w 和bb b 的值; 注: 在第一次创建神经网络的时候,没有ww w 和bb b ,这时系统就会自动调用net.build()函数来创建权重ww w 和bb b ;
9. 网络中的维度设置出错会导致报错
(1) 如上图所示,net.build(input_shape=(None, 20)
)表示建立的网络的维度为20; out=net(tf.random.randn((4, 12)))
表示输入到这层神经网络中的数据的维度是12; 那么计算时就会报错; (2) out=net(tf.random.randn((4, 20)))
: 将输入到这层神经网络中的数据的维度改为20,最终out.shape=[4, 10];
10. Multi-Layers——keras.Sequential() 多层神经网络,只需要调用容器——keras.Sequential()即可。
keras.Sequential([layer1, layer2, layer3]) Sequential (1) x = random.normal([2, 3])
: 创建一个维度为[2, 3]的Tensor; (2) model = keras.Sequential([keras.layers.Dense(2, activation=‘relu’), keras.layers.Dense(2, activation=‘relu’), keras.layers.Dense(2)])
: 共设置3层全连接层: [3]→[2]→[2]→[2][3]→[2]→[2]→[2] [ 3 ] → [ 2 ] → [ 2 ] → [ 2 ] (3) model.build(input_shape=[None, 3])
: 给定一个维度为3的输入; (4) model.summary(): 相当于print(model)
,方便查看网络结构的函数; (5) for p in model.trainable_variables
: 所有权重参数都被称为“可训练的变量”,即trainable_variables,在这个网络结构中,就是[w1,b1,w2,b2,w3,b3][w_1,b_1,w_2,b_2,w_3,b_3] [ w 1 , b 1 , w 2 , b 2 , w 3 , b 3 ] ; (6) print(p.name, p.shape)
: 打印这些参数的名字和其维度;
11. 全连接层实战 import tensorflow as tf from tensorflow import kerasx = tf.random.normal([2, 3])model = keras.Sequential([keras.layers.Dense(2, activation='relu'),keras.layers.Dense(2, activation='relu'),keras.layers.Dense(2) ]) model.build(input_shape=[None, 4]) model.summary()for p in model.trainable_variables:print(p.name, p.shape)
运行结果如下:
(1) Param为参数量,第一层有2×3+2=82×3+2=8 2 × 3 + 2 = 8 个; 第二层有2×2+2=62×2+2=6 2 × 2 + 2 = 6 个; 第三层有2×2+2=62×2+2=6 2 × 2 + 2 = 6 个; (2) dense_n/kernel
: 第n层的ww w 的值; dense_n/bias: 第n层的bb b 的值;
12. Next 神经网络层与训练方法 图片识别 文本理解 艺术创作 自动决策 … 参考文献: [1] 龙良曲:《深度学习与TensorFlow2入门实战》 [2] https://www.youtube.com/watch?v=FwFduRA_L6Q [3] https://capacitybc.com/mini-series-part-3-the-hero1 [4] https://www.facebook.com/deepdreamgenerator/photos/a.892441237472223/1449674838415524/?type=1&theater [5] https://study.163.com/provider/480000001847407/index.htm?share=2&shareld=480000001847407