网络编程
位置:首页>> 网络编程>> Python编程>> pytorch创建tensor函数详情

pytorch创建tensor函数详情

作者:就是一顿骚操作  发布时间:2021-10-30 15:09:56 

标签:pytorch,创建,tensor,函数

1、通过复制数据构造张量

1.1 torch.tensor()

torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])
Out[111]: 
tensor([[0.1000, 1.2000],
        [2.2000, 3.1000],
        [4.9000, 5.2000]])
torch.tensor([0, 1]) 
Out[112]: tensor([0, 1])
torch.tensor([[0.11111, 0.222222, 0.3333333]],
             dtype=torch.float64,              device=torch.device('cpu'))
Out[113]: tensor([[0.1111, 0.2222, 0.3333]], dtype=torch.float64)
torch.tensor(3.14159)
Out[114]: tensor(3.1416)
torch.tensor([]) 
Out[115]: tensor([])

torch.tensor([[0.11111, 0.222222, 0.3333333]],
             dtype=torch.float64,              device=torch.device('cpu'), requires_grad=True, pin_memory=False)
Out[117]: tensor([[0.1111, 0.2222, 0.3333]], dtype=torch.float64, requires_grad=True)
  • dtype(torch.dtype,可选)–返回张量的所需数据类型。默认值:如果没有,则根据数据推断数据类型。

  • device(torch.device,可选)–构造张量的装置。如果没有,并且数据是张量,那么就使用数据设备。如果没有且数据不是张量,则结果张量在CPU上构造。

  • require_grad(bool,可选)– 是否需要保留梯度信息。默认值:False。

  • pin_memory(bool,可选)–如果设置了,返回的张量将分配到pind内存中。仅适用于CPU张量。默认值:False。

1.2 将numpy的ndarray转为tensor

>>> a = numpy.array([1, 2, 3])
>>> t = torch.as_tensor(a)
>>> t
tensor([1, 2, 3])
>>> t[0] = -1
>>> a
array([-1,  2,  3])
>>> a = numpy.array([1, 2, 3])
>>> t = torch.as_tensor(a, device=torch.device('cuda'))
>>> t[0] = -1
>>> a
array([1, 2, 3])

t = torch.as_tensor([2, 2, 2], device=torch.device('cuda'))
>>> t
tensor([2, 2, 2], device='cuda:0')

a = numpy.array([1, 2, 3])
t = torch.from_numpy(a)
t
Out[38]: tensor([1, 2, 3])
t[0] = -1
a
Out[40]: array([-1,  2,  3])

2、生成全0或者全1的tensor

torch.zeros(2, 3)
Out[41]: 
tensor([[0., 0., 0.],
        [0., 0., 0.]])
torch.zeros(5)
Out[42]: tensor([0., 0., 0., 0., 0.])

torch.ones(2, 3)
Out[43]: 
tensor([[1., 1., 1.],
        [1., 1., 1.]])
torch.ones(5)
Out[44]: tensor([1., 1., 1., 1., 1.])

参数列表:

  • out:输出的对象

  • dtype: 返回的张量的所需数据类型。默认值:如果没有,则使用全局默认值(请参阅torch.set_Default_tensor_type())。

  • layout

  • device: 构造张量的装置。如果没有,并且数据是张量,那么就使用数据设备。如果没有且数据不是张量,则结果张量在CPU上构造。

  • requires_grad: 是否需要保留梯度信息。默认值:False。

3、生成序列

3.1、 生成一个指定步长的等差序列

torch.arange(5)
Out[45]: tensor([0, 1, 2, 3, 4])
torch.arange(1, 4)
Out[46]: tensor([1, 2, 3])
torch.arange(1, 2.5, 0.5)
Out[47]: tensor([1.0000, 1.5000, 2.0000])
  • start: 点集的开始值。默认值:0。

  • end: 点集的结束值

  • step: 每对相邻点之间的间隙。默认值:1,可以是小数。

3.2 生成一个指定步数的等差数列

torch.linspace(3, 10, steps=5)
Out[49]: tensor([ 3.0000,  4.7500,  6.5000,  8.2500, 10.0000])
torch.linspace(-10, 10, steps=5)
Out[50]: tensor([-10.,  -5.,   0.,   5.,  10.])
torch.linspace(start=-10, end=10, steps=1)
Out[51]: tensor([-10.])

4、生成指定大小的单位矩阵

torch.eye(3)
Out[58]: 
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

5、生成一个指定大小张量

torch.empty((2,3), dtype=torch.int64)
Out[59]: 
tensor([[0, 0, 0],
        [0, 0, 2]])

6、 创建一个指定大小的张量。张量的数据是填充的指定值

torch.full((2, 3), 3.141592)
Out[67]: 
tensor([[3.1416, 3.1416, 3.1416],
        [3.1416, 3.1416, 3.1416]])

来源:https://blog.csdn.net/weixin_36893273/article/details/123544373

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com