import os
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data os.environ['TF_CPP_MIN_LOG_LEVEL'] = '' batch_size = 128 # batch容量
display_step = 1 # 展示间隔
learning_rate = 0.01 # 学习率
training_epochs = 20 # 训练轮数,1轮等于n_samples/batch_size
example_to_show = 10 # 展示图像数目 n_hidden1_units = 256 # 第一隐藏层
n_hidden2_units = 128 # 第二隐藏层
n_input_units = 784
n_output_units = n_input_units def variable_summaries(var):
with tf.name_scope('summaries'):
mean = tf.reduce_mean(var)
tf.summary.histogram('mean', mean)
with tf.name_scope('stddev'):
stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
tf.summary.scalar('stddev', stddev) # 注意,这是标量
tf.summary.scalar('max', tf.reduce_max(var))
tf.summary.scalar('min', tf.reduce_min(var))
tf.summary.histogram('histogram', var)
def WeightsVariable(n_in,n_out,name_str):
return tf.Variable(tf.random_normal([n_in,n_out]),dtype=tf.float32,name=name_str) def biasesVariable(n_out,name_str):
return tf.Variable(tf.random_normal([n_out]),dtype=tf.float32,name=name_str) def encoder(x_origin,activate_func=tf.nn.sigmoid):
with tf.name_scope('Layer1'):
Weights = WeightsVariable(n_input_units,n_hidden1_units,'Weights')
biases = biasesVariable(n_hidden1_units,'biases')
x_code1 = activate_func(tf.add(tf.matmul(x_origin,Weights),biases))
variable_summaries(Weights)
variable_summaries(biases)
with tf.name_scope('Layer2'):
Weights = WeightsVariable(n_hidden1_units,n_hidden2_units,'Weights')
biases = biasesVariable(n_hidden2_units,'biases')
x_code2 = activate_func(tf.add(tf.matmul(x_code1,Weights),biases))
variable_summaries(Weights)
variable_summaries(biases)
return x_code2 def decode(x_code,activate_func=tf.nn.sigmoid):
with tf.name_scope('Layer1'):
Weights = WeightsVariable(n_hidden2_units,n_hidden1_units,'Weights')
biases = biasesVariable(n_hidden1_units,'biases')
x_decode1 = activate_func(tf.add(tf.matmul(x_code,Weights),biases))
variable_summaries(Weights)
variable_summaries(biases)
with tf.name_scope('Layer2'):
Weights = WeightsVariable(n_hidden1_units,n_output_units,'Weights')
biases = biasesVariable(n_output_units,'biases')
x_decode2 = activate_func(tf.add(tf.matmul(x_decode1,Weights),biases))
variable_summaries(Weights)
variable_summaries(biases)
return x_decode2 with tf.Graph().as_default():
with tf.name_scope('Input'):
X_input = tf.placeholder(tf.float32,[None,n_input_units])
with tf.name_scope('Encode'):
X_code = encoder(X_input)
with tf.name_scope('decode'):
X_decode = decode(X_code)
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.pow(X_input - X_decode,2))
with tf.name_scope('train'):
Optimizer = tf.train.RMSPropOptimizer(learning_rate)
train = Optimizer.minimize(loss)
# 标量汇总
with tf.name_scope('LossSummary'):
tf.summary.scalar('loss',loss)
tf.summary.scalar('learning_rate',learning_rate)
# 图像展示
with tf.name_scope('ImageSummary'):
image_original = tf.reshape(X_input,[-1, 28, 28, 1])
image_reconstruction = tf.reshape(X_decode, [-1, 28, 28, 1])
tf.summary.image('image_original', image_original, 9)
tf.summary.image('image_recinstruction', image_reconstruction, 9)
# 汇总
merged_summary = tf.summary.merge_all() init = tf.global_variables_initializer() writer = tf.summary.FileWriter(logdir='E:\\tensorboard\\logsssxx', graph=tf.get_default_graph())
writer.flush() mnist = input_data.read_data_sets('E:\\MNIST_data\\', one_hot=True) with tf.Session() as sess:
sess.run(init)
total_batch = int(mnist.train.num_examples / batch_size)
for epoch in range(training_epochs):
for i in range(total_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
_,Loss = sess.run([train,loss],feed_dict={X_input: batch_xs})
Loss = sess.run(loss,feed_dict={X_input: batch_xs})
if epoch % display_step == 0:
print('Epoch: %04d' % (epoch + 1),'loss= ','{:.9f}'.format(Loss))
summary_str = sess.run(merged_summary,feed_dict={X_input: batch_xs})
writer.add_summary(summary_str,epoch)
writer.flush()
writer.close()
print('训练完毕!')

吴裕雄 PYTHON 神经网络——TENSORFLOW 双隐藏层自编码器设计处理MNIST手写数字数据集并使用TENSORBORD描绘神经网络数据2的更多相关文章

  1. 吴裕雄 PYTHON 神经网络——TENSORFLOW 单隐藏层自编码器设计处理MNIST手写数字数据集并使用TensorBord描绘神经网络数据

    import os import numpy as np import tensorflow as tf import matplotlib.pyplot as plt from tensorflow ...

  2. 『TensorFlow』单&双隐藏层自编码器设计

    计算图设计 很简单的实践, 多了个隐藏层 没有上节的高斯噪声 网络写法由上节的面向对象改为了函数式编程, 其他没有特别需要注意的,实现如下: import numpy as np import mat ...

  3. Android+TensorFlow+CNN+MNIST 手写数字识别实现

    Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...

  4. TensorFlow——MNIST手写数字识别

    MNIST手写数字识别 MNIST数据集介绍和下载:http://yann.lecun.com/exdb/mnist/   一.数据集介绍: MNIST是一个入门级的计算机视觉数据集 下载下来的数据集 ...

  5. Tensorflow实现MNIST手写数字识别

    之前我们讲了神经网络的起源.单层神经网络.多层神经网络的搭建过程.搭建时要注意到的具体问题.以及解决这些问题的具体方法.本文将通过一个经典的案例:MNIST手写数字识别,以代码的形式来为大家梳理一遍神 ...

  6. mnist手写数字识别——深度学习入门项目(tensorflow+keras+Sequential模型)

    前言 今天记录一下深度学习的另外一个入门项目——<mnist数据集手写数字识别>,这是一个入门必备的学习案例,主要使用了tensorflow下的keras网络结构的Sequential模型 ...

  7. 基于tensorflow的MNIST手写数字识别(二)--入门篇

    http://www.jianshu.com/p/4195577585e6 基于tensorflow的MNIST手写字识别(一)--白话卷积神经网络模型 基于tensorflow的MNIST手写数字识 ...

  8. 用tensorflow搭建RNN(LSTM)进行MNIST 手写数字辨识

    用tensorflow搭建RNN(LSTM)进行MNIST 手写数字辨识 循环神经网络RNN相比传统的神经网络在处理序列化数据时更有优势,因为RNN能够将加入上(下)文信息进行考虑.一个简单的RNN如 ...

  9. Tensorflow可视化MNIST手写数字训练

    简述] 我们在学习编程语言时,往往第一个程序就是打印“Hello World”,那么对于人工智能学习系统平台来说,他的“Hello World”小程序就是MNIST手写数字训练了.MNIST是一个手写 ...

随机推荐

  1. 小tips:使用vue-cli脚手架搭建项目,关于eslint语法检测配置

    配置文件在项目根目录里,文件名以 .eslintrc.* 为名. 为了兼容以前写的代码,避免修改太多代码,把不符合自己习惯的规则去掉,简单配置代码: module.exports = { root: ...

  2. DotnetCore 使用Jwks验证JwtToken签名

    [Fact] public async Task VerfiyJwtTokenUseJwks() { var jwt = @"your jwt token"; var wellKn ...

  3. 如何删除github中的repository

    打开个人界面->点击进入想删除的repository的界面->拉到最下面的danger zone->delete

  4. go-redis 基于beego正确使用序列化存储数据和反序列化获取数据

    安装go-redis // 安装命令 go get github.com/gomodule/redigo/redis // 导入使用 import( "github.com/gomodule ...

  5. 题解【UVA12003】Array Transformer

    题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例#1 10 1 11 1 2 3 4 5 6 7 8 9 10 2 8 6 10 输出样例#1 1 2 3 4 5 6 7 8 9 6 ...

  6. 051_switch语句的使用 052_while循环详解 053_for循环详解_dowhile简介 054_嵌套循环_循环相关练习

    051_switch语句的使用 package testmode2;/** * 测试switch语句 * 遇到多值判断的时候,使用switch.当然,switch完全可以使用ifelseifelse代 ...

  7. Linux - 文件时间戳

    概述 简介 linux 文件时间戳 背景 最近感觉很消极的样子 心情不好加不知道写啥 随便水一水 能水的就那么多, 水一次, 少一次 环境 os centos7 1. 时间戳 概述 简述 时间戳 li ...

  8. 每天进步一点点------Allegro 铺铜详解

    铺铜在设计PCB板时很重要,为了加深理解,笔者写下这篇学习的过程. 首先要理解什么是正片和负片,结合网上的资料来理解一下: 正片实际就是能在底片上能看到的就是存在的 负片实际上就是在底片看到的就是不存 ...

  9. Linux服务器时间设置及同步

    闲余:夏日将到,园区计划五一期间进行大面积的电网停电检修,运维同学因此将公司测试服务器提前关闭了.收假后,测试告诉我,他发现一个bug--一段定时任务程序未执行,我的第一反应就是--会不会是假期测试服 ...

  10. makecert 产出证书

    C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Bin>makecert -r -n // -e // -sv mymuse.pvk my ...