MCPcopy Create free account
hub / github.com/computational-imaging/bacon / forward

Method forward

modules.py:616–646  ·  view source on GitHub ↗

r"""Apply positional encoding to the input. Args: tensor (torch.Tensor): Input tensor to be positionally encoded. encoding_size (optional, int): Number of encoding functions used to compute a positional encoding (default: 6). include_input

(self, tensor)

Source from the content-addressed store, hash-verified

614 self.normalization = torch.tensor(1/self.frequency_bands)
615
616 def forward(self, tensor) -> torch.Tensor:
617 r"""Apply positional encoding to the input.
618
619 Args:
620 tensor (torch.Tensor): Input tensor to be positionally encoded.
621 encoding_size (optional, int): Number of encoding functions used to compute
622 a positional encoding (default: 6).
623 include_input (optional, bool): Whether or not to include the input in the
624 positional encoding (default: True).
625
626 Returns:
627 (torch.Tensor): Positional encoding of the input tensor.
628 """
629
630 encoding = [tensor] if self.include_input else []
631 if self.gaussian_pe:
632 for func in [torch.sin, torch.cos]:
633 encoding.append(func(torch.matmul(tensor, self.gaussian_weights.T)))
634 else:
635 for idx, freq in enumerate(self.frequency_bands):
636 for func in [torch.sin, torch.cos]:
637 if self.normalization is not None:
638 encoding.append(self.normalization[idx]*func(tensor * freq))
639 else:
640 encoding.append(func(tensor * freq))
641
642 # Special case, for no positional encoding
643 if len(encoding) == 1:
644 return encoding[0]
645 else:
646 return torch.cat(encoding, dim=-1)
647
648
649def layer_factory(layer_type, w0=30):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected