python - PureFrameworkTensorFoundError,运行时错误 -FedeartedLearning

标签 python pytorch federated-learning pysyft

我正在尝试使用 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()

最佳答案

这里有一个拼写错误:

data_alice = x_data[2:0]
target_alice = y_data[2:0]

应该是[2:]

因为 data_alice 失败,所以您遇到了此错误。

关于python - PureFrameworkTensorFoundError,运行时错误 -FedeartedLearning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59484278/

相关文章:

python - 从嵌套在列表 Python 中的字典返回键的值

python - 将 Pandas 系列列表转换为 numpy 数组

python - 在验证器文件中导入模型时,Django 中的模型导入错误

image - PyTorch 自定义数据集数据加载器返回字符串(键)而不是张量

Pytorch:在 Linux 上未检测到支持 CUDA 的设备

tensorflow - 在联合训练中实现数据生成器

python - 当没有重复项时,pandas 中索引重复错误

deep-learning - pytorch如何进行多次前向传播和一次反向传播?

tensorflow-federated - TFF : test accuracy fluctuate