完整代码:
#import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
from tensorflow.examples.tutorials.mnist import input_data
#读入mnist数据,以独热的形式读入
mnist=input_data.read_data_sets('./data/MNIST_data',one_hot=True)
# img = mnist.train.images[500]
# print(img.shape)#(784,)
#定义placeholder占位符,使之接收输入
x=tf.placeholder(tf.float32,[None,784])#784——》28x28
y_=tf.placeholder(tf.float32,[None,10])
x_image=tf.reshape(x,[-1,28,28,1])#因为mnist中的数据是784位的一位数据,所以通过reshape重塑一下,转成28x28的矩阵
'''
构建权重和偏置:
权重就是层与层之间连接边上的值,一般与本层的值相乘再经过激活函数,作用是让神经网络可以进行非线性变换;
偏置的作用是让网络具有“平移能力”,初始化时,一般还会加入一些噪声,让模型看起来更加随机一些,破坏其整体的对称性
'''
def weight_variable(shape):
'''
初始化权重:
:param shape:
:return:
'''
initial=tf.truncated_normal(shape,stddev=0.1)
'