tensorFlow(四)浅层神经网络
tensorFlow见基础
实验
MNIST数据集介绍
60000个已经标注了的训练集,还有10000个用于测试的测试集。
- 上图所示的是一个只有一层隐藏层的浅层神经网络
- 我们有3个输入层节点,分别对应
i[1]i[2]i[3] - 隐藏层有4个节点,分别对应
h[0]h[1]h[2]h[3],对应的激活函数为ReLu函数 - 对于典型的
二分类任务,我们只需要1个输出节点,就是out节点,对应的激活函数是softmax函数
激活函数定义(activation function):

模型设计
MNIST数据一共有784个输入,所以我们需要一个有784个节点的输入层。- 对于中间层,我们设置为
784个节点,使用的激活函数为ReLu MNIST数据使用One-Hot格式输出,有0-910个label,分别对应是否为数字0-9,所以我们在输出层有10个节点,由于0-9的概率是互斥的,我们使用 Softmax 函数作为该层的激活函数
数据准备
MNIST的数据集。使用以下的命令进行下载:wget https://devlab-1251520893.cos.ap-guangzhou.myqcloud.com/t10k-images-idx3-ubyte.gz
wget https://devlab-1251520893.cos.ap-guangzhou.myqcloud.com/t10k-labels-idx1-ubyte.gz
wget https://devlab-1251520893.cos.ap-guangzhou.myqcloud.com/train-images-idx3-ubyte.gz
wget https://devlab-1251520893.cos.ap-guangzhou.myqcloud.com/train-labels-idx1-ubyte.gz
实例代码
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data def add_layer(inputs, in_size, out_size, activation_function=None):
W = tf.Variable(tf.random_normal([in_size, out_size]))
b = tf.Variable(tf.zeros([1, out_size]) + 0.01) Z = tf.matmul(inputs, W) + b
if activation_function is None:
outputs = Z
else:
outputs = activation_function(Z) return outputs if __name__ == "__main__": MNIST = input_data.read_data_sets("./", one_hot=True) learning_rate = 0.05
batch_size = 128
n_epochs = 10 X = tf.placeholder(tf.float32, [batch_size, 784])
Y = tf.placeholder(tf.float32, [batch_size, 10]) l1 = add_layer(X, 784, 1000, activation_function=tf.nn.relu)
prediction = add_layer(l1, 1000, 10, activation_function=None) entropy = tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=prediction)
loss = tf.reduce_mean(entropy) optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss) init = tf.initialize_all_variables() with tf.Session() as sess:
sess.run(init) n_batches = int(MNIST.train.num_examples/batch_size)
for i in range(n_epochs):
for j in range(n_batches):
X_batch, Y_batch = MNIST.train.next_batch(batch_size)
_, loss_ = sess.run([optimizer, loss], feed_dict={X: X_batch, Y: Y_batch})
if j == 0:
print "Loss of epochs[{0}] batch[{1}]: {2}".format(i, j, loss_) # test the model
n_batches = int(MNIST.test.num_examples/batch_size)
total_correct_preds = 0
for i in range(n_batches):
X_batch, Y_batch = MNIST.test.next_batch(batch_size)
preds = sess.run(prediction, feed_dict={X: X_batch, Y: Y_batch})
correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(Y_batch, 1))
accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32)) total_correct_preds += sess.run(accuracy) print "Accuracy {0}".format(total_correct_preds/MNIST.test.num_examples)
代码讲解
add_layer 函数
input_size, 本层的节点个数作为output_size, 并指定激活函数activation_function 可以看到我们调用的时候位神经网络添加了隐藏层1和输出层Loss of epochs[0] batch[0]: 219.555664062
Loss of epochs[1] batch[0]: 4.43757390976
Loss of epochs[2] batch[0]: 6.34549808502
Loss of epochs[3] batch[0]: 4.51894617081
Loss of epochs[4] batch[0]: 1.93666791916
Loss of epochs[5] batch[0]: 1.16164898872
Loss of epochs[6] batch[0]: 2.4195971489
Loss of epochs[7] batch[0]: 0.164100989699
Loss of epochs[8] batch[0]: 2.35461592674
Loss of epochs[9] batch[0]: 1.77732157707
Accuracy 0.9434
可以看到经过10轮的训练,准确度大约在94%左右
tensorFlow(四)浅层神经网络的更多相关文章
- Tensorflow MNIST浅层神经网络的解释和答复
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51416540 看到之前的一篇博文:深入 ...
- deeplearning.ai 神经网络和深度学习 week3 浅层神经网络 听课笔记
1. 第i层网络 Z[i] = W[i]A[i-1] + B[i],A[i] = f[i](Z[i]). 其中, W[i]形状是n[i]*n[i-1],n[i]是第i层神经元的数量: A[i-1]是第 ...
- deeplearning.ai 神经网络和深度学习 week3 浅层神经网络
1. 第i层网络 Z[i] = W[i]A[i-1] + B[i],A[i] = f[i](Z[i]). 其中, W[i]形状是n[i]*n[i-1],n[i]是第i层神经元的数量: A[i-1]是第 ...
- Andrew Ng - 深度学习工程师 - Part 1. 神经网络和深度学习(Week 3. 浅层神经网络)
=================第3周 浅层神经网络=============== ===3..1 神经网络概览=== ===3.2 神经网络表示=== ===3.3 计算神经网络的输出== ...
- 吴恩达《深度学习》-第一门课 (Neural Networks and Deep Learning)-第三周:浅层神经网络(Shallow neural networks) -课程笔记
第三周:浅层神经网络(Shallow neural networks) 3.1 神经网络概述(Neural Network Overview) 使用符号$ ^{[
- 矩池云 | 搭建浅层神经网络"Hello world"
作为图像识别与机器视觉界的 "hello world!" ,MNIST ("Modified National Institute of Standards and Te ...
- ng-深度学习-课程笔记-4: 浅层神经网络(Week3)
1 神经网络概览( Neural Networks Overview ) 先来快速过一遍如何实现神经网络. 首先需要输入特征x,参数w和b,计算出z,然后用激活函数计算出a,在神经网络中我们要做多次这 ...
- [DeeplearningAI笔记]神经网络与深度学习3.2_3.11(激活函数)浅层神经网络
觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.2 神经网络表示 对于一个由输入层,隐藏层,输出层三层所组成的神经网络来说,输入层,即输入数据被称为第0层,中间层被称为第1层,输出层被称为 ...
- 基于MNIST数据集使用TensorFlow训练一个没有隐含层的浅层神经网络
基础 在参考①中我们详细介绍了没有隐含层的神经网络结构,该神经网络只有输入层和输出层,并且输入层和输出层是通过全连接方式进行连接的.具体结构如下: 我们用此网络结构基于MNIST数据集(参考②)进行训 ...
随机推荐
- How do you explain Machine Learning and Data Mining to non Computer Science people?
How do you explain Machine Learning and Data Mining to non Computer Science people? Pararth Shah, ...
- OpenStack-Neutron-VPNaaS-测试和使用
准备 确认安全组规则允许vpn协议通过(tcp协议和icmp协议,测试的时候直接设置“进出”全开) 确认两个子网上的vm可以ping通对方路由的外网ip,确认下两个vm是否可以访问外网 测试环境 (1 ...
- 怎样下载youtube的字幕
第一步:安装chrome浏览器 第二步:安装Tampermonkey扩展. google.com的页面 第三步:使用下面两个链接安装(点进去后点击安装此脚本即可)脚本1,脚本2
- (Review cs231n) Training of Neural Network2
FFDNet---matlab 调用并批处理 format compact; global sigmas; % input noise level or input noise level map a ...
- 分布式系统Paxos算法
转载 原地址:https://www.jdon.com/artichect/paxos.html 主要加一个对应场景,如:Spring Cloud 的 Consul 集权之间的通信,其实是Raft算法 ...
- mysql获取随机字符串和随机数的方法
在我们开发的过程中,我们可能会需要在表中随机生成一些数据以供我们进行相应的测试. 就像我之前发的“mysql创建存储过程向数据表中加入规定条数的数据” 那么我们应该怎样生成随机的字符串和随机数字呢? ...
- activiti5/6 系列之--BpmnModel使用
BpmnModel对象,是activiti动态部署中很重要的一个对象,如果BpmnModel对象不能深入的理解,那可能如果自己需要开发一套流程设计器,使用bpmn-js使用前端或者C/S展现流程流转而 ...
- ipan笔记
// 对于mysql来说, 如果字段没有设置其 default值, 则会自动 设置 default值为null.同理没有设置not null, 则会自动允许null =yes // create ta ...
- SQLyog 字体设置
新公司连接数据库的工具是SQLyog,我以前使用的一直是Navicat,抱着多学个工具也不亏的想法我也安装了试下,但是SQLyog那紧凑的字体对于近视200度的我来说看着着实难受,在询问了公司前辈之后 ...
- 6_linux用户及权限(1)
------------用户管理: useradd,userdel,usermod,passwd,chsh,chfn,finger,id,chage组管理: groupadd,groupdel,gro ...