• tensorflow(二十九):模型的保存


    一、第一种:只保存权值

     

     

    import tensorflow as tf
    from tensorflow.python.keras import datasets, layers, optimizers, Sequential, metrics
    import os
    
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    
    
    def preprocess(x, y):
        """
        x is a simple image, not a batch
        """
        x = tf.cast(x, dtype=tf.float32) / 255.
        x = tf.reshape(x, [28*28])
        y = tf.cast(y, dtype=tf.int32)
        y = tf.one_hot(y, depth=10)
        return x, y
    
    
    batchsz = 256*2
    (x, y), (x_val, y_val) = datasets.mnist.load_data()
    print('datasets:', x.shape, y.shape, x.min(), x.max())
    
    db = tf.data.Dataset.from_tensor_slices((x, y))
    db = db.map(preprocess).shuffle(60000).batch(batchsz)
    ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
    ds_val = ds_val.map(preprocess).batch(batchsz)
    
    sample = next(iter(db))
    print(sample[0].shape, sample[1].shape)
    
    network = Sequential([layers.Dense(256, activation='relu'),
                          layers.Dense(128, activation='relu'),
                          layers.Dense(64, activation='relu'),
                          layers.Dense(32, activation='relu'),
                          layers.Dense(10)])
    network.build(input_shape=(None, 28 * 28))
    network.summary()
    
    network.compile(optimizer=optimizers.Adam(lr=0.01),
                    loss=tf.losses.CategoricalCrossentropy(from_logits=True),
                    metrics=['accuracy']
                    )
    
    network.fit(db, epochs=4, validation_data=ds_val, validation_freq=2)
    
    network.evaluate(ds_val)
    
    network.save_weights('weight.ckpt')
    print('saved weights')
    del network
    
    # 这个创建过程必须和上面的过程一模一样。
    network = Sequential([layers.Dense(256, activation='relu'),
                         layers.Dense(128, activation='relu'),
                         layers.Dense(64, activation='relu'),
                         layers.Dense(32, activation='relu'),
                         layers.Dense(10)])
    network.compile(optimizer=optimizers.Adam(lr=0.01),
            loss=tf.losses.CategoricalCrossentropy(from_logits=True),
            metrics=['accuracy'])
    
    network.load_weights('weight.ckpt')
    print('loaded weights!')
    network.evaluate(ds_val)

    二、保存所有模型

    import tensorflow as tf
    from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics
    import os
    
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
    
    def preprocess(x, y):
        """
        x is a simple image, not a batch
        """
        x = tf.cast(x, dtype=tf.float32) / 255.
        x = tf.reshape(x, [28 * 28])
        y = tf.cast(y, dtype=tf.int32)
        y = tf.one_hot(y, depth=10)
        return x, y
    
    
    batchsz = 128
    (x, y), (x_val, y_val) = datasets.mnist.load_data()
    print('datasets:', x.shape, y.shape, x.min(), x.max())
    
    db = tf.data.Dataset.from_tensor_slices((x, y))
    db = db.map(preprocess).shuffle(60000).batch(batchsz)
    ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
    ds_val = ds_val.map(preprocess).batch(batchsz)
    
    sample = next(iter(db))
    print(sample[0].shape, sample[1].shape)
    
    network = Sequential([layers.Dense(256, activation='relu'),
                          layers.Dense(128, activation='relu'),
                          layers.Dense(64, activation='relu'),
                          layers.Dense(32, activation='relu'),
                          layers.Dense(10)])
    network.build(input_shape=(None, 28 * 28))
    network.summary()
    
    network.compile(optimizer=optimizers.Adam(lr=0.01),
                    loss=tf.losses.CategoricalCrossentropy(from_logits=True),
                    metrics=['accuracy']
                    )
    
    network.fit(db, epochs=3, validation_data=ds_val, validation_freq=2)
    
    network.evaluate(ds_val)
    
    network.save('./savemodel/model.h5')
    print('saved total model.')
    del network
    
    print('load model from file')
    network = tf.keras.models.load_model('./savemodel/model.h5')
    network.compile(optimizer=optimizers.Adam(lr=0.01),
            loss=tf.losses.CategoricalCrossentropy(from_logits=True),
            metrics=['accuracy'])
    x_val = tf.cast(x_val, dtype=tf.float32) / 255.
    x_val = tf.reshape(x_val, [-1, 28 * 28])
    y_val = tf.cast(y_val, dtype=tf.int32)
    y_val = tf.one_hot(y_val, depth=10)
    ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val)).batch(128)
    network.evaluate(ds_val)

    三、更通用(其他语言也能加载)

  • 相关阅读:
    [论文收集] ICWS 2008论文
    [论文笔记] The Impact of Service Pricing Models on Service Selection (ICIW, 2009)
    [论文摘录] Web Service QoS的几个研究方向
    [论文笔记] SOA Middleware Support for Service Process Reconfiguration with EndtoEnd QoS Constraints (ICWS 2009)
    回顾与打算
    [论文笔记] Fast Quality Driven Selection of Composite Web Services (ECOWS, 2006)
    [论文调查] (Mixed) Integer Programming方法在Web Service领域应用概况
    [论文笔记] Gradual Removal of QoS Constraint Violations by Employing Recursive Bargaining Strategy for Optimizing Service Composition Execution Path (ICWS, 2009)
    [论文收集] ICSOC 2008 论文
    [论文泛读] 论文泛读2篇
  • 原文地址:https://www.cnblogs.com/zhangxianrong/p/14691674.html
Copyright © 2020-2023  润新知