吴裕雄 PYTHON 神经网络——TENSORFLOW 双隐藏层自编码器设计处理MNIST手写数字数据集并使用TENSORBORD描绘神经网络数据2
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的更多相关文章
- 吴裕雄 PYTHON 神经网络——TENSORFLOW 单隐藏层自编码器设计处理MNIST手写数字数据集并使用TensorBord描绘神经网络数据
import os import numpy as np import tensorflow as tf import matplotlib.pyplot as plt from tensorflow ...
- 『TensorFlow』单&双隐藏层自编码器设计
计算图设计 很简单的实践, 多了个隐藏层 没有上节的高斯噪声 网络写法由上节的面向对象改为了函数式编程, 其他没有特别需要注意的,实现如下: import numpy as np import mat ...
- Android+TensorFlow+CNN+MNIST 手写数字识别实现
Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...
- TensorFlow——MNIST手写数字识别
MNIST手写数字识别 MNIST数据集介绍和下载:http://yann.lecun.com/exdb/mnist/ 一.数据集介绍: MNIST是一个入门级的计算机视觉数据集 下载下来的数据集 ...
- Tensorflow实现MNIST手写数字识别
之前我们讲了神经网络的起源.单层神经网络.多层神经网络的搭建过程.搭建时要注意到的具体问题.以及解决这些问题的具体方法.本文将通过一个经典的案例:MNIST手写数字识别,以代码的形式来为大家梳理一遍神 ...
- mnist手写数字识别——深度学习入门项目(tensorflow+keras+Sequential模型)
前言 今天记录一下深度学习的另外一个入门项目——<mnist数据集手写数字识别>,这是一个入门必备的学习案例,主要使用了tensorflow下的keras网络结构的Sequential模型 ...
- 基于tensorflow的MNIST手写数字识别(二)--入门篇
http://www.jianshu.com/p/4195577585e6 基于tensorflow的MNIST手写字识别(一)--白话卷积神经网络模型 基于tensorflow的MNIST手写数字识 ...
- 用tensorflow搭建RNN(LSTM)进行MNIST 手写数字辨识
用tensorflow搭建RNN(LSTM)进行MNIST 手写数字辨识 循环神经网络RNN相比传统的神经网络在处理序列化数据时更有优势,因为RNN能够将加入上(下)文信息进行考虑.一个简单的RNN如 ...
- Tensorflow可视化MNIST手写数字训练
简述] 我们在学习编程语言时,往往第一个程序就是打印“Hello World”,那么对于人工智能学习系统平台来说,他的“Hello World”小程序就是MNIST手写数字训练了.MNIST是一个手写 ...
随机推荐
- Django | pycharm 提示 unresolved attribute referene 'objects' for class 'xxxx'
objects高亮,提示信息为unresolved attribute referene 'objects' for class 'BookInfo' 当前情况是pycharm没有识别到objects ...
- jquery-validation.js验证插件使用详解
jquery-validation 使用 一.用前必备 官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: ...
- 结合Thread Ninja明确与处理异步协程中的异常
Thread Ninja说明: Thread Ninja - Multithread Coroutine Requires Unity 3.4.0 or higher. A simple script ...
- 阿里云 Linux 挂在硬盘 翻了几篇这个最好
原文 :https://www.jianshu.com/p/fa587bbfbb60 阿里云数据盘挂载完整过程 阿里云挂载云盘第一步 在阿里云管理员后台,云盘管理中心挂载好云盘在哪个服务器上面. 登录 ...
- HTMLinput标签
<input> 标签用于搜集用户信息. 常用的属性: type指定输入项的类型 name定义 input 元素的名称. id给输入项取一个名字,方便后期找到和操作 type指定输入项的类型 ...
- 题解【洛谷P1038/CJOJ1707】[NOIP2003提高组]神经网络
[NOIP2003]神经网络 Description 问题背景:人工神经网络( Artificial Neural Network )是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款 ...
- Python多线程join/setDaemon
import threading, time class Test(): def test1(self): print("--") time.sleep(3) print(&quo ...
- linux deploy---旧手机变废为宝
前几天朋友送了我一部红米Note 1s,本来不想要,转念一想,不要白不要,就收了. 拿到之后我就想,这么一个1+8的手机能做什么呢? 翻遍了CSDN和简书,找到了一个性价比不错的方法:给旧手机装上一个 ...
- 【转载】Java开发中的23种设计模式
转自:http://zz563143188.iteye.com/blog/1847029 一.设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建 ...
- Java连载64-finally语法及其注意事项
一.finally语句块 1.注意点: (1)finally语句块可以直接和try语句块联合使用.try...finally.... (2)try.....catch.....finally也可以执行 ...