InteractiveSession
tensorflow通常用法是创建图然后用session运算图,InteractiveSession是更灵活更方便的运算类
import tensorflow as tf
sess = tf.InteractiveSession()
Placeholders
在需要接收输入变量时,先要给对应变量用占位符占位定义,接着就能对变量进行定义
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
在使用变量前,先用Session初始化
sess.run(tf.global_variables_initializer())
损失函数
这里用里交互熵损失函数,然后用sofmax归一化处理,再用reduce_mean取平均损失值
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
Multilayer convolutional network
1,初始化权重
#tf.truncated_normal生成正态随机数
#tf.truncated_normal(shape, mean=0.0, stddev=1.0,
#dtype=tf.float32, seed=None, name=None)
#生成常数tensor
#tf.constant(value, dtype=None, shape=None, name='Const')
#初始化权重和偏量
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
2,卷积和池化函数
#实现卷积的函数tf.nn.conv2d(input, filter, strides, padding, #use_cudnn_on_gpu=None, name=None)
#input:需要做卷积的输入图像,它要求是一个Tensor,具有[batch, in_height, in_width, in_channels]这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求类型为float32和float64其中之一
#filter:相当于CNN中的卷积核,也就是权重,它要求是一个Tensor,具有[filter_height, filter_width, in_channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维
#strides:卷积时在图像每一维的步长,这是一个一维的向量,长度4,通常strides取[1,stride,stride,1],比如[1,1,1,1][1,2,2,1]
#padding:"SAME"or"VALID"
#cudnn:是否使用cudnn加速,默认"true"
#然后函数返回tensor作为feature map输出
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
#最大值池化函数tf.nn.max_pool(value, ksize, strides, padding, name=None)
#value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map,依然是[batch, height, width, channels]这样的shape
#ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1
#strides:和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride,stride, 1]
#padding:和卷积类似,可以取'VALID' 或者'SAME'
#返回一个Tensor,类型不变,shape仍然是[batch, height, width, channels]这种形式
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
3,convolutional 网络部分
#[5,5,1,32]代表5*5的kernel size, 1表示输入的图片通道,32表示输出的feature map通道
#偏量是一维向量,y=x*w+b
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32]
x_image = tf.reshape(x, [-1,28,28,1])
#使用relu作为activate function和用maxpooling池化
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
#第二层的输入图片通道要和第一层的feature map通道相同,均为32
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
#添加全连接层1024个feature map,首先要reshape第二层的feature map成为向量,其实此处全连接层的意义就是用重塑成一个向量feed into ReLU
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
4,Dropout
#我们都知道dropout对于防止过拟合效果不错,dropout一般用在全连接的部分,卷积部分不会用到dropout,输出层也不会使用dropout
#tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
5,训练和评估