第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

加載后更改 MobileNet 丟失

加載后更改 MobileNet 丟失

我正在處理遷移學(xué)習(xí)問題。當(dāng)我僅從 Mobilenet 創(chuàng)建新模型時(shí),我設(shè)置了一個(gè) dropout。base_model = MobileNet(weights='imagenet', include_top=False, input_shape=(200,200,3), dropout=.15)x = base_model.outputx = GlobalAveragePooling2D()(x)x = Dense(10, activation='softmax')(x)我在訓(xùn)練時(shí)使用model_checkpoint_callback. 當(dāng)我訓(xùn)練時(shí),我會(huì)發(fā)現(xiàn)過度擬合發(fā)生的地方,并調(diào)整凍結(jié)層的數(shù)量和學(xué)習(xí)率。當(dāng)我再次保存加載的模型時(shí),我是否也可以調(diào)整 dropout?我看到了這個(gè)答案,但是 Mobilenet 中沒有實(shí)際的 dropout 層,所以這個(gè)for layer in model.layers:    if hasattr(layer, 'rate'):        print(layer.name)        layer.rate = 0.5什么都不做。
查看完整描述

1 回答

?
UYOU

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊

過去,您必須克隆模型才能讓新的 dropout 接受。我最近沒試過。


# This code allows you to change the dropout

# Load model from .json

model.load_weights(filenameToModelWeights) # Load weights

model.layers[-2].rate = 0.04  # layer[-2] is my dropout layer, rate is dropout attribute

model = keras.models.clone(model) # If I do not clone, the new rate is never used. Weights are re-init now.

model.load_weights(filenameToModelWeights) # Load weights

model.predict(x)

歸功于


http://www.gergltd.com/home/2018/03/changing-dropout-on-the-fly-during-training-time-test-time-in-keras/


如果模型一開始就沒有 dropout 層,就像 Keras 的預(yù)訓(xùn)練移動(dòng)網(wǎng)絡(luò)一樣,您必須使用方法添加它們。這是您可以做到的一種方法。


用于添加單層


def insert_single_layer_in_keras(model, layer_name, new_layer):

    layers = [l for l in model.layers]


    x = layers[0].output

    for i in range(1, len(layers)):

        x = layers[i](x)

        # add layer afterward

        if layers[i].name == layer_name:

            x = new_layer(x)


    new_model = Model(inputs=layers[0].input, outputs=x)

    return new_model


用于系統(tǒng)地添加層


def insert_layers_in_model(model, layer_common_name, new_layer):

    import re


    layers = [l for l in model.layers]

    x = layers[0].output

    layer_config = new_layer.get_config()

    base_name = layer_config['name']

    layer_class = type(dropout_layer)

    for i in range(1, len(layers)):

        x = layers[i](x)

        match = re.match(".+" + layer_common_name + "+", layers[i].name)

        # add layer afterward

        if match:

            layer_config['name'] = base_name + "_" + str(i)  # no duplicate names, could be done different

            layer_copy = layer_class.from_config(layer_config)

            x = layer_copy(x)


    new_model = Model(inputs=layers[0].input, outputs=x)

    return new_model

像這樣跑


import tensorflow as tf

from tensorflow.keras.applications.mobilenet import MobileNet

from tensorflow.keras.layers import Dropout

from tensorflow.keras.models import Model


base_model = MobileNet(weights='imagenet', include_top=False, input_shape=(192, 192, 3), dropout=.15)


dropout_layer = Dropout(0.5)

# add single layer after last dropout

mobile_net_with_dropout = insert_single_layer_in_model(base_model, "conv_pw_13_bn", dropout_layer)

# systematically add layers after any batchnorm layer

mobile_net_with_multi_dropout = insert_layers_in_model(base_model, "bn", dropout_layer)

順便說一句,您絕對(duì)應(yīng)該進(jìn)行實(shí)驗(yàn),但您不太可能希望在 batchnorm 之上對(duì)像 mobilenet 這樣的小型網(wǎng)絡(luò)進(jìn)行額外的正則化。


查看完整回答
反對(duì) 回復(fù) 2023-02-22
  • 1 回答
  • 0 關(guān)注
  • 106 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)