numpy中运算,函数,索引的规则和方法可以无缝应用到pytorch的tensor操作。但需要特别注意的一点是,pytorch中in place操作如果有下划线的话,会改变调用该操作的变量。具体参见下面的例子。
In-place functions are always post-fixed with "" will mutate the object! But if the function is not post-fixed with "", then the object will not be mutated. See the example below.
Zoo of manipulations for torch can be found here
If a variable "x" is defined by being assigned with another variable "y", the in-place operation which changes "x" will also change "y". From this, we should be careful with combination of assignment and in_place operations.
In [1]:
import torch
In [25]:
x = torch.rand(4, 3) #randomly generated tensors
print x
x.t_() #transpose of x, note that x has been changed!
print x
In [26]:
print x.t()
print x #the x is still the previous modified, which is not mutated by x.t()
In [27]:
z = torch.Tensor(3, 4)
z.copy_(x) #copy x to z
print x
print z
In [29]:
print z.add_(x)
print x
print z #note that z is replaced with the sum, but x stays the same
In [30]:
## the add() operation is not changing either x or zz
zz = x
print zz.add(x)
print x
print zz
In [31]:
### note that both x and zzz have been changed
zzz = x
print zzz.add_(x)
print x
print zzz
In [33]:
a= torch.ones(3)
print a
In [39]:
b = a.numpy()
c = torch.from_numpy(b)
print type(b), b #b is numpy array converted from a
print type(a), a #a is torch tensor
print type(c), c #c is torch tensor converted from b
注意上例中a和b共享内存,当对a进行in place操作时,不仅a的值发生了变化,b也跟着变了。
Converting a torch Tensor to a numpy array and vice versa is a breeze. The torch Tensor and numpy array will share their underlying memory locations, and changing one will change the other.
In [40]:
a.add_(2)
print a, b #Note that both a and b are changed!
Torch tensors can be moved to GPU using .cuda
function
In [22]:
if torch.cuda.is_available():
a = a.cuda()
b = b.cuda()
c = a + b
In [ ]: