Search code examples
tensorflowkeraskeras-layer

computing the norm of the output layer in Tensorflow


How I can get the values of the output layer in a variational autoencoder network and calculating its norm? Assume I have the following networks in 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')

and I want to find the L_1 norm of the output layer after running the model, i.e. tf.norm(outputs,ord=1). The available Tensorflow functions could not help me. For example after using this command K.eval(tf.norm(decoder.layers[2].output,ord=2)) I got this error:

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

Do you have any idea?


Solution

  • The output of the network is a numpy array. So, you can use numpy functions.

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

    Regarding this error:

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

    Evaluation of tf.norm(decoder.layers[2].output,ord=2), requires the latent_inputs to be fed to the decoder which has the name z_sampling.