![](/img/trans.png)
[英]how can i extract images from the last decoder layer “logits” after training my neural network?
[英]How can I add more neurons / filters to a neural network model after training?
I'm interested in training both a CNN model and a simple linear feed forward model in PyTorch, and after training to add more filters -- to the CNN layers, & neurons -- to the linear model layers and the outputs (eg from binary分類到多類分類)。 通過添加它們,我的具體意思是保持訓練的權重不變,並將隨機初始化的權重添加到新的傳入權重。
這個有點棘手,需要slice
(有關slice
的更多信息,請參閱此答案,但它應該是直觀的)。 這也是切片技巧的答案。 解釋請看評論:
import torch
def expand(
original: torch.nn.Module,
*args,
**kwargs
# Add other arguments if needed, like different stride
# They won't change weights shape, but may change behaviour
):
new = type(original)(*args, **kwargs)
new_weight_shape = torch.tensor(new.weight.shape)
new_bias_shape = torch.tensor(new.bias.shape)
original_weight_shape = torch.tensor(original.weight.shape)
original_bias_shape = torch.tensor(original.bias.shape)
# I assume bias and weight exist, if not, do some checks
# Also quick check, that new layer is "larger" than original
assert torch.all(new_weight_shape >= original_weight_shape)
assert new_bias_shape >= original_bias_shape
# All the weights will be inputted from top to bottom, bias 1D assumed
new.bias.data[:original_bias_shape] = original.bias.data
# Create slices 0:n for each dimension
slicer = tuple([slice(0, dim) for dim in original_weight_shape])
# And input the data
new.weight.data[slicer] = original.weight.data
return new
layer = torch.nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3)
new = expand(layer, in_channels=32, out_channels=64, kernel_size=3)
這應該適用於任何層(具有weight
和bias
,如果需要進行調整)。 使用這種方法,您可以重新創建神經網絡或使用 PyTorch 的apply
( 此處的文檔)
另請記住,您必須為“新層”顯式傳遞創建的*args
和**kwargs
,這將輸入經過訓練的連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.