相关疑难解决方法(0)

Pytorch复制张量的首选方法

在Pytorch中似乎有几种创建张量副本的方法,包括

y = tensor.new_tensor(x) #a

y = x.clone().detach() #b

y = torch.empty_like(x).copy_(x) #c

y = torch.tensor(x) #d
Run Code Online (Sandbox Code Playgroud)

b明确优于ad根据UserWarning如果我既可以执行我得到ad。为什么首选它?性能?我认为它的可读性较差。

有/反对使用任何理由c

copy pytorch tensor

21
推荐指数
3
解决办法
5505
查看次数

为什么我能够在 Pytorch 中通过分离来更改张量的值,而计算图不知道它?

我可以更改需要 grad 的张量的值,而 autograd 不知道它:

def error_unexpected_way_to_by_pass_safety():
    import torch 
    a = torch.tensor([1,2,3.], requires_grad=True)
    # are detached tensor's leafs? yes they are
    a_detached = a.detach()
    #a.fill_(2) # illegal, warns you that a tensor which requires grads is used in an inplace op (so it won't be recorded in computation graph so it wont take the right derivative of the forward path as this op won't be in it)
    a_detached.fill_(2) # weird that this one is allowed, seems to allow me to …
Run Code Online (Sandbox Code Playgroud)

python pytorch

2
推荐指数
1
解决办法
2222
查看次数

标签 统计

pytorch ×2

copy ×1

python ×1

tensor ×1