深度学习-pytorch-张量创建方式

22 阅读3分钟

衔接深度学习之pytorch, 代码和内容已经完全翻过来了。

创建张量的方式, 有四大类别

一、基本创建类型

二、创建线性张量的方式

三、创建随机张量

四、创建0,1,定制张量

```python
import torch
from numpy import dtype
```


```python
# 创建张量的4种类型
## 一、基本创建类型
# 1. 使用数据创建张量, 也可以当做数组或者ny数据转张量使用

torch.tensor([1,2,3], dtype=torch.float)
# tensor([1., 2., 3.])
```




    tensor([1., 2., 3.])




```python
# 2. 创建有shape的张量, 默认是float32
T = torch.Tensor( size=(2,3))
print(T, T.dtype, T.ndim, T.shape)
# tensor([[-9.0127e+15,  9.6830e-43,  0.0000e+00],[ 0.0000e+00,  0.0000e+00,  0.0000e+00]]) torch.float32 2 torch.Size([2, 3])
```

    tensor([[-9.0127e+15,  9.6830e-43,  0.0000e+00],
            [ 0.0000e+00,  0.0000e+00,  0.0000e+00]]) torch.float32 2 torch.Size([2, 3])
    


```python
# 3, 创建有特定类型的张量
v = torch.IntTensor( size=(2,3))
print(v, v.dtype)
# tensor([[0, 0, 0],[0, 0, 0]], dtype=torch.int32) torch.int32
```

    tensor([[0, 0, 0],
            [0, 0, 0]], dtype=torch.int32) torch.int32
    


```python
v = torch.ShortTensor( size=(2,3))
print(v, v.dtype)
# tensor([[0, 0, 0],[0, 0, 0]], dtype=torch.int16) torch.int16
```


```python
v = torch.LongTensor( size=(2,3))
print(v, v.dtype)
# tensor([[0, 0, 0],[0, 0, 0]], dtype=torch.int64) torch.int64
```


```python
v = torch.HalfTensor( size=(2,3))
print(v, v.dtype)
# tensor([[0., 0., 0.],[0., 0., 0.]], dtype=torch.float16) torch.float16
```

    tensor([[1.5318e-05, 1.5318e-05, 1.5318e-05],
            [1.9844e+00, 0.0000e+00, 0.0000e+00]], dtype=torch.float16) torch.float16
    


```python
v = torch.FloatTensor( size=(2,3))
print(v, v.dtype)
# tensor([[0, 0, 0],[0, 0, 0]], dtype=torch.int64) torch.float32
```

    tensor([[0., 0., 0.],
            [0., 0., 0.]]) torch.float32
    


```python
v = torch.DoubleTensor( size=(2,3))
print(v, v.dtype)
# tensor([[0., 0., 0.],[0., 0., 0.]], dtype=torch.float64) torch.float64
```

    tensor([[1.4681e-311,  0.0000e+00,  0.0000e+00],
            [ 0.0000e+00,  0.0000e+00,  0.0000e+00]], dtype=torch.float64) torch.float64
    


```python
## 二、创建线性张量的方式
### 1. 使用torch.arange()创建张量, 创建一个从start到end的等差数列,步长为step的线性张量, 默认是int64
torch.arange(start=0, end=10, step=2)
# tensor([0, 2, 4, 6, 8])
v = torch.arange(start=0, end=10)
print(v, v.dtype)
# tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) torch.int64
```

    tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) torch.int64
    


```python
### 2. 使用torch.linspace()创建张量, 创建一个从start到end的等差数列,步长为step的线性张量, 默认是float32, end包含
v = torch.linspace(start=0, end=10, steps=5)
print(v, v.dtype)
# tensor([0., 2.5, 5., 7.5, 10.]) torch.float32
```

    tensor([ 0.0000,  2.5000,  5.0000,  7.5000, 10.0000]) torch.float32
    


```python
## 三、创建随机张量
torch.manual_seed(22)
### 1. 使用torch.rand()创建张量, 创建一个随机张量, 默认是float32
v = torch.rand(size=(2,3))
print(v, v.dtype)
# tensor([[0.4643, 0.1722, 0.5736],[0.1342, 0.8445, 0.9602]]) torch.float32
```

    tensor([[0.4643, 0.1722, 0.5736],
            [0.1342, 0.8445, 0.9602]]) torch.float32
    


```python
### 1. 使用torch.randint()创建张量, 创建一个随机张量, 默认是int64
v = torch.randint(low=0, high=10, size=(2,3))
print(v, v.dtype)
# tensor([[5, 7, 1],[2, 5, 9]]) torch.int64
```

    tensor([[4, 0, 5],
            [3, 2, 9]]) torch.int64
    


```python
### 2. 使用torch.randn()创建张量, 创建一个随机张量, 默认是float32 正太分布
v = torch.randn(size=(2,3))
print(v, v.dtype)
# tensor([[-0.5121,  0.5121, -0.5121],[-0.5121,  0.5121, -0.5121]]) torch.float32
```

    tensor([[-0.1309,  0.4177, -0.4349],
            [ 1.5602, -0.3921, -0.1812]]) torch.float32
    


```python
## 四、创建0,1,定制张量
## 1. 使用torch.zeros()创建张量, 创建一个全0张量, 默认是float32
v = torch.zeros(size=(2,3))
print(v, v.dtype)
# tensor([[0., 0., 0.],[0., 0., 0.]]) torch.float32

```

    tensor([[0., 0., 0.],
            [0., 0., 0.]]) torch.float32
    tensor([[1., 1., 1.],
            [1., 1., 1.]]) torch.float32
    


```python
## 1.2 使用torch.zeros_like()创建张量, 创建一个全0张量, 默认是float32
b = torch.zeros_like(v)
print(b, b.dtype)
```

    tensor([[0., 0., 0.],
            [0., 0., 0.]]) torch.float32
    


```python
## 2. 使用torch.ones()创建张量, 创建一个全1张量, 默认是float32
v = torch.ones(size=(2,3))
print(v, v.dtype)
# tensor([[1., 1., 1.],[1., 1., 1.]]) torch.float32
```

    tensor([[1., 1., 1.],
            [1., 1., 1.]]) torch.float32
    


```python
## 2.2 使用torch.ones_like()创建张量, 创建一个全1张量, 默认是float32
b = torch.ones_like(v)
print(b, b.dtype)
# tensor([[1., 1., 1.],[1., 1., 1.]]) torch.float32
```

    tensor([[1., 1., 1.],
            [1., 1., 1.]]) torch.float32
    


```python

## 3. 使用torch.empty()创建张量, 创建一个随机张量, 默认是float32
v = torch.empty(size=(2,3))
print(v, v.dtype)
# tensor([[-0.5121,  0.5121, -0.5121],[-0.5121,  0.5121, -0.5121]]) torch.float32
```

    tensor([[0.1309, 0.4177, 0.4349],
            [1.5602, 0.3921, 0.1812]]) torch.float32
    


```python
### 4. 使用torch.full()创建张量, 创建一个全指定值张量, 默认是int64
v = torch.full(size=(2,3), fill_value=5)
print(v, v.dtype)
# tensor([[5, 5, 5],[5, 5, 5]]) torch.int64
```

    tensor([[5, 5, 5],
            [5, 5, 5]]) torch.int64
    


```python
### 4.1 使用torch.full_like()创建张量, 创建一个全指定值张量, 默认是int64
b = torch.full_like(v, fill_value=9)
print(b, b.dtype)
# tensor([[9, 9, 9],[9, 9, 9]]) torch.int64
```

    tensor([[9, 9, 9],
            [9, 9, 9]]) torch.int64
    


```python
# 总结一下
# 创建张量的方式, 有四大类别
# 一、基本创建类型
# 二、创建线性张量的方式
# 三、创建随机张量
# 四、创建0,1,定制张量
```