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

Class TiedLinear

astroclip/modules.py:296–338  ·  view source on GitHub ↗

A dense linear layer whose parameters are tied to a tensor provided by the user. Using this layer is equivalent to using the functional form, :func:`~torch.nn.functional.linear`. The utility of having a module is that it will show up in module summaries, which can help to make the struc

Source from the content-addressed store, hash-verified

294
295
296class TiedLinear(nn.Module):
297 """A dense linear layer whose parameters are tied to a tensor provided by the user.
298
299 Using this layer is equivalent to using the functional form,
300 :func:`~torch.nn.functional.linear`. The utility of having a module is that it will
301 show up in module summaries, which can help to make the structure of the model more
302 transparent.
303
304 :param weight: weight tensor
305 :param bias: bias tensor; if not provided, there will be no bias
306 """
307
308 in_features: int
309 """size of each input sample."""
310
311 out_features: int
312 """size of each output sample."""
313
314 def __init__(
315 self,
316 weight: Union[torch.Tensor, nn.Parameter],
317 bias: Union[None, torch.Tensor, nn.Parameter],
318 ):
319 super().__init__()
320
321 if weight.ndim != 2:
322 raise ValueError(
323 f"weight parameter has {weight.ndim} dimensions, should have 2"
324 )
325 self.out_features, self.in_features = weight.shape
326
327 self.register_buffer("weight", weight)
328 self.register_buffer("bias", bias)
329
330 def forward(self, x: torch.Tensor) -> torch.Tensor:
331 return F.linear(x, self.weight, self.bias)
332
333 def extra_repr(self) -> str:
334 return (
335 f"in_features={self.in_features}, "
336 f"out_features={self.out_features}, "
337 f"bias={self.bias is not None}"
338 )
339
340
341def _init_by_depth(module: nn.Module, depth: int) -> None:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected