亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何實現keras模型參數,模型保存,中間結果輸出

發布時間:2020-07-07 10:28:21 來源:億速云 閱讀:305 作者:清晨 欄目:開發技術

這篇文章將為大家詳細講解有關如何實現keras模型參數,模型保存,中間結果輸出,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

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

'''
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}.h6'
# filepath = 'D:\\machineTest\\model-ep{epoch:03d}-loss{loss:.3f}.h6'
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)
# #獲取模型結構狀況
# 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 )    
# =================================================================
# #根據下標和名稱返回層對象
# layer = model.get_layer(index = 0)
# 獲取模型權重,設置權重model.set_weights()
weights = np.array(model.get_weights())
print(weights.shape)
# (4,)權重由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

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

# 查看中間結果,必須要先聲明個函數式模型
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)

# 如果是函數式模型,則可以直接輸出
# 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)

中間結果輸出可以查看conv過之后的圖像:

原始圖像:

如何實現keras模型參數,模型保存,中間結果輸出

經過一層conv以后,輸出其中4張圖片:

如何實現keras模型參數,模型保存,中間結果輸出

如何實現keras模型參數,模型保存,中間結果輸出

如何實現keras模型參數,模型保存,中間結果輸出

如何實現keras模型參數,模型保存,中間結果輸出

關于如何實現keras模型參數,模型保存,中間結果輸出就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

临猗县| 岚皋县| 曲松县| 桐乡市| 金阳县| 都江堰市| 东丽区| 顺平县| 渭南市| 芦溪县| 台安县| 揭东县| 鸡泽县| 永修县| 丹寨县| 宁晋县| 海盐县| 巫山县| 甘谷县| 襄汾县| 广丰县| 兴山县| 剑河县| 徐水县| 遵化市| 定州市| 台东县| 岳池县| 寻乌县| 淳安县| 丰宁| 弥渡县| 丽水市| 明星| 梓潼县| 土默特左旗| 堆龙德庆县| 壤塘县| 青田县| 盐边县| 新绛县|