tensorflow统计网络参数量

通过tf.trainable_variables来统计整个网络的参数量

本文列举摘抄了七种方法,但是大同小异,得出的结果也都相同
def count1():
    total_parameters = 0
    for variable in tf.trainable_variables():
        # shape is an array of tf.Dimension
        shape = variable.get_shape()
        # print(shape)
        # print(len(shape))
        variable_parameters = 1
        for dim in shape:
            # print(dim)
            variable_parameters *= dim.value
        # print(variable_parameters)
        total_parameters += variable_parameters
    print(total_parameters)
def count2():
    print np.sum([np.prod(v.get_shape().as_list()) for v in tf.trainable_variables()])
def get_nb_params_shape(shape):
    '''
    Computes the total number of params for a given shap.
    Works for any number of shapes etc [D,F] or [W,H,C] computes D*F and W*H*C.
    '''
    nb_params = 1
    for dim in shape:
        nb_params = nb_params*int(dim)
    return nb_params

def count3():
    tot_nb_params = 0
    for trainable_variable in tf.trainable_variables():
        shape = trainable_variable.get_shape()  # e.g [D,F] or [W,H,C]
        current_nb_params = get_nb_params_shape(shape)
        tot_nb_params = tot_nb_params + current_nb_params
    print tot_nb_params
def count4():
    size = lambda v: reduce(lambda x, y: x * y, v.get_shape().as_list())
    n = sum(size(v) for v in tf.trainable_variables())
    # print "Model size: %dK" % (n / 1000,)
    print n
def count5():
    total_parameters = 0
    # iterating over all variables
    for variable in tf.trainable_variables():
        local_parameters = 1
        shape = variable.get_shape()  # getting shape of a variable
        for i in shape:
            local_parameters *= i.value  # mutiplying dimension values
        total_parameters += local_parameters
    print(total_parameters)
def count6():
    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)
def count7():
    from functools import reduce
    from operator import mul
    num_params = 0
    for variable in tf.trainable_variables():
        shape = variable.get_shape()
        num_params += reduce(mul, [dim.value for dim in shape], 1)
    print num_params

1.How to count total number of trainable parameters in a tensorflow model?

2.What is the best way to count the total number of parameters in a model in TensorFlow?
3.Number of CNN learnable parameters - Python / TensorFlow

4.tensorflow 获取模型所有参数总和数量

### 如何计算和输出神经网络模型参数量 #### 使用 PyTorch 计算模型参数量 在 PyTorch 中,可以通过遍历模型的参数统计总的可训练参数数量。下面是一个简单的函数用于计算并打印模型中的总参数数: ```python import torch.nn as nn def count_parameters(model): total_params = sum(p.numel() for p in model.parameters()) trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad) print(f'Total parameters: {total_params}') print(f'Trainable parameters: {trainable_params}') # 假设有一个定义好的模型 `model` class SimpleModel(nn.Module): def __init__(self): super(SimpleModel, self).__init__() self.fc1 = nn.Linear(784, 256) self.fc2 = nn.Linear(256, 10) def forward(self, x): x = self.fc1(x) x = self.fc2(x) return x model = SimpleModel() count_parameters(model) ``` 此方法能够区分总数与仅限于可以更新权重的部分(即`requires_grad=True`),这对于理解哪些层参与实际的学习过程非常有用[^1]。 #### 使用 TensorFlow/Keras 计算模型参数量 对于基于 Keras API 的 TensorFlow 模型来说,内置了一个方便的方法可以直接获取整个模型的信息摘要,其中包括每一层以及它们各自的参数数目: ```python from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense # 创建一个简单序列模型实例 model = Sequential([ Dense(256, input_shape=(784,), activation='relu'), Dense(10, activation='softmax') ]) # 打印模型结构及其参数详情 model.summary() # 如果想要单独获得总的参数数量也可以这样做: print('Total params:', model.count_params()) ``` 这种方法不仅提供了每层的具体配置细节,还给出了整体参数的数量概览,在调试和优化过程中十分便利[^2]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值