MNIST机器学习进阶
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 17 08:49:28 2018
@author: Administrator
"""
import tensorflow as tf
"引入input_data.py,注:Python文件必须与input_data.py在同一文件夹下"
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('./input_data', one_hot=True, validation_size=100)
sess = tf.InteractiveSession()
x = tf.placeholder("float",shape=[None,784])
y_ = tf.placeholder("float",shape=[None,10])
"定义权重W和偏置b"
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
"变量在session中初始化"
sess.run(tf.initialize_all_variables())
"权重初始化"
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')
"第一层卷积"
"前两个维度是patch的大小,接着是输入的通道数目,最后是输出的通道数目。"
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1,28,28,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
"第二层卷积"
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
"密集连接层"
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
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)
"在输出层之前加入dropout"
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
"添加softmax层"
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
"训练和评估模型"
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
sess.run(tf.initialize_all_variables())
for i in range(20000):
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}))
MNIST机器学习进阶的更多相关文章
- TensorFlow 学习(4)——MNIST机器学习进阶
要进一步改进MNIST学习算法,需要对卷积神经网络进行学习和了解 学习材料参见https://www.cnblogs.com/skyfsm/p/6790245.html 卷积神经网络依旧是层级网络,只 ...
- [转]MNIST机器学习入门
MNIST机器学习入门 转自:http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_beginners.html?plg_ ...
- Tensorflow之MNIST机器学习入门
MNIST机器学习的原理: 通过一次次的 输入某张图片的像素值(用784维向量表示)以及这张图片对应的数字(用10维向量表示比如数字1用[0,1,0,0,0,0,0,0,0,0]表示),来优化10*7 ...
- tensorfllow MNIST机器学习入门
MNIST机器学习入门 这个教程的目标读者是对机器学习和TensorFlow都不太了解的新手.如果你已经了解MNIST和softmax回归(softmax regression)的相关知识,你可以阅读 ...
- Tensorflow学习笔记(一):MNIST机器学习入门
学习深度学习,首先从深度学习的入门MNIST入手.通过这个例子,了解Tensorflow的工作流程和机器学习的基本概念. 一 MNIST数据集 MNIST是入门级的计算机视觉数据集,包含了各种手写数 ...
- MNIST机器学习
MNIST是一个入门级的计算机视觉数据集,它包含各种手写数字图片: 1. MNIST数据集 MNIST,是不是听起来特高端大气,不知道这个是什么东西? == 手写数字分类问题所要用到的(经典)MNIS ...
- TensorFlow框架(3)之MNIST机器学习入门
1. MNIST数据集 1.1 概述 Tensorflow框架载tensorflow.contrib.learn.python.learn.datasets包中提供多个机器学习的数据集.本节介绍的是M ...
- MNIST机器学习数据集
介绍 在学习机器学习的时候,首当其冲的就是准备一份通用的数据集,方便与其他的算法进行比较.在这里,我写了一个用于加载MNIST数据集的方法,并将其进行封装,主要用于将MNIST数据集转换成numpy. ...
- MNIST机器学习入门(一)
一.简介 首先介绍MNIST 数据集.如图1-1 所示, MNIST 数据集主要由一些手写数字的图片和相应的标签组成,图片一共有10 类,分别对应从0-9 ,共10 个阿拉伯数字. 原始的MNIST ...
随机推荐
- Linux 搭建批量网络装机
- 实验九 FBG 团队项目需求改进与系统设计
任务一 A.<项目需求规格说明书>分析 根据老师的指导以及本周所学的OOA,分析改进上周编写的<项目需求规格说明书>,发现需求项目书UML图例描述不够完善,仅仅是用例图没办法更 ...
- 移动vue项目,启动错误:Module build failed: Error: No PostCSS Config found in:
解决办法:在根目录新建postcss.config.js module.exports = { plugins: { 'autoprefixer': {browsers: 'last 5 versio ...
- 远程连接bat
@Echo offSet SERVER=10.40.61.101Set USERNAME=AdministratorSet PASSWORD=Marvin2008 Cmdkey /generic:TE ...
- python学习(十一)
- Mtlab:抛物型方程的交替方向隐格式(ADI)
tic; clear clc M=[,,,,]; N=M; :length(M) h=/M(p);% 这里定义空间步长等距 tau=/N(p); % 时间步长 x=:h:; y=:h:; t=:tau ...
- 微信小程序分享及信息追踪
我就是个搬用工—来源:https://www.jianshu.com/p/87a75ec2fd53 小程序分享群及信息追踪 需求 页面分享 小程序页面分享链接增加source参数,值为用户ID加密 ...
- WebSocket 实战--转
原文地址:http://www.ibm.com/developerworks/cn/java/j-lo-WebSocket/ WebSocket 前世今生 众所周知,Web 应用的交互过程通常是客户端 ...
- C++标准模板库(STL)之Vector
在C中,有很多东西需要自己实现.C++提供了标准模板库(Standard Template Libray,STL),其中封装了很多容器,不需要费力去实现它们的细节而直接调用函数来实现功能. 具体容器链 ...
- markdown在线编辑插件mditor
官方地址 https://bh-lay.github.io/mditor/ ##使用方法 #1.页面添加dom ```javascript <textarea id="md_edito ...