首页 > 解决方案 > 手动更改的量化权重未反映在 state_dict()

问题描述

PyTorch:1.7.0

我将量化权重修改为净后量化,如下所示:

# instantiate the quantized net (not shown here).

# get one of the conv layers
tmp = model_int8.state_dict()['features.0.weight']
scales = tmp.q_per_channel_scales()
zero_pts = tmp.q_per_channel_zero_points()
axis = tmp.q_per_channel_axis()

# get int repr
tmp_int8 = tmp.int_repr()

# change value (value change is dependent on the int8 value)
tmp_int8[0][0][0][0] = new_value

# how to convert tmp_int8 to torch.qint8 type?
new_tmp = torch._make_per_channel_quantized_tensor(tmp_int8, scales, zero_pts, axis)

# based on the above step:
model_int8.features[0].weight = new_tmp

但是,model_int8.features[0].weight显示更新的值,但model_int8.state_dict()['features.0.weight']显示旧的值。

我也尝试保存修改后的模型并重新加载,但问题仍然存在。

问题是哪些权重值被用于推理?我没有看到分类结果的变化。

标签: pythonpytorchquantization

解决方案


推荐阅读