• 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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    示例二:

    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)
    
    
    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
  • 相关阅读:
    新能源汽车如何融合互联网,结合消费者习惯打造全新的营销策略?
    神经网络照片解读下载,神经网络识别图像原理
    深入解析Kafka消息丢失的原因与解决方案
    手撕C++入门基础
    npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
    Android核心组件:Activity
    Armv9读取cache内容:Direct access to internal memory
    Kubernetes学习篇之对象
    连续六个季度实现盈利改善,达达集团内外双重确定性凸显
    MySQL JDBC编程
  • 原文地址:https://blog.csdn.net/guoqingru0311/article/details/134514539