# 保存和读取 TensorFlow 模型

## 保存模型的代码

# 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 数据

# 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

# Define loss and optimizer
# 定义损失函数和优化器
cost = tf.reduce_mean(\
tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
.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
# 打印权重和偏置项的名称

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

print('Loaded Weights and Bias successfully.')

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120