變分自編碼VAE實戰

2021-09-06 13:23:04 字數 3185 閱讀 5342

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('mnist_data',one_hot=true)

import matplotlib.pyplot as plt

import numpy as np

class net:

def __init__(self):

self.x = tf.placeholder(dtype=tf.float32,shape=[none,784])#為要仿照的樣本預留位置

#構造乙個編碼器和乙個解碼器

self.encode = encoder()

self.decode = decoder()

"""在前向計算中,需要獲得樣本的真實分布的均值和方差

然後生成一些隨機數來使得其分布盡量接近樣本的分布

"""def forward(self):

#通過編碼器去獲得樣本分佈的均值和對數似然

self.mean,self.logvar = self.encode.forward(self.x)

#獲得方差和標準差

self.var = tf.exp(self.logvar)

self.std = tf.sqrt(self.var)

#生成標準正態分佈的隨機數

normal_y = tf.random_normal(shape=[128])#128是乙個超參,因為在編碼獲得均值和方差的時候返回的是128

y = self.mean+self.std*normal_y#在原樣本均值的基礎上新增一些雜訊,而這個雜訊就是隨機數乘上樣本的標準差

#將得到的數進行解碼得到輸出

self.output = self.decode.forward(y)

"""得到兩個損失,分別是輸出損失和kl損失

然後再將兩個損失相加得到總的損失,將總的損失拿過去優化

"""def backward(self):

out_loss = tf.reduce_mean((self.output-self.x)**2)

kl_loss = tf.reduce_mean(0.5*(-self.logvar+self.mean**2+self.var-1))#(-log(var)+u^2+var-1)/2

self.loss = out_loss+kl_loss

self.opt = tf.train.adamoptimizer().minimize(self.loss)

"""用於生成隨機數帶進生成網路進行測試看看效果好壞"""

def decode(self):

normal_x = tf.random_normal(shape=[1,128])#[1,128]而不是[none,128]是為了生成一張用於檢驗網路訓練的效果

return self.decode.forward(normal_x)

class encoder:

def __init__(self):

self.w = tf.variable(tf.truncated_normal(dtype=tf.float32,shape=[784,100],stddev=0.1))

self.b = tf.variable(tf.zeros([100]))

self.logvar_w = tf.variable(tf.random_normal(dtype=tf.float32,shape=[100,128],stddev=0.1))

self.mean_w = tf.variable(tf.random_normal(dtype=tf.float32,shape=[100,128],stddev=0.1))

def forward(self,x):

#傳進來的x是真實的樣本,需要獲得它的均值和方差的對數,而形狀是[none,784]

y = tf.nn.relu(tf.matmul(x,self.w)+self.b)#[none,100]

mean = tf.matmul(y,self.mean_w)#[none,128]

logvar = tf.matmul(y,self.logvar_w)

return mean,logvar

class decoder:

#任務是將[none,128]的資料換回到[none,784]

def __init__(self):

self.w = tf.variable(tf.random_normal(dtype=tf.float32,shape=[128,100],stddev=0.1))

self.b = tf.variable(tf.zeros([100]))

self.out_w = tf.variable(tf.random_normal(dtype=tf.float32,shape=[100,784],stddev=0.1))

def forward(self,x):

y = tf.nn.relu(tf.matmul(x,self.w)+self.b)

return tf.matmul(y,self.out_w)

if __name__ == '__main__':

net = net()

net.forward()

net.backward()

init = tf.global_variables_initializer()

test = net.decode()

with tf.session() as sess:

sess.run(init)

plt.ion()

for i in range(10000):

x,_ = mnist.train.next_batch(100)

loss,_,out = sess.run([net.loss,net.opt,net.output],feed_dict=)

#每訓練100次就做一下測試看一下效果

if i%100 == 0:

print(loss)

test_img = sess.run([test])

test_img = np.reshape(test_img,[28,28])

plt.imshow(test_img)

plt.pause(0.1)

變分自編碼器VAE

auto encoding variational bayes git antixk pytorch vae a collection of variational autoencoders vae in pytorch.1 原文作者在深度學習上的實戰理論指導 2 具體原理框圖如下 vae主要由編碼...

變分自編碼VAE模型理解

從暑假看到現在終於在 這篇文章 的幫助下搞懂了,期間看了無數的中文關於vae的部落格文章,乙個個基本上不是少講了這個就是少講了那個,最後總是搞不懂,還走了不少彎路,去研究變分推斷本身,其實只是為了看懂vae的原理,是不必深度了解變分推斷的。話不多說,說正題,這裡我只會講下大概,具體的請看原始文章。自...

AI數學 變分自編碼器 VAE

auto encoding variational bayes 你要是能在一周內,把上面這篇文章的數學原理搞懂,那你就是骨骼清奇了。看 可以知道,vae 變分自編碼器 只是aevb 自編碼變分貝葉斯 的乙個應用而已。如果你只是想懂vae的話,還是比較簡單滴。對於aevb更深層的原理,在這裡不去討論,...