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_autoencoder_demo.py例子 官方例子,深度学习专用,机器学习专用,代码简单,一看就会(tensorflow autoencoder demo) 立即下载

相关推荐

autoencoder自编码器tensorflow代码

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

AutoEncoder以及TensorFlow-2.0实现代码

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

Forlogenの解忧杂货铺 2286

TensorFlow实现自编码器.py

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

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

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

u010899985的博客 2万+

LDPC码译码原理——概率公式推导

怎么说呢,一直以来都想写一下博客。近半年多来我一直在学习LDPC码译码上的东西,主要是在FPGA实现上的,所以这次想写一些关于这方面的内容,包括译码的原理、仿真以及实现。(当然是基于准循环LDPC码上实现的)不过呢,我写这个的初衷是为了让初学者理解入门用的,而不是在大牛面前班门弄斧,有错误之处欢迎大家批评指正。不知道会不会有人一开始像我一样,为了学好LDPC码,先去好好了解其它的诸如RS码或者是卷...

stand_by_me123的博客 2万+

Docker容器化部署Ceph集群:从零搭建高可用分布式存储

本文详细介绍了如何使用Docker容器化部署Ceph集群,从零搭建高可用分布式存储系统。通过环境一致性、快速扩容和资源隔离等优势,Docker显著提升了Ceph集群的部署效率和运维便捷性。文章涵盖硬件规划、镜像选择、单节点部署、多节点扩展及性能调优等实战内容,帮助开发者快速掌握分布式存储系统的容器化部署技巧。

weixin_29081101的博客 425

Tensorflow学习之Autoencoder(一)

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

Microstrong 2550

Python 上位机开发

首先,什么是上位机?上位机通常是指可以与下位机(如单片机、传感器等硬件设备)进行通信和交互,实现数据采集、控制指令发送以及数据处理和展示的计算机程序。 Python 作为一种高级编程语言,在开发上位机方面具有诸多优势。它拥有丰富的第三方库,使得开发过程变得相对简单和高效。

weixin_46807151的博客 4303

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

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

Messi-Q Blog 1063

VS2015与QT界面开发中解决‘ui_xxx.h‘文件类名不一致导致的编译错误

本文详细解析了在VS2015与QT界面开发中,因.ui文件顶层部件objectName与生成类名不一致导致'ui_xxx.h'文件出现'不完整的类型'编译错误的根本原因。通过分步排查,指导开发者统一Qt设计师对象名、自动生成文件类名及代码引用名,并提供强制重新编译UI文件、清理中间文件等解决方案,有效解决此类常见编译问题。

weixin_29009339的博客 248

BEVFusion实战:从零搭建LiDAR-Camera融合的3D目标检测环境(附避坑指南)

本文提供了BEVFusion实战指南,详细讲解如何从零搭建LiDAR-Camera融合的3D目标检测环境。内容涵盖环境配置、数据预处理、模型训练与性能优化等关键步骤,并附有实用的避坑技巧,帮助开发者高效部署这一先进的BEV感知算法。

p4q5r6s7t的博客 515

Tensorflow基于CNN的AutoEncoder

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

songbinxu的博客 6936

光通量与辐射通量之间的换算

由理论和实验可知存在两种定义: 1W1W1W(瓦特)的频率为540×1012Hz540\times10^{12}Hz540×1012Hz(赫兹)的单色辐射的辐射通量等于683lm(流明)683lm(流明)683lm(流明).。 1lm1lm1lm的频率为540×1012Hz540\times10^{12}Hz540×1012Hz的单色通量等于1683=0.001464W\frac{1}{683}=0.001464W6831​=0.001464W。 从上述定义中我们可以得到什么呢?我们知道光速: c=3×

u011699626的博客 3273

【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的博客 2757

LPDDR4 JEDEC标准测试实例解析--写操作

LPDDR4的JEDEC标准一致性测试项详解以及实例分享,满满干货!感兴趣的小伙伴可以持续关注!!

一只豌豆象的博客 7776

手把手教你玩转RISC-V指令集​​​专栏--​​1.2 对比x86/ARM:RISC-V的三大核心优势(开放、灵活、可扩展)​

​:RISC-V以“开放打破垄断、灵活适配场景、扩展定义未来”的三大优势,正在重塑计算架构格局。其核心价值不仅是技术革新,更是对全球半导体产业链话语权的重新分配。

amy_mhd的博客 130

libsdae-autoencoder-tensorflow 项目使用教程

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

gitblog_00715的博客 526

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

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

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

weixin_35753431的博客 1120

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

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

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

weixin_42309599的博客 533

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

【机器学习】基于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的博客 1230

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的博客 754

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

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

Microstrong 4069
上一篇: 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、付费专栏及课程。

余额充值