保存加载模型model.save()

当savemodel hd5 时

需要 metricstf.metrics.SparseCategoricalAcc uracy() 不能是 accuracy 字符串 否则when load model 测试精确度会有问题。将产生怀疑

    def create_model():
        model = tf.keras.models.Sequential([
            keras.layers.Dense(512, activation='relu', input_shape=(784,)),
            keras.layers.Dropout(0.2),
            keras.layers.Dense(10)
        ])

        model.compile(optimizer='adam',
                      loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
                      metrics=[tf.metrics.SparseCategoricalAccuracy()])

        return model
model = create_model()
    model.fit(train_images, train_labels, epochs=5)

    model.save('saved_model/my_model')
    new_model = tf.keras.models.load_model('saved_model/my_model')


    # # 将整个模型保存为 HDF5 文件。
    # # '.h5' 扩展名指示应将模型保存到 HDF5。
    # model.save('my_model.h5')
    #
    # # 重新创建完全相同的模型,包括其权重和优化程序
    # new_model = tf.keras.models.load_model('my_model.h5')

    # 显示网络结构
    new_model.summary()
    # new_model.fit(train_images, train_labels, epochs=5)
    loss, acc = new_model.evaluate(test_images, test_labels, verbose=2)
    print('Restored model, accuracy: {:5.2f}%'.format(100 * acc))

你可能感兴趣的:(tensorflow,深度学习)