Logistic regression turns one weighted score into one probability, which is enough for a yes-or-no answer. To choose among classes, give every class its own weights and its own score, and let the scores compete. This model is softmax regression, also called multinomial logistic regression.
For a row , each class gets a score and then a probability:
The are positive and add up to 1. Training lowers the cross-entropy cost: the average over all rows of , where is the row's true class. Its gradient has a tidy form. For one row, the error of class is
The gradient for is the average of over all rows, and the gradient for is the average of .
Task: write train_softmax(X, y, k, lr, steps). Start with every weight and bias at 0, then run steps updates of batch gradient descent, each one moving every weight and bias by lr times its gradient, downhill:
X is a list of rows of numbers. y holds each row's class, an integer from 0 to k - 1. A class may have no rows at all.(W, b): W is a list of k lists of d weights and b is a list of k biases, all rounded to 4 decimal places.Once your code passes every test, a reference solution shows up here so you can compare it with yours.