1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| n_features, i, j, n_classes = 10, 32, 16, 3 batch_size = 5
input_data = torch.randn(batch_size, n_features)
prediction = model(input_data).double()
actual = torch.randn(batch_size, n_classes).double()
mse_loss = nn.MSELoss()(prediction, actual) print("MSE Loss:", mse_loss.item())
l1_loss = nn.SmoothL1Loss()(prediction, actual) print("Smooth L1 Loss:", l1_loss.item())
actual_bce = torch.tensor([ [0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 1.0, 1.0] ]).double() bce_loss = nn.BCELoss()(prediction, actual_bce) print("BCE Loss:", bce_loss.item())
ce_loss = nn.CrossEntropyLoss()(prediction, actual) print("Cross-Entropy Loss:", ce_loss.item())
loss = ce_loss loss.backward() print("Backpropagation completed successfully!")
|