import os
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data os.environ['TF_CPP_MIN_LOG_LEVEL'] = '' learning_rate = 0.01 # 学习率
training_epochs = 20 # 训练轮数,1轮等于n_samples/batch_size
batch_size = 128 # batch容量
display_step = 1 # 展示间隔
example_to_show = 10 # 展示图像数目 n_hidden_units = 256
n_input_units = 784
n_output_units = n_input_units 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('Layer'):
Weights = WeightsVariable(n_input_units, n_hidden_units, 'Weights')
biases = biasesVariable(n_hidden_units, 'biases')
x_code = activate_func(tf.add(tf.matmul(x_origin, Weights), biases))
return x_code def decode(x_code, activate_func=tf.nn.sigmoid):
with tf.name_scope('Layer'):
Weights = WeightsVariable(n_hidden_units, n_output_units, 'Weights')
biases = biasesVariable(n_output_units, 'biases')
x_decode = activate_func(tf.add(tf.matmul(x_code, Weights), biases))
return x_decode 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) init = tf.global_variables_initializer()
writer = tf.summary.FileWriter(logdir='logs', graph=tf.get_default_graph())
writer.flush() learning_rate = 0.01 # 学习率
training_epochs = 20 # 训练轮数,1轮等于n_samples/batch_size
batch_size = 128 # batch容量
display_step = 1 # 展示间隔
example_to_show = 10 # 展示图像数目 n_hidden_units = 256
n_input_units = 784
n_output_units = n_input_units 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('Layer'):
Weights = WeightsVariable(n_input_units, n_hidden_units, 'Weights')
biases = biasesVariable(n_hidden_units, 'biases')
x_code = activate_func(tf.add(tf.matmul(x_origin, Weights), biases))
return x_code def decode(x_code, activate_func=tf.nn.sigmoid):
with tf.name_scope('Layer'):
Weights = WeightsVariable(n_hidden_units, n_output_units, 'Weights')
biases = biasesVariable(n_output_units, 'biases')
x_decode = activate_func(tf.add(tf.matmul(x_code, Weights), biases))
return x_decode 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)
init = tf.global_variables_initializer() writer = tf.summary.FileWriter(logdir='E:\\tensorboard\\logs', 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))
writer.close()
print('训练完毕!') '''比较输入和输出的图像'''
# 输出图像获取
reconstructions = sess.run(X_decode, feed_dict={X_input: mnist.test.images[:example_to_show]})
# 画布建立
f, a = plt.subplots(2, 10, figsize=(10, 2))
for i in range(example_to_show):
a[0][i].imshow(np.reshape(mnist.test.images[i], (28, 28)))
a[1][i].imshow(np.reshape(reconstructions[i], (28, 28)))
f.show() # 渲染图像
plt.draw() # 刷新图像
# plt.waitforbuttonpress()

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

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

    import os import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data os.envi ...

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

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

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

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

  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搭建RNN(LSTM)进行MNIST 手写数字辨识

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

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

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

  9. 基于TensorFlow的MNIST手写数字识别-初级

    一:MNIST数据集    下载地址 MNIST是一个包含很多手写数字图片的数据集,一共4个二进制压缩文件 分别是test set images,test set labels,training se ...

随机推荐

  1. 期货homes平台以及仿ctp接口

    实盘账户或者模拟账户可以下挂多个子账户 子账户也可以是homes母账户,理论上可以一层一层套下去. 所有交易细节全部保存,收盘定时结算. 功能很强大,并且还有很多拓展空间. 连接homes平台,需要用 ...

  2. (转载)设置虚拟机桥接模式以及解决桥接模式上不了网以及ping不通主机的问题

    解决问题的博客地址:设置虚拟机桥接模式以及解决桥接模式上不了网以及ping不通主机的问题 遇见的问题: 1.VMnet8无法设置为桥接模式 结论:只要主机网络可被桥接,VMnet8根本不需要设为桥接模 ...

  3. CentOS7识别不到win10启动项的解决方法

    Windows的文件系统是NTFS格式的,而CentOS是不支持NTFS格式的.因此,我们要安装另外的工具使CentOS能识别NTFS格式的文件系统. 这里我们选择ntfs-3g这个工具,安装过程如下 ...

  4. docker映射

    端口映射 大-P对容器暴露的所有端口进行映射 小-p可以指定对哪些端口进行映射 第一种,只指定容器的端口,宿主机的端口是随机映射的 第二种,宿主机的端口和容器的端口一一对应, 第三种,只配置容器的ip ...

  5. Java-POJ1005-I Think I Need a Houseboat

    盗用的翻译,哈哈哈!白嫖就完事了. 题目: 密西西比河岸某处陆地因为河水侵蚀,每年陆地面积都在减少,每年减少50平方英里,减少的陆地面积呈半圆形,即该半圆形面积以每年50平方英里的速度增长.在第一年初 ...

  6. python接口自动化之用HTMLTestRunner生成html测试报告

    [第一步]:引入HTMLTestRunner包 1.下载HTMLTestRunner,下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html ...

  7. 实现排行榜神器——redis zset

    需求:假如现在需要搞个 “运动消耗卡路里排行榜”,例似微信步数排名,显示排名前20人的信息和消耗的卡里路,怎样实现排序? 一般思路:存储信息,然后数据库查询,排序?(假如有几十万人参与排名,这样查my ...

  8. freopen函数的使用以及freopen与fopen的区别

    freopen函数的使用:参见这篇博客https://www.cnblogs.com/moonlit/archive/2011/06/12/2078712.html 当我们求解acm题目时,通常在设计 ...

  9. Verilog 编写规范

    在学习Python时,作者有一句话对我影响很大.作者希望我们在学习编写程序的时候注意一些业内约定的规范.在内行人眼中,你的编写格式,就已经暴露了你的程度.学习verilog也是一样的道理,一段好的ve ...

  10. Lowest Common Multiple Plus 题解

    求n个数的最小公倍数. Input输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. Output为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行.你可以假设最后的 ...