当前位置:网站首页>4.张量数据类型和创建Tensor
4.张量数据类型和创建Tensor
2022-07-27 05:13:00 【派大星的最爱海绵宝宝】
张量数据类型
1.常用数据类型
| Data type | dtype | CPU tensor | GPU tensor |
|---|---|---|---|
| 32-bit floatiing point | torch.float32 or torch.float | torch.FloatTensor | torch.cuda.FloatTensor |
| 8-bit interger(unsigned) | torch.uinf8 | torch.ByteTensor | torch.cuda.ByteTensor |
| 32-bit interger(signed) | torch.int32 or torch.int | torch.IntTensor | torch.cuda.IntTensor |
ByteTensor两个Tensor进行比较,是否相等,返回0或1。
同一个Tensor在CPU和GPU上的数据类型不相同。
a=torch.randn(2,3)
print(a.type())
print(type(a))
print(isinstance(a,torch.FloatTensor))

print(isinstance(a,torch.cuda.FloatTensor))
a=a.cuda()
print(isinstance(a,torch.cuda.FloatTensor))

x.cuda()返回一个GPU上的引用
2.标量
标量维度dim=0,最常见的是在loss计算结果中
b=torch.tensor(1.)
print(b.dim())
print(len(b.shape))

3.向量
向量dim/rank=1,最常见的是bias的值。
c=torch.tensor([1.1,5.5])
d=torch.FloatTensor(1)
e=np.ones(1)
FloatTensor接受的是数据的shape,随机初始化,可以使用numpy进行向量的创建。
假如一个矩阵是2X2,则dim=2,size=shape=[2,2],tensor=([1,2],[3,4]),size(0)=2。
向量dim/rank=2,最常见的是batch、线性输入的值。
向量dim/rank=3,最常见的是RNN、batch和输入的值。
向量dim/rank=4,最常见的是CNN的值,图片类型。
创建Tensor
从numpy中导入
a=np.array([2,3.5])
print(torch.from_numpy(a))

从numpy导入的FLOAT其实是DOUBLE类型。
从集合list导入
tensor接受现成的数据,而Tensor接受维度和现成的数据,建议小写tensor直接写数据,大写Tensor写shape。
a=torch.tensr([1,3,5])
b=FloatTensor(2,3)
未初始化的
1.Torch.empty()
申请地址空间,不需要初始化,实际也会有初始数据,且非常不规则。
2.Tensor.FloatTensor(d1,d2,d3)
3.设置默认初始类型
默认为floattensor,增强学习一般使用double,其他一般使用float
g=torch.tensor([1.2,2])
print(g.type())
torch.set_default_tensor_type(torch.DoubleTensor)
print(torch.tensor([1.2,2]).type())

随机初始化
1. torch.rand(shape)
初始化内容范围是[0,1],维度从1开始。
a=torch.rand(3,3)
print(a)

2.torch.randint(范围,shape)
输入范围是[min,max)
a=torch.randint(2,8,[2,4])
print(a)

3.*_like
输入一个tensor
print(torch.rand_like(a))

4.torch.randn(shape)
正态分布,N(0,1),均值为0,方差为1,可通过torch.normal()修改均值和方差
a=torch.randn(3,3)
print(a)
b=torch.normal(mean=torch.full([10],0.),std=torch.arange(1,0,-0.1))
print(b)
对于mean,创建一个长度为10且全为0的,计算其均值;对于std,创建一个[1,0.9,0.8,…,0],计算其方差。
注 意 \color{#FF0000}{ 注意} 注意:如果mean中的0没有加点(.)则
5.torch.full([shape],数据)
全部为某个数,若shape为空,则是标量;若shape为单个数,则是一维的,例如shape=2,则生成tensor([数据,数据])。
a=torch.full([],1)
b=torch.full([2],3)
c=torch.full([2,3],10)

6.torch.arange(最大值,最小值,步长)
范围是[最小值,最大值),range通过arange代替。
a=torch.arange(1,10,2)

7.torch.linspace/logspace(最小值,最大值,steps=数量)
范围是[最小值,最大值],等分steps份。对于logspace,在分成b等份后,每一个数再乘以以b为底数,自身为指数的数。
a=torch.linspace(0,1,steps=10)
b=torch.logspace(0,1,steps=10)
0到1分成10等份,然后0乘以10的0次方,0.1111乘以10的0.1111次方,直到1乘以10的1次方。
8.torch.ones/zeros/eye(shape)
分别为生成全是1、0,单位矩阵。
a=torch.ones(2,3)
b=torch.zeros(3,3)
c=torch.eye(4,5)
d=torch.zeros_like(a)
单位矩阵若不是方阵,对角线如以下。
可采用前面的_like,直接把一个tensor换成其他的。
9.torch.randperm()
随机打乱,生成一个0到该数,但不包括该数的tensor。
a=torch.rand(2,3)
print(a)
idx=torch.randperm(2)
print(idx)
print(a[idx])
b=torch.rand(2,2)
print(b)
idx=torch.randperm(2)
print(idx)
print(b[idx])
randperm(2)后,生成0,1.如果是3,则生成0,1,2。
边栏推荐
- How to realize master-slave synchronization in mysql5.7
- Day 15. Deep learning radiomics can predict axillary lymphnode status in early-stage breast cancer
- 18.卷积神经网络
- MySQL二级索引中的主键——MySQL存在大量相同数据分页查询优化
- 【MVC架构】MVC模型
- Day 17.The role of news sentiment in oil futures returns and volatility forecasting
- 2021中大厂php+go面试题(2)
- The NFT market pattern has not changed. Can okaleido set off a new round of waves?
- golang控制goroutine数量以及获取处理结果
- Integration and extension of robot programming and interdisciplinary
猜你喜欢

kettle的文件名通配规则

Mysql分组后时并行统计数量

NFT new paradigm, okaleido innovation NFT aggregation trading ecosystem

If the interviewer asks you about JVM, the extra answer of "escape analysis" technology will give you extra points

【mysql学习】8

What are the conditions and procedures for opening crude oil futures accounts?

Specific matters of opening accounts of futures companies

16.过拟合欠拟合

If you encounter oom online, how to solve it?

How to apply for the return of futures account opening company?
随机推荐
Do you really know session and cookies?
Inno setup package jar + H5 + MySQL + redis into exe
Fortex Fangda releases the electronic trading ecosystem to share and win-win with customers
MySQL查询操作索引优化实践
我想不通,MySQL 为什么使用 B+ 树来作索引?
GBASE 8C——SQL参考6 sql语法(6)
DDD领域驱动设计笔记
Sealem Finance - a new decentralized financial platform based on Web3
How to realize master-slave synchronization in mysql5.7
GBase 8c产品简介
GBASE 8C——SQL参考6 sql语法(1)
Day 17.The role of news sentiment in oil futures returns and volatility forecasting
You can't even do a simple function test well. What do you take to talk about salary increase with me?
GBASE 8C——SQL参考6 sql语法(11)
How MySQL and redis ensure data consistency
Face brushing payment will never be out of date, but will continue to change
vscode打造golang开发环境以及golang的debug单元测试
Seektiger will launch STI fusion mining function to obtain Oka pass
Day 4.Social Data Sentiment Analysis: Detection of Adolescent Depression Signals
MySQL二级索引中的主键——MySQL存在大量相同数据分页查询优化