PyTorch tensors are mutable, which means that they can be modified in place. tensor 是 mutable object,和 list 一样,因此在函数传参后,在函数里面时,需要注意是修改原来的 tensor,还是返回新的。

  1. Varible and tensor: Varible 是旧版的 torch 的变量, 等价于 tensor

  2. b = a if torch.is_tensor(a) else torch.tensor(a)

  3. Pytorch 与 numpy 的 Tensor 与 arrary 类型转换

    1
    2
    torch.from_numpy(a_array)
    a_tenosr.numpy()

  4. numpy 与 torch 的浮点数默认位数不同

    numpy 浮点数默认使用 64 位, torch 的浮点数默认使用 32 位

    Default dtype:

    1. PyTorch torch.float32 torch.set_default_dtype(torch.float64) to compatible with tensor.numpy() defauly np.float64

    2. numpy np.float64 int_: Default integer type (same as C long; normally either int64 or int32)

  5. shuffle tensor

    1
    2
    indexes = torch.randperm(data.shape[0])
    data = data[indexes]

    shuffle - Randomly shuffling torch tensor - Stack Overflow

  6. random tensor

    torch.tensor 默认初始化类型 torch.float/torch.FloatTensor: float 类型, 后者是 CPU float 类型

    torch.float(*(a, b, c)).uniform_(0, 100)

  7. is 和 ==

    identity operator (is) and the equality operator (==)

    is 是用来检查是否是一个地址的数据

    is not 不能当成 !=

    Python '!=' Is Not 'is not': Comparing Objects in Python – Real Python

  8. Reproducibility fix randomness

    1
    2
    3
    4
    5
    6
    7
    import torch
    import random
    import numpy as np

    random.seed(0)
    torch.manual_seed(0)
    np.random.seed(0)

    Reproducibility — PyTorch 1.13 documentation

  9. pytorch 和 numpy 中最大和最小值

pytorch 中 torch.iinfo, torch.finfo, np.iinfo, np.finfo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
In [93]: np.iinfo(np.int64)
Out[93]: iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)

In [94]: torch.iinfo(torch.int64)
Out[94]: iinfo(min=-9.22337e+18, max=9.22337e+18, dtype=int64)

In [95]: torch.finfo(torch.float64)
Out[95]: finfo(resolution=1e-15, min=-1.79769e+308, max=1.79769e+308, eps=2.22045e-16, smallest_normal=2.22507e-308, tiny=2.22507e-308, dtype=float64)

In [96]: np.iinfo(np.int64)
Out[96]: iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)

In [97]: np.iinfo(np.int64).max
Out[97]: 9223372036854775807