欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

keras 模型參數(shù),模型保存,中間結(jié)果輸出操作

 更新時(shí)間:2020年07月06日 16:22:41   作者:姚賢賢  
這篇文章主要介紹了keras 模型參數(shù),模型保存,中間結(jié)果輸出操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

我就廢話不多說(shuō)了,大家還是直接看代碼吧~

'''
Created on 2018-4-16
'''
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.models import Model
from keras.callbacks import ModelCheckpoint,Callback
import numpy as np
import tflearn
import tflearn.datasets.mnist as mnist

x_train, y_train, x_test, y_test = mnist.load_data(one_hot=True)
x_valid = x_test[:5000]
y_valid = y_test[:5000]
x_test = x_test[5000:]
y_test = y_test[5000:]
print(x_valid.shape)
print(x_test.shape)

model = Sequential()
model.add(Dense(units=64, activation='relu', input_dim=784))
model.add(Dense(units=10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
       optimizer='sgd',
       metrics=['accuracy'])
filepath = 'D:\\machineTest\\model-ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5'
# filepath = 'D:\\machineTest\\model-ep{epoch:03d}-loss{loss:.3f}.h5'
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
print(model.get_config())
# [{'class_name': 'Dense', 'config': {'bias_regularizer': None, 'use_bias': True, 'kernel_regularizer': None, 'batch_input_shape': (None, 784), 'trainable': True, 'kernel_constraint': None, 'bias_constraint': None, 'kernel_initializer': {'class_name': 'VarianceScaling', 'config': {'scale': 1.0, 'distribution': 'uniform', 'mode': 'fan_avg', 'seed': None}}, 'activity_regularizer': None, 'units': 64, 'dtype': 'float32', 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'activation': 'relu', 'name': 'dense_1'}}, {'class_name': 'Dense', 'config': {'bias_regularizer': None, 'use_bias': True, 'kernel_regularizer': None, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'kernel_constraint': None, 'bias_constraint': None, 'kernel_initializer': {'class_name': 'VarianceScaling', 'config': {'scale': 1.0, 'distribution': 'uniform', 'mode': 'fan_avg', 'seed': None}}, 'activity_regularizer': None, 'trainable': True, 'units': 10, 'activation': 'softmax', 'name': 'dense_2'}}]
# model.fit(x_train, y_train, epochs=1, batch_size=128, callbacks=[checkpoint],validation_data=(x_valid, y_valid))
model.fit(x_train, y_train, epochs=1,validation_data=(x_valid, y_valid),steps_per_epoch=10,validation_steps=1)
# score = model.evaluate(x_test, y_test, batch_size=128)
# print(score)
# #獲取模型結(jié)構(gòu)狀況
# model.summary()
# _________________________________________________________________
# Layer (type)         Output Shape       Param #  
# =================================================================
# dense_1 (Dense)       (None, 64)        50240(784*64+64(b))   
# _________________________________________________________________
# dense_2 (Dense)       (None, 10)        650(64*10 + 10 )    
# =================================================================
# #根據(jù)下標(biāo)和名稱返回層對(duì)象
# layer = model.get_layer(index = 0)
# 獲取模型權(quán)重,設(shè)置權(quán)重model.set_weights()
weights = np.array(model.get_weights())
print(weights.shape)
# (4,)權(quán)重由4部分組成
print(weights[0].shape)
# (784, 64)dense_1 w1
print(weights[1].shape)
# (64,)dense_1 b1
print(weights[2].shape)
# (64, 10)dense_2 w2
print(weights[3].shape)
# (10,)dense_2 b2

# # 保存權(quán)重和加載權(quán)重
# model.save_weights("D:\\xxx\\weights.h5")
# model.load_weights("D:\\xxx\\weights.h5", by_name=False)#by_name=True,可以根據(jù)名字匹配和層載入權(quán)重

# 查看中間結(jié)果,必須要先聲明個(gè)函數(shù)式模型
dense1_layer_model = Model(inputs=model.input,outputs=model.get_layer('dense_1').output)
out = dense1_layer_model.predict(x_test)
print(out.shape)
# (5000, 64)

# 如果是函數(shù)式模型,則可以直接輸出
# import keras
# from keras.models import Model
# from keras.callbacks import ModelCheckpoint,Callback
# import numpy as np
# from keras.layers import Input,Conv2D,MaxPooling2D
# import cv2
# 
# image = cv2.imread("D:\\machineTest\\falali.jpg")
# print(image.shape)
# cv2.imshow("1",image)
# 
# # 第一層conv
# image = image.reshape([-1, 386, 580, 3])
# img_input = Input(shape=(386, 580, 3))
# x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)
# x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)
# x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
# model = Model(inputs=img_input, outputs=x)
# out = model.predict(image)
# print(out.shape)
# out = out.reshape(193, 290,64)
# image_conv1 = out[:,:,1].reshape(193, 290)
# image_conv2 = out[:,:,20].reshape(193, 290)
# image_conv3 = out[:,:,40].reshape(193, 290)
# image_conv4 = out[:,:,60].reshape(193, 290)
# cv2.imshow("conv1",image_conv1)
# cv2.imshow("conv2",image_conv2)
# cv2.imshow("conv3",image_conv3)
# cv2.imshow("conv4",image_conv4)
# cv2.waitKey(0)

中間結(jié)果輸出可以查看conv過(guò)之后的圖像:

原始圖像:

經(jīng)過(guò)一層conv以后,輸出其中4張圖片:

以上這篇keras 模型參數(shù),模型保存,中間結(jié)果輸出操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python如何通過(guò)twisted搭建socket服務(wù)

    python如何通過(guò)twisted搭建socket服務(wù)

    這篇文章主要介紹了python如何通過(guò)twisted搭建socket服務(wù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Python 完美解決 Import “模塊“ could not be resolved ...的問(wèn)題

    Python 完美解決 Import “模塊“ could not&n

    這篇文章主要介紹了Python 完美解決 Import “模塊“ could not be resolved ...,本文給大家分享問(wèn)題原因及解決方法,需要的朋友可以參考下
    2022-11-11
  • Python的面向?qū)ο缶幊谭绞綄W(xué)習(xí)筆記

    Python的面向?qū)ο缶幊谭绞綄W(xué)習(xí)筆記

    Python深度具備面向?qū)ο缶幊陶Z(yǔ)言所應(yīng)有的特性,這里我們以類和方法為主,來(lái)整理一下Python的面向?qū)ο缶幊谭绞綄W(xué)習(xí)筆記:
    2016-07-07
  • 使用Python程序計(jì)算鋼琴88個(gè)鍵的音高

    使用Python程序計(jì)算鋼琴88個(gè)鍵的音高

    這篇文章介紹了使用Python程序計(jì)算鋼琴88個(gè)鍵的音高,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • python排列組合庫(kù)itertools的具體使用

    python排列組合庫(kù)itertools的具體使用

    排列組合是數(shù)學(xué)中必不可少的一部分, Python 提供了itertools庫(kù),該庫(kù)具有計(jì)算排列和組合的內(nèi)置函數(shù),本文主要介紹了python排列組合庫(kù)itertools的具體使用,具有一定的參考價(jià)值,感興趣的可以了解下
    2024-01-01
  • Python處理日期方法詳細(xì)大全(30種方法)

    Python處理日期方法詳細(xì)大全(30種方法)

    這篇文章主要給大家介紹了關(guān)于Python處理日期方法詳細(xì)大全,文中共介紹了30種方法,Python程序能用很多方式處理日期和時(shí)間,轉(zhuǎn)換日期格式是一個(gè)常見的功能,Python提供了一個(gè)time和calendar模塊可以用于格式化日期和時(shí)間,需要的朋友可以參考下
    2023-12-12
  • Python數(shù)據(jù)類型之列表和元組的方法實(shí)例詳解

    Python數(shù)據(jù)類型之列表和元組的方法實(shí)例詳解

    這篇文章主要介紹了Python數(shù)據(jù)類型之列表和元組的相關(guān)知識(shí),列表是一組有序項(xiàng)目的集合 ,可變的數(shù)據(jù)類型可 進(jìn)行增刪改查,本文通過(guò)實(shí)例文字相結(jié)合的形式給大家介紹的非常詳細(xì) ,需要的朋友可以參考下
    2019-07-07
  • python語(yǔ)言中有算法嗎

    python語(yǔ)言中有算法嗎

    在本篇文章里小編給大家整理的是一篇關(guān)于python里算法的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • python實(shí)現(xiàn)自動(dòng)發(fā)送郵件發(fā)送多人、群發(fā)、多附件的示例

    python實(shí)現(xiàn)自動(dòng)發(fā)送郵件發(fā)送多人、群發(fā)、多附件的示例

    下面小編就為大家分享一篇python實(shí)現(xiàn)自動(dòng)發(fā)送郵件發(fā)送多人、群發(fā)、多附件的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Python中的anydbm模版和shelve模版使用指南

    Python中的anydbm模版和shelve模版使用指南

    這篇文章主要介紹了Python中的anydbm模版和shelve模版使用指南,兩個(gè)模版都可用于數(shù)據(jù)存儲(chǔ)的序列化,需要的朋友可以參考下
    2015-07-07

最新評(píng)論