[Python] The output changes when changing print function's argument

Discussão em 'Python' iniciado por Stack, Abril 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have this class that helps me calculate gradient during backpropagation.

    import time
    visited = set()
    class Value:
    def __init__(self, data, _children=None, _op=''):
    self.data = data
    self.grad = 0.0
    self._backward = lambda: None
    self._prev = set(_children) if _children else set()
    self._op = _op

    def __add__(self, other):
    other = other if isinstance(other, Value) else Value(other)
    out = Value(self.data + other.data, (self, other), '+')

    def _backward():
    self.grad += 1.0 * out.grad
    other.grad += 1.0 * out.grad
    out._backward = _backward

    return out

    def __mul__(self, other):
    other = other if isinstance(other, Value) else Value(other)
    out = Value(self.data * other.data, (self, other), '*')

    def _backward():
    self.grad += other.data * out.grad
    other.grad += self.data * out.grad
    out._backward = _backward
    return out

    def backward(self, output=False):
    # if I change 'o' to 'oo' the a.grad changes it's value
    # print('o')
    # use time.sleep() instead of print
    time.sleep(0.1)
    if output:
    self.grad = 1.0
    self._backward()
    for child in self._prev:
    if child not in visited:
    visited.add(child)
    child.backward()

    def __repr__(self):
    return f"Value(data={self.data}, grad={self.grad})"

    a = Value(2.0)
    b = Value(3.0)
    c = a + b
    d = a * b
    e = c + d
    g = c * d
    f = e + g

    f.backward(True)
    print(f"a grad: {a.grad}")


    In function backward() if I change the print's argument the output for a.grad will change too.

    For print('o') output will be :

    o
    o
    o
    o
    o
    o
    o
    a grad: 21.0


    For print('oo') output will be:

    oo
    oo
    oo
    oo
    oo
    oo
    oo
    a grad: 4.0


    So a.grad change from 21.0 -> 4.0. Why is that?

    Update

    It seems that it's easier to reproduce using time.sleep(). I have updated code from print() to -> time.sleep(0.1)

    With time.sleep(0.1):


    a.grad = 21.0
    ​

    Without time.sleep(0.1):


    a.grad = 4.0
    ​

    P.S. I know that backward() doesn't implement backpropagation correctly.

    Continue reading...

Compartilhe esta Página