1. 程式人生 > >Pytorch 0.4 更新學習筆記

Pytorch 0.4 更新學習筆記

一.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)
目前有用的是這些。其他的以後有需要再補充