Skip to content

← Production ML — Training Stack step 7 of 12

Easy End-to-End

Model Checkpointing

A checkpoint records state that you can load later. In this exercise, serialize a small snapshot containing a weight vector, a training-step counter and a learning rate. Return the JSON text; do not write a file.

Your task

Implement serialize_state(weights, step, lr):

  • weights is a one-dimensional floating-point tensor of shape (d,).
  • step is a nonnegative integer.
  • lr is a finite, nonnegative learning rate.

Weights contain finite values. Convert them to a Python list of numbers, preserving their order and values. Build a dictionary with these keys in this order:

weights: the list of weights
step: a Python int
lr: a Python float

Use the standard-library json module to serialize that dictionary and return a Python string. Preserve the input tensor. Do not use torch.save, pickle, a tensor's printed representation or manual string concatenation.

Exact output format

This exercise compares the returned string exactly. Use the default json.dumps formatting, with spaces after commas and colons, no indentation, and no sorted keys. Do not round the weight values or append a newline.

For weights = [1.0, -0.5], step = 12 and lr = 0.01, return:

{"weights": [1.0, -0.5], "step": 12, "lr": 0.01}

The return value is the JSON string itself, not the dictionary and not a second JSON encoding of that string. JSON objects can generally use other key orders or spacing, but those alternatives are outside this exercise's exact-string contract.

What this snapshot preserves

json.loads can recover the list, step and learning rate. Reconstructing a tensor also requires choosing a framework, dtype and device; none of those is recorded in this format. Decimal output may show the precision of the stored tensor values, so serialize the values you have rather than rounding them to their originally typed spelling.

This is not a complete training-resumption checkpoint. Depending on the training setup, resuming the same computation can also require optimizer buffers, scheduler state, random-number generator state and the position in the data stream. Saving just weights, step and learning rate does not preserve those details.