import sys
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
tf.compat.v1.disable_eager_execution()
max_steps = 1000
learning_rate = 0.001
dropout = 0.9
data_dir = 'mnist'
log_dir = 'mnist'
# 初始化权值w
def weight_variable(shape):
initial = tf.compat.v1.truncated_normal(shape, stddev=0.1)
return tf.compat.v1.Variable(initial)
# 初始化偏置项b
def bias_variable(shape):
initial = tf.compat.v1.constant(0.1, shape=shape)
return tf.compat.v1.Variable(initial)
# 记录参数变化信息
def variable_summaries(var):
with tf.compat.v1.name_scope('summaries'):
# 计算参数的均值,使用tf.summary.scalar()记录
mean = tf.compat.v1.reduce_mean(var)
tf.compat.v1.summary.scalar('mean', mean)
# 计算参数的标准差
with tf.compat.v1.name_scope('stddev'):
stddev = tf.compat.v1.sqrt(tf.reduce_mean(tf.square(var-mean)))
# 记录标准差,最大值,最小值
tf.compat.v1.summary.scalar('stddev', stddev)
tf.compat.v1.summary.scalar('max', tf.reduce_max(var))
tf.compat.v1.summary.scalar('min', tf.reduce_min(var))
# 用直方图记录参数的分布
tf.compat.v1.summary.histogram('histogram', var)
# 构建隐含层
def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu):
with tf.compat.v1.name_scope(layer_name):
# 调用方法初始化权值w,并记录w信息
with tf.compat.v1.name_scope('weights'):
weights = weight_variable([input_dim, output_dim])
variable_summaries(weights)
# 调用方法初始化偏置项b,并记录b信息
with tf.compat.v1.name_scope('biases'):
biases = bias_variable([output_dim])
variable_summaries(biases)
# 执行wx+b的线性计算,并且用直方图记录下来
with tf.name_scope('linear_compute'):
preactivate = tf.compat.v1.matmul(input_tensor, weights) + biases
tf.compat.v1.summary.histogram('linear', preactivate)
# 使线性输出经过激励函数,并将输出也用直方图记录下来
activations = act(preactivate, name='activation')
tf.compat.v1.summary.histogram('activations', activations)
return activations
# 获取数据
def feed_dict(train):
if train:
xs, ys = mnist.train.next_batch(100)
k = dropout
else:
xs, ys = mnist.test.images, mnist.test.labels
k = 1.0
return {x: xs, y_: ys, keep_prob: k}
# 加载数据,并进行独热编码
mnist = input_data.read_data_sets(data_dir, one_hot=True)
# 创建session
sess = tf.compat.v1.InteractiveSession()
# 创建特征数据x, 标签数据y_
with tf.compat.v1.name_scope('input'):
x = tf.compat.v1.placeholder(tf.float32, [None, 784], name='x-input')
y_ = tf.compat.v1.placeholder(tf.float32, [None, 10], name='y-input')
# 为使图片在tensorboard展示出来,汇总图片数据
with tf.compat.v1.name_scope('input_reshape'):
image_shaped_input = tf.compat.v1.reshape(x, [-1, 28, 28, 1])
tf.compat.v1.summary.image('input', image_shaped_input, 10)
# 创建隐含层
hidden1 = nn_layer(x, 784, 500, 'layer1')
with tf.compat.v1.name_scope('dropout'):
keep_prob = tf.compat.v1.placeholder(tf.float32)
result = tf.compat.v1.summary.scalar(
'dropout_keep_probability', tf.compat.v1.reduce_sum(keep_prob))
dropped = tf.compat.v1.nn.dropout(hidden1, keep_prob)
# 创建一个输出层
y = nn_layer(dropped, 500, 10, 'layer2', act=tf.identity)
# 创建损失函数
with tf.compat.v1.name_scope('loss'):
# 计算交叉熵损失
diff = tf.compat.v1.nn.softmax_cross_entropy_with_logits(
labels=y_, logits=y)
with tf.compat.v1.name_scope('total'):
# 计算所有样本交叉熵损失的均值
cross_entropy = tf.compat.v1.reduce_mean(diff)
tf.compat.v1.summary.scalar('loss', cross_entropy)
# 使用AdamOptimizer()优化器训练模型
with tf.compat.v1.name_scope('train'):
train_step = tf.compat.v1.train.AdamOptimizer(learning_rate).minimize(
cross_entropy)
# 计算准确率
with tf.compat.v1.name_scope('accuracy'):
with tf.compat.v1.name_scope('correct_prediction'):
# 分别在预测与真实的标签中取出最大值的索引,若相同则返回1,若不同则返回0
correct_prediction = tf.compat.v1.equal(
tf.argmax(y, 1), tf.argmax(y_, 1))
with tf.compat.v1.name_scope('accuracy'):
accuracy = tf.compat.v1.reduce_mean(
tf.cast(correct_prediction, tf.float32))
tf.compat.v1.summary.scalar('accuracy', accuracy)
# 合并summary操作,运行初始化变量
merged = tf.compat.v1.summary.merge_all()
train_writer = tf.compat.v1.summary.FileWriter(log_dir + '/train', sess.graph)
test_writer = tf.compat.v1.summary.FileWriter(log_dir + '/test')
# 运行初始化所有变量
tf.compat.v1.global_variables_initializer().run()
# 训练模型
for i in range(max_steps):
if i % 10 == 0:
summary, acc = sess.run([merged, accuracy], feed_dict=feed_dict(False))
test_writer.add_summary(summary, i)
print('Accuracy at step %s:%s' % (i, acc))
else:
if i % 100 == 99:
run_options = tf.compat.v1.RunOptions()
run_metadata = tf.compat.v1.RunMetadata()
summary, _ = sess.run([merged, train_step],
feed_dict=feed_dict(True),
options=run_options,
run_metadata=run_metadata)
train_writer.add_run_metadata(run_metadata, 'step%03d' % i)
train_writer.add_summary(summary, i)
print('Adding run metadata for', i)
else:
summary, _ = sess.run([merged, train_step],
feed_dict=feed_dict(True))
train_writer.add_summary(summary, i)
train_writer.close()
test_writer.close()
Tensorflow 手写数字识别
最新推荐文章于 2023-03-08 18:10:24 发布