这里简单记录一下、
我学习VAE原理之后,对VAE进行一个简单的实现。
VAE主要包括三部分:
1.编码器 ----> 均值,方差(图像特征)
2.均值,方差(图像特征)—> 新的特征向量(符合一定分布)
3.新的特征向量 —> 解码器 —> 新的图像
这里直接贴上我关于VAE实现的代码:
import os
import warnings
warnings.filterwarnings("ignore")
import tensorflow as tf
import numpy as np
import pandas as pd
import glob
from tensorflow import keras
import matplotlib as mpl
import matplotlib.pyplot as plt
import time
%matplotlib inline
batch_size = 64
buffer_size = 60000
test_size = 10000
(train_image,_),(test_image,_) = tf.keras.datasets.mnist.load_data()
train_image = train_image.reshape(train_image.shape[0],28,28,1).astype('float32') # 将二维 图像 转换为 三维 图像
test_image = test_image.reshape(test_image.shape[0], 28, 28, 1).astype('float32')
train_image = train_image/255
test_image = test_image/255
train_datasets = tf.data.Dataset.from_tensor_slices(train_image).shuffle(buffer_size).batch(batch_size)
test_datasets = tf.data.Dataset.from_tensor_slices(test_image).shuffle(test_size).batch(batch_size)
epochs = 10
latent_dim = 64
num = 16
seed = tf.random.normal([num,latent_dim]) # 随机生成 16 个编码向量
def Encoder():
model = tf.keras.Sequential()
model