莫烦TensorFlow_11 MNIST优化使用CNN
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data #number 1 to 10 data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True) def compute_accuracy(v_xs, v_ys):
global prediction
y_pre = sess.run(prediction, feed_dict={xs:v_xs, keep_prob:1})
correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict={xs:v_xs, ys:v_ys, keep_prob:1})
return result def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1) # initial variables with normal distribution
return tf.Variable(initial) def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial) def conv2d(x, W):
#strides [1, x_movement, y_movement, 1]
#Must have strides[0] = strides[3] = 1
return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding = 'SAME') def max_pool_2x2(x):
#strides [1, x_movement, y_movement, 1]
#Must have strides[0] = strides[3] = 1
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides = [1,2,2,1], padding = 'SAME') #define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 784])
ys = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32)
x_image = tf.reshape(xs, [-1, 28, 28, 1])
#print(x_image.shape) #[n_sample, 28, 28, 1] ## conv1 layer ##
W_conv1 = weight_variable([5,5,1,32])#patch 5x5, in in size 1, out size 32
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # output size 28x28x32
h_pool1 = max_pool_2x2(h_conv1)# output size 14x14x32 ## conv2 layer ##
W_conv2 = weight_variable([5,5,32, 64])#patch 5x5, in in size 32, out size 64
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) # output size 14x14x64
h_pool2 = max_pool_2x2(h_conv2)# output size 7x7x64 ## func1 layer ##
W_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024])
# [n_sample, 7,7,64] ->> [n_sample, 7*7*64]
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)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) ## func2 layer ##
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10]) prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) # the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),
reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) sess = tf.Session()
sess.run(tf.global_variables_initializer()) for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict = {xs:batch_xs, ys:batch_ys, keep_prob:0.8})
if i% 50 == 0:
print(compute_accuracy(mnist.test.images, mnist.test.labels))
两层卷积层
训练速度慢了,但是精度提高了
莫烦TensorFlow_11 MNIST优化使用CNN的更多相关文章
- 莫烦TensorFlow_09 MNIST例子
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...
- 莫烦 - Pytorch学习笔记 [ 二 ] CNN ( 1 )
CNN原理和结构 观点提出 关于照片的三种观点引出了CNN的作用. 局部性:某一特征只出现在一张image的局部位置中. 相同性: 同一特征重复出现.例如鸟的羽毛. 不变性:subsampling下图 ...
- 稍稍乱入的CNN,本文依然是学习周莫烦视频的笔记。
稍稍乱入的CNN,本文依然是学习周莫烦视频的笔记. 还有 google 在 udacity 上的 CNN 教程. CNN(Convolutional Neural Networks) 卷积神经网络简单 ...
- 莫烦pytorch学习笔记(七)——Optimizer优化器
各种优化器的比较 莫烦的对各种优化通俗理解的视频 import torch import torch.utils.data as Data import torch.nn.functional as ...
- tensorflow学习笔记-bili莫烦
bilibili莫烦tensorflow视频教程学习笔记 1.初次使用Tensorflow实现一元线性回归 # 屏蔽警告 import os os.environ[' import numpy as ...
- 莫烦pytorch学习笔记(八)——卷积神经网络(手写数字识别实现)
莫烦视频网址 这个代码实现了预测和可视化 import os # third-party library import torch import torch.nn as nn import torch ...
- 【莫烦Pytorch】【P1】人工神经网络VS. 生物神经网络
滴:转载引用请注明哦[握爪] https://www.cnblogs.com/zyrb/p/9700343.html 莫烦教程是一个免费的机器学习(不限于)的学习教程,幽默风俗的语言让我们这些刚刚起步 ...
- tensorflow 莫烦教程
1,感谢莫烦 2,第一个实例:用tf拟合线性函数 import tensorflow as tf import numpy as np # create data x_data = np.random ...
- 莫烦大大TensorFlow学习笔记(9)----可视化
一.Matplotlib[结果可视化] #import os #os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf i ...
随机推荐
- angular的Hash 模式和 HTML 5 模式
去除地址 # ,将{ provide: LocationStrategy, useClass: HashLocationStrategy }改为 { provide: LocationStrategy ...
- T-SQL 简介
1. 变量说明语句:用来说明变量的命令. 2. 数据定义语言(Data Definition Language,DDL):用来建立数据库和定义列等数据结构,主要是create table,drop d ...
- 内网Metasploit映射到外网
下载frp Github项目地址:https://github.com/fatedier/frp 找到最新的releases下载,系统版本自行确认. 下载方法: wget https://github ...
- vue与Element实际应用参考
https://www.cnblogs.com/dmcl/p/6722315.html https://www.cnblogs.com/hbb0b0/p/8399996.html https://ww ...
- C#教程之C#属性(Attribute)用法实例解析
引用:https://www.xin3721.com/ArticlecSharp/c11686.html 属性(Attribute)是C#程序设计中非常重要的一个技术,应用范围广泛,用法灵活多变.本文 ...
- 按照官网的升级完socket.io报错Manager is being released。
查阅了很多资料和英文官网自己也提出了一些问题,估计官网以前有该类的问题历史,懒得回复. 终于功夫不负有心人原因竟然是:你的manager被释放了. you need to make sure the ...
- HTML+CSS基础 权重的计算 权重计算规则
权重的计算 将选择器上面的选择器进行叠加,叠加后的总和就是该选择器的权重. 权重计算规则
- Dapper - 一款轻量级对象关系映射(ORM)组件,DotNet 下
Dapper - a simple object mapper for .Net Official Github clone: https://github.com/SamSaffron/dapper ...
- 异步编程,await async入门
网上很多异步编程的文章,提供一篇入门: 异步编程模型 .net支持3种异步编程模式: msdn:https://docs.microsoft.com/zh-cn/dotnet/standard/asy ...
- IDEA创建xml文件
今天在用IDEA写项目的时候发现,创建xml文件只能通过File手动输入去创建,但在我看的一个学习视频上可以直接创建xml文件,好奇之下研究了一下,作此篇,希望能对需要的朋友有所帮助. 废话就不多说了 ...