nn.BCELoss()
from fastai.vision.all import *
batch_size = 5
n_classes = 3
t = [
[0, 1, 1],
[1, 1, 1],
[0, 0, 0],
[1, 0, 0],
[0, 1, 0],
]
#torch.stack([Tensor(y) for y in x] for x in t)
t = torch.tensor(t, dtype=torch.long)
t
W = torch.randn(9, n_classes)
b = torch.randn(1)
X = torch.randn(batch_size, 9)
y = F.relu(X@W + b)
y.shape, y
y.shape, t.shape
loss = nn.BCEWithLogitsLoss()(y, t.float())
loss
y0 = y.sigmoid()
y0
t
-torch.where(t==1, y0, 1-y0).log().mean()
def accum(y, tgt, thr):
return ((y.sigmoid()>thr)==tgt).float().mean()
xs = torch.linspace(0.05,0.95,29)
accs = [accum(y, t, thr=i) for i in xs]
plt.plot(xs,accs)