我正在使用 Pytorch 0.3.0 版本。我尝试在同一层内选择性地复制一个神经元及其权重,然后用另一组权重替换原始神经元。以下是我尝试的方法:
reshaped_data2 = data2.unsqueeze(0)new_layer_data = torch.cat([new_layer.data, reshaped_data2], dim=0)new_layer_data[i] = data1new_layer.data.copy_(new_layer_data)
首先,我对 data2
进行 unsqueeze 操作,使其从 0*X
张量变为 1*X
张量。然后,我将层的张量与重塑后的 data2
沿第0维度进行拼接。接着,我用 data1
替换位于索引 i
处的原始 data2
。最后,我将所有这些复制到我的层中。
我遇到的错误是:
RuntimeError: inconsistent tensor size, expected tensor [10 x 128] and src [11 x 128] to have the same number of elements, but got 1280 and 1408 elements respectively at /Users/soumith/code/builder/wheel/pytorch-src/torch/lib/TH/generic/THTensorCopy.c:86
如果我使用简单的赋值而不是复制操作,我会得到:
RuntimeError: The expanded size of the tensor (11) must match the existing size (10) at non-singleton dimension 1. at /Users/soumith/code/builder/wheel/pytorch-src/torch/lib/TH/generic/THTensor.c:309
我理解这个错误,但正确的处理方法是什么呢?
回答:
解决方案是创建一个具有正确大小的新模型,并将权重作为默认值传递。没有找到动态扩展的解决方案。