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)








你可能感兴趣的:(keras,分类,人工智能)