MCPcopy Create free account
hub / github.com/PolymathicAI/AstroCLIP / CLIPLoss

Class CLIPLoss

astroclip/models/astroclip.py:117–154  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

115
116
117class CLIPLoss(nn.Module):
118 def get_logits(
119 self,
120 image_features: torch.FloatTensor,
121 spectrum_features: torch.FloatTensor,
122 logit_scale: float,
123 ) -> Tuple[torch.FloatTensor, torch.FloatTensor]:
124 # Normalize image features
125 image_features = F.normalize(image_features, dim=-1, eps=1e-3)
126
127 # Normalize spectrum features
128 spectrum_features = F.normalize(spectrum_features, dim=-1, eps=1e-3)
129
130 # Calculate the logits for the image and spectrum features
131 logits_per_image = logit_scale * image_features @ spectrum_features.T
132 return logits_per_image, logits_per_image.T
133
134 def forward(
135 self,
136 image_features: torch.FloatTensor,
137 spectrum_features: torch.FloatTensor,
138 logit_scale: float,
139 output_dict: bool = False,
140 ) -> torch.FloatTensor:
141 # Get the logits for the image and spectrum features
142 logits_per_image, logits_per_spectrum = self.get_logits(
143 image_features, spectrum_features, logit_scale
144 )
145
146 # Calculate the contrastive loss
147 labels = torch.arange(
148 logits_per_image.shape[0], device=image_features.device, dtype=torch.long
149 )
150 total_loss = (
151 F.cross_entropy(logits_per_image, labels)
152 + F.cross_entropy(logits_per_spectrum, labels)
153 ) / 2
154 return {"contrastive_loss": total_loss} if output_dict else total_loss
155
156
157class ImageHead(nn.Module):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected