tf.keras.layers.Dense 全连接层
该层的效果是将输入的最高维(也就是最后一维)转成指定的维度数
如下例,将[5,2]形状的向量输入给Dense(8),最后输出形状维[5,8]
import tensorflow as tf
import numpy as np
input_data = np.arange(10).reshape(5,2)
input_data
'''
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
'''
x1 = tf.keras.layers.Dense(8)(input_data)
x1
'''
<tf.Tensor: shape=(5, 8), dtype=float32, numpy=
array([[-0.12366056, -0.6986265 , 0.77080464, -0.24608284, 0.40985382,
0.28273118, 0.30870295, -0.1619857 ],
[ 0.5133357 , -0.72846746, 1.3266207 , 0.33265072, 2.6006622 ,
-0.22271645, 0.5417254 , 0.72683674],
[ 1.150332 , -0.7583084 , 1.8824368 , 0.91138434, 4.7914705 ,
-0.7281641 , 0.77474785, 1.6156592 ],
[ 1.7873282 , -0.78814936, 2.438253 , 1.4901178 , 6.982279 ,
-1.2336117 , 1.0077703 , 2.5044816 ],
[ 2.4243245 , -0.8179903 , 2.9940686 , 2.0688515 , 9.173087 ,
-1.7390594 , 1.2407928 , 3.393304 ]], dtype=float32)>
'''
tf.keras.layers.Flatten扁平层
该层的效果是将除第一维度(也就是batch_size这一维)外的所有维度,都转成一个维度。
如下例: 向量形状为[2,2,5],通过Flatten层后,变为[2,10]形状。即将第一维之外的所有维数相乘:2x5
input_data1 = np.arange(20).reshape(2,2,5)
input_data1
'''
array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9]],
[[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]]])
'''
x2_f = tf.keras.layers.Flatten()(x2)
x2_f
'''
<tf.Tensor: shape=(2, 10), dtype=int32, numpy=
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]])>
'''