吴裕雄 PYTHON 神经网络——TENSORFLOW 单隐藏层自编码器设计处理MNIST手写数字数据集并使用TensorBord描绘神经网络数据
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描绘神经网络数据的更多相关文章
- 吴裕雄 PYTHON 神经网络——TENSORFLOW 双隐藏层自编码器设计处理MNIST手写数字数据集并使用TENSORBORD描绘神经网络数据2
import os import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data os.envi ...
- Android+TensorFlow+CNN+MNIST 手写数字识别实现
Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...
- 基于tensorflow的MNIST手写数字识别(二)--入门篇
http://www.jianshu.com/p/4195577585e6 基于tensorflow的MNIST手写字识别(一)--白话卷积神经网络模型 基于tensorflow的MNIST手写数字识 ...
- TensorFlow——MNIST手写数字识别
MNIST手写数字识别 MNIST数据集介绍和下载:http://yann.lecun.com/exdb/mnist/ 一.数据集介绍: MNIST是一个入门级的计算机视觉数据集 下载下来的数据集 ...
- Tensorflow实现MNIST手写数字识别
之前我们讲了神经网络的起源.单层神经网络.多层神经网络的搭建过程.搭建时要注意到的具体问题.以及解决这些问题的具体方法.本文将通过一个经典的案例:MNIST手写数字识别,以代码的形式来为大家梳理一遍神 ...
- mnist手写数字识别——深度学习入门项目(tensorflow+keras+Sequential模型)
前言 今天记录一下深度学习的另外一个入门项目——<mnist数据集手写数字识别>,这是一个入门必备的学习案例,主要使用了tensorflow下的keras网络结构的Sequential模型 ...
- 用tensorflow搭建RNN(LSTM)进行MNIST 手写数字辨识
用tensorflow搭建RNN(LSTM)进行MNIST 手写数字辨识 循环神经网络RNN相比传统的神经网络在处理序列化数据时更有优势,因为RNN能够将加入上(下)文信息进行考虑.一个简单的RNN如 ...
- Tensorflow可视化MNIST手写数字训练
简述] 我们在学习编程语言时,往往第一个程序就是打印“Hello World”,那么对于人工智能学习系统平台来说,他的“Hello World”小程序就是MNIST手写数字训练了.MNIST是一个手写 ...
- 基于TensorFlow的MNIST手写数字识别-初级
一:MNIST数据集 下载地址 MNIST是一个包含很多手写数字图片的数据集,一共4个二进制压缩文件 分别是test set images,test set labels,training se ...
随机推荐
- Winfrom控件 特效
链接:https://pan.baidu.com/s/1O9e7sxnYFYWD55Vh5fxFQg 提取码:5cey 复制这段内容后打开百度网盘手机App,操作更方便哦 Winfrom控件查询手册. ...
- CSS的长度单位
对于css的长度单位真的有必要知道一下.那么css长度单位有哪些呢? 分成两大类: 1.绝对单位:不会因为其他元素的尺寸变化而变化.坚持自我. 2.相对单位:没有一个确定的值,而是由其他元素的尺寸影响 ...
- MySql 中关键字 case when then else end 的用法
解释: SELECT case -------------如果 ' then '男' -------------sex='1',则返回值'男' ' then '女' -------------sex= ...
- Python记
在企业应用领域,Java或C#都是不错的选择.
- windows查询端口,杀进程
C:\Users\chenquan>tasklist | findstr 4720Thunder.exe 4720 Console 1 3,456 K C:\Users\chenquan> ...
- python面试的100题(2)
def print_directory_contents(sPath): """ 这个函数接收文件夹的名称作为输入参数 返回该文件夹中文件的路径 以及其包含文件夹中文件的 ...
- 阿里云的maven仓库 地址
<mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name&g ...
- 题解【CJOJ2608】[JZOJ 100043]第k小数
P2608 - [JZOJ 100043]第k小数 Description 有两个非负整数数列,元素个数分别为N和M.从两个数列中分别任取一个数相乘,这样一共可以得到N*M个数,询问这N*M个数中第K ...
- 题解【Codeforces886B】Vlad and Cafes
本题是模拟题. 我们可以用b数组记录每个数字在a数组中出现的最后位置,然后从0到2·10^5依次寻找最后一次出现最早的数(注意是0!),最后统计输出即可. AC代码: #include <bit ...
- goahead web 服务器
https://blog.csdn.net/xieyihua1994/article/details/74002413