Search code examples
tensorflowvisualizationtensorboard

How to show distributions of multiple features in one tensor in TensorBoard


I have a tensor X which is an output of the batch normalization layer (tf.layers.batch_normalization) and has the shape of [batch_size, 15]. To monitor its distribution, I created a histogram for X with tf.summary.histogram('out_BN_0', X). The graph is what I got in Tensorboard after > 70k steps (~ 130 epoches). Is it the average results of all 15 features? or any particular feature in X? How do I get the distribution of just one feature (e.g. 5th)?

enter image description here


Solution

  • How about building a histogram per each feature?

    import tensorflow as tf
    import numpy as np
    
    batch_size = 100
    num_features = 15
    
    X = tf.constant(np.random.uniform(size=(batch_size, num_features)))
    hists = {feature_index: tf.summary.histogram(f'hist_{feature_index}', X[:, feature_index]) 
                  for feature_index in range(num_features)}