gpt4 book ai didi

python - Keras my_layer.output 返回 KerasTensor 对象而不是 Tensor 对象(在自定义损失函数中)

转载 作者:行者123 更新时间:2023-12-05 04:55:14 24 4
gpt4 key购买 nike

我正在尝试在 Keras v2.4.3 中构建自定义损失函数:(如本 answer 中所述)

def vae_loss(x: tf.Tensor, x_decoded_mean: tf.Tensor,
original_dim=original_dim):
z_mean = encoder.get_layer('mean').output
z_log_var = encoder.get_layer('log-var').output

xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean)
kl_loss = - 0.5 * K.sum(
1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
vae_loss = K.mean(xent_loss + kl_loss)
return vae_loss

但我认为它的行为与预期有很大不同(可能是因为我的 Keras 版本?),我收到此错误:

TypeError: Cannot convert a symbolic Keras input/output to a numpy array. This error may indicate that you're trying to pass a symbolic value to a NumPy call, which is not supported. Or, you may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model.

我认为这是因为 encoder.get_layer('mean').output 返回的是 KerasTensor 对象而不是 tf.Tensor对象(如其他答案所示)。

我在这里做错了什么?如何从自定义损失函数内部访问给定层的输出?

最佳答案

我认为使用 model.add_loss() 非常简单。此功能使您能够将多个输入传递给您的自定义损失。

为了提供一个可靠的示例,我生成了一个简单的 VAE,其中我使用 model.add_loss()

添加了 VAE 损失

完整的模型结构如下:

def sampling(args):

z_mean, z_log_sigma = args
batch_size = tf.shape(z_mean)[0]
epsilon = K.random_normal(shape=(batch_size, latent_dim), mean=0., stddev=1.)

return z_mean + K.exp(0.5 * z_log_sigma) * epsilon

def vae_loss(x, x_decoded_mean, z_log_var, z_mean):

xent_loss = original_dim * K.binary_crossentropy(x, x_decoded_mean)
kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var))
vae_loss = K.mean(xent_loss + kl_loss)

return vae_loss

def get_model():

### encoder ###

inp = Input(shape=(n_features,))
enc = Dense(64)(inp)

z = Dense(32, activation="relu")(enc)
z_mean = Dense(latent_dim)(z)
z_log_var = Dense(latent_dim)(z)

encoder = Model(inp, [z_mean, z_log_var])

### decoder ###

inp_z = Input(shape=(latent_dim,))
dec = Dense(64)(inp_z)

out = Dense(n_features)(dec)

decoder = Model(inp_z, out)

### encoder + decoder ###

z_mean, z_log_sigma = encoder(inp)
z = Lambda(sampling)([z_mean, z_log_var])
pred = decoder(z)

vae = Model(inp, pred)
vae.add_loss(vae_loss(inp, pred, z_log_var, z_mean)) # <======= add_loss
vae.compile(loss=None, optimizer='adam')

return vae, encoder, decoder

运行的笔记本在这里可用:https://colab.research.google.com/drive/18day9KMEbH8FeYNJlCum0xMLOtf1bXn8?usp=sharing

关于python - Keras my_layer.output 返回 KerasTensor 对象而不是 Tensor 对象(在自定义损失函数中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65491452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com