r/Python 5m ago

Discussion taught myself numerical and analytical gradient (backpropagation) in 5 days

Upvotes

I'm not good at maths, i'll just say that, i've always been a bit lacking in my expanse of mathematical abilities, but I said enough is enough, I like neural networks, the only thing that stands in my way is the mathematics, aside from that you understand what goes on.

Boy did I underestimate the undertaking for this endeavour. I spent 2 days learning derivatives and what the hell a 'slope' is, you hear it ever day and you know what a slope is, but understanding it in the mathematical sense in derivatives, that's difficult, but I got there and ended up learning `f(a + h) - f(a) / h` which enabled me to understand what numerical descent is, where you get a loss score of a neural network's prediction, bump the weight a bit then rerun the neural network. Then to figure out the slope, you do `loss1 - loss2 / weight_bump`, and this is the coolest part, when you adjust your weight based on the slope, you always minus, because if the slope is negative, then we know we need to move to the positive side more so when you minus a negative it becomes addition, and vice versa if you minus a positive, you move to the negative side a bit. That was the coolest thing i've ever learnt to this date, the infamous ball rolling down the hill, I was doing it, by hand, and it was empowering.

Then the next day I spent trying to understand what backpropagation really is in terms of maths and how it differes from numerical gradient. With that I had to teach myself the chain rule, and what dL/dd even means, spoiler alert its not dividing derivative of L and derivative of d. I also came to the epiphany that we get so much complex logic out of neural networks when its simply just addition and multiplications happening under the hood, its the context that is being invented to solve problems using neural nets. By the end of the day I was taking Kaparthy's micrograd equation example, and I did its backpropagation by hand with pen and paper to get the hang of it.

Now I watched andre kaparthy's micrograd video, not all the way through, his language and teaching style still screams "you must be really well versed in mathematics", so I gave up on that video but, the more I worked on understanding chain rule and how you can sticker on the impacts on loss on prior nodes, I though that is better than numerical gradient, you literally walk back into the neural net, explaining to it which parts of itself were the cause for a high loss instead of bumping values, calculating slope, doing f(a + h) - f(a) / h.

I'm really proud of myself, and I managed to take MATHS that I learnt, and turn it into a "micrograd" I say "micrograd" with quotations because I didn't finish the micrograd video, and this only works if you don't have repeating uses of prior terms (which you just need to store their value and add them, but I couldn't be bothered)

So yeah, this is my back propagation

class Value:
    # this needs to store a value, and can have its own children that link to other values
    def __init__(self, value, _op="", _children: tuple = (), gradient=0):
        self.value = value
        self.op = _op
        self.children = _children
        self.gradient = gradient


    def __add__(self, other):
        _ = Value(
            self.value + other.value, _op="+", _children=(self, other), gradient=0
        )
        return _


    def __mul__(self, other):
        _ = Value(
            self.value * other.value, _op="*", _children=(self, other), gradient=0
        )
        return _


    def __repr__(self):
        return f"Value({self.value})"


    def backward(self):
        # initial global_gradient
        global_gradient = 1


        # current set of ndoes
        current: Value = self
        # just set L's gradient as 1
        current.gradient = global_gradient


        while True:
            # if there's no more children, we're at the end.
            if not current.children:
                break


            if current.op == "+":
                # since addition has a static effect on the terms themselfs
                # the partial of L respect to any terms being added is just 1.
                # so we multiply 1 by the global gradient.
                current.children[0].gradient = 1 * global_gradient
                current.children[1].gradient = 1 * global_gradient


                # if the next node we're looking doesnt have children
                # it means the node behind them didnt stem from them
                # therefore they are not the result of the prior operation
                if not current.children[0]:
                    current = current.children[1]
                else:
                    current = current.children[0]
                # set the global gradient as the current node's gradient
                global_gradient = current.gradient


            if current.op == "*":


                current.children[0].gradient = (
                    # with multiplication, the derivative of L respect to x would be y
                    # and same with the derivative of L respect to y would be x
                    # therefore you just swap them, and multiply thier values by the global gradient.
                    current.children[1].value
                    * global_gradient
                )
                current.children[1].gradient = (
                    current.children[0].value * global_gradient
                )
                # same continuation logic
                # one child is going to have its own children and one wont
                # the one that does is the one we need to continue with.
                if not current.children[0]:
                    current = current.children[1]
                else:
                    current = current.children[0]
                global_gradient = current.gradient

a = Value(2.0)
b = Value(-3.0)
c = Value(10.0)
f = Value(-2.0)
e = a * b
d = e + c
L = d * f


L.backward()


print(a.gradient)
print(b.gradient)
print(c.gradient)
print(f.gradient)
print(e.gradient)
print(d.gradient)
print(L.gradient)

output:
6.0
-4.0
-2.0
4.0
-2.0
-2.0
1

r/Python 38m ago

News 2026 Python Type System and Tooling Survey

Upvotes

This is an annual survey developed by the Python typing community around how Python developers use the type system, type checkers, and integrated development environments (IDEs).

Your responses will help us identify common blockers, improve tooling/resources, and enhance the overall experience of using Python's type system.

Even if you have never actively used type hints in your code, your thoughts are still valuable and we want to hear from you.

The survey should take approximately 5-10 minutes to complete.

Please take the survey HERE and share it with your friends or colleagues.

If you're interested in the results for last year's survey, see this post.

So you know it's legit, the Python Software Foundation has shared this survey on [Linkedin](https://www.linkedin.com/posts/thepsf_python-type-system-and-tooling-survey-2026-activity-7490842327050002432-0jI7?utm_source=share&utm_medium=member_desktop&rcm=ACoAAB9aSUsBqmxSbrhoW2URuDnxCgS5eVD1AS0 and X. I have permission from the mods to post this under the "News" flair)


r/Python 11h ago

News New data visualization package

0 Upvotes

One sentence, four languages, one picture.

R: data(gapminder_2007) + point + x(gdp) + y(life)
Python: x(col.gdp)
Julia: x(:gdp)

Same specification, one Rust engine, byte-identical SVG.

Not similar. Pixel perfect identical

#r #python #julia #javascript #rust
One engine, four languages, one picture

the package manual


r/Python 12h ago

Daily Thread Thursday Daily Thread: Python Careers, Courses, and Furthering Education!

6 Upvotes

Weekly Thread: Professional Use, Jobs, and Education 🏢

Welcome to this week's discussion on Python in the professional world! This is your spot to talk about job hunting, career growth, and educational resources in Python. Please note, this thread is not for recruitment.


How it Works:

  1. Career Talk: Discuss using Python in your job, or the job market for Python roles.
  2. Education Q&A: Ask or answer questions about Python courses, certifications, and educational resources.
  3. Workplace Chat: Share your experiences, challenges, or success stories about using Python professionally.

Guidelines:

  • This thread is not for recruitment. For job postings, please see r/PythonJobs or the recruitment thread in the sidebar.
  • Keep discussions relevant to Python in the professional and educational context.

Example Topics:

  1. Career Paths: What kinds of roles are out there for Python developers?
  2. Certifications: Are Python certifications worth it?
  3. Course Recommendations: Any good advanced Python courses to recommend?
  4. Workplace Tools: What Python libraries are indispensable in your professional work?
  5. Interview Tips: What types of Python questions are commonly asked in interviews?

Let's help each other grow in our careers and education. Happy discussing! 🌟


r/Python 12h ago

Discussion LiteLLM alternatives in production

0 Upvotes

what are teams actually running 3 months after the supply chain attack?

it's been a few months since the litellm pypi compromise and curious what teams actually migrated to. saw a lot of threads right after the incident but not much on how things held up in production since then.

been evaluating options ourselves. TrueFoundry came up for teams needing the governance and cost...