![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
pytorch的tensor
weixin_45091814
这个作者很懒,什么都没留下…
展开
-
pytorch:tensor的梯度求导
创建一个tensor,如果不指定requires_grad时,默认为False,此时将不能进行梯度传播,需要设置requires_grad=True x = torch.randn(3,requires_grad=True) print(x) 输出 tensor([-0.5517, -0.7468, -0.5076], requires_grad=True) 然后对张量做操作,grad_fn显示操作的类型 y = x+2 print(y) Out[52]: tensor([1.4483, 1.2532,原创 2021-09-21 14:14:57 · 543 阅读 · 0 评论 -
pytorch的tensor转换成numpy,不同方式
pytorch的tensor转换成numpy,不同方式 第一种方式 import torch import numpy tensor = torch.ones(3,4) n = tensor.data n = n+1 print(tensor) print(n) 输出 tensor([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]) tensor([[2., 2., 2., 2.], [2., 2原创 2021-05-19 20:07:35 · 1922 阅读 · 0 评论