日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

ValueError: You are trying to load a weight file containing 12 layers into a model with 2 layers.

發(fā)布時(shí)間:2023/12/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ValueError: You are trying to load a weight file containing 12 layers into a model with 2 layers. 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文解決兩個(gè)問(wèn)題:

1、ValueError: You are trying to load a weight file containing 12 layers into a model with 2 layers.

2、The name "embedding_1_input" is used 3 times in the model?



第一個(gè)問(wèn)題:

相當(dāng)麻煩的一個(gè)問(wèn)題,百度和google都沒(méi)有

而且似乎有時(shí)候不能重現(xiàn),有時(shí)候又莫名其妙消失了。

結(jié)果實(shí)驗(yàn),不要使用下面的語(yǔ)句,原因是,這種語(yǔ)句不能針對(duì)使用Merge類(lèi)建立的嵌套型神經(jīng)網(wǎng)絡(luò),


這種報(bào)錯(cuò)在何種情況下會(huì)發(fā)生呢?

假定

model_left、model_right和model_3是三個(gè)神經(jīng)網(wǎng)絡(luò)

然后用以下代碼讓這三個(gè)子神經(jīng)網(wǎng)絡(luò)組合 merged = Merge([model_left, model_right,model_3], mode='concat') #merge model = Sequential() model.add(merged) # add merge
然后輸入

model.save('./model/my_model_weights.h5') model=load_model('./model/my_model_weights.h5')

就會(huì)報(bào)錯(cuò)

ValueError: You are trying to load a weight file containing 12 layers into a model with 2 layers.

因?yàn)檫@個(gè)神經(jīng)網(wǎng)絡(luò)是嵌套的,也算是這個(gè)python庫(kù)的一個(gè)bug吧。

改用以下代碼即可。

model.save_weights('test.h5') model.load_weights('test.h5',by_name=True) json_string = model.to_json() model = model_from_json(json_string)


第二個(gè)問(wèn)題:

但是,由于該神經(jīng)網(wǎng)絡(luò)存在嵌套

即使改為以上代碼后,想要在加載模型后。使用plot_model繪制神經(jīng)網(wǎng)絡(luò),也同樣是不可能的,會(huì)有如下報(bào)錯(cuò):

The name "embedding_1_input" is used 3 times in the model

具體報(bào)錯(cuò):

RuntimeError: (u'The name "embedding_1_input" is used 3 times in the model. All layer names should be unique. Layer names: ', [u'embedding_1_input',u'embedding_1_input',u'embedding_1_input',u'embedding_1',u'embedding_1',u'embedding_1',u'conv1d_1',u'conv1d_4',u'conv1d_7',u'max_pooling1d_1',u'max_pooling1d_4',u'max_pooling1d_7',u'conv1d_2',u'conv1d_5',u'conv1d_8',u'max_pooling1d_2',u'max_pooling1d_5',u'max_pooling1d_8',u'conv1d_3',u'conv1d_6',u'conv1d_9',u'max_pooling1d_3',u'max_pooling1d_6',u'max_pooling1d_9',u'flatten_1',u'flatten_2',u'flatten_3',u'merge_1',u'dense_1',u'dense_2' ])
所以對(duì)于這種嵌套型神經(jīng)網(wǎng)絡(luò),想要繪制神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)圖,在進(jìn)行訓(xùn)練模型后就要立刻使用?

plot_model(model, to_file='model.png')不要在保存模型、加載模型后再去使用上面這句代碼,是會(huì)報(bào)錯(cuò)有l(wèi)ayer重名的。

總結(jié)

以上是生活随笔為你收集整理的ValueError: You are trying to load a weight file containing 12 layers into a model with 2 layers.的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。