RuntimeError: 输入类型(torch.cuda.FloatTensor)和权重类型(torch.FloatTensor)应相同

我已经将模型和数据设置到相同的设备上,但总是会引发这样的错误:

RuntimeError: 输入类型(torch.cuda.FloatTensor)和权重类型(torch.FloatTensor)应相同

以下是我的训练代码,希望你能解答这个问题。谢谢!

def train(train_img_path, train_label_path, pths_path, interval, log_file):    file_num = len(os.listdir(train_img_path))    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")    net = EAST(extractor=extractor, geometry_mode=geometry_mode, pretrained=True)    net = net.to(device)    trainset = custom_dataset(train_img_path, train_label_path)    train_loader = data.DataLoader(trainset, batch_size=batch_size,                                   shuffle=True, num_workers=num_workers, drop_last=True)    optimizer = optim.SGD(net.parameters(), lr=initial_lr, momentum=momentum, weight_decay=weight_decay_sgd)    criterion = Loss(weight_geo, weight_angle, geometry_mode="RBOX")    net.train()    epoch_loss = 0.    for epoch in range(max_epoch):        epoch_time = time.time()        for i, (img, score_gt, geo_gt, ignored_map) in enumerate(train_loader):            start_time = time.time()            img, score_gt, geo_gt, ignored_map = img.to(device), score_gt.to(device),\                                                 geo_gt.to(device), ignored_map.to(device)            score_pred, geo_pred = net(img)            total_loss, score_loss, loss_AABB, loss_angle = criterion(score_pred, geo_pred, score_gt, geo_gt, ignored_map)            epoch_loss += total_loss.item()            optimizer.zero_grad()            total_loss.backward()            optimizer.step()

回答:

我怀疑你的损失函数内部有自己的参数,因此你也应该

criterion = Loss(weight_geo, weight_angle, geometry_mode="RBOX").to(device)

如果你能提供完整的错误追踪,指出是哪一行确切地引发了错误,就更容易发现问题所在了。

Related Posts

如何从数据集中移除EXIF数据?

我在尝试从数据集中的图像中移除EXIF数据(这些数据将…

用于Python中的“智能点”游戏的遗传算法不工作

过去几天我一直在尝试实现所谓的“智能点”游戏。我第一次…

哪个R平方得分更有帮助?

data.drop(‘Movie Title’, ax…

使用线性回归预测GRE分数对录取率的影响

我正在学习线性回归,并尝试在Jupyter笔记本中用P…

使用mlrMBO贝叶斯优化进行SVM超参数调优时出现错误

我试图针对一个分类任务优化SVM,这个方法在许多其他模…

Keras模型的二元交叉熵准确率未发生变化

我在网上看到了很多关于这个问题的提问,但没有找到明确的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注