import tensorflow
as tf
from tensorflow
.examples
.tutorials
.mnist
import input_data
mnist
= input_data
.read_data_sets
('MNIST_data', one_hot
=True)
def compute_accuracy(v_xs
, v_ys
):
global prediction
y_pre
= sess
.run
(prediction
, feed_dict
={xs
: v_xs
, keep_prob
: 1})
correct_prediction
= tf
.equal
(tf
.argmax
(y_pre
,1), tf
.argmax
(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
, keep_prob
: 1})
return result
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
)
def conv2d(x
, W
):
return tf
.nn
.conv2d
(x
, W
, strides
=[1, 1, 1, 1], padding
='SAME')
def max_pool_2x2(x
):
return tf
.nn
.max_pool
(x
, ksize
=[1, 2, 2, 1], strides
=[1, 2, 2, 1], padding
='SAME')
xs
= tf
.compat
.v1
.placeholder
(tf
.float32
, [None, 784])
ys
= tf
.compat
.v1
.placeholder
(tf
.float32
, [None, 10])
keep_prob
= tf
.compat
.v1
.placeholder
(tf
.float32
)
x_image
= tf
.reshape
(xs
, [-1, 28, 28, 1])
W_conv1
= weight_variable
([5, 5, 1, 32])
b_conv1
= bias_variable
([32])
h_conv1
= tf
.nn
.relu
(conv2d
(x_image
, W_conv1
) + b_conv1
)
h_pool1
= max_pool_2x2
(h_conv1
)
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
)
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
)
h_fc1_drop
= tf
.nn
.dropout
(h_fc1
, keep_prob
)
W_fc2
= weight_variable
([1024, 10])
b_fc2
= bias_variable
([10])
prediction
= tf
.nn
.softmax
(tf
.matmul
(h_fc1_drop
, W_fc2
) + b_fc2
)
h_fc1_drop
= tf
.nn
.dropout
(h_fc1
, keep_prob
)
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
.initialize_all_variables
())
for i
in range(1000):
batch_xs
, batch_ys
= mnist
.train
.next_batch
(1000)
sess
.run
(train_step
, feed_dict
={xs
: batch_xs
, ys
: batch_ys
, keep_prob
: 0.5})
if i
% 100 == 0:
print(compute_accuracy
(
mnist
.test
.images
[:1000], mnist
.test
.labels
[:1000]))
tf
.nn
.conv2d
(x
, W
, strides
=[1, 1, 1, 1], padding
='SAME')
## tf.nn.conv2d (input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None) 参数: input : 输入的要做卷积的图片,要求为一个张量,shape为 [ batch, in_height, in_weight, in_channel ],其中batch为图片的数量,in_height 为图片高度,in_weight 为图片宽度,in_channel 为图片的通道数,灰度图该值为1,彩色图为3**。(也可以用其它值,但是具体含义不是很理解) filter: 卷积核,要求也是一个张量,shape为 [ filter_height, filter_weight, in_channel, out_channels ],其中 filter_height 为卷积核高度,filter_weight 为卷积核宽度,in_channel 是图像通道数 ,和 input 的 in_channel 要保持一致,out_channel 是卷积核数量。 strides: 卷积时在图像每一维的步长,这是一个一维的向量,[ 1, strides, strides, 1],第一位和最后一位固定必须是1 padding: string类型,值为“SAME” 和 “VALID”,表示的是卷积的形式,是否考虑边界。padding: string类型,值为**“SAME” 和 “VALID”,表示的是卷积的形式,是否考虑边界。"SAME"是考虑边界,不足的时候用0去填充周围**,"VALID"则不考虑 use_cudnn_on_gpu: bool类型,是否使用cudnn加速,默认为true
tf
.nn
.max_pool
(x
, ksize
=[1, 2, 2, 1], strides
=[1, 2, 2, 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]这种形式 tf.nn.softmax的用法 通过Softmax回归,将logistic的预测二分类的概率的问题推广到了n分类的概率的问题. softmax loss
tf.train.AdamOptimizer()函数是Adam优化算法:是一个寻找全局最优点的优化算法,引入了二次方梯度校正。
__init__
(
learning_rate
=0.001,
beta1
=0.9,
beta2
=0.999,
epsilon
=1e-08,
use_locking
=False,
name
='Adam'
)
参数含义:
learning_rate
: A Tensor
or a floating point value
. (学习率)
beta1
: A
float value
or a constant
float tensor
. (一阶矩估计的指数衰减率)
beta2
: A
float value
or a constant
float tensor
. (二阶矩估计的指数衰减率)
epsilon
: A small constant
for numerical stability
. (一个非常小的数,防止除以零)
use_locking
: 如果为真,则使用锁进行更新操作。
name
: 使用梯度时创建的操作的可选名称,默认为
"Adam"。
mnist.train.next_batch
next_batch函数 如何保证每一次调用next_batch函数还能记住上一次的位置呢?tensorflow源码是将dataset输入写为一个类,self._index_in_epoch就相当于一个类变量,记住上一次的位置。