keras 模型参数,模型保存,中间结果输出操作

作者:姚贤贤 时间:2023-06-05 09:52:33 

我就废话不多说了,大家还是直接看代码吧~


'''
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)
# #获取模型结构状况
# 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.h5")
# model.load_weights("D:\\xxx\\weights.h5", 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 模型参数,模型保存,中间结果输出操作

来源:https://blog.csdn.net/u011311291/article/details/79963831

标签:keras,模型,参数,保存,结果
0
投稿

猜你喜欢

  • python实现电子词典

    2021-12-19 08:29:03
  • Django 中使用日志的方法

    2022-09-12 11:17:40
  • SWF FLASH的param属性参数详解

    2008-10-25 15:12:00
  • 原生JS实现左右箭头选择日期实例代码

    2023-08-06 04:55:27
  • PyCharm2019.3永久激活破解详细图文教程,亲测可用(不定期更新)

    2022-04-27 23:51:16
  • Python实现栈的方法详解【基于数组和单链表两种方法】

    2022-06-05 19:03:47
  • sqlserver 动态创建临时表的语句分享

    2024-01-17 23:57:45
  • Go语言基础知识点介绍

    2024-04-26 17:22:50
  • MySQL 查询的排序、分页相关

    2024-01-19 18:26:59
  • mysql查询的控制语句图文详解

    2024-01-27 00:02:38
  • Pytorch自动求导函数详解流程以及与TensorFlow搭建网络的对比

    2023-07-08 18:44:37
  • 支持鼠标拖拽的简单目录树代码

    2011-07-01 12:34:09
  • pandas DataFrame 交集并集补集的实现

    2023-05-02 12:03:27
  • Python PyQt5实现的简易计算器功能示例

    2022-01-22 12:03:19
  • MySQL数据入库时特殊字符处理详解

    2024-01-13 01:36:09
  • ASP.NET MVC4入门教程(二):添加一个控制器

    2024-05-11 09:26:25
  • 浅谈python连续赋值可能引发的错误

    2023-07-12 04:13:32
  • Linux下mysql 8.0安装教程

    2024-01-15 18:21:49
  • mysql中的日期相减的天数函数

    2024-01-20 01:00:51
  • PHP 修复未正常关闭的HTML标签实现代码(支持嵌套和就近闭合)

    2024-04-28 09:44:51
  • asp之家 网络编程 m.aspxhome.com