[英]Binary classification with PyTorch
Below is code I've written for binary classification in PyTorch:下面是我为 PyTorch 中的二进制分类编写的代码:
%reset -f
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
import numpy as np
import matplotlib.pyplot as plt
import torch.utils.data as data_utils
import torch.nn as nn
import torch.nn.functional as F
device = 'cpu'
num_epochs = 10
hidden_size = 500
num_classes = 2
learning_rate = .001
torch.manual_seed(24)
x1 = np.array([0,0])
x2 = np.array([0,1])
x3 = np.array([1,0])
x4 = np.array([1,1])
x = torch.tensor([x1,x2,x3,x4]).float()
y = torch.tensor([1,0,1,1]).long()
train = data_utils.TensorDataset(x,y)
train_loader = data_utils.DataLoader(train , batch_size=2 , shuffle=True)
input_size = len(x[0])
def weights_init(m):
if type(m) == nn.Linear:
m.weight.data.normal_(0.0, 1)
class NeuralNet(nn.Module) :
def __init__(self, input_size, hidden_size, num_classes) :
super(NeuralNet, self).__init__()
self.fc1 = nn.Linear(input_size , hidden_size)
self.fc2 = nn.Linear(hidden_size , 100)
self.fc3 = nn.Linear(100 , num_classes)
self.sigmoid = nn.Sigmoid()
def forward(self, x) :
out = self.fc1(x)
out = self.sigmoid(out)
out = self.fc2(out)
out = self.sigmoid(out)
out = self.fc3(out)
out = self.sigmoid(out)
return out
model = NeuralNet(input_size, hidden_size, num_classes).to(device)
model.apply(weights_init)
criterionCE = nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
loss_values = []
for i in range(0 , 140) :
print('in i' , i)
total_step = len(train_loader)
for epoch in range(num_epochs) :
for i,(images , labels) in enumerate(train_loader) :
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
print('outputs' , outputs)
loss = criterionCE(outputs , labels)
loss_values.append(loss)
optimizer.zero_grad()
loss.backward()
optimizer.step()
outputs = model(x)
print(outputs.data.max(1)[1])
This code throws an error:此代码引发错误:
~/opt/miniconda3/envs/ds1/lib/python3.8/site-packages/torch/nn/functional.py in binary_cross_entropy(input, target, weight, size_average, reduce, reduction)
2067 stacklevel=2)
2068 if input.numel() != target.numel():
-> 2069 raise ValueError("Target and input must have the same number of elements. target nelement ({}) "
2070 "!= input nelement ({})".format(target.numel(), input.numel()))
2071
ValueError: Target and input must have the same number of elements. target nelement (2) != input nelement (4)
I printed where I think is the cause of the error:我打印了我认为是错误原因的地方:
print('outputs' , outputs)
renders渲染
outputs tensor([[9.9988e-01, 1.4011e-05],
[9.9325e-01, 1.2087e-05]], grad_fn=<SigmoidBackward>)
So outputs should be a 2x1 instead of a 2x2 result.所以输出应该是 2x1 而不是 2x2 结果。 Have I not defined the setup of the model correctly?我没有正确定义 model 的设置吗? As I'm using sigmoid shouldn't a 2x1 output be created instead of 2x2?当我使用 sigmoid 时,不应该创建 2x1 output 而不是 2x2?
If you are working on a binary classification task your model should only output one logit.如果您正在处理二进制分类任务,您的 model 应该只有 output 一个 logit。 Since you've set self.fc3
to have 2 neurons, you will get 2 logits as the output.由于您已将self.fc3
设置为具有2 个神经元,因此您将获得2 个logits 作为 output。 Therefore, you should set self.fc3
as nn.Linear(100, 1)
.因此,您应该将self.fc3
设置为nn.Linear(100, 1)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.