tensorflow学习笔记:卷积神经网络

该博客介绍了如何使用神经网络和卷积神经网络(CNN)对MNIST手写数字数据集进行分类。首先,展示了一个简单的神经网络实现,然后通过引入CNN提升模型性能。在训练过程中,应用了dropout防止过拟合,并定期评估测试集的分类准确度。
摘要由CSDN通过智能技术生成

用神经网络对手写数字数据集进行分类,并打印出分类准确度

一般神经网络对于手写数字数据集的分类准确度:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
mnist = input_data.read_data_sets('MNIST', one_hot=True)  # 下载数据包


# 定义每个层的函数
def add_layer(inputs, in_size, out_size, activation_function=None):
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, )
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    Wx_plus_b = tf.nn.dropout(Wx_plus_b, keep_prob)
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs


# 计算准确度的函数
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))  # cast(x,tf.float32) 将x转换为tf.float32类型的数据
    result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 1})
    return result


# 为网络的输入定义placeholder
xs = tf.placeholder(tf.float32, [None, 784])  # 28*28 每张图片有784个像素点
ys = tf.placeholder(tf.float32, [None, 10])  # 每个sample有10个输出
keep_prob = tf.placeholder(tf.float32)
# 添加网络层
prediction = add_layer(xs, 784, 10, activation_function=tf.nn.softmax)

# loss
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), reduction_indices=[1]))
# reduction_indices作用:指定维度 不可与 axis 同时使用,否则报错

# train_step
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

init = tf.global_variables_initializer()  # 一定要先初始化
sess = tf.Session()
sess.run(init)
for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    # 从下载的数据集中提取出一部分的数据sample
    # 不是每次都学习整个数据集,会非常耗时,分批次学习
    sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: 1})
    if i % 50 == 0:
        # 打印出计算准确度
        print(compute_accuracy(mnist.test.images, mnist.test.labels))  # 用测试集去测试准确度

输出:

cnn的分类准确度:

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
mnist = input_data.read_data_sets('MNIST', one_hot=True)  # 下载数据包


# 定义每个层的函数
def add_layer(inputs, in_size, out_size, activation_function=None):
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, )
    Wx_plus_b = tf.matmul(inputs, Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs


# 计算准确度的函数
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))  # cast(x,tf.float32) 将x转换为tf.float32类型的数据
    result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: 1})
    return result


def weight_variable(shape):
    inital = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(inital)


def biases_variable(shape):
    inital = tf.constant(0.1, shape=shape)
    return tf.Variable(inital)


def conv2d(x, W):
    """
    :param x: 输入值
    :param W: 上面定义的weight_variable
    :return:
    """
    # strides[1,x_movement,y_movement,1]
    # Must have strides[0]=strides[3]=1
    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')


# 为网络的输入定义placeholder
xs = tf.placeholder(tf.float32, [None, 784])  # 28*28 每张图片有784个像素点
ys = tf.placeholder(tf.float32, [None, 10])  # 每个sample有10个输出
keep_prob = tf.placeholder(tf.float32)  # 注意这里使用了另一个placeholder,可以控制在训练和预测时是否使用Dropout
x_image = tf.reshape(xs, [-1, 28, 28, 1])
# [-1, 28, 28, 1] 其中-1表示先不管其输入的维度,置为-1,28,28表示像素点为28x28,1表示通道 = 1
# print(x_image.shape)  # 输出 [n_samples,28,28,1]

# conv1 layer
W_conv1 = weight_variable([5, 5, 1, 32])  # 5和5表示卷积核的尺寸 5x5 ,in_size=1,out_size = 32
b_conv1 = biases_variable([32])
h_conv1 = conv2d(x_image, W_conv1) + b_conv1
h_conv1 = tf.nn.relu(h_conv1)  # 再进行一个非线性的处理  其输出的尺寸为:28x28x32
h_pool1 = max_pool_2x2(h_conv1)  # 输出为:14x14x32 因为其步长strides由[1,1,1,1]变为[1,2,2,1]

# conv2 layer
W_conv2 = weight_variable([5, 5, 32, 64])  # 5和5表示卷积核的尺寸 5x5 ,in_size=32,out_size = 64
b_conv2 = biases_variable([64])
h_conv2 = conv2d(h_pool1, W_conv2) + b_conv2
h_conv2 = tf.nn.relu(h_conv2)  # 再进行一个非线性的处理  其输出的尺寸为:14x14x64
h_pool2 = max_pool_2x2(h_conv2)  # 输出为:7x7x64 因为其步长strides由[1,1,1,1]变为[1,2,2,1]

# func1 layer
# 需将上一层的输出展开为1d的神经层
# 现在图片的尺寸已经变为了7x7  加入一个具有1024个神经元的全连接层去得到完整的图像
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = biases_variable([1024])

# 平坦化
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])  # 将原来的形状[n_samples,7,7,64]变为[n_samples,7*7*64] 降维
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)  # tf.matmul 将两矩阵相乘 得到a*b

#  添加Dropout层 防止过拟合,
h_fc1_dropout = tf.nn.dropout(h_fc1, keep_prob)

# func2 layer
W_fc2 = weight_variable([1024, 10])
b_fc2 = biases_variable([10])
y_conv = tf.matmul(h_fc1_dropout, W_fc2) + b_fc2

# 输出最后结果
prediction = tf.nn.softmax(y_conv)

# 训练和评估
# 首先,需要指定一个cost function --cross_entropy,在输出层使用softmax。然后指定optimizer--adam。
# 需要特别指出的是,一定要记得初始化变量

# 计算误差 cross entropy(交叉熵),再用Softmax计算百分比的概率
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction), reduction_indices=[1]))

# 用Adam 优化器来最小化误差,学习率0.001 类似梯度下降
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

sess = tf.Session()

init = tf.global_variables_initializer()  # 初始化变量
sess.run(init)

for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)  # 从训练集中取下一个样本
    # 从下载的数据集中提取出一部分的数据sample
    # 不是每次都学习整个数据集,会非常耗时,分批次学习
    sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: 0.5})
    if i % 50 == 0:
        # 打印出计算准确度
        print(compute_accuracy(mnist.test.images, mnist.test.labels))  # 用测试集去测试准确度


输出结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值