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

神经网络反向传播代码不工作

我需要编写一个简单的由1个输出节点、1个包含3个节点的…

值错误:y 包含先前未见过的标签:

我使用了 决策树分类器,我想将我的 输入 作为 字符串…

使用不平衡数据集进行特征选择时遇到的问题

我正在使用不平衡数据集(54:38:7%)进行特征选择…

广义随机森林/因果森林在Python上的应用

我在寻找Python上的广义随机森林/因果森林算法,但…

如何用PyTorch仅用标量损失来训练神经网络?

假设我们有一个神经网络,我们希望它能根据输入预测三个值…

什么是RNN中间隐藏状态的良好用途?

我已经以三种不同的方式使用了RNN/LSTM: 多对多…

发表回复

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