Exercises
Exercises
Conceptual
Exercise 1 Conceptual
Dropout with rate \(p\) sets each activation to zero with probability \(p\) and divides the rest by \(1-p\).
- Show that the expected value of an activation is unchanged.
- Why do we divide by \(1-p\) during training instead of multiplying by \(1-p\) at test time.
- Explain in two sentences why dropout reduces overfitting.
Exercise 2 Conceptual
We model counts with a Poisson distribution and a log link.
- Write down the negative log-likelihood and drop the terms that do not depend on the parameters.
- A coefficient in the linear version is 0.2. By what factor does the expected count change for a one unit increase in that predictor.
- Why does a Poisson model have no separate noise parameter.
Exercise 3 Conceptual
The hour of the day is encoded in three different ways below. For each one, say what the model can and cannot express.
- As a single number from 0 to 23.
- As 24 one-hot columns.
- As \(\sin(2\pi h/24)\) and \(\cos(2\pi h/24)\).
Which one would you choose for the bicycle data, and why.
Critique
Exercise 4 Critique
The code below trains a network on the bicycle data and reports a good test loss. It contains two bugs that make the reported number wrong, and one that makes the model worse than it should be.
scaler = StandardScaler()
X = scaler.fit_transform(features)
X_train, X_test, y_train, y_test = train_test_split(X, counts, test_size=0.25)
model = PoissonNet(X.shape[1], dropout=0.3)
opt = torch.optim.AdamW(model.parameters(), lr=1e-2)
for epoch in range(200):
for xb, yb in loader:
opt.zero_grad()
loss = torch.mean((model(xb) - yb) ** 2)
loss.backward()
opt.step()
with torch.no_grad():
pred = model(torch.tensor(X_test))
print("test loss:", float(torch.mean((pred - y_test) ** 2)))- Find all three problems.
- For each one, say whether it makes the reported number too good, too bad, or simply wrong.
- Fix the code.
Applied
Exercise 5 Applied
Work through the ladder of models on the real bicycle data from OpenML, id 42712.
- Fit a Poisson GLM on the raw predictors and report the cross-validated deviance.
- Add one-hot coding for the hour and the weekday, and report again.
- Replace the one-hot hour by the cyclic encoding. Which is better here, and by how much.
- Fit the Poisson network. Does it beat the best GLM.
- Score the winner once on a test set that you held out before you started.
Exercise 6 Applied
Compare the four ways of regularizing a network on the same data.
- No regularization.
- Weight decay, three values.
- Dropout, three rates.
- Early stopping.
Report the test deviance for each and say which one gives the best result per unit of effort spent tuning.
Exercise 7 Applied · optional
Replace the Poisson likelihood by a negative binomial one, which has an extra parameter for the dispersion.
- Write down the loss.
- Fit it as a second output of the same network.
- Compare to the Poisson model. Is the data overdispersed.