Keras训练一个基本体系化的分类模型流程案例

news/2024/6/19 6:25:52 标签: keras, 分类, 人工智能

Keras训练一个基本体系化的分类模型流程案例

在这里插入图片描述

import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils        # 导入keras提供的numpy工具包
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD

# 载入数据
(x_train,y_train),(x_test,y_test) = mnist.load_data()
print("x_shape:",x_train.shape,"y_shape:",y_train.shape)

# 将数据转换由(60000,28,28)转换为(60000,784),并进行归一化除以255
x_train = x_train.reshape(x_train.shape[0],-1)/255.0
x_test = x_test.reshape(x_test.shape[0], -1)/255.0
print(x_train.shape,x_test.shape)

# 将数据转换为one-hot编码的格式
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)

# 创建模型,输入是784个神经元,输出为10个神经元
model = Sequential(
    [Dense(units=10,input_dim=784,bias_initializer="one",activation="softmax")]
)

# 自定义优化器
sgd=SGD(lr=0.01)
# 编译模型,定义优化器,loss_function,训练过程中计算准确率
# model.compile(optimizer=sgd,loss="mse",metrics=["accuracy"],)   # 使用均方差作为损失函数
model.compile(optimizer=sgd,loss="categorical_crossentropy",metrics=["accuracy"],)   # 使用交叉熵作为损失函数

# 喂入数据集,并规定每次喂入32张图像,设定迭代周期
model.fit(x_train,y_train,batch_size=32,epochs=100)

# 评估模型
loss,accuracy = model.evaluate(x_test, y_test)
print("test losee:",loss,"accuracy:",accuracy)

示例二:

import pickle

from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import numpy as np

from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import SGD
from keras.regularizers import l2
import matplotlib.pyplot as plt


data_dict = pickle.load(open('./data.pickle', 'rb'))

data = data_dict['data']
labels = data_dict['labels']
for index_data,data_arr in enumerate(data):
    if len(data_arr) != 42:
        print('---删除掉异常元素---', index_data)
        del data[index_data]
        del labels[index_data]

data=np.vstack(data)

print('+++++++++++++++++++',data.shape)
labels = np.asarray(data_dict['labels'])

x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, shuffle=True, stratify=labels)

print(x_train.shape)


# 将数据集标签改为one-hot编码的格式
y_train = np_utils.to_categorical(y_train, num_classes=9)
y_test = np_utils.to_categorical(y_test, num_classes=9)


# 创建网络模型,输入是784个神经元,输出为10类
model = Sequential([
    Dense(units=200,input_dim=42,bias_initializer="one",activation="relu",kernel_regularizer=l2(0.0003)),
    # Dropout(0.5),

    Dense(units=500,bias_initializer="one",activation="relu",kernel_regularizer=l2(0.0003)),
    Dense(units=100,bias_initializer="one",activation="relu",kernel_regularizer=l2(0.0003)),
    # Dropout(0.5),
    Dense(units=9,bias_initializer="one",activation="softmax",kernel_regularizer=l2(0.0003))
])

# 定义优化器
sgd = SGD(lr=0.01)
# 编译模型,定义优化器,loss_function,训练过程计算准确率
model.compile(optimizer=sgd,loss="categorical_crossentropy",metrics=["accuracy"])

# 训练数据集
history =model.fit(x_train,y_train,batch_size=32,epochs=200,validation_data=(x_test,y_test))
model.save("classmodel.h5")  # 会保存成HDF5的文件,pip install h5py
# 此种模型保存方式为保存模型的通用方式,既可以保存模型的结构,又可以保存模型的参数

# 评估模型
loss,accuracy = model.evaluate(x_test, y_test)
print("test loss:",loss,"accuracy:",accuracy)



history_dict = history.history
print(history_dict.keys())

import matplotlib.pyplot as plt

"""********************绘制训练损失与验证损失的训练结果********************"""

# 纵坐标,所需绘制的数据
history_dict = history.history
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']

# 横坐标,步长
epochs = range(1, len(loss_values) + 1)

# 绘制图像
plt.plot(epochs, loss_values, 'bo', label='Training loss')
plt.plot(epochs, val_loss_values, 'b', label='Validation loss')

# 标题
plt.title('Training and validation loss')

# 横、纵坐标标签
plt.xlabel('Epochs')
plt.ylabel('Loss')

# 自适应标签的位置
plt.legend()

# 显示图像
plt.show()


"""********************绘制训练精度与验证精度的训练结果********************"""
# 清除图像
plt.clf()

acc = history_dict['acc']
val_acc = history_dict['val_acc']

plt.plot(epochs, val_acc, 'bo', label='Training acc')
plt.plot(epochs, acc, 'b', label='Validation acc')

plt.title('Training and validation acc')

plt.xlabel('Epochs')
plt.ylabel('Acc')

plt.legend()

plt.show()



# 加载模型并检测
from keras.models import load_model
model = load_model('classmodel.h5')    # 需要安装keras==2.0.4版本
result=model.predict(x_test)
print(result)









http://www.niftyadmin.cn/n/5197970.html

相关文章

机器学习赋予用户“超人”的能力来打开和控制虚拟现实中的工具

原创 | 文 BFT机器人 最近,剑桥的研究人员开发了一种虚拟现实应用程序,只需用户手部的移动即可打开和控制一系列3D建模工具。 来自剑桥大学的研究人员利用机器学习开发了“HotGestures”类似于许多桌面应用程序中使用的热键(快捷键&#xff…

苹果(Apple)公司的新产品开发流程(一)

目录 简介 ANPP CSDN学院推荐 作者简介 简介 苹果这家企业给人的长期印象就是颠覆和创新。 而流程跟创新似乎是完全不搭边的两个平行线: 流程是一个做事的标准,定义了权力的边界,对应人员按章办事;而创新的主旋律是发散&am…

Linux文件目录以及文件类型

文章目录 Home根目录 //bin/sbin/etc/root/lib/dev/proc/sys/tmp/boot/mnt/media/usr 文件类型 Home 当尝试使用gedit等编辑器保存文件时,系统默认通常会先打开个人用户的“家”(home)目录, 建议在通常情况下个人相关的内容也是保…

你听说过“消费多少返利多少的”模式吗?

今天分享一个新的销售套路,看懂套路奋斗节约3年,你听说过“消费多少返利多少的”模式吗? 消费报销模式就是消费者在平台的消费,根据贡献度和活跃度平台去把之前消费的模式,给你返本了甚至还额外给你补贴奖励&#xff…

Python loglog()函数

常用坐标下的图像显示 import matplotlib.pyplot as plt import numpy as np import mathplt.figure() x_input np.linspace(1, 10, 50) y_input x_input**2plt.plot(x_input, y_input,r-,linewidth2) plt.show()在loglog函数尺度下的曲线 plt.loglog(x_input, y_input,r-,…

python 就是随便玩玩,生成gif图,生成汉字图片,超级简单

文章目录 主方法调用LetterDrawingWordDoingImage 上图 你也想玩的话,可以直接上码云去看 码云链接 主方法调用 import analysisdata.WordDoingImage as WordDoingImage import analysisdata.LetterDrawing as LetterDrawingif __name__ __main__:# 输入的文本&a…

算法学习 day26

第二十六天 最大子数组和 53. 最大子数组和 - 力扣&#xff08;LeetCode&#xff09; 动态规划问题 class Solution {public int maxSubArray(int[] nums) {int len nums.length;int[] dp new int[len];dp[0] nums[0];int res dp[0];for(int i 1; i < len; i){dp[i] …

解决Spring Cloud整合Nacos与Gateway的探险之旅

&#x1f38f;&#xff1a;你只管努力&#xff0c;剩下的交给时间 &#x1f3e0; &#xff1a;小破站 解决Spring Cloud整合Nacos与Gateway的探险之旅&#xff08;报错汇总&#xff09; 前言Caused by: com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.channel.Abstra…