import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#每个批次的大小
batch_size = 100
#定义初始化权值 在卷积层中 是卷积核 在全连接层是权值 和偏置
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')
#以上定义造砖完毕 开始调用 设计卷积层
x=tf.placeholder(tf.float32,[None,784])
y=tf.placeholder(tf.float32,[None,10])
x_image=tf.reshape(x,[-1,28,28,1])
#第一层卷积
#设置第一层卷积核 偏置
W_conv1=weight_variable([5,5,1,32]) #在卷积中 权重矩阵 前面是扫过的快 后面是前后的连接
b_conv1=bias_variable([32])
#卷积 激活 池化
conv2d_1=conv2d(x_image,W_conv1)+b_conv1
h_co
CNN代码
最新推荐文章于 2025-02-26 11:11:41 发布