## pytorch学习(2)
### 创建tensor
1. 从list中创建tensor
2. 从numpy中创建tensor / 将numpy转换为tensor
3. 有初始化值创建
4. 无初始化值创建
5. 从tensor创建tensor(torch.**__like)
#### 从list中创建tensor
```python
import torch
a = torch.tensor([2,3.4])
print(a)
print(a.dtype)
b = torch.FloatTensor([2,3.4])
print(b)
print(b.dtype)
c = torch.IntTensor([2,3.4])
print(c)
print(c.dtype)
d = torch.tensor([[1,2],[3,4]])
print(d)
print(d.dtype)
-------------------------------------------------------
#a
tensor([2.0000, 3.4000])
torch.float32
#b
tensor([2.0000, 3.4000])
torch.float32
#c
tensor([2, 3], dtype=torch.int32)
torch.int32
#d
tensor([[1, 2],
[3, 4]])
torch.int64
```
#### 从numpy中创建tensor
- numpy是python的一个科学计算库
- 初始化方法 import numpy as np
- np.array([1,2,3])创建一个矩阵即tensor
```python
# 初始化方法
import numpy as np
import torch
a = np.array([2,3,3]) #创建一个矩阵
print(a)
print(type(a))
---------------------------------------------------------------------------------
[2 3 3]
<class 'numpy.ndarray'>
```
#### 将numpy转换为tensor类型
- 通过 torch.from_numpy() 将其转换为tensor类型
```python
# 初始化方法
import numpy as np
import torch
# 将numpy转换为tensor类型
b = torch.from_numpy(a)
print(b)
print(type(b))
print(b.dim())
---------------------------------------------------------------------------------
tensor([2, 3, 3], dtype=torch.int32)
<class 'torch.Tensor'>
1
```
#### 有初始化值创建
- torch.ones(a,b) 创建一个a行b列的全是一的tensor
- torch.zeros(a,b) 全0初始化创建tensor
- torch.eye(a,b) 创建一个a行b列的对角矩阵
- torch.arange(a,b,c) 创建一个从a到b,以c为差值的等差数组,且为左闭右开,默认c为1
- torch.linspace(a,b,c) 将向量从a到b进行c等分
- torch.randperm(n, dtype="", name=) 该api返回一个数值在0到(n-1)、并随机排列,数据类型为dtype
```python
import numpy as np
import torch
# 一、创建一个三行三列的全是1的tensor
c = torch.ones(3,3)
print(c)
print(type(c))
print(c.dim())
---------------------------------------------------------------------------------
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
<class 'torch.Tensor'>
2
# 二、全0初始化
d = torch.zeros(4,4)
print(d)
print(d.size())
print(d.dim())
---------------------------------------------------------------------------------
tensor([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
torch.Size([4, 4])
2
# 三、创建一个对角矩阵
e = torch.eye(3,3)
print(e)
---------------------------------------------------------------------------------
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
# 四、创建等差数组
f = torch.arange(1,10,2) # 默认等差为1,此处更改为2,且为左闭右开
print(f)
---------------------------------------------------------------------------------
tensor([1, 3, 5, 7, 9])
# 五、利用linspace将向量均分
g = torch.linspace(0,10,7) #从0开始到10,进行7等分
print(g)
---------------------------------------------------------------------------------
tensor([ 0.0000, 1.6667, 3.3333, 5.0000, 6.6667, 8.3333, 10.0000])
# 六、torch.randperm
a_randperm = torch.randperm(10)
print(a_randperm)
---------------------------------------------------------------------------------
tensor([0, 1, 3, 9, 4, 2, 5, 6, 8, 7])
```
#### 无初始化值创建
- torch.empty((a,b),dtype=,device="") 创建一个a行b列的tensor,由于empty不初始化,故值随机
- torch.Tensor(a,b) 创建一个a行b列的张量,无初始化(随机值)
- torch.rand(a,b) 随机生成服从均匀分布的数据,返回值为tensor
- torch.randn(a,b) 随机生成正态分布的数据,返回数据为tensor
- torch.randint(low=,high=,size=(a,b),dtype=) 返回一个填充了随机整数的张量,这些整数在low和high之间均匀生成,张量的shape由变量参数size定义
```python
# 一、函数torch.empty()返回填充有未初始化数据的张量,张量的形状由可变的参数大小定义
h = torch.empty((2,3),dtype=torch.int,device="cpu") #创建一个2行3列的tensor,由于empty不初始化,故值为随机
print(h)
---------------------------------------------------------------------------------
tensor([[-1785343488, 397, -595384939],
[ 297807692, 0, 0]], dtype=torch.int32)
# 二、torch.Tensor
#当传入数据时,torch.Tensor 使用全局默认 dtype(Float.Tensor)
I = torch.Tensor(2,3) #创建一个2行3列的张量,无初始化(随机值)
print(I)
print(type(I))
---------------------------------------------------------------------------------
tensor([[0.0000, 1.6667, 3.3333],
[5.0000, 6.6667, 8.3333]])
<class 'torch.Tensor'>
# 三、torch.rand
a_rand = torch.rand(3,3) #随机生成服从均匀分布的数据,返回值为张量
print(a_rand)
---------------------------------------------------------------------------------
tensor([[0.3637, 0.7117, 0.1330],
[0.9735, 0.2333, 0.5156],
[0.1735, 0.6980, 0.2912]])
# 四、torch.randn
a_randn = torch.randn(2,3) #随机生成服从正态分布的数据,返回数据为张量
print((a_randn))
---------------------------------------------------------------------------------
tensor([[ 1.9576, -0.2601, -0.3581],
[ 0.4199, 0.2305, -1.4449]])
# 五、torch.randint
a_randint = torch.randint(low=0,high=10,size=(3,5),dtype=int)
#返回一个填充了随机整数的张量,这些整数在low和high之间均匀生成,张量的shape由变量参数size定义
print(a_randint)
---------------------------------------------------------------------------------
tensor([[8, 6, 6, 4, 8],
[5, 1, 0, 6, 9],
[1, 9, 9, 1, 4]])
```
#### 从tensor创建tensor(torch.**__like)
> 保留原始tensor形状,改变数据
- torch.ones_like(x_data) 填充数据全为1
- torch.zeros_like(x_data) 填充数据全为0
- torch.empty_like(x_data) 填充未初始化数据
- torch.rand_like(x_data) 0-1随机填充数据
- torch.randn_like(x_data) 标准正态随机填充
- torch.randint_like(x_data,low,high) 填充[low,high)范围整数
- torch.rand_like(x_data,dtype=) 改变数据类型,添加参数dtype
```python
import torch
# 一、torch.ones_like
a_like = torch.randn(3,5)
torch.ones_like(a_like)
---------------------------------------------------------------------------------
tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
# 二、torch.zeros_like
a_like = torch.randn(3,5)
torch.zeros_like(a_like)
---------------------------------------------------------------------------------
tensor([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
# 三、torch.empty_like
a_like = torch.randn(3,5)
torch.empty_like(a_like)
---------------------------------------------------------------------------------
tensor([[ 1.1528, -0.5751, -0.1611, -1.7542, -0.2472],
[-0.5101, 0.5695, -2.4531, -0.1515, -0.2296],
[-0.6492, -0.5504, 0.0534, 1.0268, 1.7967]])
# 四、torch.rand_like
a_like = torch.randn(3,5)
torch.rand_like(a_like)
---------------------------------------------------------------------------------
tensor([[0.5981, 0.5812, 0.4548, 0.8211, 0.2102],
[0.6609, 0.6824, 0.5675, 0.9607, 0.3784],
[0.0857, 0.2712, 0.4093, 0.5663, 0.6313]])
# 五、torch.randn_like
a_like = torch.randn(3,5)
torch.randn_like(a_like)
---------------------------------------------------------------------------------
tensor([[ 0.3695, 0.7398, -0.2817, -0.4260, 1.3526],
[-0.2895, 0.1544, -2.9916, -1.3148, 0.4722],
[ 0.3858, 1.5086, 0.8246, -0.7382, 2.3265]])
# 六、torch.randint_like
a = torch.randn(3,5)
torch.randint_like(a,1,10)
---------------------------------------------------------------------------------
tensor([[1., 1., 9., 8., 4.],
[7., 6., 7., 8., 9.],
[8., 1., 2., 6., 7.]])
# 七、torch.rand_like(a,dtype=)
a = torch.randn(3,5)
torch.rand_like(a,dtype=float)
---------------------------------------------------------------------------------
tensor([[0.4782, 0.6967, 0.4566, 0.5231, 0.3441],
[0.9297, 0.0575, 0.8996, 0.0506, 0.3039],
[0.8803, 0.1614, 0.8760, 0.5411, 0.5958]], dtype=torch.float64)
```
标签:tensor,创建,torch,---------------------------------------------------------------- From: https://www.cnblogs.com/311dih/p/16583849.html