首页 文章

如何查找keras模型的参数数量?

提问于
浏览
30

对于前馈网络(FFN),可以轻松计算参数数量 . 鉴于CNN,LSTM等有一种快速查找keras模型中参数数量的方法吗?

3 回答

  • 59

    模型和图层具有用于此目的的特殊方法:

    model.count_params()
    

    此外,要获得每个图层尺寸和参数的简短摘要,您可能会发现以下方法很有用

    model.summary()
    
  • 0
    import keras.backend as K
    
    def size(model): # Compute number of params in a model (the actual number of floats)
        return sum([np.prod(K.get_value(w).shape) for w in model.trainable_weights])
    
  • 12

    追溯 print_summary() 函数,Keras开发人员计算给定 model 的可训练和不可训练参数的数量,如下所示:

    import keras.backend as K
    import numpy as np
    
    trainable_count = int(np.sum([K.count_params(p) for p in set(model.trainable_weights)]))
    
    non_trainable_count = int(np.sum([K.count_params(p) for p in set(model.non_trainable_weights)]))
    

    鉴于 K.count_params() 被定义为 np.prod(int_shape(x)) ,此解决方案非常类似于Anuj Gupta,除了使用 set() 以及检索张量形状的方式 .

相关问题