上期我们分享了CNN的基本结构,本期我们就拿MNIST数据集来训练一下手写数字的数据库,以便我们下期能够使用训练好的模型,来进行手写数字的识别。
分享一下几个可视化网站,可以看到神经网络的识别过程。
http://scs.ryerson.ca/~aharley/vis/conv/
1、插入MNIST数据集
#利用MNIST的数据来进行我们的神经网络的训练 import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
2、定义精度函数
def compute_accuracy(v_xs, v_ys): global prediction y_pre = sess.run(prediction, feed_dict={xs: v_xs}) correct_prediction = tf.equal(tf.arg_max(y_pre, 1), tf.arg_max(v_ys, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys}) return result
3、定义Weight变