# Patch-Font: Enhancing Few-Shot Font Generation with Patch-Based Attention and Multitask Encoding
## 4 Training
### Patch-Based Triplet Margin Loss with Hard Positives and Negatives  
Triplet loss was initially presented in [27; 28]. It requires three images to work: an anchor image, a positive image that belongs to the same class that an anchor image belongs to, and a negative image that has a different class compared to the anchor class. We employed triplet margin loss on patches extracted from font images. This loss is pivotal in refining the local style representation by enforcing that an anchor patch that is extracted from the reference image remains closer in the embedding space to a positive patch than to a negative patch, separated by a defined margin. In our implementation, special consideration is given to the selection of anchor positives and anchor negatives to enhance the learning process. These patches share the same style as the anchor but differ in character content. As shown in Figure 2, the green bounded image is the anchor image with character content \(\mathcal{Y}\), and the image with the blue box is the anchor positive with character \(\mathcal{C}\). Contrary to this, an anchor negative image bounded in an orange box has the same character content \(\mathcal{Y}\) but belongs to a different style class. By utilizing hard positives, we ensure that the model learns to capture the subtleties of style across different characters within the same font family. Similarly, hard negatives compel the model to discern and emphasize style-specific features distinct from character content. The selection of hard positives and hard negatives enhances the learning process, ensuring that the model learns to capture the subtleties of style across different characters within the same font family in the embedding space and separate style-specific features distinct from character content. The triplet loss function, denoted as \(L_{\text{triplet}}^{(i)}\), is a critical component in learning embeddings or features. It ensures that an anchor patch \(a_{i}\) is closer to a positive patch \(p_{i}\)--which shares similarity with \(a_{i}\)--than to a negative patch \(n_{i}\), which is dissimilar, by at least a predefined margin in the learned feature space. This loss function is applied to each \(i\)-th set of patches, indexing over all available triplets in the training dataset. Formally, the distances \(d(a_{i},p_{i})\) and \(d(a_{i},n_{i})\) measure the dissimilarity between the anchor patch \(a_{i}\) and the positive patch \(p_{i}\), and between the anchor patch \(a_{i}\) and the negative  
Figure 2: Illustration of patch extraction for triplet loss. The anchor patch (green boundary) contains the character \(\mathcal{Y}\), establishing the reference style. The positive patch (blue boundary) comes from the character \(\mathcal{C}\), matching the anchor’s style but varying in content. However, the negative patch (orange boundary) has the character \(\mathcal{Y}\) from a different font style class. This visualization underscores the mechanism of selecting challenging positives and negatives to enhance the embedding space’s discrimination of style from content. patch \(n_{i}\), respectively. The objective of the triplet loss is to minimize the distance between similar patches while maximizing the distance between dissimilar patches, according to the following formulation:  
\[\mathcal{L}_{triplet}^{(l)}=\max\{d(a_{i},p_{i})-d(a_{i},n_{i})+\text{margin},0\} \tag{8}\]  
The max function ensures that the loss is non-negative, enforcing the constraint that the distance between the anchor and the positive patch plus a margin should be less than or equal to the distance between the anchor and the negative patch. If this condition is met, indicating that the positive patch is sufficiently closer to the anchor patch compared to the negative patch by at least the margin, the loss becomes zero. This condition signifies that no further adjustments are necessary for the embeddings of these patches to satisfy the desired proximity constraint.