python - 当使用 Pytorch 的偏差完好无损时,如何仅操纵神经网络的权重?
问题描述
在下面的for
循环中,我得到了我的神经网络的name
's 和parameter
's。根据偏差标志,我将对在 'if' 和 'elif' 之后使用的weight
's 和' 做一些操作。但是,when is ,我只是对's 进行操作,但如果flag is ,我将对's 和's 都进行操作。bias
block
bias
False
weight
bias
True
weight
bias
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
是一样的,我想避免写它并拥有它一次。你能帮我解决这个问题吗?if
elif
解决方案
推荐阅读
- javascript - 如何在php中只允许png文件类型
- javascript - 如何在 C# 代码块中使用 javascript 变量
- elasticsearch - 如何在 Elasticsearch 7 NEST 7 中设置“max_result_window”
- c++ - 传递 unique_ptr 时调试和发布配置的移动不一致?
- javascript - 如何使用 Javascript 格式化数字?
- javascript - JavaScript 对象动态检索值
- php - 自定义帖子类型内容未显示在单页上
- datetime - 如何将儒略日期转换为雪花中的时间戳
- xfs - 我如何知道自动提款机交易是成功还是失败?
- docker - Light4J Oauth2 docker-compose 错误提示找不到指定的 Dockerfile