bowang-lab / BLEEP

Spatially Resolved Gene Expression Prediction from H&E Histology Images via Bi-modal Contrastive Learning
Apache License 2.0
63 stars 10 forks source link

temperature in logits vs targets #5

Closed EMCarrami closed 8 months ago

EMCarrami commented 10 months ago

Hi,

Below self.temperature is used in two different manners. While logits are divided by it, targets are multiplied by this value. Is this intentional or is it a mathematical mistake?

Thanks.

        # Calculating the Loss
        logits = (spot_embeddings @ image_embeddings.T) / self.temperature
        images_similarity = image_embeddings @ image_embeddings.T
        spots_similarity = spot_embeddings @ spot_embeddings.T
        targets = F.softmax(
            (images_similarity + spots_similarity) / 2 * self.temperature, dim=-1
        )
        spots_loss = cross_entropy(logits, targets, reduction='none')
        images_loss = cross_entropy(logits.T, targets.T, reduction='none')
        loss =  (images_loss + spots_loss) / 2.0 # shape: (batch_size)
Xrioen commented 8 months ago

Yes this should be a mistake. Good catch. I have now fixed the code. Luckily this shouldn't influence results since temperature was set to 1 for all experiments. Thanks for this.