gpt4 book ai didi

deep-learning - pytorch中的Dropconnect实现

转载 作者:行者123 更新时间:2023-12-05 07:14:07 39 4
gpt4 key购买 nike

我正在尝试为 Conv2D 和 transposeconv2D 层编写 dropconnect 代码。按照 https://pytorchnlp.readthedocs.io/en/latest/_modules/torchnlp/nn/weight_drop.html 中的教程进行操作创建它。

import torch
from torch.nn import Parameter

def _weight_drop(module, weights, dropout):
for name_w in weights:
w = getattr(module, name_w)
del module._parameters[name_w]
module.register_parameter(name_w + '_raw', Parameter(w))
original_module_forward = module.forward

def forward(*args, **kwargs):
for name_w in weights:
raw_w = getattr(module, name_w + '_raw')
w = torch.nn.functional.dropout(raw_w, p=dropout, training=module.training)
setattr(module, name_w, w)
return original_module_forward(*args, **kwargs)
setattr(module, 'forward', forward)

class WeightDropConv2d(torch.nn.Conv2d):
def __init__(self, *args, weight_dropout=0.0, **kwargs):
super().__init__(*args, **kwargs)
weights = ['weight']
_weight_drop(self, weights, weight_dropout)

class WeightDropConvTranspose2d(torch.nn.ConvTranspose2d):
def __init__(self, *args, weight_dropout=0.0, **kwargs):
super().__init__(*args, **kwargs)
weights = ['weight']
_weight_drop(self, weights, weight_dropout)

torch.version.cuda: 1.1.0torch.版本:9.0.176

我在第二个时期得到以下错误:

Traceback (most recent call last):
File "dropconnect.py", line 110, in <module>
out = model(image)
File "/home/sbhand2s/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 493, in __call__
result = self.forward(*input, **kwargs)
File "dropconnect.py", line 73, in forward
out = self.c1(x)
File "/home/sbhand2s/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 493, in __call__
result = self.forward(*input, **kwargs)
File "dropconnect.py", line 34, in forward
setattr(module, name_w, w)
File "/home/sbhand2s/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 558, in __setattr__
.format(torch.typename(value), name))
TypeError: cannot assign 'torch.cuda.FloatTensor' as parameter 'weight' (torch.nn.Parameter or None expected)

当我从 .eval() 切换到 .train() 时,这个错误发生在第二个时期。如果我不调用 .eval()

,则不会发生此错误

关于为什么会发生此错误或如何以更好的方式实现 dropconnect 有什么建议吗?

复制问题的代码:

from collections import OrderedDict
import torch
from torch import nn

layers = []
layers.append(("conv_1", WeightDropConv2d(1,3,3,1,1,weight_dropout=0.5)))
layers.append(("conv_2", WeightDropConv2d(3,3,3,1,1,weight_dropout=0.5)))
layers.append(("conv_3", WeightDropConv2d(3,1,3,1,1,weight_dropout=0.5)))

model = nn.Sequential(OrderedDict(layers))

pred = model(torch.randn([1,1,3,3]))

model.eval()
pred = model(torch.randn([1,1,3,3]))

model.train()
pred = model(torch.randn([1,1,3,3]))

最佳答案

我也无法使该方法正常工作(虽然有不同的错误),但这里有一个似乎有效的更简单的方法:

for i in range(num_batches):

orig_params = []
for n, p in model.named_parameters():
orig_params.append(p.clone())
p.copy_(F.dropout(p.data, p=drop_prob) * (1 - drop_prob))

output = model(input)
loss = nn.CrossEntropyLoss()(output, label)
optimizer.zero_grad()
loss.backward()

for orig_p, (n, p) in zip(orig_params, model.named_parameters()):
p.copy_(orig_p)

optimizer.step()

(在 Pytorch 1.4 中测试)

关于deep-learning - pytorch中的Dropconnect实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59970510/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com