我正在尝试使用 Pytorch 进行联合学习的线性回归算法,但遇到以下错误。我正在 Colab 上实现它。根据我的说法,这个错误可能是由于 train() 函数中的一些代码行。如果您曾与 Pysyft 合作过并且之前遇到过此类错误,请提供帮助。
RuntimeError: invalid argument 8: lda should be at least max(1, 0), but have 0 at /pytorch/aten/src/TH/generic/THBlas.cpp:363
以下是代码:
#import the necessasry packages
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import syft as sy
#create target and data variables as tensors
x_data=Variable(torch.Tensor([[1.0],[0.0],[1.0],[0.0]]))
y_data=Variable(torch.Tensor([[0.0],[0.0],[1.0],[1.0]]))
#Create virtual Workers
hook = sy.TorchHook(torch)
bob = sy.VirtualWorker(hook, id="bob")
alice = sy.VirtualWorker(hook, id="alice")
data_bob = x_data[0:2]
target_bob = y_data[0:2]
data_alice = x_data[2:0]
target_alice = y_data[2:0]
#creating a class that does Linear Regression
class LinearRegression (nn.Module):
def __init__(self):
super(LinearRegression,self). __init__ ()
self.linear = torch.nn.Linear(1,1)
def forward(self, x):
y_pred = self.linear(x)
return y_pred
#assign the function to the variable name 'Model'
model=LinearRegression()
#send the data to the virtual worker pointers
data_bob = data_bob.send(bob)
data_alice = data_alice.send(alice)
target_bob = target_bob.send(bob)
target_alice = target_alice.send(alice)
# organize pointers into a list
datasets = [(data_bob,target_bob),(data_alice,target_alice)]
#create optimizer and calculate the loss
opt = torch.optim.SGD(params=model.parameters(),lr=0.1)
criterion = torch.nn.MSELoss(size_average=False)
def train():
opt = torch.optim.SGD(params=model.parameters(),lr=0.1)
for epoch in range (20):
model.train()
print("Training started..")
for x_data,y_data in datasets:
model.send(x_data.location)
opt.zero_grad()
#forwardpass
#the model here is the linear regression model
y_pred = model(x_data)
#ComputeLoss
loss=criterion(y_pred,y_data)
#BackwardPass
loss.backward()
opt.step()
model.get()
print(loss.get())
train()