PyTorch installation:
https://pytorch.org/get-started/locally/
PyTorch basis:
Tensor: comprises zero-dimensional (constant), a one-dimensional (array / list / tuple), a two-dimensional (matrix) ......
Tensor of creation:
torch.tensor (list / array / tuple) # the array / list / set into tensor
torch.ones ([2,3]) # create the shape of a [2,3], and the tensor elements are all 1
torch.zeros ([2,3]) # create the shape of a [2,3], and the tensor elements are all 0
torch.empty ([2,3]) # create the shape of a [2,3], the element normally distributed tensor
torch.rand ([2,3]) # Create shape [2,3], the elements of the tensor 0-1
torch.randn ([2,3]) # Create a shape of [2,3], the element mean of 0 and standard deviation of the array 1
torch.randint (low, high, size = []) to generate a specified size, low-high, see element is a random integer tensor
tensor properties:
tensor.item () tensor only one element for acquiring the element (s elements being given)
tensor and numpy conversion:
np_ts = tensor.numpy()
ts = torch.tensor(np_ts)
tensor.size (dim) tensor shape (direction can be specified)
tensor.view (size) changes shape tensor
tensor.t (0,1) tensor transpose (the dimension can be specified)
tensor slice and the index:
Similar to the numpy
tensor methods:
tensor.max (dim) to obtain the largest element (can specify dimensions, will also return index) tensor in
tensor data type:
tensor.dtype view tensor data types
tensor.int () into int32
tensor.long () into int64
tensor.float () into float32
tensor.double () into float64
tensor calculation:
tensor + // * // tensor matrix shape corresponding to the same operating position (if different shapes, satisfying the conditions of the broadcast, may be broadcast operation)
tensor + // * // Digital
tensor1.add_ (tensor2) after adding tensor assigned tensor1
CUDA type:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
torch.tensor ([2,3], device) # Create a tensor type of cuda
tensor.to (device) # will have created a good type of tensor tensor into cuda
tensor.cpu () # cuda the type tensor into cpu type tensor