The sum of the error of the classification is 0. So why the accuracy rate is 0.995 rather than 1
import torch
from torch.autograd import Variable
from torch.nn import Sequential
import numpy as np
data=torch.ones(10,2)
x0=torch.normal(2*data);
x1=torch.normal(-2*data);
y0=torch.ones(10);
y1=torch.zeros(10);
x=torch.cat((x0,x1),0)
y=torch.cat((y0,y1),0)
x=Variable(x);
y=Variable(y.type(torch.LongTensor))
net=Sequential(
torch.nn.Linear(2,10),
torch.nn.ReLU(),
torch.nn.Linear(10,2)
)
#torch.nn.Softmax())
print(net)
opt=torch.optim.SGD(net.parameters(),lr=0.05)
loss_func=torch.nn.CrossEntropyLoss()
for t in list(range(1000)):
out=net(x);
loss=loss_func(out,y)
opt.zero_grad()
loss.backward()
opt.step()
prediction=torch.max(out,1)[1]
pred=prediction.data.numpy().squeeze()
accuray=sum(pred==y.data.numpy())/len(y) # why the accuracy is not 1
print(accuracy)
print(sum(np.abs(pred-y.data.numpy())))# the error is 0
Sorry, I misspelled the word “accuracy”
and caused the error. How to delete this post.
accuracy=sum(pred==y.data.numpy())/len(y)