Pytorch 在一层中复制一个神经元
Pytorch copy a neuron in a layer
我正在使用 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)
创建新图层,然后 copy/assign 您的 new_layer_data
.
此处的解决方案是创建一个具有正确大小的新模型并将权重作为默认值传递。没有找到动态扩容方案。
我正在使用 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)
创建新图层,然后 copy/assign 您的 new_layer_data
.
此处的解决方案是创建一个具有正确大小的新模型并将权重作为默认值传递。没有找到动态扩容方案。