| 26 | |
| 27 | |
| 28 | class Variable: |
| 29 | def __init__(self, value: torch.Tensor, name: str = None): |
| 30 | self.value = value |
| 31 | self.name = name or fresh_name() |
| 32 | |
| 33 | # We need to start with some tensors whose values were not computed |
| 34 | # inside the autograd. This function constructs leaf nodes. |
| 35 | @staticmethod |
| 36 | def constant(value: torch.Tensor, name: str = None): |
| 37 | return Variable(value, name) |
| 38 | |
| 39 | def __repr__(self): |
| 40 | return repr(self.value) |
| 41 | |
| 42 | # This performs a pointwise multiplication of a Variable, tracking gradients |
| 43 | def __mul__(self, rhs: "Variable") -> "Variable": |
| 44 | # defined later in the notebook |
| 45 | return operator_mul(self, rhs) |
| 46 | |
| 47 | def __add__(self, rhs: "Variable") -> "Variable": |
| 48 | return operator_add(self, rhs) |
| 49 | |
| 50 | def sum(self, name: Optional[str] = None) -> "Variable": |
| 51 | return operator_sum(self, name) |
| 52 | |
| 53 | def expand(self, sizes: List[int]) -> "Variable": |
| 54 | return operator_expand(self, sizes) |
| 55 | |
| 56 | |
| 57 | class TapeEntry(NamedTuple): |
no outgoing calls
searching dependent graphs…