Decode a value using the FixedPoint Encoder. Args: value (Union[int, torch.Tensor]): Value to decode. Returns: torch.Tensor: Decoded tensor. Raises: ValueError: If value is a floating torch.Tensor.
(self, value: Union[int, torch.Tensor])
| 62 | return long_value |
| 63 | |
| 64 | def decode(self, value: Union[int, torch.Tensor]) -> torch.Tensor: |
| 65 | """Decode a value using the FixedPoint Encoder. |
| 66 | |
| 67 | Args: |
| 68 | value (Union[int, torch.Tensor]): Value to decode. |
| 69 | |
| 70 | Returns: |
| 71 | torch.Tensor: Decoded tensor. |
| 72 | |
| 73 | Raises: |
| 74 | ValueError: If value is a floating torch.Tensor. |
| 75 | """ |
| 76 | if isinstance(value, torch.Tensor) and value.dtype.is_floating_point: |
| 77 | raise ValueError(f"{value} should be converted to long format") |
| 78 | |
| 79 | if isinstance(value, int): |
| 80 | value = torch.LongTensor([value]) |
| 81 | |
| 82 | tensor = value |
| 83 | if self._precision == 0: |
| 84 | return tensor |
| 85 | |
| 86 | correction = (tensor < 0).long() |
| 87 | dividend = tensor // self._scale - correction |
| 88 | remainder = tensor % self._scale |
| 89 | remainder += (remainder == 0).long() * self._scale * correction |
| 90 | |
| 91 | tensor = dividend.float() + remainder.float() / self._scale |
| 92 | return tensor |
| 93 | |
| 94 | @property |
| 95 | def precision(self): |
no outgoing calls