def test_wtac_one_hot(self):
     d = torch.tensor([[1.99, 3.01], [3.0, 2.01]])
     labels = torch.tensor([[0, 1], [1, 0]])
     actual = competitions.wtac(d, labels)
     desired = torch.tensor([[0, 1], [1, 0]])
     mismatch = np.testing.assert_array_almost_equal(actual,
                                                     desired,
                                                     decimal=5)
     self.assertIsNone(mismatch)
 def test_wtac_unequal_dist(self):
     d = torch.tensor([[2.0, 3.0, 4.0], [2.0, 3.0, 1.0]])
     labels = torch.tensor([0, 1, 1])
     actual = competitions.wtac(d, labels)
     desired = torch.tensor([0, 1])
     mismatch = np.testing.assert_array_almost_equal(actual,
                                                     desired,
                                                     decimal=5)
     self.assertIsNone(mismatch)
 def test_wtac(self):
     d = torch.tensor([[2., 3., 1.99, 3.01], [2., 3., 2.01, 3.]])
     labels = torch.tensor([0, 1, 2, 3])
     actual = competitions.wtac(d, labels)
     desired = torch.tensor([2, 0])
     mismatch = np.testing.assert_array_almost_equal(actual,
                                                     desired,
                                                     decimal=5)
     self.assertIsNone(mismatch)
Exemple #4
0
# Optimize using SGD optimizer from `torch.optim`
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
criterion = GLVQLoss(squashing="sigmoid_beta", beta=10)

x_in = torch.Tensor(x_train)
y_in = torch.Tensor(y_train)

# Training loop
title = "Prototype Visualization"
fig = plt.figure(title)
for epoch in range(70):
    # Compute loss
    dis, plabels = model(x_in)
    loss = criterion([dis, plabels], y_in)
    with torch.no_grad():
        pred = wtac(dis, plabels)
        correct = pred.eq(y_in.view_as(pred)).sum().item()
    acc = 100. * correct / len(x_train)
    print(
        f"Epoch: {epoch + 1:03d} Loss: {loss.item():05.02f} Acc: {acc:05.02f}%"
    )

    # Take a gradient descent step
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    # Get the prototypes form the model
    protos = model.proto_layer.prototypes.data.numpy()
    if np.isnan(np.sum(protos)):
        print("Stopping training because of `nan` in prototypes.")