从具有1个输出(二进制分类)的模型输出测试集预测结果的最佳方法

2024-04-26 10:47:39 发布

您现在位置:Python中文网/ 问答频道 /正文

这看起来很简单,但我正在寻找一种最好的方法来输出一个输出为1的模型(二进制分类模型)的预测结果。在本例中,我的标签是0和1。现在我可以说,如果模型输出>;0.5标签是1。但我猜这是对的。因此,我想知道是否有更好的方法,例如

prediction = np.argmax(y_hat.detach().numpy, axis=0) for multiple classes. 

目前我这样做是为了测试:

test = [[0,0],[0,1],[1,1],[1,0]]

for trial in test: 
  Xtest = torch.Tensor(trial)
  y_hat = logreg_clf(Xtest)

  if y_hat > 0.5:
    prediction = 1
  else: 
    prediction = 0
  # prediction = np.argmax(y_hat.detach().numpy, axis=0)
  print("{0} xor {1} = {2}".format(int(Xtest[0]), int(Xtest[1]), prediction))

他还想知道是否有一个函数可以自动获取测试集“test”的精度或混淆矩阵

供参考我的模型:

import torch.nn as nn
import torch.nn.functional as F

class LogisticRegression(nn.Module):
  # input_size: Dimensionality of input feature vector.
  # num_classes: The number of classes in the classification problem.
  def __init__(self, input_size, num_classes):
    # Always call the superclass (nn.Module) constructor first!
    super(LogisticRegression, self).__init__()
    # Set up the linear transform
    self.linear = nn.Linear(input_size, num_classes)
    # I do not yet include the sigmoid activation after the linear 
    # layer because our loss function will include this as you will see later

  # Forward's sole argument is the input.
  # input is of shape (batch_size, input_size)
  def forward(self, x):
    # Apply the linear transform.
    # out is of shape (batch_size, num_classes). 
    out = self.linear(x)
    out = F.sigmoid(out)
    # Softmax the out tensor to get a log-probability distribution
    # over classes for each example.
    return out

# Binary classifiation
num_outputs = 1
num_input_features = 2

# Create the logistic regression model
logreg_clf = LogisticRegression(num_input_features, num_outputs)

print(logreg_clf)

import torch 
lr_rate = 0.001  # alpha

# training set of input X and labels Y
X = torch.Tensor([[0,0],[0,1], [1,0], [1,1]])
Y = torch.Tensor([0,1,1,0]).view(-1,1) #view is similar to numpy.reshape() here it makes it into a column

# Run the forward pass of the logistic regression model
sample_output = logreg_clf(X) #completely random at the moment
print(X)

loss_function = nn.BCELoss() 
# SGD: stochastic gradient descent is used to train/fit the model
optimizer = torch.optim.SGD(logreg_clf.parameters(), lr=lr_rate)


import numpy as np 
# from torch.autograd import Variable

#training loop:

epochs = 2001 #how many times we go through the training set
steps = X.size(0) #steps = 4; we have 4 training examples (I know, tiny training set :)

for i in range(epochs):
    for j in range(steps):
        # randomly sample from the training set:
        data_point = np.random.randint(X.size(0))
        # store the retrieved datapoint into 2 separate variables of the right shape
        x_var = torch.Tensor(X[data_point]).unsqueeze(0)
        y_var = torch.Tensor(Y[data_point])

        optimizer.zero_grad() # empty (zero) the gradient buffers
        y_hat = logreg_clf(x_var) #get the output from the model
        print(y_hat)
        print(y_var)
        loss = loss_function(y_hat, y_var) #calculate the loss
        loss.backward() #backprop
        optimizer.step() #does the update

    if i % 500 == 0:
        print ("Epoch: {0}, Loss: {1}, ".format(i, loss.data.numpy()))

Tags: oftheinputsizehattrainingnntorch
1条回答
网友
1楼 · 发布于 2024-04-26 10:47:39
test_data = torch.Tensor(test)
raw_preds = logreg_clf(test_data)
preds = (raw_preds > 0.5).long()

为了一次获得所有测试数据的预测,我们首先将测试数据转换为张量,然后我们可以用该张量进行向前传递。 原始预测类似于tensor([[0.4795], [0.4749], [0.4960], [0.5006]]。 然后,我们应用“风险中性策略”,即0.5作为阈值来获得结果(其类型为torch.bool,因此我们将其转换为long

相当于一行:

preds = (logreg_clf(torch.Tensor(test)) > 0.5).long()    

因此,预测是:

tensor([[0], [0], [0], [1]])

wondering if there is a function to automatically get the accuracy or confusion matrix

您可以从^{}使用相应的函数:

from sklearn.metrics import accuracy_score, confusion_matrix

# the ground truth for the given test data
truth = torch.Tensor([0, 1, 0, 1]).view(-1, 1) 

conf_mat = confusion_matrix(truth, preds)
acc = accuracy_score(truth, preds)

这使得混淆矩阵为

array([[2, 0],
       [1, 1]], dtype=int64)

准确度为

0.75

因此,您有一个假阴性,即测试数据中的第二个样本

相关问题 更多 >