0

我正在使用 pysyft 和 pytorch 执行联合学习。我正在使用糖尿病数据集。我在训练时遇到了这个错误(张量的元素 0 不需要 grad 并且没有 grad_fn)。我附上了我的错误的屏幕截图以及笔记本文件:

在此处输入图像描述

我的代码:

epochs = 500
final_loss = []
for i in range(epochs):
    i = i + 1
    model.train()
    for batch_idx, (data, target) in enumerate(federated_train_loader): # <-- now it is a distributed dataset
        model.send(data.location) # <-- NEW: send the model to the right location
   
        data, target = data.to(device), target.to(device)
        output = model.forward(data)
        loss = loss_function(output, target)
        final_loss.append(loss)
        if i % 10 == 1:
            print('Epoch number: {} and the loss: {}'.format(i, loss.get()))
        optimizer.zero_grad() ## Clears the gradients of all optimized class
        loss.backward() ## for backward propagation and to find the derivative
        optimizer.step() ## performs a single optimization step.
        model.get()

我的模型:

class ANN_Model(nn.Module):
    def __init__(self, input_features = 8, hidden1 = 20, hidden2 = 20, out_features = 2):
        super().__init__()
        self.f_connected1 = nn.Linear(input_features, hidden1)
        self.f_connected2 = nn.Linear(hidden1, hidden2)
        self.out = nn.Linear(hidden2, out_features)
    def forward(self, x):
        x = F.relu(self.f_connected1(x))
        x = F.relu(self.f_connected2(x))
        x = self.out(x)
        return x
4

0 回答 0