一.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)目前有用的是这些。其他的以后有需要再补充
本文是关于Pytorch 0.4版本的重要更新学习笔记,主要包括Tensor与Variable的融合,不再需要单独使用Variable,现在Tensor自身可以追踪计算图;弃用了volatile,改用torch.no_grad()或torch.set_grad_enabled()来控制是否构建计算图;另外介绍了新增的三个类torch.dtype、torch.device和torch.layout,用于更精确地操作Tensor的数据类型、设备位置和布局。

393

被折叠的 条评论
为什么被折叠?



