pytorch

1、

 a   b

 c   d

 dim/len(shape):2

 shape/size:torch.Size([2, 2])

 numel == 2 * 2(number of element)

2、三种常用数据类型

 torch.FloatTensor()

 torch.ByteTensor()

 torch.IntTensor()

 torch.Tensor可设置默认类型

 括号内填shape,若想填写现成数据,则放到中括号里面

 若是GUP类型,则加cuda

3、list可以将size、shape转化成列表

4、torch.randn()torch.rand()torch.FloatTensor()区别

 rand:在[0,1]之间均匀选取   rand_like(tensor):相当于取了tensor的shape

 randint(min,max,shape)可以自己指定min和max,但是max取不到

 randn:取值服从正态(0,1)分布

5、将其他类型转化为tensor类型

 (1)由numpy导入

 np.array()、np.ones()

 torch.from_numpy

 (2)由list导入

 torch.tensor()只有一个参数,接收现成的数据,可以是numpy或list或数字

 不成文的规定,小写给数据,大写给shape(可以给数据,但是最好别用)

6、全部初始化为某一个值

 torch.full([shape], value)

7、生成等差数列

 torch.arange(min, max, 步长)

8、全部为1:ones

   全部为0:zeros

   对角为1:eye

9、索引与切片

 若想加步长,则多写一个冒号

 用...代替 :

 在某一个维度上切片:index_select(维度,[min, max])

10、维度变换

 (1)view == reshape

 (2)unsqueeze(扩张):正数在前负数在后插一个维度

     squeeze(压缩):参数为打算删减的维度

 (3)t(转置):只适用于二维

 (4)transpose(维度交换)二维的用转置,高维的用transpose

 (5)拼接:cat([tensor1, tensor2], dim=0)只有拼接的维度可以不同

              stack([tensor1, tensor2], dim=0)相当于创建一个新的维度(概念),所有的维度必须相同

     拆分:split(len, dim=0)len若为单个数,则按每份为len个均分,若为列表,则为具体分配

        chunk(num)按num份均分

11、矩阵的运算

 + - * /

 **2  **0.5

12、

 floor、ceil、trunc(取整)、frac(取小数)

 round四舍五入

13、clamp 裁剪

14、where(condition,A,B)由tensorA、tensorB构造一个新的tensorC

  gather(input,dim,index)

原文地址:https://www.cnblogs.com/liujianing/p/12168947.html