最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

13 丟棄法【動手學(xué)深度學(xué)習(xí)v2】

2023-07-14 16:32 作者:月蕪SA  | 我要投稿

丟棄法(解決overfitting問題的另一方案)

丟棄法特性:在層之間加入噪音,而不是在數(shù)據(jù)輸入時加噪音。

正則化概念:在訓(xùn)練模型時使模型的復(fù)雜度降低,防止過擬合

丟棄法原理:對所有元素以p的概率變?yōu)?,其余元素除以(1-p)。處理后元素期望值不變。

如示意圖可知,丟棄法可將一些中間層節(jié)點丟棄,對剩余節(jié)點進(jìn)行一定的增強(qiáng)。

注:dropout是正則項,僅在訓(xùn)練(train)中使用,不用于預(yù)測(inference)


丟棄法的實現(xiàn)

import torch
from torch import nn
from d2l import torch as d2l


def dropout_layer(X, dropout):
    assert 0 <= dropout <= 1
    # 在本情況中,所有元素都被丟棄
    if dropout == 1:
        return torch.zeros_like(X)
    # 在本情況中,所有元素都被保留
    if dropout == 0:
        return X
    mask = (torch.rand(X.shape) > dropout).float()
    return mask * X / (1.0 - dropout)

dropout參數(shù):選擇p值

mask = (torch.rand(X.shape) > dropout).float()

上段代碼作用:用rand生成0到1 的隨機(jī)數(shù)列,再與p值進(jìn)行大小比較,生成一系列的布爾值。

接下來對dropout進(jìn)行一些測試:

X= torch.arange(16, dtype = torch.float32).reshape((2, 8))
print(X)
print(dropout_layer(X, 0.))
print(dropout_layer(X, 0.5))
print(dropout_layer(X, 1.))
輸出結(jié)果
tensor([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11., 12., 13., 14., 15.]])
tensor([[ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11., 12., 13., 14., 15.]])
tensor([[ 0.,  2.,  0.,  6.,  8., 10.,  0.,  0.],
        [16.,  0.,  0., 22.,  0., 26.,  0.,  0.]])
tensor([[0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.]])


設(shè)置各層節(jié)點數(shù)

num_inputs, num_outputs, num_hiddens1, num_hiddens2 = 784, 10, 256, 256


網(wǎng)絡(luò)主體:注意is_training的作用,讓dropout僅在training中起作用。此外,dropout只在隱藏層起作用,本模型中有兩個hidden layers,所以要設(shè)置兩個dropout

dropout1, dropout2 = 0.2, 0.5

class Net(nn.Module):
    def __init__(self, num_inputs, num_outputs, num_hiddens1, num_hiddens2,
                 is_training = True):
        super(Net, self).__init__()
        self.num_inputs = num_inputs
        self.training = is_training
        self.lin1 = nn.Linear(num_inputs, num_hiddens1)
        self.lin2 = nn.Linear(num_hiddens1, num_hiddens2)
        self.lin3 = nn.Linear(num_hiddens2, num_outputs)
        self.relu = nn.ReLU()

    def forward(self, X):
        H1 = self.relu(self.lin1(X.reshape((-1, self.num_inputs))))
        # 只有在訓(xùn)練模型時才使用dropout
        if self.training == True:
            # 在第一個全連接層之后添加一個dropout層
            H1 = dropout_layer(H1, dropout1)
        H2 = self.relu(self.lin2(H1))
        if self.training == True:
            # 在第二個全連接層之后添加一個dropout層
            H2 = dropout_layer(H2, dropout2)
        out = self.lin3(H2)
        return out


net = Net(num_inputs, num_outputs, num_hiddens1, num_hiddens2)


簡潔實現(xiàn):注意代碼中dropout的位置

net = nn.Sequential(nn.Flatten(),
        nn.Linear(784, 256),
        nn.ReLU(),
        # 在第一個全連接層之后添加一個dropout層
        nn.Dropout(dropout1),
        nn.Linear(256, 256),
        nn.ReLU(),
        # 在第二個全連接層之后添加一個dropout層
        nn.Dropout(dropout2),
        nn.Linear(256, 10))

def init_weights(m):
    if type(m) == nn.Linear:
        nn.init.normal_(m.weight, std=0.01)

net.apply(init_weights);


注:dropout方法僅適用于全連接層 。而weight decay(權(quán)重衰退)則適用于所有模型





13 丟棄法【動手學(xué)深度學(xué)習(xí)v2】的評論 (共 條)

分享到微博請遵守國家法律
道孚县| 交口县| 武陟县| 鄂托克旗| 宜丰县| 吐鲁番市| 高要市| 花莲县| 桂东县| 满洲里市| 高邑县| 漠河县| 广南县| 黔南| 巢湖市| 仙桃市| 上饶市| 疏附县| 南汇区| 新民市| 连云港市| 东丰县| 尚志市| 平凉市| 长沙市| 崇信县| 河东区| 太保市| 景德镇市| 资兴市| 黄大仙区| 鹿邑县| 贵德县| 寿宁县| 三原县| 扬州市| 旺苍县| 沙雅县| 太仓市| 濮阳市| 长汀县|