使用3 Torch Linear图层添加我的自定义损失函数后,
我收到cuda错误
class KLDLoss(nn.Module):
def __init__(self, reduction='sum'):
super(KLDLoss, self).__init__()
self.reduction = reduction
def forward(self, mean, logvar):
# KLD loss
kld_loss = -0.5 * torch.sum(1 + logvar - mean.pow(2) - logvar.exp(), 1)
# Size average
if self.reduction == 'mean':
kld_loss = torch.mean(kld_loss)
elif self.reduction == 'sum':
kld_loss = torch.sum(kld_loss)
return kld_loss
class Latent_Classifier(nn.Module):
def __init__(self):
super(Latent_Classifier, self).__init__()
layers = []
layers += [nn.Linear(128, 750)]
layers += [nn.Linear(750, 750)]
layers += [nn.Linear(750, 1)]
self.seq = nn.Sequential(*layers)
def forward(self, latent_z):
x = self.seq(latent_z)
return -torch.mean(torch.log(x)) - torch.mean(torch.log(1 - x))
KLDLoss没有错误,但是在optimizer.step()
进行某些训练之后,潜在分类器有错误
105 denom = (max_exp_avg_sq.sqrt() / math.sqrt(bias_correction2)).add_(group['eps'])
106 else:
--> 107 denom = (exp_avg_sq.sqrt() / math.sqrt(bias_correction2)).add_(group['eps'])
108
109 step_size = group['lr'] / bias_correction1
RuntimeError: CUDA error: device-side assert triggered
我的潜在分类器代码中是否存在错误?
optimizer为AdamOptimizer
,而args为0.0002 lr, (0.5, 0.999)betas
答案 0 :(得分:0)
这些CUDA错误可能是由我的两件事引起的:
所以我的猜测是:您正在尝试在间隔[0,1 [(不包括0和1))之外的内容上使用KLDiv。在输出层中添加一个S型激活,该问题应该得到解决...
您可以在CPU上运行代码,并且会有一条更有意义的错误消息。