Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Homework Deep Learning: a detector on real seismograms (75 points)

In Chapter 4 every method was taught on synthetic data, where the truth is known and nothing else gets in the way. This homework takes the same pipeline to real records.

The data. miniPNW is a labeled subset of the Pacific Northwest AI-ready seismic benchmark (Ni et al., 2023) that you met in 2.11. Each record is three components of ground velocity (north, east, vertical) at one station of the Pacific Northwest Seismic Network (PNSN), labeled by its analysts as an earthquake, an explosion (mostly quarry blasts), a surface event (rockfall, avalanche, debris flow), a sonic boom or thunder.

The task. A binary detector: from 30 s of three-component ground motion, decide whether the window holds an event (any of the five sources) or no event (background noise only). Detection is the first step of every earthquake catalog.

What you do.

PartPointsWhat
110Look at the data
215Split without leakage, and two baselines
320Write the training loop and train a convolutional network (CNN)
420Three training decisions of notebook 4.7, tested on real data
57The test set, used once
63Reflection and AI-use disclosure

One network trains in about 20 s on a laptop CPU. Part 4 trains about fifteen of them: start it early.

0. Setup

Download the data at home (671 MB). From the top of your mlgeo-book folder:

mkdir -p book/Chapter2-DataManipulation/data
cd book/Chapter2-DataManipulation/data
BASE=https://dasway.ess.washington.edu/shared/niyiyu/PNW-ML
curl -fL -C - -O $BASE/miniPNW_metadata.csv
curl -fL -C - -O $BASE/miniPNW_waveforms.hdf5

The cell below cuts two 30 s windows from each record. The event window (label 1) starts 5 s before the analyst’s P pick. The no event window (label 0) is the 30 s just before it, at the same station. Both are low-pass filtered and kept at 25 samples per second. The windows are saved in a 30 MB file, so the next run takes a second.

Scaling. Each window is divided by the standard deviation of its own 30 s. This uses no other window, so it cannot leak anything between the training and test sets. It also hides the absolute loudness from the network (part 2.4 says why that matters).

1. Look at the data (10 points)

1.1 Counts (3 points)

Task: print the number of records of each source type, the number of distinct stations (station_code) and events (event_id), and the number of events recorded at more than one station. Use meta, with value_counts() and nunique().

The map of sources and stations, for context.

1.2 One record, both windows (4 points)

Task: pick one earthquake record. Plot its event window (X_all[i]) and its noise window (N_all[i]), the three components on three rows, time in seconds on the x-axis (t). Mark the P pick, 5 s into the event window, with a vertical line.

1.3 Real against synthetic (3 points)

Task: name three ways these records differ from the synthetic seismograms of notebooks 4.3 and 4.7, and say for each one how it could make the detector harder (or easier) to train.

2. Split without leakage, and two baselines (15 points)

2.1 A split grouped by event (5 points)

The event window and the noise window of one record come from the same station minutes apart, and one earthquake is often recorded at several stations. All windows of one event must go to the same set.

Task: write split_by_group(groups, seed=SEED, val=0.15, test=0.15). It returns the indices of the training, validation and test windows such that no group appears in two of them. Use GroupShuffleSplit twice: once to set aside the test windows, once to split the rest into training and validation. Then split with groups_event and print how many windows each set has.

2.2 Why group (3 points)

Task: answer in three sentences. (1) What would a split of windows drawn at random let the network see during training that it should not? (2) A split grouped by station is stricter. What question would it answer that the split by event does not? (3) Which of the two splits fits a detector meant to run on a new station?

2.3 Two baselines (5 points)

A score means something only next to a model without learning and a classical model on the same split.

Task: on the validation windows, compute the balanced accuracy of (1) a model that always answers “no event”, and (2) a LogisticRegression(max_iter=2000) trained on rms_table(Xs), the log RMS amplitude of each component in each second (90 numbers per window). The balanced accuracy is the mean of the fraction of events found and the fraction of noise windows called noise (balanced_accuracy_score).

2.4 The loudness shortcut (2 points)

Task: train a logistic regression on one number per window, the log of its standard deviation in raw counts (np.concatenate([X_all, N_all]), before scaling), and report its validation balanced accuracy. In one sentence: why would a detector that learned “loud means event” fail on a continuous record?

3. Train a convolutional network (20 points)

The network is given: three blocks of convolution, ReLU and max pooling, then an average over time and one linear layer that gives two scores, one per class (notebook 4.3). build draws the starting weights at random with a seed. make_fcn is a fully connected network on the 90 RMS numbers, for part 4.

3.1 The loss before training (2 points)

Task: compute the cross-entropy loss (nn.CrossEntropyLoss()) of the untrained model on the first 64 training windows. A network that gives both classes a probability of 1/2 has a loss of ln⁡2=0.69\ln 2 = 0.69. Is your untrained network close to that? In one sentence, why is it not exactly ln⁡2\ln 2?

3.2 The training loop (10 points)

Task: write train(model, idx_tr, idx_va, lr=1e-3, batch=64, epochs=8, seed=0, loss_fn=None, augment=None). It returns a dictionary hist with three lists, one value per epoch: train_loss, val_loss, val_score.

  • loss_fn defaults to nn.CrossEntropyLoss(); the optimizer is torch.optim.Adam with learning rate lr.

  • Each epoch: a new random order of the training windows (torch.randperm with a torch.Generator seeded by seed), batches of batch windows, and for each batch the five steps: scores, loss, zero_grad, backward, step.

  • If augment is given, apply it to each batch before the scores (part 4).

  • After each epoch: the validation loss with model.eval() and torch.no_grad(), the validation balanced accuracy with score, and model.train() again.

  • Keep a copy of the weights of the epoch with the lowest validation loss (model.state_dict()), and load them back at the end.

3.3 Train, and plot the learning curve (4 points)

Task: train build("cnn", seed=0) for 8 epochs. Plot the training and validation loss against the epoch on one panel, and the validation balanced accuracy on a second panel, with the two baselines of 2.3 as horizontal lines.

3.4 Read the curve (4 points)

Task: in two or three sentences. Is the network still learning at epoch 8, or has it started to fit the training windows rather than the classes? Which epoch did train keep? Is the CNN better than the logistic regression by more than you would expect from a change of seed (try seed=1 once)?

4. Three training decisions (20 points)

Notebook 4.7 tested each decision of a modeler on synthetic data. Pick three of the six below and test them on miniPNW. For each one:

  1. Predict the result before you run anything, in one sentence (1 point).

  2. Measure: one controlled experiment that changes only that decision, at least two seeds per setting, scored on the validation set. Show a table or a figure (3 points).

  3. Write a rule in the format of the class rubric (4.7): what to do, how someone else checks it, and your number as evidence (2 points).

DecisionOne experiment
AStarting weightsdefault random weights, all zeros, random × 10
BLearning rate and batch sizelearning rates 1e-4, 1e-3, 1e-2 with batches of 16 and 256
CModel size and amount of datawidths 4, 16, 32 with 25%, 50% and 100% of the training windows
DConvolutional or fully connectedbuild("cnn") against build("fcn") on the RMS table
EAugmentationrandom shifts of ±10 s and random sign; score on validation windows shifted with shift_records
FThe loss when events are rarekeep one event window in ten in the training set; cross-entropy, weighted cross-entropy, focal loss; report the fraction of events found and of false alarms

Write each experiment in its own cell below.

4.1 First decision (6 points)

Prediction:

Rule (what to do / how to check / evidence):

4.2 Second decision (6 points)

Prediction:

Rule (what to do / how to check / evidence):

4.3 Third decision (6 points)

Prediction:

Rule (what to do / how to check / evidence):

4.4 Signal or seed? (2 points)

Task: for each of your three experiments, is the difference you measured larger than the difference between two seeds of the same setting? One sentence each.

5. The test set, used once (7 points)

5.1 Final model (3 points)

Task: choose your final settings from the validation results of parts 3 and 4, and state them. Train that model, then compute its balanced accuracy on the test windows with a 95% bootstrap confidence interval (bootstrap_ci below). Do the same for the logistic regression of 2.3, on the same test windows.

5.2 Which events are missed (2 points)

Task: on the test event windows only, print the fraction found by your final model for each source type (win.loc[idx, "source_type"]), with the number of windows of each type.

5.3 Verdict (2 points)

Task: in two or three sentences: does the CNN beat the logistic regression on the test set, given the two intervals? Which source type is missed most often, and what about its signal (frequency, duration, amplitude, the way its window was cut) could explain it?

6. Reflection and AI-use disclosure (3 points)

Reflection (two or three sentences each, course policy):

  1. What did you learn from this homework? Which result on real data differed most from what notebook 4.7 showed on synthetic data?

  2. What did you learn from interacting with agents or chatbots?

  3. What would you change in this homework so it meets its learning goals better?

Disclosure (1.8, 6.4): one row per tool, the task it did, and what you verified. If you used no assistant, write one row saying so. The training loop of 3.2 was 🔒 by hand and must not appear as an assistant task.

ToolTaskWhat I verified
References
  1. Ni, Y., Hutko, A., Skene, F., Denolle, M., Malone, S., Bodin, P., Hartog, R., & Wright, A. (2023). Curated Pacific Northwest AI-ready Seismic Dataset. Seismica, 2(1). 10.26443/seismica.v2i1.368