數據類型
- 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_()版本;