1.定义准确率计算函数
import tensorflow as tf def compute_accuracy(v_xs,v_ys): global prediction y_pre = sess.run(prediction,feed_dict={xs:v_xs,keep_prob: 1.0}) 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))#tf.cast()函数的作用是执行 tensorflow 中张量数据类型转换 res = sess.run(accuracy,feed_dict={xs:v_xs,ys:v_ys,keep_prob: 1.0}) return res
!pip3 list|grep tensor
tensorboard 1.15.0 tensorflow 1.15.2 tensorflow-estimator 1.15.1
2.定义变量函数
这里用到了truncated_normal这个函数,该函数产生截断正态分布随机数,取值范围为 [ mean - 2 * stddev, mean + 2 * stddev ]。说明如下:
truncated_normal( shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None )
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
shape | 是 | 1维整形张量或array | 输出张量的维度 |
mean | 否 | 0维张量或数值 | 均值 |
stddev | 否 | 0维张量或数值 | 标准差 |
dtype | 否 | dtype | 输出类型 |
seed | 否 | 数值 | 随机种子,若 seed 赋值,每次产生相同随机数 |
name | 否 | string | 运算名称 |
def weight_variable(shape): inital = tf.truncated_normal(shape,stddev=0.1) return tf.Variable(inital) def bias_variable(shape): inital = tf.constant(0.1,shape=shape) return tf.Variable(inital) def conv2d(x,W): #stride=[1,x_movement,y_movement,1] return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')#'VALID PADDING' and 'SAME PADDING'两种方法 #这里作者用SAME做的padding,这样卷积完激活图像和原图大小一样 def max_pool_2X2(x): #stride=[1,x_movement,y_movement,1] #pooling这里设置步长为[2,2],然后那个ksize表示池化窗口大小ksize=[1,池化窗口长,池化窗口宽,1] return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
3.处理输入
import tensorflow as tf import ssl ssl._create_default_https_context = ssl._create_unverified_context#不加这个会报错的,当你urllib.urlopen一个 https 的时候会验证一次 SSL 证书 ,当目标使用的是自签名的证书时就会爆出该错误消息。 from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data', one_hot=True)#这个数据集每一张图片都有784个像素点28*28 xs = tf.placeholder(tf.float32,[None,784])#28*28 ys = tf.placeholder(tf.float32,[None,10])#每一个图片对应十个标签的输出 x_image = tf.reshape(xs,[-1,28,28,1])#【第一个维度不管,长为28,宽为28,深度为1】 keep_prob=tf.placeholder(tf.float32) #print(x_image.shape)
Extracting MNIST_data/train-images-idx3-ubyte.gz Extracting MNIST_data/train-labels-idx1-ubyte.gz Extracting MNIST_data/t10k-images-idx3-ubyte.gz Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
4.定义各个层
#conv1 layer W_conv1 = weight_variable([5,5,1,32])#过滤器长宽为5*5,insize=1就是输入深度(滤波器深度为1)。outsize=32也就是输出深度为32,也就是滤波器的个数为32 b_conv1 = bias_variable([32]) h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1) #输出大小为28*28*32 h_pool1 = max_pool_2X2(h_conv1) #输出大小为14*14*32 #conv1 layer #conv2 layer W_conv2 = weight_variable([5,5,32,64])#过滤器长宽为5*5,insize=32就是输入深度(滤波器深度为32)。outsize=32也就是输出深度为64,也就是滤波器的个数为64 b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2) #输出大小为14*14*64 h_pool2 = max_pool_2X2(h_conv2) #输出大小为7*7*64 #conv2 layer #function layer1 h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])#展开成一维[n_simple,7,7,64]->[n_simple,7*7*64] W_func1 = weight_variable([7*7*64,1024]) b_func1 = weight_variable([1024]) h_func1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_func1)+b_func1) h_fc1_drop = tf.nn.dropout(h_func1,keep_prob) #防止过拟合 #function layer1 #function layer2 W_func2 = weight_variable([1024,10]) b_func2 = weight_variable([10]) prediction = tf.nn.softmax(tf.matmul(h_fc1_drop,W_func2)+b_func2) #function layer2 cross_entropy=tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),reduction_indices=[1])) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) sess = tf.Session() sess.run(tf.global_variables_initializer()) for i in range(500): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys,keep_prob:1.0}) if i%50==0: print(compute_accuracy(mnist.test.images,mnist.test.labels))
0.0501 0.8089 0.8967 0.9206 .......