torch.linspace¶
- torch.linspace(start, end, steps, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor ¶
建立一個大小為
steps
的一維 Tensor,其值均勻分佈在從start
到end
(含) 之間。 也就是說,這些值是從 PyTorch 1.11 開始,linspace 需要 steps 參數。 使用 steps=100 來恢復先前的行為。
- 參數
- 關鍵字參數
out (Tensor, 可選) – 輸出 tensor。
dtype (torch.dtype, 可選) – 執行計算的資料類型。 預設值:如果為 None,則當
start
和end
都是實數時,使用全域預設 dtype (請參閱 torch.get_default_dtype()),當其中一個為複數時,使用對應的複數 dtype。layout (
torch.layout
, 可選) – 返回的 Tensor 的所需 layout。 預設值:torch.strided
。device (
torch.device
, 可選) – 返回的 tensor 的所需裝置。 預設值:如果None
,則對預設 tensor 類型使用目前的裝置 (請參閱torch.set_default_device()
)。device
對於 CPU tensor 類型將為 CPU,對於 CUDA tensor 類型將為目前的 CUDA 裝置。requires_grad (bool, 可選) – 如果 autograd 應該記錄返回的 tensor 上的操作。 預設值:
False
。
範例
>>> torch.linspace(3, 10, steps=5) tensor([ 3.0000, 4.7500, 6.5000, 8.2500, 10.0000]) >>> torch.linspace(-10, 10, steps=5) tensor([-10., -5., 0., 5., 10.]) >>> torch.linspace(start=-10, end=10, steps=5) tensor([-10., -5., 0., 5., 10.]) >>> torch.linspace(start=-10, end=10, steps=1) tensor([-10.])