一.Tensor 和 Variable合并为Tensor。
Tensor可以追踪计算图。所以没必要使用Variable了。
type(x) -> x.type()
使用detach()来获取Tensor的数值。
二.弃用volatile
使用torch.no_grad()/torch.set_grad_enabled(grad_mode)来屏蔽计算图。
>>> x = torch.zeros(1, requires_grad=True)
>>> with torch.no_grad():
... y = x * 2
>>> y.requires_grad
False
>>>
>>> is_train = False
>>> with torch.set_grad_enabled(is_train):
... y = x * 2
>>> y.requires_grad
False
三.三个类torch.dtype/torch.device/torch.layout
torch.dtype(使用Tensor的的dtype属性获取数据类型)
Data type | torch.dtype | Tensor types |
---|---|---|
32-bit floating point | torch.float32 or torch.float | torch.*.FloatTensor |
64-bit floating point | torch.float64 or torch.double | torch.*.DoubleTensor |
16-bit floating point | torch.float16 or torch.half | torch.*.HalfTensor |
8-bit integer (unsigned) | torch.uint8 | torch.*.ByteTensor |
8-bit integer (signed) | torch.int8 | torch.*.CharTensor |
16-bit integer (signed) | torch.int16 or torch.short | torch.*.ShortTensor |
32-bit integer (signed) | torch.int32 or torch.int | torch.*.IntTensor |
64-bit integer (signed) | torch.int64 or torch.long | torch.*.LongTensor |
torch.device(使用Tensor的device属性获取Tensor的位置,cpu或gpu)
torch.device('cpu')/torch.device('cuda')
创建Tensor的方法
>>> device = torch.device("cuda:1") >>> x = torch.randn(3, 3, dtype=torch.float64, device=device
>>> cuda = torch.device("cuda")
>>> torch.tensor([[1], [2], [3]], dtype=torch.half, device=cuda)
>>> x = torch.randn(3, dtype=torch.float64)
>>> torch.zeros_like(x)
tensor([ 0., 0., 0.], dtype=torch.float64)
>>> x = torch.randn(3, dtype=torch.float64)
>>> x.new_ones(2)
tensor([ 1., 1.], dtype=torch.float64)
目前有用的是这些。其他的以后有需要再补充