TensorFlow【极简】CNN

单层卷积神经网络

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

"""加载样本集:手写数字"""
mnist = input_data.read_data_sets('data/', one_hot=True)

"""卷积神经网络"""
# 输入层
X = tf.placeholder('float', [None, 28, 28, 1])  # 批量、高、宽、通道数
y = tf.placeholder('float', [None, 10])  # 10分类(0~9)

# 卷积层
W_conv = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1))  # 高、宽、通道数、filter个数
b_conv = tf.Variable(tf.constant(.1, shape=[32]))  # filter个数
h_conv = tf.nn.conv2d(input=X, filter=W_conv, strides=[1, 1, 1, 1], padding='SAME') + b_conv  # 滑窗步幅:水平1、垂直1

# ReLU激活
h_relu = tf.nn.relu(h_conv)

# 池化层
h_pool = tf.nn.max_pool(h_relu, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')  # 滑窗大小:水平2、垂直2;滑窗步幅:水平2、垂直2

# 平化层
h_flat = tf.reshape(h_pool, [-1, 14 * 14 * 32])

# 全连接层1
W_fc1 = tf.Variable(tf.truncated_normal([14 * 14 * 32, 256], stddev=.1))  # 指定256个神经元
b_fc1 = tf.Variable(tf.constant(.1, shape=[256]))
h_fc1 = tf.matmul(h_flat, W_fc1) + b_fc1
h_fc1 = tf.nn.relu(h_fc1)

# 全连接层2
W_fc2 = tf.Variable(tf.truncated_normal([256, 10], stddev=.1))
b_fc2 = tf.Variable(tf.constant(.1, shape=[10])<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小基基o_O

您的鼓励是我创作的巨大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值