提交 a386fb1c 编写于 作者: D DrDavidS

校对张量和自动梯度

上级 b6b65b39
# PyTorch:张量 Autograd
# PyTorch:张量 Autograd
> 原文:<https://pytorch.org/tutorials/beginner/examples_autograd/polynomial_autograd.html#sphx-glr-beginner-examples-autograd-polynomial-autograd-py>
经过训练的三阶多项式,可以通过最小化平方的欧几里得距离来预测`y = sin(x)``-pi``pi`
这里我们准备一个三阶多项式,通过最小化平方欧几里得距离来训练,并预测函数 `y = sin(x)``-pi``pi`上的值
此实现使用 PyTorch 张量上的运算来计算正向传播,并使用 PyTorch Autograd 来计算梯度。
此实现使用了 PyTorch 张量(tensor)运算来实现前向传播,并使用 PyTorch Autograd 来计算梯度。
PyTorch 张量表示计算图中的一个节点。 如果`x`具有`x.requires_grad=True`的张量,则`x.grad`是另一个张量,其保持`x`相对于某个标量值的梯度。
PyTorch 张量表示计算图中的一个节点。 如果`x`一个张亮,且`x.requires_grad=True`,则`x.grad`是另一个张量,它保存了`x`相对于某个标量值的梯度。
```py
import torch
......@@ -74,4 +74,4 @@ print(f'Result: y = {a.item()} + {b.item()} x + {c.item()} x^2 + {d.item()} x^3'
[下载 Jupyter 笔记本:`polynomial_autograd.ipynb`](https://pytorch.org/tutorials/_downloads/e1d4d0ca7bd75ea2fff8032fcb79076e/polynomial_autograd.ipynb)
[由 Sphinx 画廊](https://sphinx-gallery.readthedocs.io)生成的画廊
\ No newline at end of file
校对:DrDavidS
\ No newline at end of file
......@@ -2,11 +2,13 @@
> 原文:<https://pytorch.org/tutorials/beginner/examples_autograd/polynomial_custom_function.html#sphx-glr-beginner-examples-autograd-polynomial-custom-function-py>
经过训练的三阶多项式,可以通过最小化平方的欧几里得距离来预测`y = sin(x)``-pi``pi`。 而不是将多项式写为`y = a + bx + cx ^ 2 + dx ^ 3`,我们将多项式写为`y = a + b P[3](c + dx)`其中`P[3](x) = 1/2 (5x ^ 3 - 3x)`是三次的[勒让德多项式](https://en.wikipedia.org/wiki/Legendre_polynomials)
这里我们准备一个三阶多项式,通过最小化平方欧几里得距离来训练,并预测函数 `y = sin(x)``-pi``pi`上的值
此实现使用 PyTorch 张量上的运算来计算正向传播,并使用 PyTorch Autograd 来计算梯度
这里我们不将多项式写为`y = a + bx + cx^2 + dx^3`,而是将多项式写为`y = a + bP_3(c + dx)`,其中`P_3(x) = 1/2 (5x ^ 3 - 3x)`是三次[勒让德多项式](https://en.wikipedia.org/wiki/Legendre_polynomials)
在此实现中,我们实现了自己的自定义 Autograd 函数来执行`P'[3](x)`。 通过数学,`P'[3](x) = 3/2 (5x ^ 2 - 1)`
此实现使用了 PyTorch 张量(tensor)运算来实现前向传播,并使用 PyTorch Autograd 来计算梯度。
在此实现中,我们实现了自己的自定义 Autograd 函数来执行`P'_3(x)`。 从数学定义上讲,`P'_3(x) = 3/2 (5x ^ 2 - 1)`
```py
import torch
......@@ -100,4 +102,4 @@ print(f'Result: y = {a.item()} + {b.item()} * P3({c.item()} + {d.item()} x)')
[下载 Jupyter 笔记本:`polynomial_custom_function.ipynb`](https://pytorch.org/tutorials/_downloads/0a64809624bf2f3eb497d30d5303a9a0/polynomial_custom_function.ipynb)
[由 Sphinx 画廊](https://sphinx-gallery.readthedocs.io)生成的画廊
\ No newline at end of file
校对:DrDavidS
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册