Tensorflow实例:实现简单的卷积神经网络

CNN最大的特点在于卷积的权值共享结构,可以大幅减少神经网络的参数量,防止过拟合的同时又降低了神经网络模型的复杂度。本文通过Tensorflow实现一个简单的CNN,用于MNIST手写数字识别。
摘要由CSDN通过智能技术生成

CNN最大的特点在于卷积的权值共享结构,可以大幅减少神经网络的参数量,防止过拟合的同时又降低了神经网络模型的复杂度。在CNN中,第一个卷积层会直接接受图像像素级的输入,每一个卷积操作只处理一小块图像,进行卷积变化后再传到后面的网络,每一层卷积都会提取数据中最有效的特征。这种方法可以提取到图像中最基础的特征,比如不同方向的边或者拐角,而后再进行组合和抽象形成更高阶的特征。
一般的卷积神经网络由多个卷积层构成,每个卷积层中通常会进行如下几个操作:

  1. 图像通过多个不同的卷积核的滤波,并加偏置(bias),特取出局部特征,每个卷积核会映射出一个新的2D图像。
  2. 将前面卷积核的滤波输出结果,进行非线性的激活函数处理。目前最常见的是使用ReLU函数,而以前Sigmoid函数用得比较多。
  3. 对激活函数的结果再进行池化操作(即降采样,比如将2*2的图片将为1*1的图片),目前一般是使用最大池化,保留最显著的特征,并提升模型的畸变容忍能力。

总结一下,CNN的要点是局部连接(local Connection)、权值共享(Weight Sharing)和池化层(Pooling)中的降采样(Down-Sampling)。

本文将使用Tensorflow实现一个简单的卷积神经网络,使用的数据集是MNIST,网络结构:两个卷积层加一个全连接层。

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

# 载入MNIST数据集,并创建默认的Interactive Session。
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
sess = tf.InteractiveSession()

# 创建权重和偏置,以便重复使用。我们需要给权重制造一些随机的噪声来打破完全对称,比如截断的正态分布噪声,标准差设为0.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 tf.Variable(initial)

# 创建卷积层、池化层,以便重复使用
def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的二维卷积神经网络TensorFlow 实现示例: ``` import tensorflow as tf # 定义输入数据的占位符 x = tf.placeholder(tf.float32, [None, 28, 28, 1]) y = tf.placeholder(tf.float32, [None, 10]) # 定义第一层卷积核和偏置项 w_conv1 = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1)) b_conv1 = tf.Variable(tf.constant(0.1, shape=[32])) # 定义第二层卷积核和偏置项 w_conv2 = tf.Variable(tf.truncated_normal([5, 5, 32, 64], stddev=0.1)) b_conv2 = tf.Variable(tf.constant(0.1, shape=[64])) # 定义全连接层的权重和偏置项 w_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1)) b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024])) w_fc2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1)) b_fc2 = tf.Variable(tf.constant(0.1, shape=[10])) # 定义卷积和池化函数 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') # 第一层卷积和池化 h_conv1 = tf.nn.relu(conv2d(x, w_conv1) + b_conv1) h_pool1 = max_pool_2x2(h_conv1) # 第二层卷积和池化 h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2) h_pool2 = max_pool_2x2(h_conv2) # 将卷积层输出的张量展平 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=0.5) y_ = 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_)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) # 定义评估模型准确度的操作 correct_prediction = tf.equal(tf.argmax(y_, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 训练模型 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(1000): batch = mnist.train.next_batch(50) 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})) ``` 该模型包含两层卷积和池化层以及两个全连接层。在训练过程中,采用了交叉熵作为损失函数,Adam 优化器进行优化,同时使用 dropout 技术进行正则化。在测试集上,可以通过计算准确度评估模型的性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值