文章目錄
- 張量的創建
- 1. 安裝 PyTorch
- 2. 基本創建方式
- 2.1 torch.tensor 根據指定數據創建張量
- 2.2. torch.Tensor 根據形狀創建張量, 其也可用來創建指定數據的張量
- 2.3. 指定類型的張量創建
- 2.3.1. torch.IntTensor:創建整數類型張量
- 2.3.2. torch.FloatTensor:創建浮點類型張量
- 2.3.3. torch.DoubleTensor:創建雙精度浮點類型張量
- 2.4. 指定數據類型的其他方式
- 2.5. 總結
- 3. 創建線性和隨機張量
- 3.1 創建線性張量
- 3.1.1. torch.arange:創建等間隔的整數張量
- 3.1.2. torch.linspace:創建等間隔的浮點數張量
- 3.2. 隨機種子設置
- 3.2.1 torch.random.manual_seed:設置隨機種子
- 3.3. 創建隨機張量
- 3.3.1. torch.randn:創建標準正態分布的隨機張量
- 3.3.2. torch.rand:創建均勻分布的隨機張量
- 3.3.3. torch.randint:創建整數隨機張量
- 3.4. 總結
- 4. 創建全 0\1 張量
- 4.1. 創建全1 張量
- 4.1.1. torch.ones:創建全 1 張量
- 4.1.2. torch.ones_like:創建與輸入張量形狀相同的全 1 張量
- 4.2. 創建全 0 張量
- 4.2.1. torch.zeros:創建全 0 張量
- 4.2.2. torch.zeros_like:創建與輸入張量形狀相同的全 0 張量
- 4.3. 創建全為指定值的張量
- 4.3.1. torch.full:創建全為指定值的張量
- 4.3.2. torch.full_like:創建與輸入張量形狀相同的全為指定值的張量
- 4.4. 總結
- 5. 張量元素類型轉換
- 5.1. 使用 tensor.type() 方法
- 5.2. 使用 tensor.double() 方法
- 5.3. 其他類型轉換方法
- 5.4. 使用 tensor.to() 方法
- 5.4.1. 轉換數據類型
- 5.4.2. 同時轉換設備和數據類型
- 5.5. 總結
張量的創建
PyTorch 是一個廣泛使用的開源深度學習框架,由 Facebook 的 AI 研究團隊開發,并以其靈活性和易于使用的特性而受到許多研究者和開發者的喜愛。它將數據封裝成張量(Tensor)來進行運算。PyTorch 中的張量就是元素為同一種數據類型的多維矩陣。在 PyTorch 中,張量以 “類” 的形式封裝起來,對張量的一些運算、處理的方法被封裝在類中。
1. 安裝 PyTorch
首先,確保你已經安裝了 PyTorch。你可以通過 Python 的包管理器 pip 來安裝它:
pip install torch torchvision
2. 基本創建方式
torch.IntTensor、torch.FloatTensor、torch.DoubleTensor 創建指定類型的張量
2.1 torch.tensor 根據指定數據創建張量
torch.tensor 是直接從數據(如列表、元組等)創建張量的方法。它會根據輸入的數據推斷數據類型(dtype)。
import torch
# 從列表創建張量
data = [1, 2, 3, 4]
x = torch.tensor(data)
print(x)
print(x.dtype) # 默認推斷為 torch.int64
程序輸出:
tensor([1, 2, 3, 4])
torch.int64
2.2. torch.Tensor 根據形狀創建張量, 其也可用來創建指定數據的張量
torch.Tensor 是一個類,可以用來創建未初始化的張量。它接受形狀(shape)作為參數,返回一個未初始化的張量。
# 創建一個 2x3 的未初始化張量
x = torch.Tensor(2, 3)
print(x)
輸出:
tensor([[1.4013e-45, 0.0000e+00, 1.4013e-45],[0.0000e+00, 1.4013e-45, 0.0000e+00]])
注意:torch.Tensor 也可以用來創建指定數據的張量,但不推薦這種方式,因為它的行為不如 torch.tensor 直觀。
# 不推薦:從列表創建張量
data = [1, 2, 3, 4]
x = torch.Tensor(data)
print(x)
輸出:
tensor([1., 2., 3., 4.])
2.3. 指定類型的張量創建
PyTorch 提供了多種數據類型(dtype),可以通過以下方式創建指定類型的張量:
2.3.1. torch.IntTensor:創建整數類型張量
# 創建一個 2x2 的整數類型張量
x = torch.IntTensor([[1, 2], [3, 4]])
print(x)
print(x.dtype) # torch.int32
輸出:
tensor([[1, 2],[3, 4]], dtype=torch.int32)
torch.int32
2.3.2. torch.FloatTensor:創建浮點類型張量
# 創建一個 2x2 的浮點類型張量
x = torch.FloatTensor([[1, 2], [3, 4]])
print(x)
print(x.dtype) # torch.float32
輸出:
ensor([[1., 2.],[3., 4.]])
torch.float32
2.3.3. torch.DoubleTensor:創建雙精度浮點類型張量
# 創建一個 2x2 的雙精度浮點類型張量
x = torch.DoubleTensor([[1, 2], [3, 4]])
print(x)
print(x.dtype) # torch.float64
輸出:
tensor([[1., 2.],[3., 4.]], dtype=torch.float64)
torch.float64
2.4. 指定數據類型的其他方式
除了使用 torch.IntTensor、torch.FloatTensor 等,還可以通過 dtype 參數直接指定數據類型。
# 創建一個 2x2 的浮點類型張量
x = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32)
print(x)
print(x.dtype) # torch.float32
輸出:
tensor([[1., 2.],[3., 4.]])
torch.float32
2.5. 總結
方法 | 用途 |
---|---|
torch.tensor(data) | 根據數據創建張量,自動推斷數據類型。 |
torch.Tensor(shape) | 根據形狀創建未初始化的張量。 |
torch.IntTensor(data) | 創建整數類型(torch.int32)的張量。 |
torch.FloatTensor(data) | 創建浮點類型(torch.float32)的張量。 |
torch.DoubleTensor(data) | 創建雙精度浮點類型(torch.float64)的張量。 |
torch.tensor(data, dtype=…) | 創建指定數據類型的張量(推薦,更直觀且靈活)。 |
3. 創建線性和隨機張量
3.1 創建線性張量
3.1.1. torch.arange:創建等間隔的整數張量
torch.arange 用于創建一個從起始值到結束值(不包括結束值)的等間隔整數張量。
import torch
# 創建一個從 0 到 9 的張量
x = torch.arange(10)
print(x)
輸出:
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
可以指定起始值、結束值和步長:
# 創建一個從 2 到 8,步長為 2 的張量
x = torch.arange(2, 10, 2)
print(x)
輸出:
tensor([2, 4, 6, 8])
3.1.2. torch.linspace:創建等間隔的浮點數張量
torch.linspace 用于創建一個從起始值到結束值的等間隔浮點數張量,可以指定元素的數量。
# 創建一個從 0 到 1 的 5 個等間隔元素的張量
x = torch.linspace(0, 1, 5)
print(x)
輸出:
tensor([0.0000, 0.2500, 0.5000, 0.7500, 1.0000])
3.2. 隨機種子設置
為了保證隨機實驗的可重復性,可以設置隨機種子。
3.2.1 torch.random.manual_seed:設置隨機種子
# 設置隨機種子
torch.random.manual_seed(42)
# 創建一個隨機張量
x = torch.randn(2, 2)
print(x)
輸出:
tensor([[ 0.3367, 0.1288],[ 0.2345, 0.2303]])
注:每次運行代碼時,生成的隨機張量都會相同。
3.3. 創建隨機張量
3.3.1. torch.randn:創建標準正態分布的隨機張量
torch.randn 用于創建服從標準正態分布(均值為 0,方差為 1)的隨機張量。
# 創建一個 2x2 的標準正態分布隨機張量
x = torch.randn(2, 2)
print(x)
輸出:
tensor([[-0.4032, 0.8380],[-1.3886, -0.2935]])
3.3.2. torch.rand:創建均勻分布的隨機張量
torch.rand 用于創建在 [0, 1) 區間內均勻分布的隨機張量。
# 創建一個 2x2 的均勻分布隨機張量
x = torch.rand(2, 2)
print(x)
輸出:
tensor([[0.1234, 0.5678],[0.9101, 0.2345]])
3.3.3. torch.randint:創建整數隨機張量
torch.randint 用于創建指定范圍內的整數隨機張量。
# 創建一個 2x2 的隨機整數張量,范圍在 [0, 10)
x = torch.randint(0, 10, (2, 2))
print(x)
輸出:
tensor([[3, 7],[2, 5]])
3.4. 總結
方法 | 用途 |
---|---|
torch.arange(start, end, step) | 創建等間隔的整數張量。 |
torch.linspace(start, end, steps) | 創建等間隔的浮點數張量。 |
torch.random.manual_seed(seed) | 設置隨機種子,確保實驗可重復。 |
torch.randn(shape) | 創建標準正態分布的隨機張量。 |
torch.rand(shape) | 創建 [0, 1) 區間內均勻分布的隨機張量。 |
torch.randint(low, high, shape) | 創建指定范圍內的整數隨機張量。 |
4. 創建全 0\1 張量
4.1. 創建全1 張量
4.1.1. torch.ones:創建全 1 張量
torch.ones 用于創建指定形狀的全 1 張量。
import torch# 創建一個 2x3 的全 1 張量
x = torch.ones(2, 3)
print(x)
輸出:
tensor([[1., 1., 1.],[1., 1., 1.]])
4.1.2. torch.ones_like:創建與輸入張量形狀相同的全 1 張量
torch.ones_like 用于創建一個與輸入張量形狀相同的全 1 張量。
# 創建一個與 x 形狀相同的全 1 張量
y = torch.ones_like(x)
print(y)
輸出:
tensor([[1., 1., 1.],[1., 1., 1.]])
4.2. 創建全 0 張量
4.2.1. torch.zeros:創建全 0 張量
torch.zeros 用于創建指定形狀的全 0 張量。
# 創建一個 2x3 的全 0 張量
x = torch.zeros(2, 3)
print(x)
輸出:
tensor([[0., 0., 0.],[0., 0., 0.]])
4.2.2. torch.zeros_like:創建與輸入張量形狀相同的全 0 張量
torch.zeros_like 用于創建一個與輸入張量形狀相同的全 0 張量。
# 創建一個與 x 形狀相同的全 0 張量
y = torch.zeros_like(x)
print(y)
輸出:
tensor([[0., 0., 0.],[0., 0., 0.]])
4.3. 創建全為指定值的張量
4.3.1. torch.full:創建全為指定值的張量
torch.full 用于創建指定形狀且所有元素為指定值的張量。
# 創建一個 2x3 的全為 5 的張量
x = torch.full((2, 3), 5)
print(x)
輸出:
tensor([[5, 5, 5],[5, 5, 5]])
4.3.2. torch.full_like:創建與輸入張量形狀相同的全為指定值的張量
torch.full_like 用于創建一個與輸入張量形狀相同且所有元素為指定值的張量。
# 創建一個與 x 形狀相同的全為 10 的張量
y = torch.full_like(x, 10)
print(y)
輸出:
tensor([[10, 10, 10],[10, 10, 10]])
4.4. 總結
方法 | 用途 |
---|---|
torch.ones(shape) | 創建指定形狀的全 1 張量。 |
torch.ones_like(input) | 創建與輸入張量形狀相同的全 1 張量。 |
torch.zeros(shape) | 創建指定形狀的全 0 張量。 |
torch.zeros_like(input) | 創建與輸入張量形狀相同的全 0 張量。 |
torch.full(shape, value) | 創建指定形狀且所有元素為指定值的張量。 |
torch.full_like(input, value) | 創建與輸入張量形狀相同且所有元素為指定值的張量。 |
5. 張量元素類型轉換
5.1. 使用 tensor.type() 方法
tensor.type() 方法可以將張量轉換為指定的類型。
import torch# 創建一個浮點類型的張量
x = torch.tensor([1.0, 2.0, 3.0])
print(x.dtype) # 默認是 torch.float32# 轉換為雙精度浮點類型 (torch.float64)
x_double = x.type(torch.DoubleTensor)
print(x_double)
print(x_double.dtype)
輸出:
torch.float32
tensor([1., 2., 3.], dtype=torch.float64)
torch.float64
5.2. 使用 tensor.double() 方法
tensor.double() 是 tensor.type(torch.DoubleTensor)
的簡寫形式,用于將張量轉換為雙精度浮點類型(torch.float64)。
# 轉換為雙精度浮點類型
x_double = x.double()
print(x_double)
print(x_double.dtype)
輸出:
tensor([1., 2., 3.], dtype=torch.float64)
torch.float64
5.3. 其他類型轉換方法
PyTorch 提供了多種類型轉換方法,類似于 tensor.double(),可以直接調用:
方法 | 轉換為的類型 |
---|---|
tensor.float() | torch.float32 |
tensor.double() | torch.float64 |
tensor.half() | torch.float16 |
tensor.int() | torch.int32 |
tensor.long() | torch.int64 |
tensor.short() | torch.int16 |
tensor.byte() | torch.uint8 |
tensor.bool() | torch.bool |
示例: |
# 創建一個浮點類型的張量
x = torch.tensor([1.0, 2.0, 3.0])# 轉換為整型
x_int = x.int()
print(x_int)
print(x_int.dtype)# 轉換為布爾型
x_bool = x.bool()
print(x_bool)
print(x_bool.dtype)
輸出:
tensor([1, 2, 3], dtype=torch.int32)
torch.int32
tensor([True, True, True])
torch.bool
5.4. 使用 tensor.to() 方法
tensor.to() 是一個更通用的方法,可以用于設備(CPU/GPU)和類型的轉換。
5.4.1. 轉換數據類型
# 轉換為雙精度浮點類型
x_double = x.to(torch.float64)
print(x_double)
print(x_double.dtype)
輸出:
tensor([1., 2., 3.], dtype=torch.float64)
torch.float64
5.4.2. 同時轉換設備和數據類型
# 轉換為 GPU 上的雙精度浮點類型
if torch.cuda.is_available():x_gpu = x.to(device='cuda', dtype=torch.float64)print(x_gpu)print(x_gpu.dtype)
5.5. 總結
方法 | 用途 |
---|---|
tensor.type(torch.DoubleTensor) | 將張量轉換為指定類型(如 torch.float64)。 |
tensor.double() | 將張量轉換為雙精度浮點類型(torch.float64)。 |
tensor.float() | 將張量轉換為單精度浮點類型(torch.float32)。 |
tensor.int() | 將張量轉換為整型(torch.int32)。 |
tensor.to(dtype=…) | 通用方法,支持設備轉換和類型轉換。 |