【发布时间】:2020-07-06 23:43:14
【问题描述】:
我正在尝试实现自定义变分自动编码器。代码如下所示
image = Input(shape = (X_train.shape[1]))
label = Input(shape = (Y_train.shape[1]))
inputs = Concatenate()([image, label])
x = Dense(625, activation = 'relu')(inputs)
x = Reshape((25,25,1))(x)
x = LocallyConnected2D(8, (5,5), padding = 'valid')(x)
x = LeakyReLU()(x)
x = LocallyConnected2D(8, (5,5), padding = 'valid')(x)
x = LeakyReLU()(x)
x = LocallyConnected2D(8, (3,3), padding = 'valid')(x)
x = LeakyReLU()(x)
x = LocallyConnected2D(8, (3,3), padding = 'valid')(x)
x = LeakyReLU()(x)
x = AveragePooling2D((2, 2))(x)
encoder_out = Flatten()(x)
mu = Dense(latent_size, activation ='linear')(encoder_out)
sigma = Dense(latent_size, activation = 'linear')(encoder_out)
def sampling(args):
mu, sigma = args
eps = K.random_normal(shape=(batch_size, latent_size), mean=0., stddev=1.)
return mu + K.exp(sigma / 2) * eps
latent_space = Lambda(sampling, output_shape = (latent_size, ))([mu, sigma])
decoder_latent = Input(shape = (latent_size, ))
decoder_c = Input(shape = (c_space, ))
x = Concatenate()([decoder_latent, decoder_c])
x = Dense(288)(x)
x = Reshape((6,6,8))(x)
x = ZeroPadding2D((2,2))(x)
x = LocallyConnected2D(8, (3,3), padding = 'valid')(x)
x = LeakyReLU()(x)
x = ZeroPadding2D((2,2))(x)
x = LocallyConnected2D(8, (3,3), padding = 'valid')(x)
x = LeakyReLU()(x)
x = UpSampling2D(size = (2,2))(x)
x = LocallyConnected2D(8, (5,5), padding = 'valid')(x)
x = LeakyReLU()(x)
x = UpSampling2D(size = (2,2))(x)
x = LocallyConnected2D(8,(5,5), padding = 'valid')(x)
x = LeakyReLU()(x)
x = LocallyConnected2D(1,(4,4), padding = 'valid')(x)
decoder_out = Activation('relu')(x)
我定义为的损失函数
def DFC_loss(x_in, x_out):
kl_loss = 0.5 * K.sum(K.exp(sigma) + K.square(mu) - 1. - sigma, axis=1)
return K.mean(perceptual_loss(x_in, x_out) + kl_loss)
def perceptual_loss(x_in, x_out):
x_in = K.reshape(x_in, shape=(batch_size, 25,25,1))
x_out = K.reshape(x_out, shape=(batch_size, 25,25,1))
conv_outputs = [classifier.get_layer(l).output for l in selected_layers]
activation = Model(classifier.input, conv_outputs)
h1_list = activation(x_in)
h2_list = activation(x_out)
rc_loss = 0.0
for h1, h2, weight in zip(h1_list, h2_list, [1.0, 1.0]):
h1 = K.batch_flatten(h1)
h2 = K.batch_flatten(h2)
rc_loss = rc_loss + weight * K.sum(K.square(h1 - h2), axis=-1)
return rc_loss
CVAE.compile(optimizer = "adam", loss = DFC_loss, metrics = [perceptual_loss])
每当我运行下面的代码时
CVAE_hist = CVAE.fit([X_train,Y_train], X_train, verbose = 1, batch_size=batch_size, epochs=n_epochs, validation_data = ([X_test, Y_test], X_test))
我得到两个错误
An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
@tf.function
def has_init_scope():
my_constant = tf.constant(1.)
with tf.init_scope():
added = my_constant * 2
The graph tensor has name: dense_2_1/Identity:0
和
Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Tensor 'dense_2_1/Identity:0' shape=(None, 6) dtype=float32>, <tf.Tensor 'dense_1_1/Identity:0' shape=(None, 6) dtype=float32>]
有趣的是,每当我将损失函数设置为仅没有 Kl 散度损失的感知损失时,我的代码都没有收到错误。变分自动编码器的 KL 散度损失有很多实现,但我不知道为什么它不适用于这个特定的实现。
【问题讨论】:
标签: tensorflow keras