Skip to content

Instantly share code, notes, and snippets.

@peloyeje
Last active October 15, 2018 20:29
Show Gist options
  • Select an option

  • Save peloyeje/12be122879ad6e0b41566bcc91596359 to your computer and use it in GitHub Desktop.

Select an option

Save peloyeje/12be122879ad6e0b41566bcc91596359 to your computer and use it in GitHub Desktop.
Logistic regression example with PyTorch (Marc Lelarge's class "Deep Learning Do-it-Yourself")
import torch
import numpy as np
from scipy.stats import bernoulli
from scipy.special import expit
dtype = torch.FloatTensor
# Model
w_source = np.array([2., -3.])
b_source = np.array([1.])
# Data generation
x = np.random.random((100,2))
y = bernoulli.rvs(expit(np.dot(x, w_source) + b_source))
# Convert to tensors
x_t = torch.from_numpy(x).type(dtype)
y_t = torch.from_numpy(y).type(dtype).unsqueeze(1) # Add 1D for compatibility with the BCELoss
# Init model with sigmoid output
model = torch.nn.Sequential(
torch.nn.Linear(2, 1),
torch.nn.Sigmoid()
)
model.train()
loss_fn = torch.nn.BCELoss() # Binary cross-entropy loss
optimizer = torch.optim.SGD(model.parameters(), lr=0.01) # PyTorch automatically detects weights to optimize
for epoch in range(1000):
y_pred = model(x_t)
# The loss function is expecting 1D float tensors for both ground truth and predictions
loss = loss_fn(y_pred, y_t)
if epoch % 100 == 0:
print("progress:", "epoch:", epoch, "loss",loss.item())
# Zero gradients, perform a backward pass, and update the weights.
optimizer.zero_grad()
loss.backward()
optimizer.step()
print("estimation of the parameters:")
for param in model.parameters():
print(param)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment