關于Keras Dense層整理

這篇文章主要介紹了關于Keras Dense層整理,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

文章源自四五設計網-http://www.wasochina.com/39457.html

我就廢話不多說了,大家還是直接看代碼吧!文章源自四五設計網-http://www.wasochina.com/39457.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'''
Created on 2018-4-4
'''
keras.layers.core.Dense(
units, #代表該層的輸出維度
activation=None, #激活函數.但是默認 liner
use_bias=True, #是否使用b
kernel_initializer='glorot_uniform', #初始化w權重,keras/initializers.py
bias_initializer='zeros', #初始化b權重
kernel_regularizer=None, #施加在權重w上的正則項,keras/regularizer.py
bias_regularizer=None, #施加在偏置向量b上的正則項
activity_regularizer=None, #施加在輸出上的正則項
kernel_constraint=None, #施加在權重w上的約束項
bias_constraint=None #施加在偏置b上的約束項
)
# 所實現的運算是output = activation(dot(input, kernel)+bias)
# model.add(Dense(units=64, activation='relu', input_dim=784))
# keras初始化所有激活函數,activation:
# keras\activations.py
# keras\backend\cntk_backend.py
# import cntk as C
# 1.softmax:
#?????? 對輸入數據的最后一維進行softmax,一般用在輸出層;
#?? ndim == 2,K.softmax(x),其實調用的是cntk,是一個模塊;
#?? ndim >= 2,e = K.exp(x - K.max(x)),s = K.sum(e),return e / s
# 2.elu
#?? K.elu(x)
# 3.selu: 可伸縮的指數線性單元
#?? alpha = 1.6732632423543772848170429916717
#?? scale = 1.0507009873554804934193349852946
#?? return scale * K.elu(x, alpha)
# 4.softplus
#?? C.softplus(x)
# 5.softsign
#?? return x / (1 + C.abs(x))
# 6.relu
#?? def relu(x, alpha=0., max_value=None):
#???? if alpha != 0.:
#?????? negative_part = C.relu(-x)
#???? x = C.relu(x)
#???? if max_value is not None:
#?????? x = C.clip(x, 0.0, max_value)
#???? if alpha != 0.:
#?????? x -= alpha * negative_part
#???? return x
# 7.tanh
#?? return C.tanh(x)
# 8.sigmoid
#?? return C.sigmoid(x)
# 9.hard_sigmoid
#?? x = (0.2 * x) + 0.5
#?? x = C.clip(x, 0.0, 1.0)
#?? return x
# 10.linear
#?? return x
# keras初始化所有方法,initializer:
# Zeros
# Ones
# Constant(固定一個值)
# RandomNormal(正態分布)
# RandomUniform(均勻分布)
# TruncatedNormal(截尾高斯分布,神經網絡權重和濾波器的推薦初始化方法)
# VarianceScaling(該初始化方法能夠自適應目標張量的shape)
# Orthogonal(隨機正交矩陣初始化)
# Identiy(單位矩陣初始化,僅適用于2D方陣)
# lecun_uniform(LeCun均勻分布初始化)
# lecun_normal(LeCun正態分布初始化)
# glorot_normal(Glorot正態分布初始化)
# glorot_uniform(Glorot均勻分布初始化)
# he_normal(He正態分布初始化)
# he_uniform(He均勻分布初始化,Keras中文文檔寫錯了)
# keras正則化,regularizer:
# import backend as K
# L1: regularization += K.sum(self.l1 * K.abs(x))
# L2: regularization += K.sum(self.l2 * K.square(x))

補充知識:keras.layers.Dense()方法及其參數文章源自四五設計網-http://www.wasochina.com/39457.html

一、Dense層文章源自四五設計網-http://www.wasochina.com/39457.html

1
2
3
4
5
6
7
8
9
10
keras.layers.Dense(units,
??activation=None,
??use_bias=True,
??kernel_initializer='glorot_uniform',
??bias_initializer='zeros',
??kernel_regularizer=None,
??bias_regularizer=None,
???activity_regularizer=None,
??kernel_constraint=None,
??bias_constraint=None)

二、參數文章源自四五設計網-http://www.wasochina.com/39457.html

units: 神經元節點數數,雞輸出空間維度。文章源自四五設計網-http://www.wasochina.com/39457.html

activation: 激活函數,若不指定,則不使用激活函數 (即線性激活: a(x) = x)。文章源自四五設計網-http://www.wasochina.com/39457.html

use_bias: 布爾值,該層是否使用偏置向量。文章源自四五設計網-http://www.wasochina.com/39457.html

kernel_initializer: kernel 權值矩陣的初始化器文章源自四五設計網-http://www.wasochina.com/39457.html

bias_initializer: 偏置向量的初始化器文章源自四五設計網-http://www.wasochina.com/39457.html

kernel_regularizer: 運用到 kernel 權值矩陣的正則化函數

bias_regularizer: 運用到偏置向的的正則化函數

activity_regularizer: 運用到層的輸出的正則化函數 (它的 “activation”)。

kernel_constraint: 運用到 kernel 權值矩陣的約束函數

bias_constraint: 運用到偏置向量的約束函數

三、示例

例1:

1
2
3
4
5
6
7
8
9
10
from keras.layers import Dense
# 作為 Sequential 模型的第一層
model = Sequential()
model.add(Dense(32, input_shape=(16,)))
# 現在模型就會以尺寸為 (*, 16) 的數組作為輸入,
# 其輸出數組的尺寸為 (*, 32)
# 在第一層之后,你就不再需要指定輸入的尺寸了:
model.add(Dense(32))

注意在Sequential模型的第一層要定義Dense層的形狀,此處定義為input_shape=(16,)

例2:

1
2
3
4
from keras.layers import Dense
model = Sequential()
model.add(Dense(512, activation= 'sigmoid', input_dim= 2, use_bias= True))

這里定義了一個有512個神經元節點,使用sigmoid激活函數的神經層,此時輸入形狀參數為input_dim,注意它與input_shape參數的區別。

input_shape:即張量的形狀,從前往后對應由外向內的維度

[[1],[2],[3]] 這個張量的shape為(3,1)

[[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]]]這個張量的shape為(3,2,2),

[1,2,3,4]這個張量的shape為(4,)

input_dim:代表張量的維度,之前3個例子的input_dim分別為2,3,1。

常見的一種用法:只提供了input_dim=32,說明輸入是一個32維的向量,相當于一個一階、擁有32個元素的張量,它的shape就是(32,)。因此,input_shape=(32, )

四、總結

本文對Dense()方法及其參數做了詳細的介紹,并對其用法進行了大概的講解,有什么問題可以評論區留言或者聯系我,我會及時解答。希望能給大家一個參考。

繼續閱讀
我的微信
微信掃一掃
weinxin
我的微信
惠生活福利社
微信掃一掃
weinxin
我的公眾號
 
  • 本文由 四五設計網小助手 發表于 2024年2月1日09:59:52
  • 轉載請務必保留本文鏈接:http://www.wasochina.com/39457.html

發表評論

匿名網友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖動滑塊以完成驗證