这似乎很简单,但是我正在寻找输出具有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()))
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]])
想知道是否具有自动获得精度或混淆矩阵的功能
您可以使用sklearn.metrics
中的相应功能:
sklearn.metrics
将混淆矩阵表示为
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)
所以您有一个假阴性,即测试数据中的第二个样本。