BlairGrowing

张量 Tensors

1、torch.is_tensor

torch.is_tensor(obj)

用法:判断是否为张量,如果是 pytorch 张量,则返回 True。

参数:obj (Object) – 判断对象

例子:

torch.is_tensor(torch.rand(2,3))
True

 

2、 torch.is_storage

torch.is_storage(obj)

用法:判断是否为pytorch Storage,如何是,则返回True

参数:input (Object) – 判断对象

例子:

torch.is_storage(torch.rand(2,3))
False

 

3、torch.numel

torch.numel(input)->int

用法:返回input 张量中的元素个数

参数:input (Tensor) – 输入张量

例子:

torch.numel(torch.rand(2,3))
6

 

4、torch.eye

torch.eye(n, m=None, out=None)

用法:返回一个2维张量,对角线数字为1,其它位置为0

参数:

  • n (int) – 行数
  • m (int, 可选) – 列数.如果为None,则默认为n
  • out (Tensor,可选) - 输出张量

例子:

torch.eye(3, m=2)
tensor([[1., 0.],
        [0., 1.],
        [0., 0.]])
torch.eye(3)
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

 

5、torch.from_numpy

torch.from_numpy(ndarray) → Tensor

用法:将 numpy.ndarray 转换为 Tensor。 返回的张量 tensor 和 numpy 的 ndarray 共享同一内存空间。修改一个会导致另外一个也被修改。返回的张量不能调整大小。

参数:ndarray

例子:

x = np.random.rand(2,3)
x
array([[0.84130586, 0.64710973, 0.82838384],
       [0.50825928, 0.3054745 , 0.22876226]])
y = torch.from_numpy(x)
y
tensor([[0.8413, 0.6471, 0.8284],
        [0.5083, 0.3055, 0.2288]], dtype=torch.float64)

 

6、torch.linspace

torch.linspace(start, end, steps=100, out=None) → Tensor

用法:返回start和end之间长度为steps的一维张量 参数:

参数:

  • start (float) – 点集的起始值
  • end (float) – 点集的最终值
  • steps (int) – 在start 和 end间的采样数,即返回多少个数
  • out (Tensor, 可选的) – 结果张量

例子:

x = torch.linspace(1,10,steps=5)
x
tensor([ 1.0000,  3.2500,  5.5000,  7.7500, 10.0000])

 

7、torch.logspace

torch.logspace(start, end, steps=100, out=None) → Tensor

用法:返回一个 1 维张量,包含在区间10^start和10^end上以对数刻度均匀间隔的steps个点。 输出1维张量的长度为steps

参数:

  • start (float) – 该点集的起始点
  • end (float) – 该点集的最终值
  • steps (int) – 在start 和 end间生成的样本数
  • out (Tensor, 可选) – 结果张量

例子:

x = torch.logspace(1,10,steps=5)
x
tensor([1.0000e+01, 1.7783e+03, 3.1623e+05, 5.6234e+07, 1.0000e+10])

 

8、torch.ones

torch.ones(*sizes, out=None) → Tensor

用法:返回一个全为1的张量,形状由可变参数sizes定义。

参数:

  • sizes (int...) – 整数序列,定义了输出形状,如:(5,5),(2)
  • out (Tensor, 可选) – 结果张量

例子:

x = torch.ones(5,5)
x
tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]])
x = torch.ones(5)
x
tensor([1., 1., 1., 1., 1.])

 

9、torch.randn

torch.randn(*sizes, out=None) → Tensor

用法:返回一个张量,包含了从正态分布(均值为0,方差为 1,即高斯白噪声)中抽取一组随机数。 Tensor的形状由变量sizes定义。

参数:

  • sizes (int...) – 整数序列,定义了输出形状

  • `out (Tensor, 可选) - 结果张量

例子:

x = torch.randn(2)
x
tensor([ 0.1526, -0.0788])
x = torch.randn(2,2)
x
tensor([[-0.4000,  0.6303],
        [ 0.5029,  0.3646]])

 

10、torch.randperm

torch.randperm(n, out=None) → LongTensor

用法:输入参数n,返回一个从0 到n -1的随机整数排列。

参数:

  • n(int) – 上限(独占),即最大值

例子:

x = torch.randperm(10)
x
tensor([9, 2, 5, 3, 1, 0, 8, 4, 7, 6])

 

11、torch.arange

torch.arange(start, end, step=1, out=None) → Tensor

 用法:返回一个1维张量,长度为floor((end−start)/step),floor代表向下取整。包含从startend,以step为步长的一组序列值(默认步长为1)。

参数:

  • start (float) – 该点集的起始点
  • end (float) – 该点集的终止点
  • step (float) – 相邻点的间隔大小
  • out (Tensor, 可选的) – 结果张量

 

例子:

x = torch.arange(1,10,step=2)
x
tensor([1, 3, 5, 7, 9])
x = torch.arange(1, 2.5, 0.5)
x
tensor([1.0000, 1.5000, 2.0000])

 

12、torch.range

torch.range(start, end, step=1, out=None) → Tensor

用法:返回一个1维张量,长度为floor((end−start)/step)+1,其中floor代表向下取整数。从start开始,end为结尾,以step为步长的一组值。 step 是两个值之间的间隔,即 $X_i+1=X_i+step$

参数:

  • start (float) – 该点集的起始点
  • end (float) – 该点集的最终值
  • step (int) – 相邻点之间的间隔大小
  • out (Tensor, 可选的) – 结果张量

例子:

x = torch.range(1,10,step=2)
x
tensor([1., 3., 5., 7., 9.])
x = torch.range(1, 2.5, 0.5)
x
tensor([1.0000, 1.5000, 2.0000, 2.5000])

 

13、torch.zeros

torch.zeros(*sizes, out=None) → Tensor

用法:返回一个全0的张量,形状由可变参数sizes定义。

参数:

  • sizes (int...) – 整数序列,定义了输出形状
  • out (Tensor, 可选) – 结果张量

例子:

x = torch.zeros(3)
x
tensor([0., 0., 0.])
x = torch.zeros(3,2)
x
tensor([[0., 0.],
        [0., 0.],
        [0., 0.]])

索引,切片,连接,变异操作

1、torch.cat

torch.cat(seq, dim=0, out=None) → Tensor

用法:在给定维度上对输入的张量序列seq 进行连接操作。

参数:

  • seq(Tensors的序列) - 可以是相同类型的Tensor的任何python序列。
  • dim(int,可选) - 张量连接的尺寸
  • out(Tensor,可选) - 输出参数

例子:

x = torch.arange(0,4).view(-1,2)
print(x[0])
print(x[1])
tensor([0, 1])
tensor([2, 3])
torch.cat((x,x,x),dim=0)
tensor([[0, 1],
        [2, 3],
        [0, 1],
        [2, 3],
        [0, 1],
        [2, 3]])
torch.cat((x,x,x),dim=1)
tensor([[0, 1, 0, 1, 0, 1],
        [2, 3, 2, 3, 2, 3]])

C=torch.cat((A,B),0)就表示按维数0(行)拼接A和B,也就是竖着拼接,A上B下。

C=torch.cat((A,B),1)就表示按维数1(列)拼接A和B,也就是横着拼接,A左B右。

 

2、torch.nonzero

torch.nonzero(input, out=None) → LongTensor

用法:返回一个包含输入input中非零元素索引的张量。输出张量中的每行包含输入中非零元素的索引。

   如果输入input有n维,则输出的索引张量output的形状为 z x n, 这里 z 是输入张量input中所有非零元素的个数。

参数:

  • input (Tensor) – 源张量
  • out (LongTensor, 可选的) – 包含索引值的结果张量

例子:

torch.nonzero(torch.Tensor([1, 1, 1, 0, 1]))
tensor([[0],
        [1],
        [2],
        [4]])
torch.nonzero(torch.Tensor([[0.6, 0.0, 1.0, 0.0],
                             [0.0, 0.4, 0.0, 0.0],
                             [0.0, 0.0, 1.2, 0.0],
                             [0.0, 0.0, 0.0,-0.4]]))
tensor([[0, 0],
        [0, 2],
        [1, 1],
        [2, 2],
        [3, 3]])

3、torch.split

torch.split(tensor, split_size, dim=0)

用法:将输入张量分割成相等形状的chunks(如果可分)。 如果沿指定维的张量形状大小不能被split_size 整分, 则最后一个分块会小于其它分块。

参数:

  • tensor (Tensor) – 待分割张量
  • split_size (int) – 单个分块的形状大小
  • dim (int) – 沿着此维进行分割

例子:

x = torch.Tensor([[0.6, 0.0, 1.0, 0.0],
                 [0.0, 0.4, 0.0, 0.0],
                 [0.0, 0.0, 1.2, 0.0],
                 [0.0, 0.0, 0.0,-0.4]])
torch.split(x, 2, dim=0)
(tensor([[0.6000, 0.0000, 1.0000, 0.0000],
         [0.0000, 0.4000, 0.0000, 0.0000]]),
 tensor([[ 0.0000,  0.0000,  1.2000,  0.0000],
         [ 0.0000,  0.0000,  0.0000, -0.4000]]))
torch.split(x, 2, dim=1)
(tensor([[0.6000, 0.0000],
         [0.0000, 0.4000],
         [0.0000, 0.0000],
         [0.0000, 0.0000]]),
 tensor([[ 1.0000,  0.0000],
         [ 0.0000,  0.0000],
         [ 1.2000,  0.0000],
         [ 0.0000, -0.4000]]))

 

4、torch.squeeze

torch.squeeze(input, dim=None, out=None)

用法:将输入张量形状中的1 去除并返回。 如果输入是形如($A \times 1\times B \times 1 \times C \times 1 \times D$) ,那么输出形状就为:($A \times B \times C \times D$) 

     当给定dim时,那么挤压操作只在给定维度上。例如,输入形状为: $(A \times 1 \times B) $, squeeze(input, 0) 将会保持张量不变,只有用 squeeze(input, 1),形状会变成 $(A \times B )$。

参数:

  • input (Tensor) – 输入张量
  • dim (int, 可选的) – 如果给定,则input只会在给定维度挤压
  • out (Tensor, 可选的) – 输出张量

例子:

x = torch.zeros(2,1,2,1,2)
x.size()
torch.Size([2, 1, 2, 1, 2])
y = torch.squeeze(x)
y.size()
torch.Size([2, 2, 2])
torch.Size([2, 1, 2, 1, 2])
torch.Size([2, 1, 2, 1, 2])
y = torch.squeeze(x, 1)
y.size()
torch.Size([2, 2, 1, 2])

 

5、torch.stack

torch.stack(sequence, dim=0)

用法:沿着一个新维度对输入张量序列进行连接。 序列中所有的张量都应该为相同形状。

参数:

  • sqequence (Sequence) – 待连接的张量序列
  • dim (int) – 插入的维度。必须介于 0 与 待连接的张量序列数之间。

 

6、torch.t

torch.t(input, out=None) → Tensor

用法:输入一个矩阵(2维张量),并转置0, 1维。 可以被视为函数transpose(input, 0, 1)的简写函数。

参数:

  • input (Tensor) – 输入张量
  • out (Tensor, 可选的) – 结果张量

例子:

x = torch.randn(2, 3)
torch.t(x)
tensor([[ 0.2929,  0.1270],
        [-0.0673, -0.3026],
        [-0.4359,  0.4589]])

 

7、torch.transpose

torch.transpose(input, dim0, dim1, out=None) → Tensor

用法:返回输入矩阵input的转置。交换维度dim0dim1。 输出张量与输入张量共享内存,所以改变其中一个会导致另外一个也被修改。

参数:

  • input (Tensor) – 输入张量
  • dim0 (int) – 转置的第一维
  • dim1 (int) – 转置的第二维

例子:

x = torch.randn(2, 3)
x
tensor([[-0.0635, -0.4873,  0.1029],
        [ 0.3269,  1.8284,  0.1268]])
torch.transpose(x, 0, 1)
tensor([[-0.0635,  0.3269],
        [-0.4873,  1.8284],
        [ 0.1029,  0.1268]])

 

8、torch.unbind

torch.unbind(tensor, dim=0)[source]

用法:移除指定维后,返回一个元组,包含了沿着指定维切片后的各个切片

参数:

  • tensor (Tensor) – 输入张量
  • dim (int) – 删除的维度

 

9、torch.unsqueeze

torch.unsqueeze(input, dim, out=None)

用法:返回一个新的张量,对输入的制定位置插入维度 1

参数:

  • tensor (Tensor) – 输入张量
  • dim (int) – 插入维度的索引
  • out (Tensor, 可选的) – 结果张量

例子:

x = torch.Tensor([1, 2, 3, 4])
torch.unsqueeze(x, 0)
tensor([[1., 2., 3., 4.]])
torch.unsqueeze(x, 1)
tensor([[1.],
        [2.],
        [3.],
        [4.]])

 

随机抽样 Random sampling

1、torch.manual_seed

torch.manual_seed(seed)

用法:设定生成随机数的种子,并返回一个 _torch.C.Generator 对象.

参数:seed (int or long) – 种子.

例子:

torch.manual_seed(1)
<torch._C.Generator at 0x19749eb5890>

 

2、torch.initial_seed

torch.initial_seed()

用法:返回生成随机数的原始种子值(python long)。

例子:

torch.manual_seed(12)
torch.initial_seed()
12

 

3、torch.bernoulli

torch.bernoulli(input, out=None) → Tensor

用法:

 

  从伯努利分布中抽取二元随机数(0 或者 1)。

  输入张量须包含用于抽取上述二元随机值的概率。 因此,输入中的所有值都必须在[0,1]区间,即 $( 0<=input_i<=1 )$

  输出张量的第 $i$ 个元素值, 将会以输入张量的第 $i$ 个概率值等于1

  返回值将会是与输入相同大小的张量,每个值为 0 或者 1 参数:

参数:

  • input (Tensor) – 输入为伯努利分布的概率值
  • out (Tensor, 可选的) – 输出张量(可选)

例子:

a = torch.Tensor(3, 3).uniform_(0, 1) # generate a uniform random matrix with range [0, 1]
a
tensor([[0.4657, 0.2328, 0.4527],
        [0.5871, 0.4086, 0.1272],
        [0.6373, 0.2421, 0.7312]])
torch.bernoulli(a)
tensor([[0., 1., 0.],
        [1., 0., 0.],
        [1., 0., 0.]])
a = torch.ones(3, 3) # probability of drawing "1" is 1
torch.bernoulli(a)
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
a = torch.zeros(3, 3) # probability of drawing "1" is 0
torch.bernoulli(a)
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])

 

4、torch.multinomial

torch.multinomial(input, num_samples,replacement=False, out=None) → LongTensor

用法:

 

  返回一个张量,每行包含从input相应行中定义的多项分布中抽取的num_samples个样本。

 

  当抽取样本时,依次从左到右排列(第一个样本对应第一列)。

 

  如果输入input是一个向量,输出out也是一个相同长度 num_samples 的向量。如果输入 input 是有 m 行的矩阵,输出 out 是形如 $m \times n$ 的矩阵。

 

  如果参数 replacement 为 True, 则样本抽取可以重复。否则,一个样本在每行不能被重复抽取。

 

  参数 num_samples 必须小于 input 长度(即,input 的列数,如果是 input 是一个矩阵)。

 

参数:

 

  • input (Tensor) – 包含概率值的张量
  • num_samples (int) – 抽取的样本数
  • replacement (bool, 可选的) – 布尔值,决定是否能重复抽取
  • out (Tensor, 可选的) – 结果张量

 

例子:

weights = torch.Tensor([0, 10, 3, 0]) # create a Tensor of weights
torch.multinomial(weights, 4)
tensor([1, 2, 0, 3])
torch.multinomial(weights, 4, replacement=True)
tensor([2, 1, 1, 1])

 

5、torch.normal()

torch.normal(means, std, out=None)

用法:返回一个张量,包含从给定参数means,std的离散正态分布中抽取随机数。 均值means是一个张量,包含每个输出元素相关的正态分布的均值。 std是一个张量,包含每个输出元素相关的正态分布的标准差。 均值和标准差的形状不须匹配,但每个张量的元素个数须相同。

参数:

  • means (Tensor) – 均值
  • std (Tensor) – 标准差
  • out (Tensor) – 可选的输出张量

例子:

torch.normal(means=torch.arange(1, 11), std=torch.arange(1, 0, -0.1))

 1.5104

 1.6955

 2.4895

 4.9185

 4.9895

 6.9155

 7.3683

 8.1836

 8.7164

 9.8916

 

分类:

技术点:

相关文章: