In TensorFlow, is there any function to something I can do to find out the amount of learning parameters in my network?
2 Answers
No function I am aware of, but you can still count yourself using a for loop on the tf.trainable_variables():
total_parameters = 0
for variable in tf.trainable_variables():
variable_parameters = 1
for dim in variable.get_shape():
variable_parameters *= dim.value
total_parameters += variable_parameters
print("Total number of trainable parameters: %d" % total_parameters)
2 Comments
whoisraibolt
Got it! Thank you! And what would be the best place to put that piece of code? After initializing the TensorFlow session?
Pop
You can put that before session init. It only reads the graph variables and does not need a tf session
You can do this with a simple one-liner:
np.sum([np.prod(v.get_shape().as_list()) for v in tf.trainable_variables()])
If you need a little bit more details, here is a helper function I use to see all the trainable parameters:
def show_params():
total = 0
for v in tf.trainable_variables():
dims = v.get_shape().as_list()
num = int(np.prod(dims))
total += num
print(' %s \t\t Num: %d \t\t Shape %s ' % (v.name, num, dims))
print('\nTotal number of params: %d' % total)
It prints you information like this:
params/weights/W1:0 Num: 34992 Shape [3, 3, 18, 216]
params/weights/W2:0 Num: 839808 Shape [3, 3, 216, 432]
params/weights/W3:0 Num: 839808 Shape [3, 3, 432, 216]
params/weights/W4:0 Num: 57856 Shape [226, 256]
params/weights/W5:0 Num: 32768 Shape [256, 128]
params/weights/W6:0 Num: 8192 Shape [128, 64]
params/weights/W7:0 Num: 64 Shape [64, 1]
params/biases/b1:0 Num: 216 Shape [216]
params/biases/b2:0 Num: 432 Shape [432]
params/biases/b3:0 Num: 216 Shape [216]
params/biases/b4:0 Num: 256 Shape [256]
params/biases/b5:0 Num: 128 Shape [128]
params/biases/b6:0 Num: 64 Shape [64]
params/biases/b7:0 Num: 1 Shape [1]
Total number of params: 1814801