栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytorch doc_pytorch点积?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytorch doc_pytorch点积?

在torch中张量的创建 列表中创建张量
>>> a = [1,2,3.]
>>> type(a)

>>> b = torch.tensor(a)
>>> b
tensor([1., 2., 3.])
>>> type(b)

>>> b.dtype
torch.float32
numpy中创建张量
>>> import numpy as np
>>> import torch
>>> np.random.normal(2)
2.1885829330398607
>>> np.random.normal((2,3))
array([3.1159779 , 3.46278534])
>>> a=np.random.normal((2,3))
>>> torch.tensor(a)
tensor([3.0776, 3.3629], dtype=torch.float64)
>>> b = torch.tensor(a)
>>> b
tensor([3.0776, 3.3629], dtype=torch.float64)
>>> c = torch.ones_like(b)# 产生和张量b大小一样的全是1的张量
>>> c
tensor([1., 1.], dtype=torch.float64)
>>> c = torch.zeros_like(b)#全是0
>>> c
tensor([0., 0.], dtype=torch.float64)
>>> c = torch.rand_like(b)#随机数
>>> c
tensor([0.8036, 0.6563], dtype=torch.float64)
>>> torch.rand((2,2))
tensor([[0.1414, 0.3805],
        [0.1663, 0.6417]])
>>> torch.rand([2,2])
tensor([[0.1943, 0.0680],
        [0.3675, 0.4552]])
>>> torch.rand((2,2,))
tensor([[0.8623, 0.1806],
        [0.8660, 0.9106]])
>>> torch.rand((2,2,)).dtype
torch.float32
torch属性
>>> a = torch.rand([2,2,])
>>> a
tensor([[0.6406, 0.7986],
        [0.8093, 0.2699]])
>>> a.dtype
torch.float32
>>> a.shape
torch.Size([2, 2])
>>> a.device
device(type='cpu')
Tensors的操作
# 移动到gpu上运行
if torch.cuda.is_available():
	tensor = tensor.to('cuda')

100中张量的操作

>>> a
tensor([[0.6406, 0.7986],
        [0.8093, 0.2699]])
>>> torch.is_tensor(a)
True
>>> torch.is_complex(a)
False
>>> torch.is_floating_point(a)
True
# 非零的标量张量
>>> a = torch.tensor(1.0)
>>> torch.is_nonzero(a)
True
>>> a = torch.tensor(0)
>>> torch.is_nonzero(a)
False
# 返回张量中所有元素数目
>>> a = torch.rand([2,2,])
>>> a
tensor([[0.9897, 0.1273],
        [0.2993, 0.4886]])
>>> torch.numel(a)
4

返回全0

>>> torch.zeros([5,5])
tensor([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])

设置默认数据类型

>>> torch.zeros([5,5],dtype=torch.int32)
tensor([[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]], dtype=torch.int32)
>>> torch.zeros([5,5]).dtype
torch.float32# 默认是float32类型

全1

>>> a = torch.zeros([5,5],dtype=torch.int32)
>>> b = torch.ones_like(a)
>>> b
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]], dtype=torch.int32)

arange

>>> torch.arange(5)
tensor([0, 1, 2, 3, 4])


默认开始为0

>>> torch.arange(0,5,2)
tensor([0, 2, 4])

range(现在已经不再使用了)

比arange长一个单位

>>> for i in torch.arange(10):
...    print("epoch:",i)
>>>    
epoch: tensor(0)
epoch: tensor(1)
epoch: tensor(2)
epoch: tensor(3)
epoch: tensor(4)
epoch: tensor(5)
epoch: tensor(6)
epoch: tensor(7)
epoch: tensor(8)
epoch: tensor(9)

eye:创建一个2D的张量,对角线上全为1,其他为0

>>> torch.eye(3)
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

full:创建一个大小为Size的张量,用full_value填充

>>> torch.full([2,2],5)
tensor([[5, 5],
        [5, 5]])

full_like:用已有tensor构建新的tensor,size和数据类型都和原来相同 索引、切片、聚合、旋转

cat:相当于concat

保证除了concat的那个维度以外,其他维度都是一样的

>>> a = torch.rand([2,2])
>>> b = torch.rand([2,3])
>>> a
tensor([[0.6999, 0.9184],
        [0.2796, 0.1800]])
>>> b
tensor([[0.7243, 0.3705, 0.4654],
        [0.8309, 0.4728, 0.7327]])
>>> torch.cat([a,b],dim=1)
tensor([[0.6999, 0.9184, 0.7243, 0.3705, 0.4654],
        [0.2796, 0.1800, 0.8309, 0.4728, 0.7327]])

>>> a = torch.rand([2,2])
>>> b = torch.rand([3,2])
>>> a
tensor([[0.1727, 0.4451],
        [0.4587, 0.9845]])
>>> b
tensor([[0.5461, 0.9072],
        [0.2116, 0.9429],
        [0.9225, 0.1885]])
>>> torch.cat([a,b],dim=0)
tensor([[0.1727, 0.4451],
        [0.4587, 0.9845],
        [0.5461, 0.9072],

chunk:将一个张量分割成特定数目的张量,如果维度不能被整除,最后一个会比较小。

>>> b
tensor([[0.4976, 0.0441, 0.7566],
        [0.8283, 0.6617, 0.0814],
        [0.7360, 0.8517, 0.4190]])
>>> torch.chunk(b,chunks=2)
(tensor([[0.4976, 0.0441, 0.7566],
        [0.8283, 0.6617, 0.0814]]), tensor([[0.7360, 0.8517, 0.4190]]))
>>> c,d = torch.chunk(b,chunks=2)
>>> c
tensor([[0.4976, 0.0441, 0.7566],
        [0.8283, 0.6617, 0.0814]])
>>> d
tensor([[0.7360, 0.8517, 0.4190]])

>>> b = torch.rand([3,2])
>>> b
tensor([[0.1250, 0.2930],
        [0.7466, 0.3966],
        [0.9748, 0.5332]])
>>> c,d = torch.chunk(b,chunks=2,dim=1)
>>> c
tensor([[0.1250],
        [0.7466],
        [0.9748]])
>>> d
tensor([[0.2930],
        [0.3966],
        [0.5332]])

gather: 沿着某一维取变量

>>> t = torch.tensor([[1,2],[3,4]])
>>> t
tensor([[1, 2],
        [3, 4]])
>>> torch.gather(t,1,torch.tensor([[0,0],[1,0]]))
tensor([[1, 1],
        [4, 3]])
>>> torch.gather(t,1,torch.tensor([[0,0],[0,1]]))
tensor([[1, 1],
        [3, 4]])

torch.reshape(input,shape) → Tensor
不会改变顺序,只要乘积相等

>>> a = torch.arange(4.)
>>> torch.reshape(a,(2,2))
tensor([[0., 1.],
        [2., 3.]])
>>> a = torch.arange(4.)
>>> a
tensor([0., 1., 2., 3.])
>>> torch.reshape(a,(2,2))
tensor([[0., 1.],
        [2., 3.]])
>>> b = torch.tensor([[0,1],[2,3]])
>>> torch.reshape(b,(-1,))# -1默认总的长度
tensor([0, 1, 2, 3])
>>> b
tensor([[0, 1],
        [2, 3]])

scatter_,当从src函数写入张量中,indices指名是哪个索引
inplace 加了下划线:内存位置没有发生改变

Tensor.scatter_(dim,index,src,reduce=None)→Tensor

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786702.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号