RuntimeError:后端CPU的预期对象,但参数#2'weight'的后端CUDA为

时间:2019-08-09 09:19:56

标签: python-3.x pytorch

我试图对皮肤图像进行分类。以下代码用于训练数据,但是在中途训练时会遇到诸如标题之类的问题。

使用
python:3.7
pytorch:1.1
系统:win10

start_epoch=0
for epoch in range(num_epochs):
    print('Starting train epoch %d / %d' % (start_epoch + epoch + 1, num_epochs))
    model = model.to(device)
    print(device)
    #model.train()
    running_loss = 0
    count = 0
    epoch_loss = 0

    #for i, (input, depth) in enumerate(train_loader):
    for step,(input, depth) in enumerate(train_loader):
        # input, depth = data
        input = input.to(device)
        depth = depth.to(device)

        input_var=torch.tensor(input)
        depth_var=torch.tensor(depth).squeeze(1)
        #input_tensor = input_var.to(device)
        #depth_tensor = depth_var.to(device)

        output = model.forward(input_var)
        #new_output = output.to(device)
        #result = output.type(torch.FloatTensor)
        loss = loss_fn(output, depth_var)
        #loss = loss.type(torch.FloatTensor)
        print('count: ',count,' loss:', loss.item())
        count += 1
        running_loss += loss.data.cpu().numpy()
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if count%100==0:
            torch.save(model,"./pkl/cifar.pkl")

0 个答案:

没有答案