Skip to content

Instantly share code, notes, and snippets.

@someshfengde
Created August 18, 2023 05:30
Show Gist options
  • Save someshfengde/28e52708071b1b34a84de8ed1454b84f to your computer and use it in GitHub Desktop.
Save someshfengde/28e52708071b1b34a84de8ed1454b84f to your computer and use it in GitHub Desktop.
smooth_loss_function(out1, torch.tensor(400)), smooth_loss_function(out2, torch.tensor(420))
# Let's check what will happen to both the losses when the losses are increasing
# let's generate data for dummy predictions and actual values
dummy_preds = torch.tensor(np.sort( np.random.uniform(0, 2, 100))).unsqueeze(1)
dummy_actuals = torch.tensor(np.ones(100)).unsqueeze(1)
fig = px.line(y = [dummy_preds.squeeze(), dummy_actuals.squeeze()], title = 'Sample data for understanding')
fig.data[0].name = "dummy predictions"
fig.data[1].name = "dummy acutal values "
fig.update_layout(legend_title_text="Legend")
fig.show()
dummy_l1_loss = [loss_function(pred,act) for pred,act in zip(dummy_preds,dummy_actuals)]
dummy_smooth_l1_loss = [smooth_loss_function(pred,act) for pred,act in zip(dummy_preds, dummy_actuals)]
fig = px.line(
y=[dummy_l1_loss, dummy_smooth_l1_loss],
title="Dummy values losses for L1 and smooth L1",
labels={"y": "Loss", "x": "increasing losses"},
color_discrete_sequence=["blue", "green"], # Optional custom colors
)
fig.data[0].name = "L1 Loss Output"
fig.data[1].name = "Smooth Loss Output"
fig.update_layout(legend_title_text="Loss Type")
fig.show()
px.line( loss_output, title = "L1 loss across 200 epochs")
out1 = model(torch.tensor([20]).to(torch.float32))
out2 = model(torch.tensor([21]).to(torch.float32))
out1, out2
loss_function(out1, torch.tensor(400)), loss_function(out2, torch.tensor(420))
model_1 = LinearRegressor()
smooth_loss_function = torch.nn.SmoothL1Loss()
smooth_loss_output = train_model(model_1,smooth_loss_function)
fig = px.line(
y=[loss_output, smooth_loss_output],
title="Smooth_L1_loss across 200 epochs",
labels={"y": "Loss", "x": "Epoch"},
color_discrete_sequence=["blue", "green"], # Optional custom colors
)
fig.data[0].name = "L1 Loss Output"
fig.data[1].name = "Smooth Loss Output"
fig.update_layout(legend_title_text="Loss Type")
fig.show()
out1 = model_1(torch.tensor([20]).to(torch.float32))
out2 = model_1(torch.tensor([21]).to(torch.float32))
out1, out2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment