關(guān)于Keras Dense層整理

  • A+
所屬分類:百科知識(shí)

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

我就廢話不多說了,大家還是直接看代碼吧!

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, #激活函數(shù).但是默認(rèn) liner
use_bias=True, #是否使用b
kernel_initializer='glorot_uniform', #初始化w權(quán)重,keras/initializers.py
bias_initializer='zeros', #初始化b權(quán)重
kernel_regularizer=None, #施加在權(quán)重w上的正則項(xiàng),keras/regularizer.py
bias_regularizer=None, #施加在偏置向量b上的正則項(xiàng)
activity_regularizer=None, #施加在輸出上的正則項(xiàng)
kernel_constraint=None, #施加在權(quán)重w上的約束項(xiàng)
bias_constraint=None #施加在偏置b上的約束項(xiàng)
)
# 所實(shí)現(xiàn)的運(yùn)算是output = activation(dot(input, kernel)+bias)
# model.add(Dense(units=64, activation='relu', input_dim=784))
# keras初始化所有激活函數(shù),activation:
# keras\activations.py
# keras\backend\cntk_backend.py
# import cntk as C
# 1.softmax:
#?????? 對(duì)輸入數(shù)據(jù)的最后一維進(jìn)行softmax,一般用在輸出層;
#?? ndim == 2,K.softmax(x),其實(shí)調(diào)用的是cntk,是一個(gè)模塊;
#?? ndim >= 2,e = K.exp(x - K.max(x)),s = K.sum(e),return e / s
# 2.elu
#?? K.elu(x)
# 3.selu: 可伸縮的指數(shù)線性單元
#?? 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(固定一個(gè)值)
# RandomNormal(正態(tài)分布)
# RandomUniform(均勻分布)
# TruncatedNormal(截尾高斯分布,神經(jīng)網(wǎng)絡(luò)權(quán)重和濾波器的推薦初始化方法)
# VarianceScaling(該初始化方法能夠自適應(yīng)目標(biāo)張量的shape)
# Orthogonal(隨機(jī)正交矩陣初始化)
# Identiy(單位矩陣初始化,僅適用于2D方陣)
# lecun_uniform(LeCun均勻分布初始化)
# lecun_normal(LeCun正態(tài)分布初始化)
# glorot_normal(Glorot正態(tài)分布初始化)
# glorot_uniform(Glorot均勻分布初始化)
# he_normal(He正態(tài)分布初始化)
# he_uniform(He均勻分布初始化,Keras中文文檔寫錯(cuò)了)
# keras正則化,regularizer:
# import backend as K
# L1: regularization += K.sum(self.l1 * K.abs(x))
# L2: regularization += K.sum(self.l2 * K.square(x))

補(bǔ)充知識(shí):keras.layers.Dense()方法及其參數(shù)

一、Dense層

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)

二、參數(shù)

units: 神經(jīng)元節(jié)點(diǎn)數(shù)數(shù),雞輸出空間維度。

activation: 激活函數(shù),若不指定,則不使用激活函數(shù) (即線性激活: a(x) = x)。

use_bias: 布爾值,該層是否使用偏置向量。

kernel_initializer: kernel 權(quán)值矩陣的初始化器

bias_initializer: 偏置向量的初始化器

kernel_regularizer: 運(yùn)用到 kernel 權(quán)值矩陣的正則化函數(shù)

bias_regularizer: 運(yùn)用到偏置向的的正則化函數(shù)

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

kernel_constraint: 運(yùn)用到 kernel 權(quán)值矩陣的約束函數(shù)

bias_constraint: 運(yùn)用到偏置向量的約束函數(shù)

三、示例

例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,)))
# 現(xiàn)在模型就會(huì)以尺寸為 (*, 16) 的數(shù)組作為輸入,
# 其輸出數(shù)組的尺寸為 (*, 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))

這里定義了一個(gè)有512個(gè)神經(jīng)元節(jié)點(diǎn),使用sigmoid激活函數(shù)的神經(jīng)層,此時(shí)輸入形狀參數(shù)為input_dim,注意它與input_shape參數(shù)的區(qū)別。

input_shape:即張量的形狀,從前往后對(duì)應(yīng)由外向內(nèi)的維度

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

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

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

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

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

四、總結(jié)

本文對(duì)Dense()方法及其參數(shù)做了詳細(xì)的介紹,并對(duì)其用法進(jìn)行了大概的講解,有什么問題可以評(píng)論區(qū)留言或者聯(lián)系我,我會(huì)及時(shí)解答。希望能給大家一個(gè)參考。

歷史上的今天:

推薦應(yīng)用

發(fā)表評(píng)論

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