tf学习(七)—— CNN-MNIST实例学习分析

这篇博客通过一个CNN-MNIST实例详细介绍了卷积神经网络的工作原理。首先,解释了卷积层输入为何通常为四维,然后逐步解析模型创建过程,包括卷积、池化、全连接层和softmax层的操作。博主讨论了全连接层的权重获取、dropout防止过拟合以及softmax层在概率分类中的作用。最后,总结指出卷积层负责图像特征提取,而全连接层用于分类,强调了CNN相对于全连接网络的优势。
摘要由CSDN通过智能技术生成

程序

程序为转载的,如下

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("MNIST-data",one_hot=True)


#每个批次的大小
batch_size=100
#计算一共有多少个批次
n_batch=mnist.train.num_examples//batch_size


#初始化权值
def weight_variable(shape):
    initial=tf.truncated_normal(shape,stddev=0.1)#生成一个截断的正态分布,
                                       # shape表示生成张量的维度
    return tf.Variable(initial)  #tf.Variable()函数用来变量声明


#初始化偏置
def bias_variable(shape):
    initial=tf.constant(0.1,shape=shape)  #constant 为常量
    return tf.Variable(initial)


#卷积层
def conv2d(x,W):
    #x input tensor of shape '[batch,in_height,in_width,in_channles]'
    #W filter / kernel tensor of shape [filter_height,filter_width,in_channels,out_channels]
    #`strides[0] = strides[3] = 1`. strides[1]代表x方向的步长,strides[2]代表y方向的步长
    #padding: A `string` from: `"SAME", "VALID"`
    return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')#2d的意思是二维的卷积操作


#池化层
def max_pool_2x2(x):
    #ksize [1,x,y,1]  ksize 2*2为窗口大小, strides 步长,分别为横向与纵向
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')


#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])#28*28
y = tf.placeholder(tf.float32,[None,10])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值