我正在使用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] = data1
new_layer.data.copy_(new_layer_data)
首先,我解了data2
,使之成为1*X
张量,而不是0*X
。
然后,我将层的张量与沿维度0重新整形的data2
连接起来。
然后,我将位于索引i
的原始data2
替换为data1
。
最后,我将所有这些复制到我的图层中
我得到的错误是:
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
我理解错误,但正确的方法是什么
这里的解决方案是创建具有正确大小的新模型,并将权重作为默认值传入。未找到动态扩展解决方案
您试图用
11x128
张量替换10x128
张量,这是模型不允许的。new_layer
是否用大小(11, 128)
初始化? 如果没有,请尝试创建具有所需大小的新层(11, 128)
,然后复制/分配new_layer_data
相关问题 更多 >
编程相关推荐