Frames Between Two Sketches

Learning to CompressEasy+10 XP+10

Frames Between Two Sketches

An autoencoder has been trained on small black-and-white sketches. Encode a sketch of a circle and you get one code; encode a sketch of a square and you get another. The lesson's experiment is to mix those two codes by a sliding amount and decode every mixture, producing a strip of frames that morphs from one sketch into the other.

The decoder here is a single layer. It multiplies the code by a weight matrix, adds a bias, and replaces every negative result with 000:

outi=max⁡(0,  ∑jWij zj+bi)\text{out}_i = \max\Big(0,\; \sum_j W_{ij}\, z_j + b_i\Big)outi​=max(0,∑j​Wij​zj​+bi​)

Task: write latent_walk(code_a, code_b, weights, bias, points), returning the decoded frames along the walk as a list of lists, every value rounded to 4 decimal places.

  • points is the number of frames, at least 222. The mixing amounts are evenly spaced from 000 to 111 inclusive, so the first frame is the decoding of code_a and the last is the decoding of code_b.
  • At mixing amount ttt the code is z=(1−t) a+t bz = (1 - t)\,a + t\,bz=(1−t)a+tb, entry by entry, where aaa is code_a and bbb is code_b.
  • weights has one row per output value and one column per code entry; bias has one entry per output value.

Worked through, latent_walk([1.0], [3.0], [[1.0], [-1.0]], [0.0, 2.0], 3):

kkktttcodebefore clippingreturned frame
0000[1][1][1][1, 1][1,\ 1][1, 1][1.0, 1.0]
10.50.50.5[2][2][2][2, 0][2,\ 0][2, 0][2.0, 0.0]
2111[3][3][3][3, −1][3,\ -1][3, −1][3.0, 0.0]

so the function returns [[1.0, 1.0], [2.0, 0.0], [3.0, 0.0]].

Only the two end frames correspond to sketches anyone actually drew. Every frame in between comes from a code no training input was ever encoded to, which is exactly why a walk like this is the standard way to find out what a decoder learned.

A
B
0.50

Encode two inputs, mix their codes, decode the mixture. Every point along the way is a new output that was never in the training data.

Examples

code_a = [2.0, 0.0]
code_b = [0.0, 2.0]
weights = [[1.0, -1.0], [0.5, 0.5]]
bias = [0.0, 0.0]
points = 3
returns [[2.0, 1.0], [0.0, 1.0], [0.0, 1.0]]
code_a = [1.0, -1.0, 0.5]
code_b = [-1.0, 2.0, 0.5]
weights = [[1.0, 0.5, -1.0], [-0.5, 1.0, 2.0], [0.25, 0.25, 0.25]]
bias = [0.5, -1.0, 0.0]
points = 5
returns [[0.5, 0.0, 0.125], [0.375, 0.0, 0.1875], [0.25, 0.5, 0.25], [0.125, 1.5, 0.3125], [0.0, 2.5, 0.375]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run