计算 Tensorflow 中输出层的范数

computing the norm of the output layer in Tensorflow

如何在变分自动编码器网络中获取输出层的值并计算其范数?假设我在 Tensorflow 中有以下网络:

    inputs = Input(shape=(dim,))
    x = Dense(intermediate_dim, activation='relu')(inputs)
    z_mean = Dense(latent_dim, name='z_mean')(x)
    z_log_var = Dense(latent_dim, name='z_log_var')(x)

    z = Lambda(sampling, output_shape=(latent_dim,), name='Z')([z_mean, z_log_var])
    encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')

    latent_inputs = Input(shape=(latent_dim,), name='z_sampling')
    x = Dense(intermediate_dim, activation='relu',name='Hidden_Layer')(latent_inputs)
    outputs = Dense(dim, activation='sigmoid')(x)

    decoder = Model(latent_inputs, outputs, name='decoder')
    outputs = decoder(encoder(inputs)[2])

    vae = Model(inputs, outputs, name='vae_mlp')

我想在 运行 模型之后找到输出层的 L_1 范数,即 tf.norm(outputs,ord=1)。可用的 Tensorflow 函数帮不了我。例如使用此命令后 K.eval(tf.norm(decoder.layers[2].output,ord=2)) 我收到此错误:

InvalidArgumentError: You must feed value for placeholder tensor 'z_sampling' with dtype float and shape [?,]

你有什么想法吗?

网络的输出是一个numpy数组。所以,你可以使用 numpy 函数。

import numpy as np
output = vae.predict(input)
l1_norm = np.linalg.norm(output, 1)

关于这个错误:

InvalidArgumentError: You must feed value for placeholder tensor 'z_sampling' with dtype float and shape [?,]

tf.norm(decoder.layers[2].output,ord=2) 的评估,需要将 latent_inputs 馈送到名称为 z_sampling 的解码器。