在TensorFlow中,我能做些什么来找出网络中学习参数的数量?
解决方法
你可以用一个简单的单行代码来做到这一点:
np.sum([np.prod(v.get_shape().as_list()) for v in tf.trainable_variables()])
如果你需要更多细节,这里有一个帮助函数我用来查看所有可训练的参数:
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)
它会打印出如下信息:
params/weights/W1:0 Num: 34992 Shape [3,3,18,216] params/weights/W2:0 Num: 839808 Shape [3,216,432] params/weights/W3:0 Num: 839808 Shape [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