The iteration order here is not deterministic. You should use `sorted(inputs_to_layers)`, perhaps.