We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Recurrent Networks step 5 of 6
Sequence Classifier
Classify one token sequence by looking up embeddings, updating a recurrent hidden state and projecting the final state to class scores.
Your task
Implement sequence_classifier(indices, embedding_table, W_xh, W_hh, b_h, h0, W_out, b_out) using the supplied parameters.
- indices has shape
(seq_len,)and contains valid zero-based token IDs. The runtime delivers integer-valued floats; cast them to integers for lookup. - embedding_table has shape
(vocab_size, embed_dim). - W_xh has shape
(embed_dim, hidden_dim). - W_hh has shape
(hidden_dim, hidden_dim). - b_h and h0 each have shape
(hidden_dim,). - W_out has shape
(hidden_dim, num_classes); b_out has shape(num_classes,).
The floating-point parameters share a dtype and device. Return logits of shape
(num_classes,) with that dtype and device. Leave all inputs unchanged.
There is one sequence, not a batch, and no padding mask: every supplied token
is processed, including repeated IDs and ID 0.
Carry the hidden state forward
Start with h=h0. For each token in order, look up its embedding e and compute:
h_next = tanh(e @ W_xh + h @ W_hh + b_h)
h = h_next
Use the previous h for the whole update, then pass the new vector to the next token. The weights use row-vector orientation as written above. Do not reset h between tokens, apply tanh separately to the two products or average hidden states.
After the last token, compute logits = h @ W_out + b_out. Return these raw
class scores, not softmax probabilities or an argmax class ID. With an empty
sequence, project h0 directly. Do not apply tanh to h0 before that projection.
For a one-dimensional hidden state, embedding value 1, W_xh=1, W_hh=0.5, b_h=0 and h0=0, two repeated tokens give:
h after token 1 = tanh(1) = about 0.761594
h after token 2 = tanh(1 + 0.5*0.761594) = about 0.881130
With W_out=2 and b_out=0, the single output logit is approximately 1.762259. Repetition still matters because the second update receives a different state.
The diagram below illustrates carrying a state through a sequence. Its scalar decay rule is an analogy, not the RNN equation or the numbers in this example.
Loading visualization…
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Update a state as you read a sequence
A recurrent neural network carries a hidden state from one time step to the next. A basic cell computes
$$h_t=\tanh(x_tW_x+h_{t-1}W_h+b).$$
x_t is the current input and h_(t-1) is the previous state. The weights and
bias are shared across time steps. The hidden state changes with the input;
it is not a separate learned parameter at each step.
Even when the input and previous state are zero, a nonzero bias can produce a nonzero new state. For sequence classification, a classifier can read the final state. For token labeling or language modeling, it may need an output at every step instead.
Gates control how information is carried forward
An LSTM maintains a cell state as well as a hidden state. Input, forget, and output gates control how information enters, remains in, and is read from that cell state. A GRU uses update and reset gates with a candidate hidden state.
Follow the given equations for gate order and placement. In particular, applying a reset gate before a matrix multiplication is generally different from applying it afterward.
A bidirectional RNN reads the sequence in both directions. Align the two states with their original token positions before concatenating them. The reverse recurrence can use later tokens, so its representation cannot be used unchanged for a causal next-token predictor.
Add gradient contributions across time
Backpropagation through time treats the recurrence as repeated uses of the same cell. Each use of a weight contributes to that weight's total gradient. A hidden state can receive gradient from its current output and from later states.
Repeated multiplication by derivatives can shrink or amplify gradients. Gradient clipping bounds the gradient passed to the optimizer; the resulting parameter step also depends on the optimizer and its state. Gates provide other paths for information and gradients, but do not replace correct handling of padding, initial states, and sequence lengths.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
PyTorch reference solution
Sign in to track your attempts toward unlocking the reference solution.