keras 数据集学习笔记 2/3

作者: gaoshine | 来源:发表于2017-09-26 20:59 被阅读665次

    keras 数据集的学习笔记 2/3

    上次学习一些常用的数据集,本次将学习数据集的具体应用。

    1. 各种常用的数据集
    2. 数据集如何使用
    3. 定义自己的数据集


    深度学习四部曲

    1. 找数据
    2. 搭模型
    3. 做训练
    4. 搞评测
    # 1.找数据
    # 考虑到 MNIST的使用的广泛性,我们先从 MNIST数据集入手
    # MNIST手写数字识别
    
    
    %matplotlib inline
    from keras.datasets import mnist
    import matplotlib.pyplot as plt
    
    (X_train, y_train), (X_test, y_test) = mnist.load_data()
    
    print ('MNIST数据集的形状:', X_train.shape)
    print ('训练集的第一个数的向量:', X_train[0])
    print ('训练集的第一个数的标签:', y_train[0])
    
    # 可视化数据集
    import matplotlib.pyplot as plt
    %matplotlib inline  
    img = X_train[0]
    title = y_train[0]
    plt.imshow(img)
    plt.title(title)
    plt.show()
    
    
    MNIST数据集的形状: (60000, 28, 28)
    训练集的第一个数的向量: [[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   3  18  18  18 126 136
      175  26 166 255 247 127   0   0   0   0]
     [  0   0   0   0   0   0   0   0  30  36  94 154 170 253 253 253 253 253
      225 172 253 242 195  64   0   0   0   0]
     [  0   0   0   0   0   0   0  49 238 253 253 253 253 253 253 253 253 251
       93  82  82  56  39   0   0   0   0   0]
     [  0   0   0   0   0   0   0  18 219 253 253 253 253 253 198 182 247 241
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0  80 156 107 253 253 205  11   0  43 154
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0  14   1 154 253  90   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0 139 253 190   2   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0  11 190 253  70   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0  35 241 225 160 108   1
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0  81 240 253 253 119
       25   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0  45 186 253 253
      150  27   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  16  93 252
      253 187   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 249
      253 249  64   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0  46 130 183 253
      253 207   2   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0  39 148 229 253 253 253
      250 182   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0  24 114 221 253 253 253 253 201
       78   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0  23  66 213 253 253 253 253 198  81   2
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0  18 171 219 253 253 253 253 195  80   9   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0  55 172 226 253 253 253 253 244 133  11   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0 136 253 253 253 212 135 132  16   0   0   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]
     [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
        0   0   0   0   0   0   0   0   0   0]]
    训练集的第一个数的标签: 5
    
    output_2_1.png

    这个大家应该很熟悉了吧,上次运行的MNIST数据集, 有60,000个用于训练的28*28的灰度手写数字图片,10,000个测试图片

    # 搭模型
    # 对于MNIST比较简单的(28*28的灰度图片)数据,我们先做个简单的数据模型吧模型吧
    # 2个层的
    
    
    from keras.models import Sequential
    from keras.layers import Dense, Activation
    
    
    from IPython.display import SVG
    from keras.utils.vis_utils import model_to_dot
    import pandas as pd
    import matplotlib.pyplot as plt
    
    # 定义模型
    model = Sequential()
    
    model.add(Dense(32, input_shape=(784,)))
    model.add(Activation('relu'))
    model.add(Dense(10))
    model.add(Activation('softmax'))
    
    # 编译模型
    model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])
    
    # 模型可视化
    model.summary()
    SVG(model_to_dot(model,show_shapes=True).create(prog='dot', format='svg'))
    
    
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    dense_1 (Dense)              (None, 32)                25120     
    _________________________________________________________________
    activation_1 (Activation)    (None, 32)                0         
    _________________________________________________________________
    dense_2 (Dense)              (None, 10)                330       
    _________________________________________________________________
    activation_2 (Activation)    (None, 10)                0         
    =================================================================
    Total params: 25,450
    Trainable params: 25,450
    Non-trainable params: 0
    _________________________________________________________________
    

    以上的模型简单也清楚

    1. 28*28的特征输入 input shape =784
    2. 全连接层(32)激活函数使用relu
    3. 全连接层(10)激活函数softmax(用于分类)
      看看上图,已经很清楚了。
      优化器采用了rmsprop,梯度下降loss采用了categorical_crossentropy,具体一些参数应用可以参考官方的keras的手册
    # 做训练
    from keras.utils import np_utils
    # 在进行数据处理之前,需要做一下数据的预处理,一般包括数据清洗,归一化等等
    X_train = X_train.reshape(X_train.shape[0], -1) / 255.   # 归一化
    X_test = X_test.reshape(X_test.shape[0], -1) / 255.      # 归一化
    
    # 分类标签要做onehot处理
    y_train = np_utils.to_categorical(y_train, num_classes=10)
    y_test = np_utils.to_categorical(y_test, num_classes=10)
    
    

    数据预处理是数据进入训练以前必须要做的工作,包括数据清洗,归一化等等

    keras内部自带的数据集,像MINIST、CIFAR10等等都是非常标准的数据集,在实际应用中你找不到如此好的数据集,不得不做大量的数据清洗、数据标注等工作,这些都是看起来高大上的深度学习领域中的力气活。

    预处理完成后,可以把数据送入编译好的模型中训练。

    print('开始训练 ------------')
    model.fit(X_train, y_train, epochs=2, batch_size=32)
    
    print('\n测试中 ------------')
    loss, accuracy = model.evaluate(X_test, y_test)
    
    print('测试的 loss: ', loss)
    print('测试的 accuracy: ', accuracy)
    
    开始训练 ------------
    Epoch 1/2
    60000/60000 [==============================] - 6s - loss: 0.5595 - acc: 0.8537     
    Epoch 2/2
    60000/60000 [==============================] - 5s - loss: 0.4568 - acc: 0.8777     
    
    测试中 ------------
     9024/10000 [==========================>...] - ETA: 0s测试的 loss:  0.409912618136
    测试的 accuracy:  0.8912
    

    因为minist数据集不大,所以很快就会训练完毕,我们发现测试的losss降到 0.45 正确率到了0.877,貌似还不是很理想。
    我们看2个重要的参数 epochs=2, batch_size=32 ,epochs是训练几轮,batch_size是批次的数据数量
    我们试图改变一下 把 epochs从2 变成 10看看

    print('开始训练 ------------')
    model.fit(X_train, y_train, epochs=10, batch_size=32)
    
    print('\n测试中 ------------')
    loss, accuracy = model.evaluate(X_test, y_test)
    
    print('测试的 loss: ', loss)
    print('测试的 accuracy: ', accuracy)
    
    开始训练 ------------
    Epoch 1/10
    60000/60000 [==============================] - 5s - loss: 0.4029 - acc: 0.8901     
    Epoch 2/10
    60000/60000 [==============================] - 5s - loss: 0.3708 - acc: 0.8976     
    Epoch 3/10
    60000/60000 [==============================] - 5s - loss: 0.3503 - acc: 0.9018     
    Epoch 4/10
    60000/60000 [==============================] - 5s - loss: 0.3356 - acc: 0.9055     
    Epoch 5/10
    60000/60000 [==============================] - 5s - loss: 0.3246 - acc: 0.9079     
    Epoch 6/10
    60000/60000 [==============================] - 5s - loss: 0.3159 - acc: 0.9095     
    Epoch 7/10
    60000/60000 [==============================] - 5s - loss: 0.3089 - acc: 0.9115     
    Epoch 8/10
    60000/60000 [==============================] - 5s - loss: 0.3028 - acc: 0.9138     
    Epoch 9/10
    60000/60000 [==============================] - 5s - loss: 0.2972 - acc: 0.9148     
    Epoch 10/10
    60000/60000 [==============================] - 5s - loss: 0.2926 - acc: 0.9159     
    
    测试中 ------------
     9920/10000 [============================>.] - ETA: 0s测试的 loss:  0.283633694214
    测试的 accuracy:  0.9171
    

    很棒,我们只是简单的增加了10轮训练,结果loss就降为0.2926,正确率升到 0.9159,不错啊,恭喜一下吧!
    那么下一个问题就是,如果我们继续提高 epochs的数值,看看学习效果会不会继续提高呢?

    # 为了把学习的效率曲线显示出来,我们这次把loss和accuracy以曲线方式显示,这样很直观
    from IPython.display import SVG
    from keras.utils.vis_utils import model_to_dot
    import pandas as pd
    import matplotlib.pyplot as plt
    
    loss = []
    acc = []
    val_loss = []
    val_acc = []
    for iteration in range(1, 100):
    
        print('开始训练 ------------')
        hist = model.fit(X_train, y_train, epochs=1, batch_size=32, validation_data=(X_test, y_test))
    
        print(hist.history)
        loss.append(hist.history['loss'][0])
        acc.append(hist.history['acc'][0])
        val_loss.append(hist.history['val_loss'][0])
        val_acc.append(hist.history['val_acc'][0])    
    
    
    
    # 图形化整个训练过程
    ax = pd.DataFrame(
        {
            'val_loss': val_loss,
            'val_acc': val_acc,
            'loss': loss,
            'acc': acc,      
        }
    ).rolling(5).mean()[5:].plot(title='Training loss', logy=True)
    
    ax.set_xlabel("Epochs")
    ax.set_ylabel("Loss&Acc")
    
    开始训练 ------------
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 7s - loss: 0.2886 - acc: 0.9172 - val_loss: 0.2803 - val_acc: 0.9183
    {'val_loss': [0.28033412151634696], 'val_acc': [0.91830000000000001], 'loss': [0.28855849148035051], 'acc': [0.91715000000000002]}
    
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.2322 - acc: 0.9344 - val_loss: 0.2335 - val_acc: 0.9340
    {'val_loss': [0.23352896861284972], 'val_acc': [0.93400000000000005], 'loss': [0.23224472224811712], 'acc': [0.93438333333333334]}
    
    60000/60000 [==============================] - 6s - loss: 0.2182 - acc: 0.9388 - val_loss: 0.2232 - val_acc: 0.9345
    {'val_loss': [0.22320981065034867], 'val_acc': [0.9345], 'loss': [0.21823361753970386], 'acc': [0.9388333333333333]}
    
    开始训练 ------------
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.2070 - acc: 0.9423 - val_loss: 0.2130 - val_acc: 0.9396
    {'val_loss': [0.21298817498832942], 'val_acc': [0.93959999999999999], 'loss': [0.2070259754722317], 'acc': [0.94225000000000003]}
    
    开始训练 ------------
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1983 - acc: 0.9444 - val_loss: 0.2076 - val_acc: 0.9414
    {'val_loss': [0.20755480074435473], 'val_acc': [0.94140000000000001], 'loss': [0.19831858826875687], 'acc': [0.94443333333333335]}
    开始训练 ------------
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 7s - loss: 0.1970 - acc: 0.9450 - val_loss: 0.2069 - val_acc: 0.9411
    {'val_loss': [0.20689115332663061], 'val_acc': [0.94110000000000005], 'loss': [0.1969709436525901], 'acc': [0.94496666666666662]}
    开始训练 ------------
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1958 - acc: 0.9449 - val_loss: 0.2053 - val_acc: 0.9414
    {'val_loss': [0.20527385253384708], 'val_acc': [0.94140000000000001], 'loss': [0.19576028747359911], 'acc': [0.94484999999999997]}
    开始训练 ------------
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1945 - acc: 0.9459 - val_loss: 0.2029 - val_acc: 0.9424
    {'val_loss': [0.20291464587822555], 'val_acc': [0.94240000000000002], 'loss': [0.1944937266851465], 'acc': [0.94589999999999996]}
    
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1628 - acc: 0.9546 - val_loss: 0.1803 - val_acc: 0.9497
    {'val_loss': [0.18027099168971181], 'val_acc': [0.94969999999999999], 'loss': [0.16278639560242494], 'acc': [0.95461666666666667]}
    
    开始训练 ------------
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 5s - loss: 0.1590 - acc: 0.9553 - val_loss: 0.1798 - val_acc: 0.9498
    {'val_loss': [0.17983917704746127], 'val_acc': [0.94979999999999998], 'loss': [0.15900170163338384], 'acc': [0.9553166666666667]}
    开始训练 ------------
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1589 - acc: 0.9558 - val_loss: 0.1780 - val_acc: 0.9504
    {'val_loss': [0.17799708335101605], 'val_acc': [0.95040000000000002], 'loss': [0.15885208675066631], 'acc': [0.95579999999999998]}
    
    开始训练 ------------
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1561 - acc: 0.9564 - val_loss: 0.1760 - val_acc: 0.9516
    {'val_loss': [0.17602033813893794], 'val_acc': [0.9516], 'loss': [0.15613150059555966], 'acc': [0.95638333333333336]}
    开始训练 ------------
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1558 - acc: 0.9571 - val_loss: 0.1778 - val_acc: 0.9498
    {'val_loss': [0.17784477516561747], 'val_acc': [0.94979999999999998], 'loss': [0.15577643673568964], 'acc': [0.95711666666666662]}
    开始训练 ------------
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1554 - acc: 0.9564 - val_loss: 0.1758 - val_acc: 0.9514
    {'val_loss': [0.17576773921772837], 'val_acc': [0.95140000000000002], 'loss': [0.15542485673725606], 'acc': [0.95640000000000003]}
    
    开始训练 ------------
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1523 - acc: 0.9575 - val_loss: 0.1732 - val_acc: 0.9529
    {'val_loss': [0.17322715676948428], 'val_acc': [0.95289999999999997], 'loss': [0.15226549789408841], 'acc': [0.95746666666666669]}
    开始训练 ------------
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1522 - acc: 0.9571 - val_loss: 0.1733 - val_acc: 0.9524
    {'val_loss': [0.17329887943789363], 'val_acc': [0.95240000000000002], 'loss': [0.15217291439001759], 'acc': [0.95713333333333328]}
    
    output_12_2.png
    # 图形化整个训练过程
    ax = pd.DataFrame(
        {
            'val_loss': val_loss,
            'val_acc': val_acc,
            'loss': loss,
            'acc': acc,      
        }
    ).rolling(5).mean()[5:].plot(title='Training loss', logy=True)
    
    ax.set_xlabel("Epochs")
    ax.set_ylabel("Loss&Acc")
    
    output_13_1.png

    我们发现通过增大训练次数,正确率不断攀升,错误率也是不断下降,只是趋势越来越趋于缓和。
    我们再进行100次训练 看看曲线如何?

    # 为了把学习的效率曲线显示出来,我们这次把loss和accuracy以曲线方式显示,这样很直观
    from IPython.display import SVG
    from keras.utils.vis_utils import model_to_dot
    import pandas as pd
    import matplotlib.pyplot as plt
    
    loss = []
    acc = []
    val_loss = []
    val_acc = []
    for iteration in range(1, 100):
    
        print('开始训练 %d: ------------', iteration)
        hist = model.fit(X_train, y_train, epochs=1, batch_size=32, validation_data=(X_test, y_test))
    
        #print(hist.history)
        loss.append(hist.history['loss'][0])
        acc.append(hist.history['acc'][0])
        val_loss.append(hist.history['val_loss'][0])
        val_acc.append(hist.history['val_acc'][0])    
    
    
    
    # 图形化整个训练过程
    ax = pd.DataFrame(
        {
            'val_loss': val_loss,
            'val_acc': val_acc,
            'loss': loss,
            'acc': acc,      
        }
    ).rolling(5).mean()[5:].plot(title='Training loss', logy=True)
    
    ax.set_xlabel("Epochs")
    ax.set_ylabel("Loss&Acc")
    
    开始训练 %d: ------------ 1
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 7s - loss: 0.1513 - acc: 0.9579 - val_loss: 0.1743 - val_acc: 0.9530
    开始训练 %d: ------------ 2
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1511 - acc: 0.9576 - val_loss: 0.1741 - val_acc: 0.9527
    开始训练 %d: ------------ 3
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1505 - acc: 0.9579 - val_loss: 0.1736 - val_acc: 0.9519
    开始训练 %d: ------------ 4
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1498 - acc: 0.9580 - val_loss: 0.1743 - val_acc: 0.9523
    开始训练 %d: ------------ 5
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1490 - acc: 0.9587 - val_loss: 0.1716 - val_acc: 0.9529
    开始训练 %d: ------------ 6
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1486 - acc: 0.9583 - val_loss: 0.1722 - val_acc: 0.9534
    开始训练 %d: ------------ 7
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1478 - acc: 0.9587 - val_loss: 0.1711 - val_acc: 0.9539
    开始训练 %d: ------------ 8
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1476 - acc: 0.9590 - val_loss: 0.1713 - val_acc: 0.9533
    开始训练 %d: ------------ 9
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1472 - acc: 0.9588 - val_loss: 0.1725 - val_acc: 0.9533
    开始训练 %d: ------------ 10
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1472 - acc: 0.9587 - val_loss: 0.1715 - val_acc: 0.9527
    开始训练 %d: ------------ 11
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1465 - acc: 
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1236 - acc: 0.9664 - val_loss: 0.1611 - val_acc: 0.9606
    开始训练 %d: ------------ 98
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1233 - acc: 0.9663 - val_loss: 0.1629 - val_acc: 0.9593
    开始训练 %d: ------------ 99
    Train on 60000 samples, validate on 10000 samples
    Epoch 1/1
    60000/60000 [==============================] - 6s - loss: 0.1231 - acc: 0.9663 - val_loss: 0.1609 - val_acc: 0.9593
    
    output_15_2.png

    我们又做了100次训练,对比上次的100次,发现曲线几乎不再下降了,就是说继续学习下去也不会有太大效果了。深度学习里的术语是:数据不再收敛了。

    本次训练的过程:

    1. 先做2次 loss: 0.4568 - acc: 0.8777 val_loss: 0.409912618136 val_ acc: 0.8912
    2. 再做10次 loss: 0.2926 - acc: 0.9159 val_loss: 0.283633694214 val acc: 0.9171
    3. 再做100次 loss: 0.1522 - acc: 0.9571 - val_loss: 0.1733 - val_acc: 0.9524
    4. 继续做100次 loss: 0.1231 - acc: 0.9663 - val_loss: 0.1609 - val_acc: 0.9593

    如果不考虑继续优化模型,我们一般认为,这个时候已经训练完成了,那么我们下一步就是把训练的成果保留住,避免每次使用时都重新训练学习,直接使用训练成果。

    from keras.models import Sequential
    from keras.layers import Dense, Activation
    model.summary()
    
    # 搞评测 
    # 这种方式也可以用于部署训练好的模型
    
    from keras.models import load_model
    
    # 保存模型和训练的权重
    model.save('my_model.h5')
    
    del model  # 删除模型
    
    
    
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    dense_1 (Dense)              (None, 32)                25120     
    _________________________________________________________________
    activation_1 (Activation)    (None, 32)                0         
    _________________________________________________________________
    dense_2 (Dense)              (None, 10)                330       
    _________________________________________________________________
    activation_2 (Activation)    (None, 10)                0         
    =================================================================
    Total params: 25,450
    Trainable params: 25,450
    Non-trainable params: 0
    _________________________________________________________________
    
    
    # 加载训练好的模型
    model = load_model('my_model.h5')
    model.summary()
    # 测试一下
    print('模型加载后测试: ', model.predict(X_test[0:1]),y_test[0:1])
    
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    dense_1 (Dense)              (None, 32)                25120     
    _________________________________________________________________
    activation_1 (Activation)    (None, 32)                0         
    _________________________________________________________________
    dense_2 (Dense)              (None, 10)                330       
    _________________________________________________________________
    activation_2 (Activation)    (None, 10)                0         
    =================================================================
    Total params: 25,450
    Trainable params: 25,450
    Non-trainable params: 0
    _________________________________________________________________
    模型加载后测试:  [[  3.08926704e-11   1.18216120e-17   1.04918041e-09   1.81104348e-03
        3.38958772e-09   1.17798572e-06   1.94675519e-20   9.98184144e-01
        5.47902061e-07   2.99487897e-06]] [[ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]]
    

    以后的代码只加载预训练文件 my_model.h5 就可以了,这个文件包括了模型和训练好的权重。
    加载后,就可以使用 model.predict 直接预测结果。我们打印出 X_test[0:1]),y_test[0:1] 看到学习的还不错,都预测对了

    相关文章

      网友评论

        本文标题:keras 数据集学习笔记 2/3

        本文链接:https://www.haomeiwen.com/subject/ziueextx.html