| 145 | |
| 146 | |
| 147 | def operator_add(self: Variable, rhs: Variable) -> Variable: |
| 148 | # Add follows a similar pattern to Mul, but it doesn't end up |
| 149 | # capturing any variables. |
| 150 | r = Variable(self.value + rhs.value) |
| 151 | # print(f'{r.name} = {self.name} + {rhs.name}') |
| 152 | |
| 153 | def propagate(dL_doutputs: List[Variable]): |
| 154 | (dL_dr,) = dL_doutputs |
| 155 | dr_dself = 1.0 |
| 156 | dr_drhs = 1.0 |
| 157 | dL_dself = dL_dr * dr_dself |
| 158 | dL_drhs = dL_dr * dr_drhs |
| 159 | return [dL_dself, dL_drhs] |
| 160 | |
| 161 | gradient_tape.append( |
| 162 | TapeEntry(inputs=[self.name, rhs.name], outputs=[r.name], propagate=propagate) |
| 163 | ) |
| 164 | return r |
| 165 | |
| 166 | |
| 167 | def operator_sum(self: Variable, name: Optional[str]) -> "Variable": |