It starts with a tensor
NO.6 Numeric types
- The data type specifies the possible values that the tensor can hold (integers versus floating-point numbers) and the number of bytes per value
torch.float32 or torch.float—32-bit floating-point
torch.float64 or torch.double—64-bit, double-precision floating-point
torch.float16 or torch.half—16-bit, half-precision floating-point
torch.int8—Signed 8-bit integers
torch.uint8—Unsigned 8-bit integers
torch.int16 or torch.short—Signed 16-bit integers
torch.int32 or torch.int—Signed 32-bit integers
torch.int64 or torch.long—Signed 64-bit integers
- 指定数据类型的方法
num = torch.tensor([[1,2], [3,4]], dtype=torch.float64)
>>> tensor([[1., 2.],
[3., 4.]], dtype=torch.float64)
num1 = torch.tensor([[1,2], [3,4]]).double()
>>> tensor([[1., 2.],
[3., 4.]], dtype=torch.float64)
num2 = torch.tensor([[1,2],[3,4]]).to(dtype=torch.float64)
>>> tensor([[1., 2.],
[3., 4.]], dtype=torch.float64)
NO.7 Indexing tensors
NO.8 NumPy interoperability
import torch
import numpy as np
point = torch.ones(2,2)
point_np = point.numpy()
>>> Numpy
---------
point = torch.from_numpy(point_np)
>>> 返回张量
returns a NumPy multidimensional array of the right size, shape, and numerical type. Interestingly, the returned array shares an underlying buffer with the tensor storage. As a result, the numpy method can be executed effectively at essentially no cost as long as the data sits in CPU RAM, and modifying the NumPy array leads to a change in the originating tensor.
NO.9 Serializing tensors
- save tensors to a file.
torch.save(张量, '位置/xxx.t)'
with open('../data/p1ch3/ourpoints.t','wb') as f:
torch.save(points, f)
- Loading your points back is similarly a one-liner:
points = torch.load('../data/p1ch3/ourpoints.t')
with open('../data/p1ch3/ourpoints.t','rb') as f:
points = torch.load(f)
- HDF5 is a portable, widely supported format for representing serialized multidimensional arrays, organized in a nested key-value dictionary.
import h5py
f = h5py.File('../data/p1ch3/ourpoints.hdf5', 'w')
dset = f.create_dataset('coords', data=points.numpy())
f.close()
NO.10 Moving tensors to the GPU
- create a tensor on the GPU
points_gpu = torch.tensor(
[[1.0, 4.0],
[2.0, 1.0],
[3.0, 4.0]],
device='cuda')
- You could instead copy a tensor created on the CPU to the GPU by using the to method:
points_gpu = points.to(device='cuda')
- has more than one GPU, you can decide which GPU to allocate the tensor to by passing a zero-based integer identifying the GPU on the machine:
points_gpu = points.to(device='cuda:0')
- 当指定了张量在GPU进行,接下来对张量的任何操作都在GPU进行:
- 当计算结束后,张量不会返回到CPU:
- the addition is still performed on the GPU, and no information flows to the CPU (except if you print or access the resulting tensor). To move the tensor back to the CPU, you need to provide a cpu argument to the to method:
points_cpu = points_gpu.to(device='cpu')
points_gpu = points.cuda()
points_gpu = points.cuda(0)
points_cpu = points_gpu.cpu()
- PyTorch uses a trailing underscore to indicate that a function operates in-place on a tensor (such as Tensor.sqrt_).