tensorflow AutoEncoder

TensorFlow上实现AutoEncoder自编码器 一、概述 AutoEncoder大致是一个将数据的高维特征进行压缩降维编码,再经过相反的解码过程的一种学习方法。学习过程中通过解码得到的最终结果与原数据进行比较,通过修正权重偏置参数降低损失函数,不断提高对原数据的复原能力。学习完成后,前半段的编码过程得到结果即可代表原数据的低维“特征值”。通过学习得到的自编码器模型可以实现将高维数据压缩至所期望的维度,原理与PCA相似。 二、模型 阅读详情
import tensorflow as tf

import numpy as np

import tensorflow.examples.tutorials.mnist.input_data as input_data

import math

import scipy

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
#------------------分割线------------------#

batch_size=64

smooth=0.1

path = '/Users/changxingya/Documents/test_image'

#------------------分割线------------------#
#use the msra to initialize the parameters
'''
input_: Tensor of input, format NHWC

shape: [filter_height, filter_width, in_channel, out_channel]

k_step: The size of kernel

name: Name of model

'''
def conv2d(input_, shape, k_step, name):

  with tf.variable_scope(name):

    msra_num = 1.0

    fan_in = k_step * k_step * int(input_.get_shape()[-1])

    stddev = msra_num * (math.sqrt(2. / float(fan_in)))

    w = tf.get_variable('w', shape, initializer= tf.truncated_normal_initializer(stddev=stddev))

    b = tf.get_variable('b', [shape[-1]], initializer=tf.constant_initializer(value=0.0))

    conv = tf.nn.conv2d(input_, w, strides=[1,k_step,k_step,1], padding = 'SAME') + b

    return conv

'''
  input_: Tensor of input, format NHWC

  output: [batch, out_height, out_width, out_channel]

  k_step: The size of kernel is k_step*k_step

  d_step: we can define the generated image size, if you define the d_step=2,
          we can get the double size of generated image

'''

def deconv2d(input_, out_shape, k_step, d_step, name):

  with tf.variable_scope(name):

    msra_num = 1.0

    fan_in = k_step * k_step * int(input_.get_shape()[-1])

    stddev = msra_num * (math.sqrt(2. / float(fan_in) * float(d_step) * float(d_step)))

    w = tf.get_variable('w', [k_step, k_step, out_shape[-1], input_.get_shape()[-1]])

    deconv = tf.nn.conv2d_transpose(input_, w, output_shape=out_shape, strides=[1, d_step, d_step, 1])

  return deconv

'''

input_: Tensor of input, format NHWC

shape: [input_channel, out_channel]

'''

def fully_contact(input_, shape, name):

  with tf.variable_scope(name):

    msra_num = 1.0

    fan_in = int(input_.get_shape()[-1])

    stddev = msra_num * (math.sqrt(2./float(fan_in)))

    w = tf.get_variable('w', shape, initializer= tf.truncated_normal_initializer(stddev=stddev))

    b = tf.get_variable('b', shape[-1], initializer= tf.constant_initializer(value=0.0))

    fc = tf.matmul(input_, w) + b

    return fc

'''

define the function of leakrelu

'''

def leakyrelu(x, leak=0.2):

  k1 = (1 + leak)*0.5

  k2 = (1 - leak)*0.5

  return k1 * x + k2 * tf.abs(x)

'''

Restore pixel [-1, 1] to [0, 255]

'''

def rescale_image(image):

  convert_image = (image / 1.5 + 0.5) * 255

  return convert_image

'''

input: The tensor of input, format NHWC

size: recevie the number of images, such as size=8, we can get the 64 images, simultaneously

image_path: the path to store image

colorL: Ture is color image, Flase is gray image

iter: record continous storage images

'''

def save_image(input_, size, image_path, color, iter):

  h, w = input_.shape[1],input_.shape[2]

  convert_input = input_.reshape(batch_size, h, w)

  if color is True:

    image = np.zeros((h * size, w * size, 3))

  else:

    image = np.zeros((h * size, w * size))

  for index, img in enumerate(convert_input):

    i = index % size

    j = math.floor(index / size)

    if color is True:

      image[h*j:h*j+h, i*w:i*w+w,:] = img

    else:

      image[h*j:h*j+h, i*w:i*w+w] = img

  scipy.misc.toimage(rescale_image(image),cmin=0, cmax=255).save(image_path+'/tr_gt_%s.png' % (iter))



#------------------分割线------------------#

def AutoEncoder(input_):

  with tf.variable_scope("Auotencoder", reuse = tf.AUTO_REUSE) as scope0:


      conv1 = conv2d(input_, [5, 5, 1, 32], 2, "conv1")

      conv1 = leakyrelu(conv1)

      conv2 = conv2d(conv1, [5, 5, 32, 64], 2, "conv2")

      conv2 = leakyrelu(conv2)

      deconv1 = deconv2d(conv2, [batch_size, 14, 14, 32], 5, 2, "deconv1")

      deconv1 = leakyrelu(deconv1)

      deconv2 = deconv2d(deconv1, [batch_size, 28, 28, 1], 5, 2, "deconv2")

      output = tf.tanh(deconv2)

      return output

#------------------分割线------------------#

input_image = tf.placeholder(tf.float32, [None, 28, 28, 1])

#------------------分割线------------------#

generate_image = AutoEncoder(input_image)

#------------------分割线------------------#

Auto_loss = tf.reduce_mean(tf.reduce_sum(tf.pow(tf.subtract(generate_image, input_image), 2), 3))

#------------------分割线------------------#

train_var = tf.trainable_variables()

with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):

  train_loss = tf.train.AdamOptimizer(0.001, beta1=0.9).minimize(Auto_loss)

init = tf.global_variables_initializer()

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)

#------------------分割线------------------#

with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:

  sess.run(init)

  for i in range(1000):

    mnist_image= mnist.train.next_batch(batch_size)

    batch_image = mnist_image[0].reshape(batch_size, 28, 28, 1)

    batch_image = batch_image * 2 - 1

    sess.run(train_loss, feed_dict={input_image: batch_image})

    if i % 10 == 0:

      print(sess.run(Auto_loss, feed_dict={input_image: batch_image}))

      output_image = sess.run(generate_image, feed_dict={input_image: batch_image})

      save_image(output_image, 8, path, False, i)

 

 

 

TensorFlow实现自编码器.py 使用TensorFlow实现自编码器,这里实现的是最有代表性的去噪自编码器,因为去噪自编码器的适用范围最广泛也最通用。代码简单,并且有标注,通俗易懂,适合初学者参考。 立即下载

相关推荐

autoencoder自编码器tensorflow代码

tensorflow实现的自编码器,带有详细注释,使用MNIST作为数据集,安装好python及tensorflow即可运行

AutoEncoder以及TensorFlow-2.0实现代码

自编码器(Auto-encoder) Auto-encoder输入前馈神经网络的一种,它借助了稀疏编码的思想,目标是借助抽取到的高阶特征来重构输入,而不只是简单的复制。auto-encoder曾经主要用于降维和特征抽取,现在被扩展到了生成模型。 Auto-encoder的模型架构可以简单地表示为: 实现流程为: Auto-encoder的思想很简单,下面我们来看一下如何用代码实现,这里使用的是...

Forlogenの解忧杂货铺 2287

tensorflow_autoencoder_demo.py例子

官方例子,深度学习专用,机器学习专用,代码简单,一看就会(tensorflow autoencoder demo)

自编码器(AutoEncoder)入门及TensorFlow实现

自编码器(Autoencoder,AE),是一种利用反向传播算法使得输出值等于输入值的神经网络,它先将输入压缩成潜在空间表征,然后通过这种表征来重构输出。自编码器由两部分组成:编码器(encoder):这部分能将输入压缩成潜在空间表征,可以用编码函数h=f(x)表示。解码器(decoder):这部分重构来自潜在空间表征的输入,可以用解码函数r=g(h)表示。因此,整个自编码器可以用函数g(f(x)...

u010899985的博客 2万+

告别反复格式化!用Ventoy制作2025年终极启动盘,Windows/Linux/macOS一网打尽

本文详细介绍了如何使用Ventoy制作2025年终极多系统启动盘,彻底告别反复格式化U盘的烦恼。Ventoy通过创新的引导加载器架构,允许用户在一个U盘内同时存放并直接启动Windows、Linux、macOS等多种系统镜像和WePE等维护工具,实现真正的非破坏性管理和极高的U盘利用率。文章涵盖从安装配置、镜像库构建到安全启动设置等全流程实战指南。

weixin_29280219的博客 263

地下管线管网三维建模之管道与附属物的无缝衔接

地下管网数据,实现简单、快速建模,所建模型达到项目应用级别

luoyun620的博客 2631

Tensorflow学习之Autoencoder(一)

三篇文章掌握Autoencoder: 1. Tensorflow学习之Autoencoder(一) 2. Tensorflow学习之Autoencoder(二)图片降维并还原图片 3. Tensorflow学习之Autoencoder(三)图片降维的结果展示 微信公众号 1. 什么是自编码器(Autoencoder)? 自编码器是一种神经网络的形式。 人工神经网络是一种具有层次结构...

Microstrong 2552

STC32G电调调试血泪史:从TPH1R403NL MOS管烧毁到PWM信号异常排查全记录

本文详细记录了STC32G无刷电调调试过程中的常见问题与解决方案,包括MOS管烧毁、PWM信号异常等关键故障排查。通过电源系统检测、驱动电路验证及三相平衡调试等实战经验,帮助硬件爱好者快速定位问题,提升电调调试效率。特别针对TPH1R403NL等MOS管的保护措施进行了重点分析。

weixin_33736048的博客 560

Autoencoder的实现(Tensorflow 2.0版本,Pytorch版本)

Autoencoder 简介 机器学习中,处理大量数据会导致需要大量的计算;有时候,我们会需要选择对我们机器学习模型的学习贡献最大的重点数据部分,从而减少计算量;而选择数据重要部分的过程,这是自动编码器(Autoencoder )的应用案例之一。 神经网络是一种计算模型,用于查找描述数据特征x及其值(回归任务)或标签(分类任务)y之间的关系的函数,即y = f(x );自动编码器也是一种神经...

Messi-Q Blog 1063

倍福Twincat 3.0软件与C++通讯问题(ADS通讯)

倍福Twincat 3.0软件与C++通讯问题 倍福公司所开发的Twincat 3.0软件是基于Visual studio开发环境的一款自动化软件,目前其与C++程序通讯的方式主要还是靠ADS-DLL接口进行数据和信息传递,作者在Visual C++上进行混合编程开发,通过TwinCAT路由器和C语言API函数实现混合编程。 倍福软件提供: TcAdsDll.dll—动态链接库; TcAdsDll...

weixin_42811651的博客 1万+

【webrtc】openh264编码:动态配置码控参数触发IDR编码

SetRates 下发RateControlParameters 给到openh264 编码器 void SetRates(const RateControlParameters& parameters) override; LayerConfig 编码器这边的配置是layerconfig,具体到层的: std::vector<LayerConfig> configurations_; void H264EncoderImpl::SetRates(const RateCo.

突围 736

Tensorflow基于CNN的AutoEncoder

Tensorflow基于CNN的AutoEncoder 完整代码:https://github.com/SongDark/cnn_autoencoder_mnist 一、概述 AutoEncoder属于无监督学习,由一个编码器Encoder和一个解码器Decoder组成,是一个Encoder-Decoder结构,它学习的目标是还原输入,不需要提供标签。 二、数据源 使用Mnist作为训练数据,M...

songbinxu的博客 6937

想在 Visual Studio Code 里进行 ABAP 开发,需要安装的扩展列表

安装完毕后,我们可以将 SAP 系统配置为 Visual Studio Code 连接的远程文件系统。为了连接到 SAP 系统,需要启用 ABAP 开发工具 (ADT) ICF 节点,这与 SAP Hana Studio 等工具使用的服务相同。打开 SAP GUI 并连接到目标系统,运行事务 SICF,然后激活 ADT_SRV 服务,如下所示。这启用了允许代码编辑器和 IDE 连接到 SAP 系统的服务。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 3157

【tensorflow】加性高斯噪声AutoEncoder

来源于TensorFlow实战, 黄文坚, 唐源 python 4_2_AutoEncoder.py (tensorflow_1.1.0)) 输出 Extracting MNIST_data/train-images-idx3-ubyte.gz Extracting MNIST_data/train-labels-idx1-ubyte.gz Extracting MNIST_data/t10k-i...

W_LAILAI的博客 2758

基于Qt的工业组态软件开发:图形绘制与TCP通讯实现

在数据接收时,通过 readyRead 信号触发解析函数,校验数据包完整性后提取有效数据,并更新对应图形元素的状态,例如根据温度值改变矩形的填充色,实现设备状态可视化。测试阶段,我们将软件部署到Windows工控机,连接PLC设备进行通讯测试,结果表明图形元素响应及时,TCP数据传输稳定,满足工业现场的基本需求。综上所述,基于Qt的组态软件开发,核心在于利用 QGraphicsView 框架实现图形元素的灵活绘制与管理,结合 QTcpSocket 完成工业数据的可靠交互。

谢谢大家的关注和点赞!这里只有纯纯的知识干货,没有一句废话。希望能实实在在帮到大家~要是觉得有用,别忘了给我点支持哟,你 279

ParaView :在paraview创建渲染视图

ParaView :在paraview创建渲染视图

希望我的博客,能帮上你解决学习中工作中所遇到的问题 1351

libsdae-autoencoder-tensorflow 项目使用教程

libsdae-autoencoder-tensorflow 项目使用教程 1. 项目的目录结构及介绍 libsdae-autoencoder-tensorflow/ ├── data/ │ └── ... ├── deepautoencoder/ │ └── ... ├── .travis.yml ├── LICENSE ├── MANIFEST ├── README.md ├── li...

gitblog_00715的博客 526

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

如何用TensorFlow做异常检测?Autoencoder实现详解

利用Autoencoder在无监督场景下捕捉设备运行中的异常模式,结合TensorFlow实现从数据预处理、模型构建到生产部署的完整流程。通过重构误差识别偏离正常行为的异常,适用于工业物联网等缺乏标注数据的领域。

weixin_35753431的博客 1121

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

如何用TensorFlow构建AutoEncoder进行降维?

面对高维数据带来的存储与计算负担,自编码器提供了一种高效的无监督降维方案。借助TensorFlow,可快速搭建具备压缩与重构能力的神经网络,通过瓶颈层提取数据本质特征。相比PCA,其非线性表达能力更擅长捕捉复杂结构,适用于图像、传感器等场景。训练后所得低维特征可用于聚类、异常检测等任务,且模型易于部署到生产环境。

weixin_42309599的博客 533

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

【机器学习】基于AutoEncoder的BP神经网络的tensorflow实现

【机器学习】基于AutoEncoder的BP神经网络的tensorflow实现一、基于AutoEncoder的BP神经网络1.1、BP神经网络1.1.1、BP神经网络结构1.1.2、BP神经网络的训练1.2、AutoEncoder(自编码器)二、基于AutoEncoder的BP神经网络的tensorflow实现参考资料 一、基于AutoEncoder的BP神经网络 1.1、BP神经网络 1.1.1...

Luqiang_Shi的博客 1762

小白学Tensorflow之自编码Autoencoder

Autoencoder(自编码)是一种非监督学习算法,如下图,就是一个简单的自编码神经网络结构。 Autoencoder.jpg 为了得到图中的W0,我们需要增加Layer2层,来进行训练,输出层为y=x。也就是说,网络的输出和输入是同一个结果。其中,L...

chuange6363的博客 174

tensorflow1.1/autoencoder2

环境:tensorflow1.1 , python 3, matplotlib 2.02encoder编码器将编码得到的低维特征在空间中可视化出来,直观显示数据的聚类效果。实验过程中采用过激活函数 relu ,tanh,sigmoid,发现采用激活函数relu时聚类效果最好#coding:utf-8 """ tensorflow 1.1 python 3 matplotlib 2.02""" imp

szj_huhu的博客 1231

TensorFlow实例:Autoencoder与聚类结合在预测用户偏好中的应用

实例:Autoencoder与聚类结合在预测用户偏好中的应用 上篇博文我们用tensorflow实现了Autoencoder 用tensorflow构建一个两层的auto-encoder 本文具体数据集与源代码可从我的GitHub地址获取 https://github.com/liuzuoping/Deep_Learning_note 项目背景: 电商数据,包含30w条数据,一共150种商品品类...

liuzuoping的博客 797

Tensorflow Day19 Denoising Autoencoder

今日目標 了解 Denoising Autoencoder訓練 Denoising Autoencoder測試不同輸入情形下的 Denoising Autoencoder 表現 Github Ipython Notebook 好讀完整版 Introduction 什麼是 denoising 呢?意思就是把去除雜訊的意思,也就是說這裡的 autoencoder 有把輸入的雜訊

omnispace的博客 755

Tensorflow学习之Autoencoder(二)图片降维并还原图片

三篇文章掌握Autoencoder: 1. Tensorflow学习之Autoencoder(一) 2. Tensorflow学习之Autoencoder(二)图片降维并还原图片 3. Tensorflow学习之Autoencoder(三)图片降维的结果展示 微信公众号 实现功能: 用Tensorflow实现Autoencoder,通过对图片特征的压缩并解压,将结果与原始数据进行对...

Microstrong 4078
上一篇: Generative adversarial net with Mnist
下一篇: tensorflow mnist
SoyCoder
博客等级 码龄9年 12粉丝 16原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址