首先在这个网站下载MNIST数据集。下载后的数据如图所示:
代码如下:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#载入数据
mnist = input_data.read_data_sets("E:/mnist",one_hot=True)
#每个批次大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples//batch_size #整除
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
网络部分:网络搭建比较灵活,不同的层数、节点数等都会对训练出的模型产生影响,这里只有一层隐藏层,节点为10。
#输入层到隐藏层
W = tf.Variable(tf.zeros([784,10]))#这里W初始化为0,可以更快收敛
b = tf.Variable(tf.zeros([10]))
Wx_plus_b_L1 = tf.matmul(x,W) + b
#隐藏层到输出层
W2 =