阅读 438

tensorflow入门——MNIST手写数字识别(MLP)

一、环境

macOS 10.15.2

python 2.7.16

tensorflow 1.3.0

通过anaconda安装tensorflow1.3.0的方法:

  1. 安装对应python版本的anaconda
  2. 创建tensorflow的虚拟环境:conda create -n tensorflow python=2.7
  3. 如果显示“Conda command not found”,则首先 vim ~/.zshrc ,然后加一行export PATH=/Users/XXX/opt/anaconda2/bin:$PATH,最后:wq 保存退出。source ~/.zshrc进行生效
  4. 激活tensorflow环境:conda activate tensorflow
  5. 安装tensorflow:pip install --ignore-installed --upgrade \\n https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.3.0-py2-none-any.whl
  6. 验证是否成功:python
>>> import tensorflow as tf
>>> tf.__version__
'1.3.0'
复制代码
  1. 每次使用tensorflow前,使用source activate tensorflow激活,使用后通过conda deactivate关闭

二、设计说明

MNIST数据集包括60000行的训练数据集(mnist.train)和10000行的测试数据集 (mnist.test)。每张图片的28*28个像素点组成长度为784的一维数组,作为输入特征。图片的标签以一维数组形式给出,每个元素表示对应分类出现的概率。

本项目使用多层感知神经网络(MLP)完成。

三、代码

#coding:utf-8

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

# 隐藏层节点
LAYER1_NODE = 100
LAYER2_NODE = 100
# 每轮抽取的训练数量
BATCH_SIZE = 100
# 训练轮数
STEPS = 10000

def forward(x):
	# 输入层到第一层
	w1 = tf.Variable(tf.truncated_normal(shape=[784, LAYER1_NODE], dtype=tf.float32, stddev=0.1))  
	b1 = tf.Variable(tf.zeros([LAYER1_NODE])) 
	y1 = tf.nn.relu(tf.matmul(x, w1) + b1)
	# 第一层到第二层
	w2 = tf.Variable(tf.truncated_normal(shape=[LAYER1_NODE, LAYER2_NODE], dtype=tf.float32, stddev=0.1))  
	b2 = tf.Variable(tf.zeros([LAYER2_NODE])) 
	y2 = tf.nn.relu(tf.matmul(y1, w2) + b2)
	# 第二层到输出层
	w3 = tf.Variable(tf.truncated_normal(shape=[LAYER2_NODE, 10], dtype=tf.float32, stddev=0.1))  
	b3 = tf.Variable(tf.zeros([10])) 
	y = tf.matmul(y2, w3) + b3

	return y

def backward(mnist):
	# 用placeholder给训练数据x和标签y_占位
	x = tf.placeholder(tf.float32, [None, 784])
	y_ = tf.placeholder(tf.float32, [None, 10])
	# 调用forword()函数,计算训练数据集上的预测结果y
	y = forward(x)
	# 损失函数
	loss = tf.reduce_mean(tf.square(y - y_))
	# 使用AdamOptimizer对模型优化
	train = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
	# 准确率
	z = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
	acc = tf.reduce_mean(tf.cast(z, tf.float32))

	with tf.Session() as sess:
		init_op = tf.global_variables_initializer()
		sess.run(init_op)
		# 每次喂入BATCH_SIZE组训练数据,循环迭代STEPS轮
		for i in range(STEPS):
			# 从测视集里取
			xs, ys = mnist.train.next_batch(BATCH_SIZE)
			_, loss_value= sess.run([train, loss], feed_dict={x: xs, y_: ys})

			if i % 500 == 0:
				# 使用测试集测试
				testx, testy = mnist.test.next_batch(BATCH_SIZE)
				# 接受准确率返回值
				accscore = sess.run(acc, feed_dict={x: testx, y_: testy})
				# 打印
				print("After %s training step(s), loss: %g, test accuracy: %g" % (i, loss_value, accscore))


def main():
	# 读入mnist
	mnist = input_data.read_data_sets("./MNIST_data/", one_hot=True)
	backward(mnist)


if __name__ == '__main__':
	main()
复制代码

四、结果