-
大小: 7KB文件类型: .py金币: 2下载: 1 次发布日期: 2021-06-05
- 语言: Python
- 标签: CNN python MNIST TensorFlow
资源简介
CNN实现MNIST分类,在测试集上实现准确率0.99,TensorFlow实现,容易上手
代码片段和文件信息
# Lab 11 MNIST and Convolutional Neural Network
import tensorflow as tf
import random
import scipy.io as scio
# import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
tf.set_random_seed(777) # reproducibility
mnist = input_data.read_data_sets(“MNIST_data/“ one_hot=True)
# Check out https://www.tensorflow.org/get_started/mnist/beginners for
# more information about the mnist dataset
# hyper parameters
learning_rate = 0.002
training_epochs = 3
batch_size = 50
# input place holders
X = tf.placeholder(tf.float32 [None 784])
X_img = tf.reshape(X [-1 28 28 1]) # img 28x28x1 (black/white)
Y = tf.placeholder(tf.float32 [None 10])
# L1 ImgIn shape=(? 28 28 1)
W1 = tf.Variable(tf.random_normal([5 5 1 6] stddev=0.01))
b1 = tf.Variable(tf.random_normal([6]))
# Conv -> (? 24 24 6)
# Pool -> (? 12 12 6)
L1 = tf.nn.conv2d(X_img W1 strides=[1 1 1 1] padding=‘VALID‘)
L1 = tf.nn.relu(L1 + b1)
L1 = tf.nn.avg_pool(L1 ksize=[1 2 2 1]
strides=[1 2 2 1] padding=‘SAME‘)
‘‘‘
Tensor(“Conv2D:0“ shape=(? 24 24 6) dtype=float32)
Tensor(“Relu:0“ shape=(? 24 24 6) dtype=float32)
Tensor(“avgPool:0“ shape=(? 12 12 6) dtype=float32)
‘‘‘
# L2 ImgIn shape=(? 12 12 6)
W2 = tf.Variable(tf.random_normal([5 5 6 16] stddev=0.01))
b2 = tf.Variable(tf.random_normal([16]))
# Conv ->(? 8 8 16)
# Pool ->(? 4 4 16)
L2 = tf.nn.conv2d(L1 W2 strides=[1 1 1 1] padding=‘VALID‘)
L2 = tf.nn.relu(L2 + b2)
L2 = tf.nn.avg_pool(L2 ksize=[1 2 2 1]
strides=[1 2 2 1] padding=‘SAME‘)
L2_flat = tf.reshape(L2 [-1 4 * 4 * 16])
#L2_flat = tf.nn.relu(L2_flat)
‘‘‘
Tensor(“Conv2D_1:0“ shape=(? 14 14 64) dtype=float32)
Tensor(“Relu_1:0“ shape=(? 14 14 64) dtype=float32)
Tensor(“MaxPool_1:0“ shape=(? 7 7 64) dtype=float32)
Tensor(“Reshape_1:0“ shape=(? 3136) dtype=float32)
‘‘‘
# Final FC 7x7x64 inputs -> 10 outputs
W3 = tf.Variable(tf.random_normal([4*4*16 10] stddev=0.01))
#W3 = tf.get_variable(“W3“ shape=[4 * 4 * 16 10]
# initializer=tf.contrib.layers.xavier_initializer())
b3 = tf.Variable(tf.random_normal([10]))
logits = tf.matmul(L2_flat W3) + b3
#logits = tf.nn.relu(logits)
# define cost/loss & optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
logits=logits labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
#creat a saver
saver = tf.train.Saver()
# initialize
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# train my model
print(‘Learning started. It takes sometime.‘)
for epoch in range(training_epochs):
avg_cost = 0
total_batch = int(mnist.train.num_examples / batch_size)
for i in range(total_batch):
batch_xs batch_ys = mnist.train.next_batch(batc
相关资源
- python实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论