You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已知Keras有count_params()统计参数,是否有函数统计ANN总单元数?

Counting Total Units (Neurons) in a Keras Model

Great question! Unfortunately, Keras doesn’t come with a built-in function that directly mirrors model.count_params() for counting total units/neurons across all layers of your ANN. But calculating this yourself is straightforward with a quick custom function.

Here's a simple implementation:

def count_total_units(model):
    total_units = 0
    for layer in model.layers:
        # Add units from Dense layers
        if hasattr(layer, 'units'):
            total_units += layer.units
        # Add filters (output channels) from convolutional layers
        elif hasattr(layer, 'filters'):
            total_units += layer.filters
        # Add units from recurrent layers like LSTM/GRU
        elif hasattr(layer, 'cell') and hasattr(layer.cell, 'units'):
            total_units += layer.cell.units
    return total_units

How to use it:

Just pass your trained or compiled Keras model into the function:

total_units = count_total_units(your_model)
print(f"Total units in the model: {total_units}")

Notes:

  • The definition of "units" can vary by layer type:
    • For Dense layers, units directly refers to the number of neurons.
    • For convolutional layers (like Conv2D), filters represents the number of output feature maps, which is often considered the "unit count" for those layers.
    • For recurrent layers (LSTM/GRU), the unit count is stored in the layer's cell attribute.
  • You can tweak the function to include or exclude specific layer types based on your exact needs. For example, if you only care about fully connected neurons, you can remove the convolutional and recurrent layer checks.

内容的提问来源于stack exchange,提问作者Xavier M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:40:01