【周末AI课堂】理解变分自编码器 | 机器学习你会遇到的“坑”( 十 )


    ent_loss = binary_crossentropy(xy)
    kl_loss =-0.5* K.mean(1+ z_sigma*z_sigma - K.square(z_mean) \\
                             - K.log(z_sigma*z_sigma) axis=-1)
returnent_loss + kl_loss

接下来 , 我们可以很方便地写出我们的模型:

from keras import modelslayers

from keras.layers import InputDenseLambda
from keras.models import Model

defVAE(shapeinter_dimdim):
  x = Input(shape=shape)
  hidden = Dense(inter_dim activation='relu')(x)
  z_mean = Dense(dim)(hidden)
  z_sigma = Dense(dim)(hidden)
    z = Lambda(sampling output_shape=(dim))([z_meanz_sigma
)
    decoder_hidden = Dense(inter_dimactivation='relu')(z)

推荐阅读