我想使用https://github.com/mmbejani/TikhonovRegularizationTerm。这个库包括正则化吉洪诺夫条款的实现,从2010年到现在(2020年)出版。
我试图在mnist数据集上用这个库来训练一个非常基本的前馈nn,并得到以下错误:
AttributeError: 'WeightDecay' object has no attribute 'backward'
optimizer = optim.SGD(model.parameters(), lr=0.003, momentum=0.9)
time0 = time()
epochs = 15
for e in range(epochs):
running_loss = 0
for images, labels in trainloader:
# Flatten MNIST images into a 784 long vector
images = images.view(images.shape[0], -1)
# Training pass
optimizer.zero_grad()
output = model(images)
loss_function = nn.CrossEntropyLoss()
loss_function_with_regularization = WeightDecay(model, loss_function)
#This is where the model learns by backpropagating
loss_function_with_regularization.backward()
#And optimizes its weights here
optimizer.step()
running_loss += loss.item()
else:
print("Epoch {} - Training loss: {}".format(e, running_loss/len(trainloader)))
print("\nTraining Time (in minutes) =",(time()-time0)/60)`
1条答案
按热度按时间fjaof16o1#
错误说明
WeightDecay
类上没有backward
属性。下面是类的定义:https://github.com/mmbejani/TikhonovRegularizationTerm/blob/main/regularization.py#L11
它不包含名为
backward
的属性。它具有属性forward
,但没有backward
。类
WeightDecay
继承自类torch.nn.Module
,其定义如下:https://github.com/pytorch/pytorch/blob/master/torch/nn/modules/module.py#L366-它也不包含名称为backward
的属性。以下是通常定义属性
forward
和backward
的方式:https://stackoverflow.com/a/69501410/3694363-我没有任何类似的代码在https://github.com/mmbejani/TikhonovRegularizationTerm。要么是
README
没有反映代码的实际使用情况,要么是pytorch在过去两年中对API进行了大量修改,以至于这些代码不再适用于当前版本。