最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

keras導入weights方式

 更新時間:2020年06月12日 14:47:22   作者:wangyin_2014  
這篇文章主要介紹了keras導入weights方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

keras源碼engine中toplogy.py定義了加載權重的函數(shù):

load_weights(self, filepath, by_name=False)

其中默認by_name為False,這時候加載權重按照網(wǎng)絡拓撲結構加載,適合直接使用keras中自帶的網(wǎng)絡模型,如VGG16

VGG19/resnet50等,源碼描述如下:

If `by_name` is False (default) weights are loaded
based on the network's topology, meaning the architecture
should be the same as when the weights were saved.
Note that layers that don't have weights are not taken
into account in the topological ordering, so adding or
removing layers is fine as long as they don't have weights.

若將by_name改為True則加載權重按照layer的name進行,layer的name相同時加載權重,適合用于改變了

模型的相關結構或增加了節(jié)點但利用了原網(wǎng)絡的主體結構情況下使用,源碼描述如下:

If `by_name` is True, weights are loaded into layers
only if they share the same name. This is useful
for fine-tuning or transfer-learning models where
some of the layers have changed.

在進行邊緣檢測時,利用VGG網(wǎng)絡的主體結構,網(wǎng)絡中增加反卷積層,這時加載權重應該使用

model.load_weights(filepath,by_name=True)

補充知識:Keras下實現(xiàn)mnist手寫數(shù)字

之前一直在用tensorflow,被同學推薦來用keras了,把之前文檔中的mnist手寫數(shù)字數(shù)據(jù)集拿來練手,

代碼如下。

import struct
import numpy as np
import os
 
import keras
from keras.models import Sequential 
from keras.layers import Dense
from keras.optimizers import SGD
 
def load_mnist(path, kind):
  labels_path = os.path.join(path, '%s-labels.idx1-ubyte' % kind)
  images_path = os.path.join(path, '%s-images.idx3-ubyte' % kind)
  with open(labels_path, 'rb') as lbpath:
    magic, n = struct.unpack('>II', lbpath.read(8))
    labels = np.fromfile(lbpath, dtype=np.uint8)
  with open(images_path, 'rb') as imgpath:
    magic, num, rows, cols = struct.unpack(">IIII", imgpath.read(16))
    images = np.fromfile(imgpath, dtype=np.uint8).reshape(len(labels), 784) #28*28=784
  return images, labels
 
#loading train and test data
X_train, Y_train = load_mnist('.\\data', kind='train')
X_test, Y_test = load_mnist('.\\data', kind='t10k')
 
#turn labels to one_hot code
Y_train_ohe = keras.utils.to_categorical(Y_train, num_classes=10)
 
#define models
model = Sequential()
 
model.add(Dense(input_dim=X_train.shape[1],output_dim=50,init='uniform',activation='tanh'))
model.add(Dense(input_dim=50,output_dim=50,init='uniform',activation='tanh'))
model.add(Dense(input_dim=50,output_dim=Y_train_ohe.shape[1],init='uniform',activation='softmax')) 
 
sgd = SGD(lr=0.001, decay=1e-7, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=["accuracy"])
 
#start training
model.fit(X_train,Y_train_ohe,epochs=50,batch_size=300,shuffle=True,verbose=1,validation_split=0.3)
 
#count accuracy
y_train_pred = model.predict_classes(X_train, verbose=0)
 
train_acc = np.sum(Y_train == y_train_pred, axis=0) / X_train.shape[0] 
print('Training accuracy: %.2f%%' % (train_acc * 100))
 
y_test_pred = model.predict_classes(X_test, verbose=0)
test_acc = np.sum(Y_test == y_test_pred, axis=0) / X_test.shape[0] 
print('Test accuracy: %.2f%%' % (test_acc * 100))

訓練結果如下:

Epoch 45/50
42000/42000 [==============================] - 1s 17us/step - loss: 0.2174 - acc: 0.9380 - val_loss: 0.2341 - val_acc: 0.9323
Epoch 46/50
42000/42000 [==============================] - 1s 17us/step - loss: 0.2061 - acc: 0.9404 - val_loss: 0.2244 - val_acc: 0.9358
Epoch 47/50
42000/42000 [==============================] - 1s 17us/step - loss: 0.1994 - acc: 0.9413 - val_loss: 0.2295 - val_acc: 0.9347
Epoch 48/50
42000/42000 [==============================] - 1s 17us/step - loss: 0.2003 - acc: 0.9413 - val_loss: 0.2224 - val_acc: 0.9350
Epoch 49/50
42000/42000 [==============================] - 1s 18us/step - loss: 0.2013 - acc: 0.9417 - val_loss: 0.2248 - val_acc: 0.9359
Epoch 50/50
42000/42000 [==============================] - 1s 17us/step - loss: 0.1960 - acc: 0.9433 - val_loss: 0.2300 - val_acc: 0.9346
Training accuracy: 94.11%
Test accuracy: 93.61%

以上這篇keras導入weights方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 如何將自己的python庫打包成wheel文件并上傳到pypi

    如何將自己的python庫打包成wheel文件并上傳到pypi

    這篇文章主要介紹了如何將自己的python庫打包成wheel文件并上傳到pypi,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • Matplotlib使用和繪制二維圖表教程

    Matplotlib使用和繪制二維圖表教程

    Matplotlib是一個強大的Python繪圖庫,可以用來繪制各種靜態(tài)、動態(tài)和交互式的圖表,文章介紹了Matplotlib的基本概念、繪制折線圖、散點圖、柱狀圖、直方圖和餅圖等方法,并詳細解釋了Matplotlib的三層結構
    2025-02-02
  • np.random.seed() 的使用詳解

    np.random.seed() 的使用詳解

    這篇文章主要介紹了np.random.seed() 的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-01-01
  • python中filter,map,reduce的作用

    python中filter,map,reduce的作用

    這篇文章主要介紹了python中filter,map,reduce的作用,文章首先通過map函數(shù)展開,map主要作用是計算一個序列或者多個序列進行函數(shù)映射之后的值,感興趣的朋友可以參考一下
    2022-06-06
  • python實現(xiàn)把二維列表變?yōu)橐痪S列表的方法分析

    python實現(xiàn)把二維列表變?yōu)橐痪S列表的方法分析

    這篇文章主要介紹了python實現(xiàn)把二維列表變?yōu)橐痪S列表的方法,結合實例形式總結分析了Python列表推導式、嵌套、循環(huán)等相關操作技巧,需要的朋友可以參考下
    2019-10-10
  • PyTorch 如何檢查模型梯度是否可導

    PyTorch 如何檢查模型梯度是否可導

    這篇文章主要介紹了PyTorch 檢查模型梯度是否可導的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python+Pygame實戰(zhàn)之俄羅斯方塊游戲的實現(xiàn)

    Python+Pygame實戰(zhàn)之俄羅斯方塊游戲的實現(xiàn)

    俄羅斯方塊,作為是一款家喻戶曉的游戲,陪伴70、80甚至90后,度過無憂的兒時歲月,它上手簡單能自由組合、拼接技巧也很多。本文就來用Python中的Pygame模塊實現(xiàn)這一經(jīng)典游戲,需要的可以參考一下
    2022-12-12
  • 深度剖析使用python抓取網(wǎng)頁正文的源碼

    深度剖析使用python抓取網(wǎng)頁正文的源碼

    平時打開一個網(wǎng)頁,除了文章的正文內容,通常會有一大堆的導航,廣告和其他方面的信息。本文的目的,在于說明如何從一個網(wǎng)頁中提取出文章的正文內容,而過渡掉其他無關的的信息。
    2014-06-06
  • python 列表刪除所有指定元素的方法

    python 列表刪除所有指定元素的方法

    下面小編就為大家分享一篇python 列表刪除所有指定元素的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • 12個步驟教你理解Python裝飾器

    12個步驟教你理解Python裝飾器

    這篇文章主要介紹了12個步驟教你理解Python裝飾器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07

最新評論

建阳市| 全南县| 闵行区| 赫章县| 融水| 怀来县| 阿城市| 灌云县| 如东县| 克什克腾旗| 志丹县| 昌黎县| 云林县| 平凉市| 和静县| 六盘水市| 北流市| 临沂市| 仁寿县| 南城县| 涿州市| 和静县| 成武县| 宁城县| 察雅县| 临朐县| 故城县| 年辖:市辖区| 乌拉特前旗| 渭南市| 桃江县| 沁阳市| 晋江市| 夹江县| 射阳县| 梓潼县| 星座| 宁陵县| 孟州市| 富顺县| 上犹县|