一、torch.Tensors https://pytorch-cn.readthedocs.io/zh/latest/package_references/Tensor/
輸出格式設置:torch.set_printoptions(precision=3, sci_mode=False) 這里禁用科學計數法輸出
1、tensor類型

torch.IntTensor() 接受三種參數
- torch.IntTensor([[1,2,3],[4,5,6]]) #隨機初始化
- torch.IntTensor(np.arange(6).reshape(2,3))
- torch.IntTensor(2,3).zero_()
①.zero_()尾巴的使用 常見用法
.add_(value) .abs_() .sin_() .cos_() .eq_(value) .floor() . x.dim()-> int len(x)==x.shape[0]
②類型轉換
x.byte()、x.long()、x.float()、x.char()
③設備轉換 tensor和numpy轉換(numpy只能運行在cpu上)
y = x.cuda() x = y.cpu() n = t.numpy() t = torch.from_numpy(n)
④設備數據
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device) mytensor.to(device)
二、torch基礎 https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch
torch.is_tensor(obj)->bool
torch.numel(input)->int #返回張量中元素個數
1、創建Tensor
torch.eye(3)#返回3維單位方陣
torch.from_numpy(np.ones(shape=(2,3)))
torch.linspace(start(float), end(float), steps=100(int))
torch.ones(*sizes) #torch.ones(2,3)
torch.zeros(*sizes)
torch.rand(*sizes) #返回(0,1)的小數
torch.randn(*sizes) #均值為0,方差為1
torch.randperm(n) #返回0到n-1的數列,但隨機排列
torch.arange(start(float), end(float), step=1(float))
2、索引,切片,連接,換位
torch.cat( [x, x, x], 0) #沿第0維將這3個x張量拼接,torch.stack(sequence, dim=0)則會增加一個維度
torch.chunk(x, int, dim=0) #沿第0維將x拆分為長為2的tensor list
torch.split(tensor, 2, dim=0) #沿着第0維拆分,每個分片內2行記錄
torch.index_select(x, 0, torch.LongTensor([0, 2])) #沿着第0維挑選,第0 2行組成一個新的張量
torch.nonzero(x)->LongTensor #輸出非0值元素的坐標list
torch.squeeze(x, 2) #默認去除所有shape=1的維度,如果第2維的shape==1則去掉第2維度,否則啥也不執行
torch.transpose(input, dim0, dim1) #轉置torch.transpose(x,0,1) torch.transpose(x,1,0)
torch.unsqueeze(input, dim) #指定維插入一個shape=1

3、序列化 並行化
torch.save(obj,path) model = InceptionV3()
model = torch.load(path) _ = model.load_state_dict(torch.load(model_weight_path), strict=False)
torch.get_num_threads() → int #6 cpu並行線程數
torch.set_num_threads(int)
4、數學操作
①一元
torch.abs(x)、 torch.acos(x)、torch.cos(x)、torch.pow(x, value)、torch.log(x)、torch.ceil(x)
torch.exp(x) #torch.exp(torch.Tensor([0, math.log(2)])) == tensor([1., 2.])
torch.round(x) #四舍五入到最近的整數 torch.neg(x) #返回-x torch.reciprocal(x) #返回1.0/x
torch.sigmoid(x)、 torch.sqrt(x)
②二元
torch.add(x,value) #張量每個元素都加上標量value
+ - * /: 兩個運算數維度相同,至少有一個對應的相等

torch.add(x,value=1,y) #x y都是張量,當shape[0]不同時,但元素數量必須相同,返回shape以x為准。value為標量,x += value * y 。
torch.addcdiv(x, value=1, y, z) #x += value* (y/z)
torch.addcmul(x, value=1, y, z) #x += value* (y*z)
torch.div(x, value/y) #當x y的shape[0]不匹配時,但元素數量必須相同
torch.mul(x, value/y)
torch.fmod(x, value) #value可整可浮,torch.fmod(torch.Tensor([1, -2, 3, 4]), 1.5) == torch.FloatTensor([1.0, -0.5, 0.0, 1.0])
torch.frac(x) #返回小數部分
torch.trunc(x) #返回整數部分
torch.maximum(x, y) torch.minimum(x, y)
5、Reduction operation
torch.clamp(x, min, max) #x中元素小於min則置為min
torch.lerp(x, y, weight(float)) #線性插值,x y都是tensor,weight是標量,返回x + weight*(y - x)
torch.reciprocal(x, dim) #指定維度的累計積 yi=x1+x2+x3+...+xi
torch.cumsum(x, dim) #指定維度的累計和 yi=x1+x2+x3+...+xi
torch.dist(x, y, p=2) #返回(x - y)的p范數
torch.norm(x, p=2) #輸入張量的p范數
torch.median(x, dim=-1) #指定維度上的中位數
torch.mode(x, dim=-1) #指定維度上的眾數值

②均值方差,總和總積
torch.mean(x) # 所有元素的均值, dim表示消去某一維度的意思
torch.mean(x, dim) #指定維度上的均值
torch.sum(x)
torch.sum(x, dim)
torch.var(x) #所有元素的方差
torch.var(x,dim)
torch.prod(x) #所有元素的積
torch.prod(x, dim)
torch.std(x) #所有元素的標准差
torch.std(x, dim)

6、比較操作
torch.eq(x, y) #x = torch.rand(2,3); y = torch.randn(2,3);test = torch.eq(x,y) ,x y test的shape相同,test元素的值為bool類型。
# eq==, ge>=, gt>,ne!=, le<=, ge<
torch.max(x) # 返回x中元素最大值
torch.max(x, dim) #返回x中指定維度上的最大值
torch.kthvalue(x, dim) #返回x中指定維度上的第k小的值
torch.sort(input, dim=None, descending=False) #默認輸出 按最后一維降序排序的結果
torch.topk(input, k, dim=None) #默認輸出 按最后一維的k個最大值(維度內部降序排好)
2021-10-19 15:32:31
