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

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

將最后一層(輸出層)的權(quán)重從經(jīng)過(guò)訓(xùn)練的網(wǎng)絡(luò)加載到新模型

將最后一層(輸出層)的權(quán)重從經(jīng)過(guò)訓(xùn)練的網(wǎng)絡(luò)加載到新模型

繁星淼淼 2023-06-06 14:56:29
是否可以使用 set_weights 和 get_weights 方案將權(quán)重從經(jīng)過(guò)訓(xùn)練的網(wǎng)絡(luò)加載到我的新模型的最后一層?關(guān)鍵是,我將每一層的權(quán)重保存為一個(gè) mat 文件(訓(xùn)練后),以便在 Matlab 中進(jìn)行一些計(jì)算,我只想將最后一層的修改權(quán)重加載到我的新模型和其他層的最后一層獲得與訓(xùn)練模型相同的權(quán)重。這有點(diǎn)棘手,因?yàn)楸4娴母袷绞?mat。weights1 = lstm_model1.layers[0].get_weights()[0]biases1 = lstm_model1.layers[0].get_weights()[1]weights2 = lstm_model1.layers[2].get_weights()[0]biases2 = lstm_model1.layers[2].get_weights()[1]weights3 = lstm_model1.layers[4].get_weights()[0]biases3 = lstm_model1.layers[4].get_weights()[1]# Save the weights and biases for adaptation algorithm savemat("weights1.mat", mdict={'weights1': weights1})  savemat("biases1.mat", mdict={'biases1': biases1})      savemat("weights2.mat", mdict={'weights2': weights2})   savemat("biases2.mat", mdict={'biases2': biases2})      savemat("weights3.mat", mdict={'weights3': weights3}) savemat("biases3.mat", mdict={'biases3': biases3})  我如何才能將其他層的舊權(quán)重加載到新模型(沒有最后一層)并將最后一層的修改權(quán)重加載到新模型的最后一層?
查看完整描述

1 回答

?
飲歌長(zhǎng)嘯

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

如果將其另存為 .h5 文件格式,則可以正常工作。但是,我不確定 .mat:


簡(jiǎn)單來(lái)說(shuō),您只需調(diào)用get_weights所需的層,類似地,set_weights調(diào)用另一個(gè)模型的相應(yīng)層:


last_layer_weights = old_model.layers[-1].get_weights()

new_model.layers[-1].set_weights(last_layer_weights)

如需更完整的代碼示例,請(qǐng)點(diǎn)擊此處:


# Create an arbitrary model with some weights, for example

model = Sequential(layers = [

    Dense(70, input_shape = (100,)),

    Dense(60),

    Dense(50),

    Dense(5)])


# Save the weights of the model

model.save_weights(“model.h5”)


# Later, load in the model (we only really need the layer in question)

old_model = Sequential(layers = [

    Dense(70, input_shape = (100,)),

    Dense(60),

    Dense(50),

    Dense(5)])


old_model.load_weights(“model.h5”)


# Create a new model with slightly different architecture (except for the layer in question, at least)

new_model = Sequential(layers = [

    Dense(80, input_shape = (100,)),

    Dense(60),

    Dense(50),

    Dense(5)])


# Set the weights of the final layer of the new model to the weights of the final layer of the old model, but leaving other layers unchanged.

new_model.layers[-1].set_weights(old_model.layers[-1].get_weights())


# Assert that the weights of the final layer is the same, but other are not.

print (np.all(new_model.layers[-1].get_weights()[0] == old_model.layers[-1].get_weights()[0]))

>> True


print (np.all(new_model.layers[-2].get_weights()[0] == old_model.layers[-2].get_weights()[0]))

>> False


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

添加回答

舉報(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)