如何在 Pytorch 中找到神经网络的测试准确性。如何制定将预测与数据进行比较的方程

发布于 2025-01-17 13:08:10 字数 1166 浏览 0 评论 0原文

我无法确定测试数据的准确性。我是 pytorch 的新手,每次迭代的准确度都是 0。测试和训练损失值是合理的,并且随着迭代的进行而改进。但是,我不确定我是否评估了错误数据的准确性。我正在使用预测数据和 test_target 数据。 test_input 被输入到模型中以生成预测。一旦我理解了这一部分,我就能够找到训练的准确性。

for e in range(num_epochs):
    
    optimizer.zero_grad()
    out = model(train_input)
    loss = loss_fn(out, train_target)
    loss.backward()
    optimizer.step()

    with torch.no_grad():
        pred = model(train_input, future_preds=future)
        training_loss = loss_fn(pred[:,:-future], train_target)
        train_loss.append(training_loss)


        
        pred = model(test_input, future_preds=future)
        test_loss = loss_fn(pred[:,:-future], test_target)
        testing_loss.append(test_loss)
        
        predictions = pred.detach().numpy()
        testing_accu= sum(pred[:,:-future]==test_target)/20
        test_accu.append(testing_accu)

        print("Testing Accuracy: ", ((pred[:,:-future] > 0.5) == test_target).float().mean().item())

 return testing_loss, train_loss, test_accu

我的数据的tensor.shape是:

train_input : (80,999)

train_target: (80,999)

test_input : (20,999)

test_target : (20,999)

I am having trouble finding the accuracy of my testing data. I am new in pytorch and keep getting an accuracy of 0 for every iteration. Testing and training loss values are reasonable and they improve as the iterations go on. However, I am not sure if I am evaluating the wrong data for the accuracy. I am using the predicted data and the test_target data. The test_input is feed into the model to generate the predictions. Once I can understand this part I will able to find also the training accuracy.

for e in range(num_epochs):
    
    optimizer.zero_grad()
    out = model(train_input)
    loss = loss_fn(out, train_target)
    loss.backward()
    optimizer.step()

    with torch.no_grad():
        pred = model(train_input, future_preds=future)
        training_loss = loss_fn(pred[:,:-future], train_target)
        train_loss.append(training_loss)


        
        pred = model(test_input, future_preds=future)
        test_loss = loss_fn(pred[:,:-future], test_target)
        testing_loss.append(test_loss)
        
        predictions = pred.detach().numpy()
        testing_accu= sum(pred[:,:-future]==test_target)/20
        test_accu.append(testing_accu)

        print("Testing Accuracy: ", ((pred[:,:-future] > 0.5) == test_target).float().mean().item())

 return testing_loss, train_loss, test_accu

The tensor.shape for my data is:

train_input : (80,999)

train_target: (80,999)

test_input : (20,999)

test_target : (20,999)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文