美文网首页
keras----模型保存

keras----模型保存

作者: 陶_306c | 来源:发表于2021-04-24 20:45 被阅读0次

    一、模型保存

    
    inputs = Input(shape=(784, ))
    x = Dense(64, activation='relu')(inputs)
    x = Dense(64, activation='relu')(x)
    y = Dense(10, activation='softmax')(x)
     
    model = Model(inputs=inputs, outputs=y)
     
    model.save('m1.h5')
    model.summary()
    model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
    model.fit(x_train, y_train, batch_size=32, epochs=10)
    #loss,accuracy=model.evaluate(x_test,y_test)
     
    model.save('m2.h5')
    model.save_weights('m3.h5')
    
    

    m1保存的是模型图结构
    m2保存的是训练后的模型参数和模型图结构
    m3保存的是模型参数,没有保存图结构,加载的时候需要先构建模型结构。

    二、加载模型

    1、保存了模型结构的权重文件才能直接使用load_model直接加载

    from keras.models import load_model
     
    model = load_model('m1.h5')
    #model = load_model('m2.h5')
    model.summary()
    

    2、只保存了权重参数的文件需要重新构建模型结构才能加载。

    from keras.models import Model
    from keras.layers import Input, Dense
    
    inputs = Input(shape=(784, ))
    x = Dense(64, activation='relu')(inputs)
    x = Dense(64, activation='relu')(x)
    y = Dense(10, activation='softmax')(x)
     
    model = Model(inputs=inputs, outputs=y)
    model.load_weights('m3.h5')
    
    

    相关文章

      网友评论

          本文标题:keras----模型保存

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