Keras - Dense 層



Dense 層是普通的全連線神經網路層。它是最常見和最常用的層。Dense 層對輸入進行以下操作並返回輸出。

output = activation(dot(input, kernel) + bias)

其中,

  • 輸入表示輸入資料

  • 表示權重資料

  • 點積表示所有輸入與其對應權重的 NumPy 點積

  • 偏置表示機器學習中用於最佳化模型的偏置值

  • 啟用函式表示啟用函式。

讓我們考慮以下示例輸入和權重,並嘗試找到結果:

  • 輸入為 2 x 2 矩陣 [ [1, 2], [3, 4] ]

  • 核為 2 x 2 矩陣 [ [0.5, 0.75], [0.25, 0.5] ]

  • 偏置值為 0

  • 啟用函式為 線性。正如我們前面學到的,線性啟用函式什麼也不做。

>>> import numpy as np 

>>> input = [ [1, 2], [3, 4] ] 
>>> kernel = [ [0.5, 0.75], [0.25, 0.5] ] 
>>> result = np.dot(input, kernel) 
>>> result array([[1. , 1.75], [2.5 , 4.25]])
>>>

結果是輸出,它將傳遞到下一層。

Dense 層的輸出形狀將受 Dense 層中指定的單元/神經元數量的影響。例如,如果輸入形狀為 (8,),單元數量為 16,則輸出形狀為 (16,)。所有層都將批次大小作為第一維度,因此,輸入形狀將表示為 (None, 8),輸出形狀為 (None, 16)。目前,批次大小為 None,因為它未設定。批次大小通常在訓練階段設定。

>>> from keras.models import Sequential 
>>> from keras.layers import Activation, Dense 

>>> model = Sequential() 
>>> layer_1 = Dense(16, input_shape = (8,)) 
>>> model.add(layer_1) 
>>> layer_1.input_shape 
(None, 8) 
>>> layer_1.output_shape 
(None, 16)
>>>

其中,

  • layer_1.input_shape 返回層的輸入形狀。

  • layer_1.output_shape 返回層的輸出形狀。

Dense 層支援的引數如下:

  • units 表示單元數量,它影響輸出層。

  • activation 表示啟用函式。

  • use_bias 表示層是否使用偏置向量。

  • kernel_initializer 表示用於核的初始化器。

  • bias_initializer 表示用於偏置向量的初始化器。

  • kernel_regularizer 表示應用於核權重矩陣的正則化函式。

  • bias_regularizer 表示應用於偏置向量的正則化函式。

  • activity_regularizer 表示應用於層輸出的正則化函式。

  • kernel_constraint 表示應用於核權重矩陣的約束函式。

  • bias_constraint 表示應用於偏置向量的約束函式。

正如您所看到的,沒有引數可用於指定輸入資料的input_shapeinput_shape 是一個特殊引數,只有當層被設計為模型中的第一層時,層才會接受它。

此外,所有 Keras 層都有一些常用的方法,如下所示:

get_weights

獲取層中使用的所有權重的完整列表。

>>> from keras.models import Sequential 
>>> from keras.layers import Activation, Dense 
>>> model = Sequential() 
>>> layer_1 = Dense(16, input_shape = (8,)) 
>>> model.add(layer_1) 
>>> layer_1.get_weights() 
>>> [array([[-0.19929028, 0.4162618 , 0.20081699,  
   -0.25589502, 0.3612864 , 0.25088787, -0.47544873, 0.0321095 , 
   -0.26070702, -0.24102116, 0.32778358, 0.4667952 , -0.43322265, 
   -0.14500427, 0.04341269, -0.34929228], [ 0.41898954, 0.42256463, 
   0.2399621 , -0.272717 , -0.37069297, -0.37802136, 0.11428618, 0.12749982, 
   0.10182762, 0.14897704, 0.06569374, 0.15424263, 0.42638576, 0.34037888, -0.15504825, 
   -0.0740819 ], [-0.3132702 , 0.34885168, -0.3259498 , -0.47076607, 0.33696914, 
   -0.49143505, -0.04318619, -0.11252558, 0.29669464, -0.28431225, -0.43165374, 
   -0.49687648, 0.13632 , -0.21099591, -0.10608876, -0.13568914], [-0.27421212, 
   -0.180812 , 0.37240648,  0.25100648, -0.07199466, -0.23680925, -0.21271884, 
   -0.08706653, 0.4393121 , 0.23259485, 0.2616762 , 0.23966897, -0.4502542 , 0.0058881 
   , 0.14847124, 0.08835125], [-0.36905527, 0.08948278, -0.19254792, 0.26783705, 
   0.25979865, -0.46963632, 0.32761025, -0.25718856, 0.48987913, 0.3588251 , 
   -0.06586111, 0.2591269 , 0.48289275, 0.3368858 , -0.17145419, -0.35674667], 
   [-0.32851398, 0.42289603, -0.47025883, 0.29027188, -0.0498147 , 0.46215963, 
   -0.10123312, 0.23069787, 0.00844061, -0.11867595, -0.2602347 , 
   -0.27917898, 0.22910392, 0.18214619, -0.40857887, 0.2606709 ], [-0.19066167, 
   -0.11464512, -0.06768692, -0.21878994, -0.2573272 , 0.13698077, 0.45221198, 
   0.10634196, 0.06784797, 0.07192957, 0.2946936 , 
   0.04968262, -0.15899467, 0.15757453, -0.1343019 , 0.24561536], [-0.04272163, 
   0.48315823, -0.13382411, 0.01752126, -0.1630218 , 0.4629662 , -0.21412933, 
   -0.1445911 , -0.03567278, -0.20948446, 0.15742278, 0.11139905, 0.11066687, 
   0.17430818, 0.36413217, 0.19864106]], dtype=float32), array([0., 0., 0., 0., 0., 0., 
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype = float32)]
>>>
  • set_weights - 設定層的權重

  • get_config - 獲取層的完整配置作為物件,該物件可以隨時重新載入。

config = layer_1.get_config()

from_config

從層的配置物件載入層。

config = layer_1.get_config() reload_layer = Dense.from_config(config)

input_shape

獲取輸入形狀,僅當層只有一個節點時。

>>> from keras.models import Sequential 
>>> from keras.layers import Activation, Dense 
>>> model = Sequential() 
>>> layer_1 = Dense(16, input_shape = (8,)) 
>>> model.add(layer_1) 
>>> layer_1.get_weights() 
>>> layer_1.input_shape 
(None, 8)

input

獲取輸入資料,僅當層只有一個節點時。

>>> from keras.models import Sequential 
>>> from keras.layers import Activation, Dense 
>>> model = Sequential() 
>>> layer_1 = Dense(16, input_shape = (8,)) 
>>> model.add(layer_1) 
>>> layer_1.get_weights() 
>>> layer_1.input 
<tf.Tensor 'dense_1_input:0' shape = (?, 8) dtype = float32>
  • get_input_at - 獲取指定索引處的輸入資料,如果層有多個節點。

  • get_input_shape_at - 獲取指定索引處的輸入形狀,如果層有多個節點。

  • output_shape - 獲取輸出形狀,僅當層只有一個節點時。

>>> from keras.models import Sequential 
>>> from keras.layers import Activation, Dense 
>>> model = Sequential() 
>>> layer_1 = Dense(16, input_shape = (8,)) 
>>> model.add(layer_1) 
>>> layer_1.get_weights()
>>> layer_1.output_shape (None, 16)

output

獲取輸出資料,僅當層只有一個節點時。

>>> from keras.models import Sequential 
>>> from keras.layers import Activation, Dense 
>>> model = Sequential() 
>>> layer_1 = Dense(16, input_shape = (8,)) 
>>> model.add(layer_1) 
>>> layer_1.get_weights() 
>>> layer_1.output 
<tf.Tensor 'dense_1/BiasAdd:0' shape = (?, 16) dtype = float32>
  • get_output_at - 獲取指定索引處的輸出資料,如果層有多個節點。

  • get_output_shape_at - 獲取指定索引處的輸出形狀,如果層有多個節點。

廣告
© . All rights reserved.