(4) 张量

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

1

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
  1. save tensors to a file.
torch.save(张量, '位置/xxx.t)'
with open('../data/p1ch3/ourpoints.t','wb') as f:
	torch.save(points, f)
  1. 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)
  1. 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
  1. create a tensor on the GPU
points_gpu = torch.tensor(
				[[1.0, 4.0], 
				 [2.0, 1.0], 
				 [3.0, 4.0]], 
				 device='cuda')
  1. You could instead copy a tensor created on the CPU to the GPU by using the to method:
points_gpu = points.to(device='cuda')
  1. 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')
  1. 当指定了张量在GPU进行,接下来对张量的任何操作都在GPU进行:
    1
  2. 当计算结束后,张量不会返回到CPU:
    1
  3. 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()
  1. PyTorch uses a trailing underscore to indicate that a function operates in-place on a tensor (such as Tensor.sqrt_).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值