深度学习 之五 【TensorFlow 保存模型】

保存和读取 TensorFlow 模型

训练一个模型的时间很长。但是你一旦关闭了 TensorFlow session,你所有训练的权重和偏置项都丢失了。如果你计划在之后重新使用这个模型,你需要重新训练

幸运的是,TensorFlow 可以让你通过一个叫 tf.train.Saver 的类把你的进程保存下来。这个类可以把任何 tf.Variable 存到你的文件系统

保存模型的代码

# Remove previous Tensors and Operations
# 移除之前的  Tensors 和运算
tf.reset_default_graph()

from tensorflow.examples.tutorials.mnist import input_data
import numpy as np

learning_rate = 0.001
n_input = 784  # MNIST 数据输入 (图片尺寸: 28*28)
n_classes = 10  # MNIST 总计类别 (数字 0-9)

# Import MNIST data
# 加载 MNIST 数据
mnist = input_data.read_data_sets('.', one_hot=True)

# Features and Labels
# 特征和标签
features = tf.placeholder(tf.float32, [None, n_input])
labels = tf.placeholder(tf.float32, [None, n_classes])

# Weights & bias
# 权重和偏置项
weights = tf.Variable(tf.random_normal([n_input, n_classes]), name="my_weights_0605")
bias = tf.Variable(tf.random_normal([n_classes]), name="my_bias_0605")

# Logits - xW + b
logits = tf.add(tf.matmul(features, weights), bias)

# Define loss and optimizer
# 定义损失函数和优化器
cost = tf.reduce_mean(\
    tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)\
    .minimize(cost)

# Calculate accuracy
# 计算准确率
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


import math

save_file = './train_model_2018_06_05.ckpt'
batch_size = 128
n_epochs = 20

saver = tf.train.Saver()

# Launch the graph
# 启动图
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    # Training cycle
    # 训练循环
    for epoch in range(n_epochs):
        total_batch = int(math.ceil(mnist.train.num_examples / batch_size))

        # Loop over all batches
        # 遍历所有 batch
        for i in range(total_batch):
            batch_features, batch_labels = mnist.train.next_batch(batch_size)
            sess.run(
                optimizer,
                feed_dict={features: batch_features, labels: batch_labels})

        # Print status for every 10 epochs
        # 每运行10个 epoch 打印一次状态
        if epoch % 10 == 0:
            valid_accuracy = sess.run(
                accuracy,
                feed_dict={
                    features: mnist.validation.images,
                    labels: mnist.validation.labels})
            print('Epoch {:<3} - Validation Accuracy: {}'.format(
                epoch,
                valid_accuracy))

    # Save the model
    # 保存模型
    saver.save(sess, save_file)
    print('Trained Model Saved.')

恢复之前保存的模型代码

# Remove the previous weights and bias
# 移除之前的权重和偏置项
tf.reset_default_graph()

n_input = 784  # MNIST 数据输入 (图片尺寸: 28*28)
n_classes = 10  # MNIST 总计类别 (数字 0-9)

# Two Variables: weights and bias
# 两个变量:权重和偏置项
weights = tf.Variable(tf.random_normal([n_input, n_classes]), name="my_weights_0605")
bias = tf.Variable(tf.random_normal([n_classes]), name="my_bias_0605")

save_file = './train_model_2018_06_05.ckpt'
saver = tf.train.Saver()

# Print the name of Weights and Bias
# 打印权重和偏置项的名称
print('Load Weights: {}'.format(weights.name))
print('Load Bias: {}'.format(bias.name))

with tf.Session() as sess:
    # Load the weights and bias - No Error
    # 加载权重和偏置项 - 没有报错
    saver.restore(sess, save_file)

print('Loaded Weights and Bias successfully.')
阅读更多
版权声明:本文为博主Victor 原创文章,未经博主Victor允许不得转载。This article is written by Blogger Victor, it coule be tranferred to under your name only gotten Victor's Permission. https://blog.csdn.net/u013538542/article/details/80587028
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭