| 參數 | 介紹 |
|---|---|
| units | 正整數, 輸出空間維度 |
| activation | 激活函數, 若不指定, 則不適用激活函數 |
| use_bias | 布爾值, 該層是否使用偏置向量 |
| kernel_initializer | kernel權值矩陣的初始化器 |
| bias_initializer | 偏執向量的初始化器 |
| kernel_regulaizer | 運用到偏執項的正則化函數 |
| bias_regularizer | 運用到偏執項的的正則化函數 |
| activity_regulatizer | 運用到層的輸出正則化函數 |
| kernel_constrint | 運用到kernel權值矩陣的約束函數 |
| bias_constraint | 運用到偏執向量的約束函數 |
例子:
# 創建正態分布
x = tf.random.normal([256, 784])
# 創建全連接層, 輸出為512
net = tf.keras.layers.Dense(512)
out = net(x)
# 調試輸出
print("w:", net.kernel.shape)
print("b:", net.bias.shape)
輸出結果:
w: (784, 512)
b: (512,)
Squential (序列模型) 是各層次之間依次順序的線性關系. 模型結構通過一個列表來制定.

格式:
tf.keras.Sequential(
layers=None, name=None
)
參數:
例子:
# 創建正態分布
x = tf.random.normal([256, 784])
# 建立網絡模型
model = tf.keras.Sequential([
tf.keras.layers.Dense(256, activation="relu"),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dense(10, activation="relu"),
])
# 傳入x
model(x)
# 調試輸出權重和偏置頂名字和形狀
for p in model.trainable_variables:
print(p.name, p.shape)
輸出結果:
w: (784, 512)
b: (512,)
dense_1/kernel:0 (784, 256)
dense_1/bias:0 (256,)
dense_2/kernel:0 (256, 128)
dense_2/bias:0 (128,)
dense_3/kernel:0 (128, 10)
dense_3/bias:0 (10,)
到此這篇關于一小時學會TensorFlow2之全連接層的文章就介紹到這了,更多相關TensorFlow2全連接層內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!