小编nmo*_*pan的帖子

pytorch:计算向量函数的向量雅可比积

再会!

我正在尝试掌握 torch.autograd 基础知识。特别是我想从https://pytorch.org/tutorials/beginner/blitz/autograd_tutorial.html#sphx-glr-beginner-blitz-autograd-tutorial-py测试这个声明


在此输入图像描述


所以我的想法是构造一个向量函数,比如:

(y_1; y_2; y_3) = (x_1*x_1 + x_2; x_2 * x_2 + x_3; x_3 * x_3)

然后计算点 (1,1,1) 处的雅可比矩阵,并将其乘以向量 (3,5,7)。

雅可比行列式 = (2x_1; 1.; 0.) (0.; 2x_2; 1.) (0.; 0.; 2x_3)

我期待结果 Jacobian(x=(1,1,1)) * v = (6+5, 10 + 7, 2 * 7) = (11, 17, 14)。

下面是我在 pytorch 中的尝试:

import torch

x = torch.ones(3, requires_grad=True)
print(x)

y = torch.tensor([x[0]**2 + x [1], x[1]**2 + x[2], x[2]**2], requires_grad=True)
print(y)

v = torch.tensor([3, 5, 7])

y.backward(v) …
Run Code Online (Sandbox Code Playgroud)

python pytorch

5
推荐指数
1
解决办法
1337
查看次数

“是”数字的比较

我接受了 Python 测试,其中一个问题是:应该将什么传递给以下函数,以便它返回True

def fun(x):
    if x + 1 is 1 + x:
        return False
    if x + 2 is not 2 + x:
        return False
    return True
Run Code Online (Sandbox Code Playgroud)

在我看来,这没有多大意义,但我只想知道正确的答案(如果存在这样的答案)。

python

4
推荐指数
1
解决办法
101
查看次数

标签 统计

python ×2

pytorch ×1