pytorch学习笔记一:张量的创建

一、直接创建

1.1 通过torch.tensor创建张量
    torch.tensor(
    			data,dtype=None,
    			device=None,
    			requires_grad=False,
    			pin_memory=False)
  • data:数据,可以是list,numpy
  • dtype:数据类型,默认与data一致
  • device:所在设备,cuda/cpu
  • requires_grad:是否需要梯度
  • pin_memory:是否存于锁页内存

举例:

arr = np.ones((3,3))
print('ndarry的数据类型:',arr.dtype)
print(arr)
# t = torch.tensor(arr,device='cuda')
t = torch.tensor(arr)
print(t)

结果:

ndarry的数据类型: float64
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
1.2 通过对torch.from_numpy创建张量

torch.from_numpy(ndarray)
从numpy创建tensor
注意:从torch.from_numpy创建的tensor与原ndarray共享内存,当修改其中一个的数据,另外一个也会被改动

举例:

arr = np.array([[1,2,3,],
                    [4,5,6]])
t = torch.from_numpy(arr)
print('numpy array:',arr)
print('tensor:',t)

#arr和tensor共享数据内存
print('\n修改arr')
arr[0,0] = 0
print('numpy array:',arr)#[[0 2 3],[4 5 6]]
print('tensor :',t)#tensor([[0, 2, 3],
                   #[4, 5, 6]], dtype=torch.int32)

print('\n修改tensor')
t[0,0] = -1
print('numpy array: ',arr)#[[-1 2 3],[4 5 6]]
print('tensor :',t)

结果:

numpy array: [[1 2 3]
 [4 5 6]]
tensor: tensor([[1, 2, 3],
        [4, 5, 6]], dtype=torch.int32)

修改arr
numpy array: [[0 2 3]
 [4 5 6]]
tensor : tensor([[0, 2, 3],
        [4, 5, 6]], dtype=torch.int32)

修改tensor
numpy array:  [[-1  2  3]
 [ 4  5  6]]
tensor : tensor([[-1,  2,  3],
        [ 4,  5,  6]], dtype=torch.int32)

二、依据数值创建

2.1 通过torch.zeros()创建张量
torch.zeros(*size,
			 out=None,
			 dtype=None,
			 layout=torch.strided,
			 device=None,
			 requires_grad=False)
  • size:张量的形状,如(3,3)、(3,224,224)
  • out :输出的张量 (不好理解)
  • layout:内存中布局形式
  • device:所在设备,gpu/cpu
  • requires_grad:是否需要梯度

举例

out_t = torch.tensor([1])
t = torch.zeros((3,3),out=out_t)

print(t,'\n',out_t)
print(id(t),id(out_t),id(t) == id(out_t))

结果:

tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]) 
 tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])
183939967400 183939967400 True
2.2 torch.zeros_like()创建张量

功能:依input形状创建全0张量

torch.zeros_like(input,
			 dtype=None,
			 layout=None,
			 device=None,
			 requires_grad=False)
  • input:创建与input同形状的全0张量
  • dtype:数据类型
  • layout:内存中布局形式
2.3 torch.ones()
2.4 torch.ones_like()

用法同上

2.5 torch.full()
2.6 torch.full_like()
torch.full(size,
			fill_value,
		    out=None,
		    dtype=None,
		    layout=torch.strided,
		    device=None,
		    requires_grad=False)
  • size:张量的形状,如(3,3)
  • fill_value:张量的值
    举例:
t = torch.full((3,3),10)
print(t)

结果:

tensor([[10., 10., 10.],
        [10., 10., 10.],
        [10., 10., 10.]])
2.7 torch.arange()

功能:创建等差的1维张量
注意:数值区间为[start,end)

torch.arange(start = 0,
			end,
			step=1,
			out=None,
			dtype=None,
			layout=torch.strided,
			device=None,
			requires_grad=False)
  • start:数列起始值
  • end:数列结束值
  • step:数列公差,默认是1

举例:

t = torch.arange(2,10,2)# tensor([2, 4, 6, 8])
print(t)

结果:

tensor([2, 4, 6, 8])
2.8 torch.linspace()

功能:创建均分的1维张量
注意:数值区间为[start,end]

torch.arange(start,
			end,
			steps=100,
			out=None,
			dtype=None,
			layout=torch.strided,
			device=None,
			requires_grad=False)
  • start:数列起始值
  • end:数列结束值
  • steps:数列长度

举例:

t = torch.linspace(2,10,5)
print(t)

结果:

tensor([ 2.,  4.,  6.,  8., 10.])
2.9 torch.logspace()

功能:创建对数均分的1维张量
注意:长度为steps,底为base

torch.logspace(start,
			end,
			steps=100,
			base=10.0,
			out=None,
			dtype=None,
			layout=torch.strided,
			device=None,
			requires_grad=False)
  • start:数列起始值
  • end:数列结束值
  • steps:数列长度
  • base:对数函数的底,默认是10
2.10 torch.eye()

功能:创建单位对角矩阵(2维张量)
注意:默认为方阵

torch.eye(n,
		  m=None,
		  out=None,
		  dtype=None,
		  layout=torch.strided,
		  device=None,
		  requires_grad=False)
  • n:矩阵行数
  • m:矩阵列数

三、依概率分布创建张量

3.1 torch.normal()

功能:创建正态分布(高斯分布)

torch.normal(mean,
		     std,
		     out=None)  
  • mean:均值
  • std:标准差

四种模式:
mean为量,std为
mean为量,std为
mean为量,std为
mean为量,std为

mean:标量 std:标量

t_normal = torch.normal(0., 1.,size=(4,))
print(t_normal)

结果:

tensor([-0.2874, -0.7555,  0.6219, -0.7967])

mean:张量 std:标量

mean = torch.arange(1,5,dtype=torch.float)
std = 1
t_normal = torch.normal(mean,std)#mean和std一一对应
print('mean:{}\nstd:{}'.format(mean,std))
print(t_normal)

结果:

mean:tensor([1., 2., 3., 4.])
std:1
tensor([2.6530, 1.0718, 2.8693, 3.9960])

mean:张量 std:张量

mean = torch.arange(1,5,dtype=torch.float)
std = torch.arange(1,5,dtype=torch.float)
t_normal = torch.normal(mean,std)#mean和std一一对应
print('mean:{}\nstd:{}'.format(mean,std))
print(t_normal)

结果:

mean:tensor([1., 2., 3., 4.])
std:tensor([1., 2., 3., 4.])
tensor([ 3.3175,  6.3296,  4.3810, -2.4837])
3.2 torch.randn()
3.3 torch.randn_like()

功能:生成标准正态分布

torch.randn(*size,
		     out=None,
		     dtype=None,
		     layout=torch.strided,
		     device=None,
		     requires_grad=False)
  • size:张量的形状
3.4 torch.rand()
3.4 torch.rand_like()
torch.rand(*size,
		     out=None,
		     dtype=None,
		     layout=torch.strided,
		     device=None,
		     requires_grad=False)

功能:在区间[0,1)上,生成均匀分布

3.6 torch.randint()
3.7 torch.randint_like()
torch.randint(low=0,
		     high,
		     size,
		     out = None,
		     dtype=None,
		     layout=torch.strided,
		     device=None,
		     requires_grad=False)

功能:在区间[low,hight)生成整数均匀分布

  • size:张量的形状
3.8 torch.randperm()
torch.randperm(n,
		     out = None,
		     dtype=torch.int64,
		     layout=torch.strided,
		     device=None,
		     requires_grad=False)

功能:生成从0到n-1的随机排列,一般用来生成索引,乱序

  • n:张量的长度
3.9 torch.bernoulli()
torch.bernoulli(input,
		     *,
		     generator=None,
		     out = None)

功能:以input为概率,生成伯努利分布(0-1分布,两点分布)

  • input:概率值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值