BYOL uses two networks: an online network and a target network.
- Online (weights θ): three stages, an encoder fθ, a projection head gθ, and a predictor qθ. It is updated by gradients.
- Target (weights ξ): an encoder fξ and a projection head gξ with the same architecture as the online network, but without a predictor. It is not updated by gradients but by a moving average of the online weights.
One training step proceeds as follows.
- Apply random data augmentations t and t′ independently to an image x to create two views v=t(x) and v′=t′(x).
- Pass v through the online network to obtain the representation yθ=fθ(v), the projection zθ=gθ(yθ), and the prediction qθ(zθ).
- Pass v′ through the target network to obtain the projection zξ′=gξ(fξ(v′)). No gradient flows here (stop-gradient).
- Normalize the prediction qθ(zθ) and zξ′ to unit length, and take their squared distance as the loss Lθ,ξ.
- Swap v and v′, repeat the same computation, and add the loss L~θ,ξ (symmetrization).
- Using the combined loss, update only the online weights θ with the optimizer.
- Update the target weights ξ as a moving average with θ.
The loss in step 4 is the mean squared error between unit-length vectors and can be rewritten in terms of cosine similarity. qˉ and zˉ′ are qθ(zθ) and zξ′ normalized to unit length.
Lθ,ξ=∥qˉ−zˉ′∥22=2−2⋅∥qθ(zθ)∥2∥zξ′∥2⟨qθ(zθ),zξ′⟩
The moving average in step 7 is given by the following, where τ∈[0,1] is the target decay rate.
θ←optimizer(θ,∇θLθ,ξBYOL,η),ξ←τξ+(1−τ)θ
Here Lθ,ξBYOL=Lθ,ξ+L~θ,ξ and η is the learning rate. τ starts at τbase=0.996 at the beginning of training and is raised to 1 toward the end. With k the current step and K the total number of steps, τ=1−(1−τbase)(cos(πk/K)+1)/2.
The figure at the top illustrates this flow. The left side shows the online (solid) and target (dashed) paths, making clear that the predictor exists only in the online network and that gradients are stopped on the target side. The right side shows the loss computation, the target update rule, and a graph of the τ schedule drawn from the formula.
After training, keep only the encoder fθ and use the representation yθ (2048 dimensions after average pooling for ResNet-50) for downstream tasks. Discard the projection head, the predictor, and the target.