变分VAE 编码器网络《一 编码过程:代码实现》

变分自编码器原理:
1将输入图像压缩成编码(采用形式为平均值和方差的分布)
2随机上采样 ,针对编码器生成的编码随机采样
3对于随机采样的元素,进行解码转换,(按照输入图片的样式),解码生成类输入图像
变分VAE 编码器网络《一 编码过程:代码实现》_第1张图片
1编码过程


import keras
from keras import layers
from keras import backend as K
from keras.models import Model
import numpy as np
img_shape = (28, 28, 1)
batch_size = 16
latent_dim = 2
input_img = keras.Input(shape=img_shape)
x = layers.Conv2D(32, 3,
padding='same', activation='relu')(input_img)
x = layers.Conv2D(64, 3,
padding='same', activation='relu',
strides=(2, 2))(x)
x = layers.Conv2D(64, 3,
padding='same', activation='relu')(x)
x = layers.Conv2D(64, 3,
padding='same', activation='relu')(x)
shape_before_flattening = K.int_shape(x)
x = layers.Flatten()(x)
x = layers.Dense(32, activation='relu')(x)
z_mean = layers.Dense(latent_dim)(x)
z_log_var = layers.Dense(latent_dim)(x)

你可能感兴趣的:(人工智能技术,计算加速)