您的位置:首页 > 理论基础 > 计算机网络

深度学习入门之第三章 神经网络 一个fashion-mnist图像识别网络

2019-02-22 20:06 260 查看
版权声明:版权归世界上所有无产阶级所有 https://blog.csdn.net/qq_41776781/article/details/87884312

前言我看书上介绍的是如果训练神经网络做图像识别的, 正好之前在21个深度学习项目之中 有用卷积神经网络做图像识别 所以就介绍一下我之前写的吧!!!!!书上介绍的思想主要是使用mlp做前向神经网络 但是现在mlp无论是在Image Recognition 还是GANs中, 都没有卷积神经网络好, 渐渐被淘汰了 

 

分享之前我碰到一个问题, 代码是不会骗人的 我们看看Image Recognition和GANs之间的区别

在Image Recognition中最后一层softmax层输出的数据我们找到概率最大的数据 并且将其设置为1

但是在GANs中识别器最后一层输出的数据做交叉熵时, 我们希望真实数据全为1 生成的图像全为0

切记这两种方法的区别!!!!!少走点错路

[code]# Image Recognition 交叉熵使用的两个参数是 真实标签和最后全连接层输出的数据

loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))

# GANs d_loss_real是真实数据的交叉熵 d_loss_fake是输入的是生成数据的交叉熵

d_loss_real = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(logits=D_real_logits, labels=tf.ones_like(D_real)))

d_loss_fake = tf.reduce_mean(
tf.nn.sigmoid_cross_entropy_with_logits(logits=D_fake_logits, labels=tf.zeros_like(D_fake)))

 

 

对于这个fashion mnist图像识别的程序而言,

每一层的操作是首先定义W(权重)和B(偏置),

然后对图像进行卷积操作,并使用relu做激活函数,对数据进行非线性转化

再然后是2*2的最大池化层

程序的最后一个全连接层将数据划分成10类

 

[code]import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

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')

if __name__ == '__main__':
# 读入数据
mnist = input_data.read_data_sets("../../Dataset/fashion-mnist", one_hot=True)
# x为训练图像的占位符、y_为训练图像标签的占位符
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])

# 将单张图片从784维向量重新还原为28x28的矩阵图片
x_image = tf.reshape(x, [-1, 28, 28, 1])

'''
经过实验可以知道卷积运算的时候
卷积核的high width channel是能够随意修改的
但是最后表示经过该层之后神经元的输出数目 其要和bias以及以及下一层的卷积层通道数相对应即可
最后重要的是tf.nn.conv2d第三个参数是不能随便修改的 不然会导致维度不对
'''
W_conv1 = weight_variable([6, 6, 1, 64])
b_conv1 = bias_variable([64])
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, 64, 128])
b_conv2 = bias_variable([128])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

# 全连接层,输出为1024维的向量
W_fc1 = weight_variable([7 * 7 * 128, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 128])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
# 使用Dropout,keep_prob是一个占位符,训练时为0.5,测试时为1
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

# 把1024维的向量转换成10维,对应10个类别
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2

# 我们不采用先Softmax再计算交叉熵的方法,而是直接用tf.nn.softmax_cross_entropy_with_logits直接计算
loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
# 同样定义train_step
optimizer = tf.train.AdamOptimizer(1e-4).minimize(loss)

# 定义测试的准确率
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

#  定义保存相关数据文件的模型
saver = tf.train.Saver(max_to_keep = 5)

# 创建Session和变量初始化
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
choose_way_train = "train"
if choose_way_train == "train":
# 训练20000步
for i in range(20000):
batch = mnist.train.next_batch(100)
# 程序有个问题 无法确定程序进行到哪一步了

'''
用于测试单个程序是否能够跑的通
train_accuracy = accuracy.eval(feed_dict={
x: batch[0], y_: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %g" % (i, train_accuracy))
'''

#赋值的时候 等号前面的和等号后面的不能一样
train_accuracy, _, val_loss= sess.run([accuracy, optimizer, loss],feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
if i %100 ==0:
print("step %d, val_loss %g" % (i, val_loss))
if i % 1000 == 0:

global_step = i
print("i have save kpt/mnist_%3d.ckpt"%global_step)
saver.save(sess, 'ckpt/mnist_%3d.ckpt'%global_step)
else:
model_file = tf.train.latest_checkpoint('ckpt/')
saver.restore(sess, model_file)
for i in range(1000):
batch = mnist.test.next_batch(50)
if i%100 ==0:
val_acc, val_loss = sess.run([accuracy,loss],feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
# 训练结束后报告在测试集上的准确度
print("test accuracy %f"% val_acc)

实验结果:  使用GPU的话 不到5分钟就已经训练完了 准确率是99.7% 所以现在mnist几乎都不用论文之中了 太简单 有兴趣的同学 可以在这个程序中加入一个画图程序  把结果画出来

[code]'''
step 19100, val_loss 0.00158436
step 19200, val_loss 0.0086459
step 19300, val_loss 0.0232048
step 19400, val_loss 0.00513635
step 19500, val_loss 0.0242389
step 19600, val_loss 0.0272156
step 19700, val_loss 0.00483749
step 19800, val_loss 0.00234459
step 19900, val_loss 0.00342627
'''

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐