torch.linspace#
- torch.linspace(start, end, steps, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor#
建立一個大小為
steps的一維張量,其值從start到end均勻分佈,包含端點。也就是說,這些值是:從 PyTorch 1.11 開始,linspace 需要 steps 引數。使用 steps=100 可恢復之前的行為。
- 引數
- 關鍵字引數
out (Tensor, optional) – 輸出張量。
dtype (torch.dtype, 可選) – 用於執行計算的資料型別。預設值:如果為 None,則當
start和end都是實數時,使用全域性預設 dtype(參見 torch.get_default_dtype()),當其中一個為複數時,使用相應的複數 dtype。layout (
torch.layout, 可選) – 返回張量的所需佈局。預設:torch.strided。device (
torch.device, 可選) – 返回張量的所需裝置。預設:如果為None,則使用當前裝置作為預設張量型別(參見torch.set_default_device())。對於 CPU 張量型別,device將是 CPU;對於 CUDA 張量型別,device將是當前的 CUDA 裝置。requires_grad (bool, optional) – 如果 autograd 應記錄在返回的張量上的操作。預設值:
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.])