数据类型
- CPU数据类型:torch.FloatTensor(torch.Tensor),torch.DoubleTensor,torch.ByteTensor,torch.CharTensor,torch.ShortTensor,torch.IntTensor,torch.LongTensor;
- GPU数据类型:torch.cuda.FloatTensor,torch.cuda.DoubleTensor,torch.cuda.HalfTensor,torch.cuda.ByteTensor,torch.cuda.CharTensor,torch.cuda.ShortTensor,torch.cuda.IntTensor,torch.cuda.LongTensor;
数据类型操作
- 从python的list或者序列构建Tensor:torch.FloatTensor([[1, 2, 3], [4, 5, 6]]);
- 指定大小构建Tensor:torch.FloatTensor.zero_();
- x.abs()和x.abs_() :前者生成副本,后者in-place运算,其他大量函数同理;
- y = x.byte():将tensor改为byte类型;
- y = x.char():将tensor改为char类型;
- y = x.clone() :返回与x有相同大小和数据类型的tensor;
- y = x.contiguous() :返回一个内存连续的有相同数据的tensor;
- x.copy_(y,async):复制拷贝操作,x和y应该有相同数目的元素,可以是不同的数据类型或存储在不同的设备上;
- y = x.cpu() :如果在CPU上没有x,则会返回一个CPU的副本;
- y = x.cuda(device, async):返回GPU副本,async=True并且资源在固定内存中,则复制的副本将会与原始数据异步;
- y = x.data_ptr():返回第一个元素地址;
- y = x.dim():返回维度;
- y = x.element_size():返回单个字节大小;
- 我在读pyTorch文档(二)中包含的数学操作基本都有y=x.function()版本或者y=x.function_()版本;