deepNN
不做卷积,只是增加多层神经网络层。
#-*- encoding:utf-8 -*-
#!/usr/local/env python 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("mnist", one_hot=True) learning_rate = 0.01
batch_size = 128
n_epochs = 70 X = tf.placeholder(tf.float32, [batch_size, 784])
Y = tf.placeholder(tf.float32, [batch_size, 10]) layer_dims = [784, 500, 500, 10]
layer_count = len(layer_dims)-1 # 不算输入层
layer_iter = X for l in range(1, layer_count): # layer [1,layer_count-1] is hidden layer
layer_iter = add_layer(layer_iter, layer_dims[l-1], layer_dims[l], activation_function=tf.nn.relu)
prediction = add_layer(layer_iter, layer_dims[layer_count-1], layer_dims[layer_count], 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.test.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 i % 10 == 5 and j == 0:
print( "Loss of epochs[{0}]: {1}".format(i, 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))
deepNN的更多相关文章
- TensorFlow框架(4)之CNN卷积神经网络
1. 卷积神经网络 1.1 多层前馈神经网络 多层前馈神经网络是指在多层的神经网络中,每层神经元与下一层神经元完全互连,神经元之间不存在同层连接,也不存在跨层连接的情况,如图 11所示. 图 11 对 ...
- Tensorflow之卷积神经网络(CNN)
前馈神经网络的弊端 前一篇文章介绍过MNIST,是采用的前馈神经网络的结构,这种结构有一个很大的弊端,就是提供的样本必须面面俱到,否则就容易出现预测失败.如下图: 同样是在一个图片中找圆形,如果左边为 ...
- Tensorflow卷积神经网络
卷积神经网络(Convolutional Neural Network, CNN)是一种前馈神经网络, 在计算机视觉等领域被广泛应用. 本文将简单介绍其原理并分析Tensorflow官方提供的示例. ...
- Tensorflow模型加载与保存、Tensorboard简单使用
先上代码: from __future__ import absolute_import from __future__ import division from __future__ import ...
- 深度学习之卷积神经网络(CNN)详解与代码实现(二)
用Tensorflow实现卷积神经网络(CNN) 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/10737065. ...
- Tensorflow卷积神经网络[转]
Tensorflow卷积神经网络 卷积神经网络(Convolutional Neural Network, CNN)是一种前馈神经网络, 在计算机视觉等领域被广泛应用. 本文将简单介绍其原理并分析Te ...
- tensorflow下基于DNN实现实时分辨人脸微表情
参加学校的国创比赛的时候,我们小组的项目有一部分内容需要用到利用摄像头实现实时检测人脸的表情,因为最近都在看深度学习方面的相关知识,所以就自己动手实现了一下这个小Demo.参考网上的资料,发现大部分是 ...
- 使用卷积神经网络CNN训练识别mnist
算的的上是自己搭建的第一个卷积神经网络.网络结构比较简单. 输入为单通道的mnist数据集.它是一张28*28,包含784个特征值的图片 我们第一层输入,使用5*5的卷积核进行卷积,输出32张特征图, ...
- TensorFlow车辆检测
1.先在UIUC Image Database for Car Detection下载训练数据集. 下载地址:http://cogcomp.org/Data/Car/ 下载解压之后文件目录如图所示,这 ...
随机推荐
- Soapui groovy 加载外部property文件
proFile = "c:\\test.properties" log.info proFile props = new java.util.Properties() props. ...
- ubuntu 51单片机环境搭建方法
首先下载个sdcc 1: sudo apt-get install sdcc 2: sudo apt-get install libvte-dev3: 安装 gSTC-ISP 下载地址:http:// ...
- hibernate使用注解设置日期默认值
用注解设置属性的默认值时 使用 @Temporal(TemporalType.TIMESTAMP) @Column(updatable = false,nullable=false,length=20 ...
- ASP.NET Core网站初探
原文地址:https://blog.csdn.net/iml6yu/article/details/74530679 目录结构如下图 目录: Properties:属性,记录了项目属性的配置文件. ...
- 代码收藏系列--php--生成简短唯一订单号(转载)
代码收藏系列--php--生成简短唯一订单号 /** * 生成商家交易单号 * <br />特点:不重复 * <br />示例: * <br />普通付款:arra ...
- python学习笔记——git的安装及使用
1 git的基本介绍 git 是目前世界上最先进的分布式版本哦内阁制系统 详细信息可参考廖雪峰的官方网站中的Git教程 比git功能更加强大的有TortoiseGit和Tortoise SVN,具体安 ...
- linux文件系统 - 初始化(二)
加载initrd(上) 一.目的 本文主要讲述linux3.10文件系统初始化过程的第二阶段:加载initrd. initrd是一个临时文件系统,由bootload负责加载到内存中,里面包含了基本的可 ...
- Sublime text —— 自定义主题Soda
编辑器的主题有两种,一种是语法高亮颜色主题,一种是编辑器自身显示主题,如果要自定义编辑器样式,个人推荐soda. Ctrl+Shift+p 输入install,接着输入 soda,选择 Theme ...
- mybatis与mysql中的Date和String之间转换
在javaweb开发过程中,难免会使用日期类型,在持久化时java的日期格式可以是String,Date.mysql格式可以是varchar,datetime.他们之间如何进行相互转化? 1 java ...
- 微信小程序如何获取屏幕宽度
微信小程序如何获取屏幕宽度 方法1: imageLoad: function () { this.setData({ imageWidth: wx.getSystemInfoSync().window ...