21个项目玩转深度学习-第一章

  • 1.1 MNIST 数据集
# coding:utf-8
import numpy as np
# 从TensorFlow.examples.tutorials.mnist 引入模块
# 这是TensorFlow为了教学MNIST而提前编制的程序
from tensorflow.examples.tutorials.mnist import input_data
# 从MNIST_data/中读取MNIST数据。这条语句会在数据不存在的时候,会自动执行下载。
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# 查看训练数据的大小
print(mnist.train.images.shape)# (55000, 784)
print(mnist.train.labels.shape)# (55000, 10)
# 查看验证数据的大小
print(mnist.validation.images.shape)# (5000, 784)
print(mnist.validation.labels.shape)# (5000, 10)
# 查看测试数据的大小
print(mnist.test.images.shape)# (10000, 784)
print(mnist.test.labels.shape)# (10000, 10)
"""
一般来说,会在训练集上训练模型,通过模型在验证集上的表现调整参数,
最后通过测试集确定模型的性能。
"""
# 打印出第0张图片的向量表示
print(mnist.train.images[0, :])
print(mnist.train.images[0, :].shape)# (784,)一行
# 打印出第0张训练图片的标签
print(mnist.train.labels[0, :])
# 查看前20张训练图片的label
for i in range(20):
    # 得到独热表示,形如(0,1,0,0,0,0,0,0,0,0)
    one_hot_label = mnist.train.labels[i, :]
    # 通过np.argmax,可以直接获得原始的label
    # 因为只有1位为1,其他都是0
    label = np.argmax(one_hot_label)# 对于一个一维向量就是取出max value index
    print("mnist_train_%d.jpg label: %d" % (i, label))
  • 1.1. 实验:将MNIST数据集保存为图片
# coding:utf-8
from tensorflow.examples.tutorials.mnist import input_data
import scipy.misc
import os

# 读取MNIST数据集。如果不存在会事先下载。
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# 把原始图片保存在MNIST_data/raw/文件夹下
# 如果没有这个文件夹,会自动创建

save_dir = "MNIST_data/raw/"
if os.path.exists(save_dir) is False:
    os.makedirs(save_dir)
# 保存前20张图片
for i in range(20):
    print(i)
    # 请注意,mnist.train.images[i, :] 就表示第i张图片(序号从0开始)
    image_array = mnist.train.images[i, :]
    # TensorFlow中的MNIST图片是一个784维的向量,我们重新把它还原为28x28维的图像。
    image_array = image_array.reshape(28,28)
    # 保存文件的格式为:
    # mnist_train_0.jpg,mnist_train_1.jpg,...,mnist_train.19.jpg.
    filename = save_dir + "mnist_train_%d.jpg" %  i
    # 将image_array保存为图片
    # 先用scipy.misc.toimage转换为图片,再调用save直接保存。
    scipy.misc.toimage(image_array,cmin=0.0, cmax=1.0).save(filename)
  • 1.2.1 Softmax 回归
"""
Logistic 回归模型为两类分类模型,
而Softmax模型为多分类分类模型。
x:代表输入的图片,它是一个784维向量,shape =(784,1)
W:是一个矩阵,shape=(784,10)
b:是一个10维向量,shape=(10,1)
y = softmax(wTx + b)
    转置后的(10,784)*(784,1)+(10,1)
"""
# 导入TensorFlow
# 这句import tensorflow as tf 是导入TensorFlow约定俗称的做法,请大家记住。
import tensorflow as tf
import one_section
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)


# 创建x,x是一个占位符(placeholder),代表待识别的图片
x = tf.placeholder(tf.float32, [None, 784])
# W是Softmax模型的参数,将一个784维的输入转换为一个10维的输出。
# 在TensorFlow中,变量的参数用tf.Variable表示
W = tf.Variable(tf.zeros([784, 10]))
# b是又一个Sostfmax模型的参数,一般叫作“偏置项”(bias)
b = tf.Variable(tf.zeros([10]))
print("b:", b.shape)
# y表示模型的输出
y = tf.nn.softmax(tf.matmul(x,W) + b)# (N, 10)
# y_是实际的图片标签,同样以占位符表示。
y_ = tf.placeholder(tf.float32, [None, 10])
# 至此,得到了两个重要的Tensor:y和y_
# y是模型的输出,y_是实际的图像标签,注意y_是独热表示的。
# 下面会根据y和y_构造损失
# 根据y和y_构造交叉熵损失
"""
在softmax回归模型中,通常使用"交叉熵"损失来衡量这种相似性。损失越小,模型的
输出就和实际标签越接近,模型的预测也就越准确。
"""
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y)))

# 有了损失,就可以梯度下降法针对模型的参数(W和b)进行优化。
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# 创建一个Session。只有在Session中才能运行优化步骤train_step
sess = tf.InteractiveSession()
# 运行之前必须要初始化所有变量,分配内存。
tf.global_variables_initializer().run()
"""
前面提到Tensor是"希望"TensorFlow进行计算的节点。而会话就可以看成这些节点进行计算的上下文。
"""
# 进行1000步梯度下降。
for _ in range(10000):
    print("i:", _)
    # 在mnist.train中取100个数据
    # batch_xs 是形状为(100,784)的图片数据,batch_ys是形如(100,10)的实际标签
    # batch_xs,batch_ys对应着两个占位符x和y_
    batch_xs, batch_ys = mnist.train.next_batch(100)
    # 在Session中运行train_step,运行时要传入占位符的值。
    # 与变量不同的是,占位符的值不会被保存,每次可以给占位符传递不同的值。
    sess.run(train_step, feed_dict={x:batch_xs, y_:batch_ys})

# 正确的预测结果
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))# axis:0表示按列,1表示按行
# 计算预测准确率,他们都是Tensor
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# 这里是获取最终模型的准确率
print(sess.run(accuracy, feed_dict={x:mnist.test.images, y_:mnist.test.labels}))
  • 1.2.2 两层卷积网络分类
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)


# x为训练图像的占位符,y_为训练图像标签的占位符
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])

# 将单张图片从784维向量重新还原为28*28的矩阵图片。
x_image = tf.reshape(x, [-1, 28, 28, 1])# -1表示形状第一维的大小是根据x自动确定的。

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

# 第一层卷积层
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)

# 全连接层,输出为1024维的向量。
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)

# 使用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直接计算
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
# 同样定义train_step
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))

# 创建Session,对变量初始化
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())

# 训练20000步
for i in range(20000):
    batch = mnist.train.next_batch(50)
    # 每100步报告一次在验证集上的准确率。
    if i % 100 == 0:
        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_step.run(feed_dict={x:batch[0], y_:batch[1], keep_prob:0.5})

# 训练结束后报告在测试集上的准确率
print("test accuracy %g" % accuracy.eval(feed_dict={x:mnist.test.images, y_:mnist.test.labels, keep_prob:1.0}))
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值