SoFunction
Updated on 2024-10-30

Tutorial on using the PyTorch derivative application

preamble

Since the basic idea of machine learning is to find a function tofit (data to a model)distribution of the sample data, thus involving thegradientgo and askminimum valueIn the hyperplane, it is difficult to get the global optimum directly, and there is no generalization, so we try to find a way to make the gradient go down in the negative direction, then we can get a local or global optimum, so the derivative becomes very important in machine learning.

Basic use

()It is possible and automatic to accumulate the gradient into thefirst (of multiple parts)

x = (3,3)
print(x.requires_grad)
x.requires_grad_(True)
print(x.requires_grad)
y = x**2/(x-2)
out = ()
print()
()
print()

False
True
None
tensor([[-0.3333, -0.3333, -0.3333],
        [-0.3333, -0.3333, -0.3333],
        [-0.3333, -0.3333, -0.3333]])

requires_grad It is possible to get thetensordifferentiable (calculus)
requires_grad_() Can be settensordifferentiable (calculus)
grad View currenttensorderivative

The above formula is very simple, the meaning of the program

1/4 * (x**2) / (x-2)

Find the derivative of x. The basic formula is below.

point of attention

We use.meanWhat you get after that isscalar quantityIf notscalar quantitywill report an error

x = (3, requires_grad=True)
y = x * 2
y = y * 2
print(y)
tensor([4., 4., 4.], grad_fn=<MulBackward0>)
()
print()

report an error

RuntimeError: grad can be implicitly created only for scalar outputs

v = ([0.1, 1.0, 0.0001], dtype=)
()
print()
tensor([4.0000e-01, 4.0000e+00, 4.0000e-04])

no_grad()scope (computing)

If we want some part of the program to be undirectable then we can use this

x = (3, requires_grad=True)
y = x * 2
print(y.requires_grad)
with torch.no_grad():
 y = y * 2 
 print(y.requires_grad)

True
False

summarize

In this chapter we use pytorch's internalbackwardThis automatically realizes the derivation of a function, which helps us in the later stage when we face a lot of functions with a large number of parameters, and the derivation becomes easy.

previous section

PyTorch Tutorial - Installation and Basic Usage

To this point this article on the use of PyTorch derivative application tutorial article is introduced to this, more related PyTorch derivative application content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!