【问题标题】:computing the norm of the output layer in Tensorflow在 TensorFlow 中计算输出层的范数
【发布时间】:2019-09-02 23:25:48
【问题描述】:

如何在变分自编码器网络中获取输出层的值并计算其范数?假设我在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 [?,]

你有什么想法吗?

【问题讨论】:

    标签: tensorflow keras keras-layer


    【解决方案1】:

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

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

    关于这个错误:

    InvalidArgumentError:您必须为占位符张量提供值 'z_sampling' 具有 dtype 浮点数和形状 [?,]

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

    【讨论】:

    • 感谢您的回答。你说得对,我应该使用 numpy 函数。
    • 你知道我怎样才能为每个时代做到这一点吗?
    • 你可以写一个回调并实现on_epoch_end()。 keras.io/callbacks/#lambdacallback
    猜你喜欢
    • 1970-01-01
    • 2018-06-25
    • 2017-08-14
    • 2019-05-03
    • 2020-03-27
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多