Tensorflow精进之路(二):两层卷积神经网络模型训练MNIST

这段时间,打算把TensorFlow再补补,提升一下技术水平~
希望我能坚持下来,抽空把这本书刷下来吧~

导入数据

 

  • 下面的代码会直接下载数据,如果没有那个文件夹的话,但是,如果有这个文件夹而且里面有那几个文件的话,就会直接调用那个数据。
  • 这次直接在网上搜MNIST下载,就知道怎么下载啦~ 这里我更关注TensorFlow和算法本身
from tensorflow.examples.tutorials.mnist import input_data 
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)

 

print(mnist.train.images.shape)
print(mnist.test.images.shape)


(55000, 784)
(10000, 784)

比较有意思的是,这里的labels的数据安排。

  • 比如说,这就是第一个图片的所对应的信息。表示是7。

[0, 0 ,0 , 0 ,0 ,0 ,0 , 1 ,0 ,0 ]



书中比较有意思的介绍了Softmax回归,之前虽然有用过Softmax,也大致知道意思,但却没有较为详细的了解过它。这倒是这次阅读的惊喜。

Softmax回归
Softmax回归,来自于Logistics 回归。只不过逻辑回归只是binary classification的活动,而Softmax就可以实现多元的回归的情况。
意思很简单,就是返回某个物体属于labels的概率集合。然后选择概率最大的label。

现在对每个类做分析。和Logistics regression一样,先会有一个线性函数来给每个类打分。同样的,这个分数是在实数范围内的,但是概率空间只有0和1,所以需要做个映射。

分数为(a, b, c)时,对应的概率分别为:


 
现在我们回想起logistics regression的sigmoid函数,会发现,其实就是假设了另外一个label的得分永远为0的情况。

所以Softmax模型其实为

继续实验部分

 

  • 导入tf包
import tensorflow as tf


tensorflow是静态图。其实很简单,就是我们需要手动的描述一张图。把整个图画完之后,再传递参数进去,让它自己开始运行(session)。

  • 描述这张图
  • 下面我们给出了几个定义。最后一个 其实表示的是真正的label值。而y是我们算出来的值。
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])

 

  • 给出损失函数和训练器
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y)))  # 使用交叉熵
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

 

  • 创建会话并开始运行
sess = tf.InteractiveSession()  # 创建Session
tf.global_variables_initializer().run() # 给所有的变量初始化,分配内存

 

  • 训练, 每次用mini batch的思路选几个来做
  • 注意每次都需要把整个图跑一遍
for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x:batch_xs, y_:batch_ys})

 

  • 算一下准确度
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,  tf.float32))

 

  • 输出一下:
print(sess.run(accuracy, feed_dict={x:mnist.test.images, y_:mnist.test.labels}))


Tips:如果想单独看下某个数据,可以使用下面的方法

a = sess.run(correct_prediction, feed_dict={x:mnist.test.images, y_:mnist.test.labels})


然后我们会很惊奇的发现,这其实是一个numpy的数据

å¨è¿éæå¥å¾çæè¿°

使用卷积神经网络来实现

 

  • 创建占位符
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
x_image = tf.reshape(x, [-1, 28, 28, 1])

 

  • 创建复用的函数
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 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')

 

  • 卷积
# 第一层卷积
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 = weight_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)
# 使用Dropout,防止过拟合
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)


 

W_fc2 = weight_variable([1024, 10])
b_fc2 = weight_variable([10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2

 

  • 误差和损失
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,  tf.float32))

 

  • 训练
sess = tf.InteractiveSession()  # 创建Session
tf.global_variables_initializer().run() # 给所有的变量初始化,分配内存
for i in range(20000):
    batch = mnist.train.next_batch(50)
    if i % 1000 == 0:
        train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_:batch[1], keep_prob:1.0})
        print(train_accuracy)
    train_step.run(feed_dict={x:batch[0], y_:batch[1], keep_prob:0.5})


结果可以达到接近100%~~~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值