首页 > 解决方案 > 当使用 Pytorch 的偏差完好无损时,如何仅操纵神经网络的权重?

问题描述

在下面的for循环中,我得到了我的神经网络的name's 和parameter's。根据偏差标志,我将对在 'if' 和 'elif' 之后使用的weight's 和' 做一些操作。但是,when is ,我只是对's 进行操作,但如果flag is ,我将对's 和's 都进行操作。biasblockbiasFalseweightbiasTrueweightbias

for name, param in model.named_parameters():
    if bias = Fasle and 'weight' in name:
        weight_np = param.data.cpu().numpy()
        alive = weight_np[np.nonzero(weight_np)] # extract nonzero values
        percentile_value = np.percentile(abs(alive), percent)

        # Convert Tensors to numpy and calculate
        weight_dev = param.device
        new_mask = np.where(abs(weight_np) < percentile_value, 0, mask[step])
    elif bias = True and 'weight' or 'bias' in name:
        weight_np = param.data.cpu().numpy()
        alive = weight_np[np.nonzero(weight_np)] # extract nonzero values
        percentile_value = np.percentile(abs(alive), percent)

        # Convert Tensors to numpy and calculate
        weight_dev = param.device
        new_mask = np.where(abs(weight_np) < percentile_value, 0, mask[step])

问题:因为andblock是一样的,我想避免写它并拥有它一次。你能帮我解决这个问题吗?ifelif

标签: pythonfor-loopif-statementpytorch

解决方案


推荐阅读