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

python—创建tensor(张量)

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

python—创建tensor(张量)

创建tensor 非随机创建 1.用数组创建
#首先导入PyTorch
import torch

#数组创建
import numpy as np
a=np.array([2,3.3])#维度为一的矩阵
torch.from_numpy(a)#转化为tensor
#out:tensor([2.0000, 3.3000], dtype=torch.float64)

a=np.ones([2,3])#2行3列全为1
torch.from_numpy(a)
'''out
tensor([[1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
'''

torch.zeros(3,3)
'''
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
'''
torch.eye(3,3)#不适用于3维以上
'''
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])
'''
torch.ones(3,3)
'''
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
'''
2.给定数字直接创建
#直接用用给定的数字创建tensor
torch.tensor([2.,3.2])#list,小写tensor接受的是现有的数据
#out:tensor([2.0000, 3.2000])
3.创建空的tensor
#创建一个空的tensor,数字随机
torch.empty(1)#未初始化的维度1的数据
#out:tensor([-1.8860e+26])
#Torch.FloatTensor(d1, d2, d3)

torch.Tensor(2,3)#创建一个2行3列的数,默认float类
'''out:
tensor([[2.5353e+30, 2.8026e-44, 8.0519e-42],
        [9.4081e-39, 7.8194e-25, 2.5353e+30]])'''

torch.IntTensor(2,3)#创建2行3列的整数
#注意会出现数据非常大或者非常小的情况,要记得覆盖数据
'''out
tensor([[1912602624,         20,       5746],
        [   6713856,  393347072, 1912602624]], dtype=torch.int32)
'''
4.如何更改默认类型
torch.set_default_tensor_type(torch.DoubleTensor)#设置默认类型为double
随机初始化 1.产生0-1之间

采用rand随即产生0-1之间的数字填充在创建的张量中

a=torch.rand(3,3)#随机2行3列数据,01之间
'''
tensor([[0.5724, 0.5070, 0.7747],
        [0.0624, 0.9298, 0.5318],
        [0.8444, 0.1081, 0.1214]])
'''
torch.rand_like(a)#_like代表的就是tensor函数,随机生成一个像a一样的3行3列的数
'''
tensor([[0.1703, 0.8234, 0.6707],
        [0.2379, 0.7012, 0.6451],
        [0.6607, 0.2193, 0.7388]])
'''
2.自定义区间

randiant自定义区间

torch.randint(1,10,[3,3])#自定义区间,最大值不包含在区间内
#区间1-10,数据是3*3的矩阵
3.自定义均值和方差
#自定义均值和方差
torch.normal(mean=torch.full([10],0.),std=torch.arange(1,0.,-0.1))
#torch.full([10],0.)生成度为10,均值为0
#std=torch.arange(1,0.,-0.1),方差在慢慢减少
4.重复数

tensor中的数字一样

#生成2行3列全是7
torch.full([2,3],7)
arange/range
#生成0-9数
torch.arange(0,10)
#tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
#range在torch中不建议使用
torch.range(0,10)
linspace/logspace等分¶
#边包含[0,10],等分切steps个数字
torch.linspace(0,10,steps=4)
#tensor([ 0.0000,  3.3333,  6.6667, 10.0000])

#返回logx
torch.logspace(0,-1,steps=10)
#tensor([1.0000, 0.7743, 0.5995, 0.4642, 0.3594, 0.2783, 0.2154, 0.1668, 0.1292,0.1000])
randperm
torch.randperm(10)#0-9之间随机生成,随即打散
#tensor([2, 6, 4, 9, 1, 3, 7, 0, 8, 5])

#掉换行
a=torch.rand(3,3)
b=torch.rand(3,2)
idx=torch.randperm(3)
a,b,idx
'''
(tensor([[0.5896, 0.2464, 0.6245],
         [0.0282, 0.2187, 0.4708],
         [0.8680, 0.9148, 0.7411]]),
 tensor([[0.7101, 0.0145],
         [0.3003, 0.3720],
         [0.4903, 0.2437]]))
 tensor([0, 2, 1])'''
idx=torch.randperm(3)
idx
#tensor([2, 1, 0])
a[idx]#给a做索引,相反
b[idx]#idx保持一致,随机打散的种子
'''
tensor([[0.8680, 0.9148, 0.7411],
        [0.0282, 0.2187, 0.4708],
        [0.5896, 0.2464, 0.6245]])  
tensor([[0.4903, 0.2437],
        [0.3003, 0.3720],
        [0.7101, 0.0145]])'''      
         
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1010090.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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