POLISCI 158 · Section 2

Week 3 · Section 2 · Lab

Train a neural network, then question it

On Wednesday you saw how machines learn from examples. Today you'll train a small neural network yourself, watch it overfit, and then test a fraud-flagging model the way an auditor would. Everything runs in this page. There's nothing to install.

Goes on your submission. Stored only in this browser.

Pairs can share a laptop, but each person submits their own answers.

0 of 9 items done

Recap: how machines learn

The five ideas from Session 3 that you'll use today. Your TA will go over them at the start of section; come back here if you need a reminder.

1 · Where machine learning fits

AI systems that mimic human reasoning and decisions
Machine learning learns rules or patterns from data
Deep learning machine learning with many-layered neural networks
Generative AI learns what data look like, to make new content
LLMs generative AI built on transformers, trained on huge amounts of text

2 · Programming vs. machine learning

ProgrammingRules (a person writes them) + Data → Answers
Machine learningData + Answers (labels) → A rule learned from the examples

3 · Three kinds of learning

Try it: which kind of learning is each task?

Predict whether a bill will pass, from 10,000 past bills marked passed or failed.
Group legislators who vote alike, with no labels given.
Improve a chatbot using people's thumbs-up and thumbs-down on its answers.
Flag benefit claims for review, learning from past claims marked correct or incorrect.

4 · Overfitting: training error vs. new-data error

training data new data too simple about right too flexible error

A flexible enough model can memorize its training examples, noise and all, so its training error keeps falling. What matters is the error on examples it has never seen: the test error. That falls at first, then rises once the model starts memorizing. This is overfitting. That's why we always hold back some data to test on.

5 · Neurons and gradient descent

x1 x2 x3 w1 w2 w3 w1x1 + w2x2 + w3x3 + b bend output

A neuron takes a weighted sum of its inputs, then bends it. A network stacks layers of neurons. The weights are the numbers it learns. It learns them by gradient descent:

  1. Start with random weights.
  2. Measure the error (the loss) on the training examples.
  3. Change every weight a little in the direction that lowers the error.
  4. Repeat. One pass through all the training examples is one epoch.

Part 1 · Will the bill get out of committee?

Each dot is a bill in a made-up legislature. The network sees two numbers about each bill:

Its job is to predict whether the bill got out of committee or died in committee. The background shows the network's guess for every possible bill. Test accuracy is the share of 1,000 new bills it gets right.

The data are simulated. In this made-up chamber, moderate sponsors need less majority support, and sponsors far from the median need a lot. The network doesn't know that rule; it has to learn it from the dots.

← left · sponsor's ideology · right → majority cosponsors →
Training data
Hidden layers
1
Neurons per layer
1
Speed

Changing the data or the network starts over from new random weights. Training stops on its own at epoch 3,000.

Epoch
0
Training loss
–
Test loss
–
Training accuracy
–
Test accuracy
–
Lowest test loss
–
training loss test loss lowest test loss
Recorded runs go into your submission automatically.
#DataNetworkEpochTrain acc.Test acc.Train lossTest lossLowest test loss at
  1. 1A · One neuron

    Data: 200 bills. Network: 1 layer, 1 neuron. Train to about epoch 500, pause, and record. What shape is the boundary between the colors? Why can't this network follow the curve in the dots?

  2. 1B · Just big enough

    Keep the 200 bills. Add neurons (and layers, if you like) until test accuracy reaches 88%. Give each network about 1,000 epochs: some get stuck for a while before they improve. What's the smallest network that gets there? Record it. How did training accuracy compare with test accuracy?

  3. 1C · Too much, too little data

    Switch to 25 bills, messy (a small sample with some coding errors). Network: 2 layers of 8 neurons. Train to at least epoch 1,500 and record. What did the two loss lines do? About when was the test loss lowest? If this model were going to be used for real, when would you have stopped training, and why?

Part 2 · Which benefit claims get flagged?

A fictional benefits agency wants a model to pick which claims get a fraud check. It can check 10% of claims. The model learns from 2,000 past claims, each marked in the agency's records as fraud or not fraud. It's then tested on 2,000 new claims, and the 10% with the highest risk scores are flagged.

The agency can give the model up to four inputs:

InputWhat it is
Income irregularitySudden jumps in reported income. Always used.
Earlier correctionsHow many times the claim was corrected before. Always used.
PostcodeThe share of non-citizens living in the claimant's postcode.
CitizenshipWhether the claimant is a citizen.

Loosely modeled on the Dutch childcare-benefits case from lecture. All claims are simulated by this page.

Inputs the model may use

Network: 1 hidden layer of 6 neurons, 300 epochs.
#InputsFlagged: cit. / non-cit.Records call fraudTest lossReally fraudHonest flagged: cit. / non-cit.
  1. 2A · Judge it by the records

    Train and record three models: (1) neither box checked, (2) postcode only, (3) postcode and citizenship. Don't open the truth yet. Judged only by what the agency can see, which model looks best? Are the differences between them large?

  2. 2B · Judge it by the truth

    Now press Show the truth. Look at honest claimants who were flagged in each group, across your three models. What happened when you dropped citizenship but kept postcode? Why?

  3. 2C · No truth button

    A real agency never gets a "Show the truth" button. Name one thing it could do to find out whether its model treats groups unfairly.

Part 3 · Back to Wednesday's question

Today's question

Is a model that predicts well good enough to use in policymaking?

Answer in 3 to 5 sentences, in your own words. Use at least one thing you saw today (in Part 1 or Part 2), and say what "predicts well" was measured against.

Submit before you leave

    1. Press Download my submission. You get a text file with your name, recorded runs and answers.
    2. On Canvas, open Section 2 lab and upload the file. (Or press Copy and paste it into the text box.)
    3. Do this before section ends. Missing items? Submit what you have; see the assignment page.
    Preview
    
        

    Going further (optional, not graded)

    The whole neural network on this page is about 100 lines of plain JavaScript, in nn.js. Save it into a folder, open that folder in your coding agent, and ask:

    Ask your agent
    This folder has nn.js, a small neural network used in my political science section. I'm not a programmer. Walk me through the step() function in plain language: where is the error measured, where does "change every weight a little" happen, and what does the learning rate do? Point to line numbers. Don't change the file.

    Remember last week's rule: the agent's explanation is a claim, not proof. If something sounds off, ask it to show you the line.

    Words you'll need

    Training data
    The examples the model learns from.
    Test data
    Examples held back to check the model on cases it has never seen.
    Label
    The answer attached to an example (passed / failed, fraud / not fraud). Someone, or some process, produced it.
    Loss
    The model's error, as one number. Lower is better. Here: cross-entropy, which punishes confident wrong answers hardest.
    Accuracy
    The share of examples the model gets right.
    Weight
    A number inside the network that it adjusts while learning.
    Epoch
    One pass through all the training examples.
    Overfitting
    Fitting the training data so closely, noise included, that the model does worse on new data.
    Proxy
    An input that stands in for another one, like postcode standing in for citizenship.