How can I visualize the weights(variables) in cnn in Tensorflow?

前端 未结 3 687
南旧
南旧 2020-11-29 19:13

After training the cnn model, I want to visualize the weight or print out the weights, what can I do? I cannot even print out the variables after training. Thank you!

3条回答
  •  野性不改
    2020-11-29 20:08

    Like @mrry said, you can use tf.image_summary. For example, for cifar10_train.py, you can put this code somewhere under def train(). Note how you access a var under scope 'conv1'

    # Visualize conv1 features
    with tf.variable_scope('conv1') as scope_conv:
      weights = tf.get_variable('weights')
    
      # scale weights to [0 255] and convert to uint8 (maybe change scaling?)
      x_min = tf.reduce_min(weights)
      x_max = tf.reduce_max(weights)
      weights_0_to_1 = (weights - x_min) / (x_max - x_min)
      weights_0_to_255_uint8 = tf.image.convert_image_dtype (weights_0_to_1, dtype=tf.uint8)
    
      # to tf.image_summary format [batch_size, height, width, channels]
      weights_transposed = tf.transpose (weights_0_to_255_uint8, [3, 0, 1, 2])
    
      # this will display random 3 filters from the 64 in conv1
      tf.image_summary('conv1/filters', weights_transposed, max_images=3)
    

    If you want to visualize all your conv1 filters in one nice grid, you would have to organize them into a grid yourself. I did that today, so now I'd like to share a gist for visualizing conv1 as a grid

提交回复
热议问题