r/learnmachinelearning • • 19d ago

How deep I should understand the concept of gradient?

7 Upvotes

19 comments sorted by

18

u/Leodip 19d ago

You should understand it. I don't think there is such a thing as a "shallow understanding" of gradients. What is your current understanding? What do you feel like you are missing?

1

u/UnderstandingOwn2913 19d ago edited 18d ago

I have conducted an experiment in my project to really understand what gradient means.
Gradient can be used to approximate the loss when the the gradient's parameter will have a small change in value. For example, when the parameter of the model will increase by 0.00001, I can approximate that loss = past loss + (0.00001 (change in parameter)*gradient of the parameter). But this loss approximation formula: loss = past loss + (change in parameter*gradient of the parameter) does not work when the change in parameter is big. For example, if the change in parameter is 1, the actual loss (when there is a change in the parameter) != past loss + (change in parameter*gradient of the parameter).

I think what I wrote is correct. But, please correct my words if wrong.

5

u/LowEngineering9405 19d ago

That local, holding-others-fixed part is the piece people skip and it bites once parameters interact. I watched a model stall because two features were near-collinear, so each gradient was tiny and the loss barely moved when I nudged either one alone, even though changing both together clearly helped.

1

u/UnderstandingOwn2913 18d ago

I have conducted an experiment in my project to really understand what gradient means.
Gradient can be used to approximate the loss when the the gradient's parameter will have a small change in value. For example, when the parameter of the model will increase by 0.00001, I can approximate that loss = past loss + (0.00001 (change in parameter)*gradient of the parameter). But this loss approximation formula: loss = past loss + (change in parameter*gradient of the parameter) does not work when the change in parameter is big. For example, if the change in parameter is 1, the actual loss (when there is a change in the parameter) != past loss + (change in parameter*gradient of the parameter).

I think what I wrote is correct. But, please correct my words if wrong.

2

u/UnknownEvil_ 19d ago

That's why the gradient is actually a vector, also, a little jiggle will reveal the gradient here

4

u/johnnymo1 19d ago

This is close to correct, but needs a bit of fixing up. What you’re describing is the partial derivative with respect to a parameter. Your description needs the caveat that it only applies locally. If the partial derivative with respect to x is 0.01 and you increase x by 1, in most circumstances you will not find that the function increases by 0.01. However, if you increase x by (very small value), you will find that the function increases by approximately 0.01*(very small value).

The gradient is a vector which points in the direction of the quickest increase of the function, and its components are the partial derivatives with respect to each parameter.

1

u/UnderstandingOwn2913 18d ago edited 18d ago

Thank you for the detailed explanation!

I have conducted an experiment in my project to really understand what gradient means.
Gradient can be used to approximate the loss when the the gradient's parameter will have a small change in value. For example, when the parameter of the model will increase by 0.00001, I can approximate that loss = past loss + (0.00001 (change in parameter)*gradient of the parameter). But this loss approximation formula: loss = past loss + (change in parameter*gradient of the parameter) does not work when the change in parameter is big. For example, if the change in parameter is 1, the actual loss (when there is a change in the parameter) != past loss + (change in parameter*gradient of the parameter).

I think what I wrote is correct. But, please correct my words if wrong.

1

u/SwimmerOld6155 19d ago

I don't think you should work it this way. You should start with the formal definition of the derivative then develop this intuition. People might disagree but this sort of thing is lossy and creates a fragile foundation for what you learn after.

If you already understand the formal definition, fair enough.

1

u/UnderstandingOwn2913 19d ago

I first tried to understand the formal definition of derivative of a function.
But, I do not yet understand the formal definition of limit which is used in the formal definition of derivative of a function.

2

u/SwimmerOld6155 19d ago edited 19d ago

I guess the problem is:

For example, if the gradient of a parameter is 0.01, it means that if the parameter increases by 1, the total loss will increase by 0.01

is not quite true as written. If the gradient is 0.01 and the parameter increases by a very small h, then the total loss will increase by about 0.01 times h. The exact formula (for nice losses) will have terms in h^2, h^3, etc. which are small enough to discard when h is small. 1 is probably big enough for these terms to be quite large.

I think these small things can be lost and why I'd recommend trying to get even an intuitive understanding of a limit.

1

u/UnderstandingOwn2913 18d ago

I have conducted an experiment in my project to really understand what gradient means.
Gradient can be used to approximate the loss when the the gradient's parameter will have a small change in value. For example, when the parameter of the model will increase by 0.00001, I can approximate that loss = past loss + (0.00001 (change in parameter)*gradient of the parameter). But this loss approximation formula: loss = past loss + (change in parameter*gradient of the parameter) does not work when the change in parameter is big. For example, if the change in parameter is 1, the actual loss (when there is a change in the parameter) != past loss + (change in parameter*gradient of the parameter).

I think what I wrote is correct. But, please correct my words if wrong.

1

u/stonerism 19d ago

The best way to get a simple understanding is to think of a gradient as the direction a ball would go if you dropped it on a hill. Your loss function is your height on this hill and it changes as you walk around on it. Gradient descent is a process to find the bottom by, and this is where the calculus part comes in, figuring out which direction the ball would roll and calculating the loss function and gradient at the new spot.

Rinse and repeat a few million times on a hill that exists in a space of several thousand or more dimensions and you'll eventually reach a local (ask your calculus teacher) minimum from where you started.

1

u/retarded_neet 18d ago

My understanding of gradient is following.

There is a vector space. In this space one can sum vectors with each other or multiply vector by a scalar. Suppose there are a vectors v1 and v2. There is also a space of linear functions that can be applied to vectors from vector space. Like if l is a linear function then l(v1) is scalar (just a number).

If linear space has scalar product then there is a theorem that l(v1) for any vector v1 can be represented as scalar product with some other vector, lets call it g. In other words l(v1) = (g,v1) for any vector v1.

Derivative of a function in vector v1 is linear approximation of this function in a neighborhood of this vector. Suppose one has a function Loss of v1 (v1 is weight here). That's a very complex function but as it has derivative in v1 then in can be represented in small neighborhood the following way: Loss(v1+h) = Loss(v1) + l(h) + some error. Where l is a linear function which approximates Loss in neighborhood of v1 (derivative in other words).

As it was said before l(h) can be represented as scalar product with some vector: l(h) = (g,h). If l is a derivative then this vector g is called gradient. So eventually: Loss(v1+h) = Loss(v1) + (g,h).

In ML one wants to minimize Loss by changing weights. So one has to find h that makes Loss(v1+h) smaller than Loss(v1). Loss in our approximations has the following form: Loss(v1+h) = Loss(v1) + (g,h) and parameter is h. To minimize this one has to minimize the scalar product (g,h). Scalar product has its minimum when h and g are opposite vectors. That means that to minimize loss one has to make a step in direction opposite to direction of gradient.

2

u/Boring-Test5522 19d ago

You need to understand Calculus inside out BEFORE you attempt to explain about gradient.

If you're in US, go to community college to take some courses in Calculus / Linear Algebra / Modern Different Equation. There is no shortcut.

1

u/Ok-Library1020 19d ago

Try a tiny gradient descent demo on (w-3)^2 by hand or in a few lines of code: log w and the loss after each update, then change the learning rate. You'll see what the gradient is telling you and why step size matters way faster than from theory alone.

1

u/Prefer_Diet_Soda 16d ago

To understand gradient descent conceptually, visual explanation should be enough. To understand how and WHY gradient is calculated and used, you need multivariate calculus knowledge (typically university sophomore level calculus in the US).

0

u/SwimmerOld6155 19d ago

I don't think there's much to understand about gradients in and of themselves. Do you know calculus?

0

u/bean_217 18d ago

If you have not yet taken a calculus course, I would recommend doing so

0

u/CelebrityChess 19d ago

For ML, a useful target is being able to explain the gradient as the vector of partial derivatives, why gradient descent steps in the opposite direction, and how the chain rule links a loss back to an earlier weight. For example, for f(x,y)=x²+4y², ∇f=(2x,8y) points toward steepest increase, so a descent step goes the other way. You don’t need to hand-derive a huge network, but it helps to recognize the local derivatives that backprop multiplies together.