【发布时间】:2020-06-12 22:19:16
【问题描述】:
我正在使用 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
我理解错误,但正确的方法是什么?
【问题讨论】:
标签: python python-2.7 machine-learning pytorch tensor