PyTorch torch

2020-09-14 11:41 更新
譯者:https//pytorch.org/docs/stable/torch.html

此外,它提供了許多實(shí)用程序,可有效地序列化張量和任意類型,以及其他有用的實(shí)用程序。

它具有CUDA對(duì)應(yīng)項(xiàng),使您能夠具有計(jì)算能力> = 3.0的NVIDIA GPU上運(yùn)行張量計(jì)算。

張量

  1. torch.is_tensor(obj)?

如果<cite> obj </ cite>是PyTorch張量,則返回True。

參數(shù)

obj(對(duì)象)–要測(cè)試的對(duì)象

  1. torch.is_storage(obj)?

如果<cite> obj </ cite>是PyTorch存儲(chǔ)對(duì)象,則返回True。

參量

obj(對(duì)象)–要測(cè)試的對(duì)象

  1. torch.is_floating_point(input) -> (bool)?

如果input的數(shù)據(jù)類型是浮點(diǎn)數(shù)據(jù)類型,即torch.float64,torch.float32torch.float16之一,則返回True。

參量

輸入(tensor)–要測(cè)試的PyTorch張量

  1. torch.set_default_dtype(d)?

將默認(rèn)浮點(diǎn)D型設(shè)置為d。類型該用作將  torch.tensor() 中類型推斷的默認(rèn)浮點(diǎn)類型。

默認(rèn)浮點(diǎn)dtype最初為torch.float32。

參量

d(torch.dtype)–浮點(diǎn)dtype,變?yōu)槌蔀橹?/font>

例:

  1. >>> torch.tensor([1.2, 3]).dtype # initial default for floating point is torch.float32
  2. torch.float32
  3. >>> torch.set_default_dtype(torch.float64)
  4. >>> torch.tensor([1.2, 3]).dtype # a new floating point tensor
  5. torch.float64
  1. torch.get_default_dtype() torch.dtype?

獲取當(dāng)前的默認(rèn)浮點(diǎn)數(shù)  torch.dtype 。

例:

  1. >>> torch.get_default_dtype() # initial default for floating point is torch.float32
  2. torch.float32
  3. >>> torch.set_default_dtype(torch.float64)
  4. >>> torch.get_default_dtype() # default is now changed to torch.float64
  5. torch.float64
  6. >>> torch.set_default_tensor_type(torch.FloatTensor) # setting tensor type also affects this
  7. >>> torch.get_default_dtype() # changed to torch.float32, the dtype for torch.FloatTensor
  8. torch.float32
  1. torch.set_default_tensor_type(t)?

默認(rèn)將的torch.Tensor類型設(shè)置為浮點(diǎn)張量類型t。類型該用作還將  torch.tensor() 中類型推斷的默認(rèn)浮點(diǎn)類型。

默認(rèn)的浮點(diǎn)張量類型最初為torch.FloatTensor。

參量

t(python:type或同軸)–浮點(diǎn)張量類型類型名稱

例:

  1. >>> torch.tensor([1.2, 3]).dtype # initial default for floating point is torch.float32
  2. torch.float32
  3. >>> torch.set_default_tensor_type(torch.DoubleTensor)
  4. >>> torch.tensor([1.2, 3]).dtype # a new floating point tensor
  5. torch.float64
  1. torch.numel(input) int?

返回input張量中的元素總數(shù)。

參量

輸入(張量)–輸入張量。

例:

  1. >>> a = torch.randn(1, 2, 3, 4, 5)
  2. >>> torch.numel(a)
  3. 120
  4. >>> a = torch.zeros(4,4)
  5. >>> torch.numel(a)
  6. 16
  1. torch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None, sci_mode=None)?

設(shè)置打印選項(xiàng)。從NumPy無(wú)恥地拿走的物品

參量

  • precision –浮點(diǎn)輸出的精度位數(shù)(默認(rèn)= 4)。
  • 閾值 –觸發(fā)匯總而不是完整的 <cite>repr</cite> 的數(shù)組元素總數(shù)(默認(rèn)= 1000)。
  • edgeitems -每個(gè)維的開(kāi)始和結(jié)束處摘要中數(shù)組項(xiàng)目的數(shù)量(默認(rèn)= 3)。
  • 線寬 –用于插入換行符的每行字符數(shù)(默認(rèn)= 80)。 閾值矩陣將忽略此參數(shù)。
  • 配置文件 – Sane 默認(rèn)用于漂亮的打印。 可以使用以上任何選項(xiàng)覆蓋。 (<cite>默認(rèn)</cite>,<cite>短</cite>,<cite>完整</cite>中的任何一種)
  • sci_mode –啟用(真)或禁用(假)科學(xué)計(jì)數(shù)法。 如果指定了 None(默認(rèn)),則該值由 <cite>_Formatter</cite> 定義
  1. torch.set_flush_denormal(mode) bool?

禁用 CPU 上的非正常浮??點(diǎn)數(shù)。

如果您的系統(tǒng)支持刷新非正規(guī)數(shù)并且已成功配置刷新非正規(guī)模式,則返回True。 set_flush_denormal() 僅在支持 SSE3 的 x86 架構(gòu)上受支持。

Parameters

模式 (bool )–控制是否啟用沖洗非正常模式

Example:

  1. >>> torch.set_flush_denormal(True)
  2. True
  3. >>> torch.tensor([1e-323], dtype=torch.float64)
  4. tensor([ 0.], dtype=torch.float64)
  5. >>> torch.set_flush_denormal(False)
  6. True
  7. >>> torch.tensor([1e-323], dtype=torch.float64)
  8. tensor(9.88131e-324 *
  9. [ 1.0000], dtype=torch.float64)

創(chuàng)作行動(dòng)

注意

隨機(jī)抽樣創(chuàng)建操作列在隨機(jī)抽樣下,包括: torch.rand() torch.rand_like()  torch.randn() torch.randn_like() torch.randint() torch.randint_like()  torch.randperm() 您也可以將 torch.empty() 與輸入一起使用 位隨機(jī)抽樣方法來(lái)創(chuàng)建 torch.Tensor ,并從更廣泛的分布范圍內(nèi)采樣值。

  1. torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False) Tensor?

data構(gòu)造一個(gè)張量。

警告

torch.tensor() 始終復(fù)制data。 如果您具有張量data并希望避免復(fù)制,請(qǐng)使用 torch.Tensor.requires_grad_() 或  torch.Tensor.detach() 。 如果您有 NumPy ndarray并想避免復(fù)制,請(qǐng)使用 torch.as_tensor() 。

Warning

當(dāng)數(shù)據(jù)是張量 <cite>x</cite> 時(shí), torch.tensor() 從傳遞的任何數(shù)據(jù)中讀出“數(shù)據(jù)”,并構(gòu)造一個(gè)葉子變量。 因此,torch.tensor(x)等同于x.clone().detach()torch.tensor(x, requires_grad=True)等同于x.clone().detach().requires_grad_(True)。 建議使用clone()detach()的等效項(xiàng)。

Parameters

  • 數(shù)據(jù) (array_like )–張量的初始數(shù)據(jù)。 可以是列表,元組,NumPy ndarray,標(biāo)量和其他類型。
  • dtype (torch.dtype ,可選)–返回張量的所需數(shù)據(jù)類型。 默認(rèn)值:如果None,則從data推斷數(shù)據(jù)類型。
  • 設(shè)備 (torch.device ,可選)–返回張量的所需設(shè)備。 默認(rèn)值:如果None,則使用當(dāng)前設(shè)備作為默認(rèn)張量類型(請(qǐng)參見(jiàn) torch.set_default_tensor_type())。 device將是用于 CPU 張量類型的 CPU,并且是用于 CUDA 張量類型的當(dāng)前 CUDA 設(shè)備。
  • require_grad (bool , 可選)–如果 autograd 應(yīng)該在返回的張量上記錄操作。 默認(rèn)值:False。
  • pin_memory (bool , 可選)–如果設(shè)置,返回的張量將分配在固定的內(nèi)存中。 僅適用于 CPU 張量。 默認(rèn)值:False。

Example:

  1. >>> torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])
  2. tensor([[ 0.1000, 1.2000],
  3. [ 2.2000, 3.1000],
  4. [ 4.9000, 5.2000]])
  5. >>> torch.tensor([0, 1]) # Type inference on data
  6. tensor([ 0, 1])
  7. >>> torch.tensor([[0.11111, 0.222222, 0.3333333]],
  8. dtype=torch.float64,
  9. device=torch.device('cuda:0')) # creates a torch.cuda.DoubleTensor
  10. tensor([[ 0.1111, 0.2222, 0.3333]], dtype=torch.float64, device='cuda:0')
  11. >>> torch.tensor(3.14159) # Create a scalar (zero-dimensional tensor)
  12. tensor(3.1416)
  13. >>> torch.tensor([]) # Create an empty tensor (of size (0,))
  14. tensor([])
  1. torch.sparse_coo_tensor(indices, values, size=None, dtype=None, device=None, requires_grad=False) Tensor?

在給定values和給定values的情況下,以非零元素構(gòu)造 COO(rdinate)格式的稀疏張量。 稀疏張量可以是<cite>而不是</cite>,在那種情況下,索引中有重復(fù)的坐標(biāo),并且該索引處的值是所有重復(fù)值條目的總和: torch.sparse 。

Parameters

  • 索引 (array_like )–張量的初始數(shù)據(jù)。 可以是列表,元組,NumPy ndarray,標(biāo)量和其他類型。 將在內(nèi)部強(qiáng)制轉(zhuǎn)換為torch.LongTensor。 索引是矩陣中非零值的坐標(biāo),因此應(yīng)為二維,其中第一維是張量維數(shù),第二維是非零值數(shù)。
  • 值 (array_like )–張量的初始值。 可以是列表,元組,NumPy ndarray,標(biāo)量和其他類型。
  • 大小(列表,元組或torch.Size,可選)–稀疏張量的大小。 如果未提供,則將推斷大小為足以容納所有非零元素的最小大小。
  • dtype (torch.dtype ,可選)–返回張量的所需數(shù)據(jù)類型。默認(rèn)值:如果為 None,則從values推斷數(shù)據(jù)類型。
  • 設(shè)備 (torch.device ,可選)–返回張量的所需設(shè)備。 默認(rèn)值:如果為 None,則使用當(dāng)前設(shè)備作為默認(rèn)張量類型(請(qǐng)參見(jiàn) torch.set_default_tensor_type())。 device將是用于 CPU 張量類型的 CPU,是用于 CUDA 張量類型的當(dāng)前 CUDA 設(shè)備。
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

  1. >>> i = torch.tensor([[0, 1, 1],
  2. [2, 0, 2]])
  3. >>> v = torch.tensor([3, 4, 5], dtype=torch.float32)
  4. >>> torch.sparse_coo_tensor(i, v, [2, 4])
  5. tensor(indices=tensor([[0, 1, 1],
  6. [2, 0, 2]]),
  7. values=tensor([3., 4., 5.]),
  8. size=(2, 4), nnz=3, layout=torch.sparse_coo)
  9. >>> torch.sparse_coo_tensor(i, v) # Shape inference
  10. tensor(indices=tensor([[0, 1, 1],
  11. [2, 0, 2]]),
  12. values=tensor([3., 4., 5.]),
  13. size=(2, 3), nnz=3, layout=torch.sparse_coo)
  14. >>> torch.sparse_coo_tensor(i, v, [2, 4],
  15. dtype=torch.float64,
  16. device=torch.device('cuda:0'))
  17. tensor(indices=tensor([[0, 1, 1],
  18. [2, 0, 2]]),
  19. values=tensor([3., 4., 5.]),
  20. device='cuda:0', size=(2, 4), nnz=3, dtype=torch.float64,
  21. layout=torch.sparse_coo)
  22. ## Create an empty sparse tensor with the following invariants:
  23. ## 1\. sparse_dim + dense_dim = len(SparseTensor.shape)
  24. ## 2\. SparseTensor._indices().shape = (sparse_dim, nnz)
  25. ## 3\. SparseTensor._values().shape = (nnz, SparseTensor.shape[sparse_dim:])
  26. #
  27. ## For instance, to create an empty sparse tensor with nnz = 0, dense_dim = 0 and
  28. ## sparse_dim = 1 (hence indices is a 2D tensor of shape = (1, 0))
  29. >>> S = torch.sparse_coo_tensor(torch.empty([1, 0]), [], [1])
  30. tensor(indices=tensor([], size=(1, 0)),
  31. values=tensor([], size=(0,)),
  32. size=(1,), nnz=0, layout=torch.sparse_coo)
  33. ## and to create an empty sparse tensor with nnz = 0, dense_dim = 1 and
  34. ## sparse_dim = 1
  35. >>> S = torch.sparse_coo_tensor(torch.empty([1, 0]), torch.empty([0, 2]), [1, 2])
  36. tensor(indices=tensor([], size=(1, 0)),
  37. values=tensor([], size=(0, 2)),
  38. size=(1, 2), nnz=0, layout=torch.sparse_coo)
  1. torch.as_tensor(data, dtype=None, device=None) Tensor?

將數(shù)據(jù)轉(zhuǎn)換為<cite>torch。張量</cite>。 如果數(shù)據(jù)已經(jīng)是具有相同 <cite>dtype</cite> 和<cite>設(shè)備</cite>的<cite>張量</cite>,則不會(huì)執(zhí)行任何復(fù)制,否則將使用新的<cite>張量</cite>。 如果數(shù)據(jù)<cite>張量</cite>>具有requires_grad=True,則返回保留計(jì)算圖的計(jì)算圖。 同樣,如果數(shù)據(jù)是對(duì)應(yīng)的 <cite>dtype</cite> 的ndarray,并且<cite>設(shè)備</cite>是 cpu,則不會(huì)執(zhí)行任何復(fù)制。

Parameters

  • data (array_like) – Initial data for the tensor. Can be a list, tuple, NumPy ndarray,scalar, and other types.
  • dtype (torch.dtype, optional)  – the desired data type of returned tensor. Default:  if None, infers data type from data.
  • device(torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

Example:

  1. >>> a = numpy.array([1, 2, 3])
  2. >>> t = torch.as_tensor(a)
  3. >>> t
  4. tensor([ 1, 2, 3])
  5. >>> t[0] = -1
  6. >>> a
  7. array([-1, 2, 3])
  8. >>> a = numpy.array([1, 2, 3])
  9. >>> t = torch.as_tensor(a, device=torch.device('cuda'))
  10. >>> t
  11. tensor([ 1, 2, 3])
  12. >>> t[0] = -1
  13. >>> a
  14. array([1, 2, 3])
  1. torch.as_strided(input, size, stride, storage_offset=0) Tensor?

創(chuàng)建具有指定size,stridestorage_offset的現(xiàn)有<cite>炬管</cite> input的視圖。

Warning

創(chuàng)建的張量中的一個(gè)以上元素可以引用單個(gè)存儲(chǔ)位置。 結(jié)果,就地操作(尤其是矢量化的操作)可能會(huì)導(dǎo)致錯(cuò)誤的行為。 如果需要寫(xiě)張量,請(qǐng)先克隆它們。

許多 PyTorch 函數(shù)可返回張量視圖,并在此函數(shù)內(nèi)部實(shí)現(xiàn)。 這些功能,例如 torch.Tensor.expand() ,更易于閱讀,因此更可取。

Parameters

  • input (Tensor) – the input tensor.
  • 大小(元組 或 python:ints )–輸出張量的形狀
  • 跨度(元組 或 python:ints )–輸出張量的跨度
  • storage_offset (python:int , 可選)–輸出張量的基礎(chǔ)存儲(chǔ)中的偏移量

Example:

  1. >>> x = torch.randn(3, 3)
  2. >>> x
  3. tensor([[ 0.9039, 0.6291, 1.0795],
  4. [ 0.1586, 2.1939, -0.4900],
  5. [-0.1909, -0.7503, 1.9355]])
  6. >>> t = torch.as_strided(x, (2, 2), (1, 2))
  7. >>> t
  8. tensor([[0.9039, 1.0795],
  9. [0.6291, 0.1586]])
  10. >>> t = torch.as_strided(x, (2, 2), (1, 2), 1)
  11. tensor([[0.6291, 0.1586],
  12. [1.0795, 2.1939]])
  1. torch.from_numpy(ndarray) Tensor?

numpy.ndarray創(chuàng)建 Tensor 。

返回的張量和ndarray共享相同的內(nèi)存。 對(duì)張量的修改將反映在ndarray中,反之亦然。 返回的張量不可調(diào)整大小。

當(dāng)前它接受具有numpy.float64,numpy.float32,numpy.float16,numpy.int64,numpy.int32, numpy.int16numpy.int8,numpy.uint8numpy.bool d 類型的ndarray

Example:

  1. >>> a = numpy.array([1, 2, 3])
  2. >>> t = torch.from_numpy(a)
  3. >>> t
  4. tensor([ 1, 2, 3])
  5. >>> t[0] = -1
  6. >>> a
  7. array([-1, 2, 3])
  1. torch.zeros(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor?

返回一個(gè)由標(biāo)量值 <cite>0</cite> 填充的張量,其形狀由變量參數(shù)size定義。

Parameters

  • 大小 (python:int ... )–定義輸出張量形狀的整數(shù)序列。 可以是可變數(shù)量的參數(shù),也可以是列表或元組之類的集合。
  • 輸出 (tensor , 可選)–輸出張量。
  • dtype  (torch.dtype ,可選)–返回張量的所需數(shù)據(jù)類型。 默認(rèn)值:如果None使用全局默認(rèn)值(請(qǐng)參見(jiàn) torch.set_default_tensor_type())。
  • 布局 (torch.layout ,可選)–返回的 Tensor 所需的布局。 默認(rèn)值:torch.strided
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor.Default: False.

Example:

  1. >>> torch.zeros(2, 3)
  2. tensor([[ 0., 0., 0.],
  3. [ 0., 0., 0.]])
  4. >>> torch.zeros(5)
  5. tensor([ 0., 0., 0., 0., 0.])
  1. torch.zeros_like(input, dtype=None, layout=None, device=None, requires_grad=False) Tensor?

返回一個(gè)由標(biāo)量值 <cite>0</cite> 填充的張量,其大小與input相同。 torch.zeros_like(input)等效于torch.zeros(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)。

Warning

從 0.4 開(kāi)始,此功能不支持out關(guān)鍵字。 作為替代,舊的torch.zeros_like(input, out=output)等效于torch.zeros(input.size(), out=output)

Parameters

  • 輸入 (tensor)– input的大小將確定輸出張量的大小。
  • dtype (torch.dtype ,可選)–返回的 Tensor 的所需數(shù)據(jù)類型。 默認(rèn)值:如果為None,則默認(rèn)為input的dtype。
  • 布局 (torch.layout ,可選)–返回張量的所需布局。 默認(rèn)值:如果為None,則默認(rèn)為input的布局。
  • 設(shè)備 (torch.device ,可選)–返回張量的所需設(shè)備。 默認(rèn)值:如果為None,則默認(rèn)為input的設(shè)備。
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Defaultt: False.

Example:

  1. >>> input = torch.empty(2, 3)
  2. >>> torch.zeros_like(input)
  3. tensor([[ 0., 0., 0.],
  4. [ 0., 0., 0.]])
  1. torch.ones(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor?

返回一個(gè)由標(biāo)量值 <cite>1</cite> 填充的張量,其形狀由變量自變量size定義。

Parameters

  • size (python:int...) – a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
  • out  (Tensor, optional) – the output tensor.
  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see  torch.set_default_tensor_type() ).
  • layout  (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
  • device(torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

  1. >>> torch.ones(2, 3)
  2. tensor([[ 1., 1., 1.],
  3. [ 1., 1., 1.]])
  4. >>> torch.ones(5)
  5. tensor([ 1., 1., 1., 1., 1.])
  1. torch.ones_like(input, dtype=None, layout=None, device=None, requires_grad=False) Tensor?

返回一個(gè)由標(biāo)量值 <cite>1</cite> 填充的張量,其大小與input相同。 torch.ones_like(input)等效于torch.ones(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)。

Warning

從 0.4 開(kāi)始,此功能不支持out關(guān)鍵字。 作為替代,舊的torch.ones_like(input, out=output)等效于torch.ones(input.size(), out=output)。

Parameters

  • input (Tensor) – the size of input will determine size of the output tensor.
  • dtype(torch.dtype, optional)  – the desired data type of returned Tensor. Default: if None, defaults to the dtype of input.
  • layout(torch.layout, optional)– the desired layout of returned tensor. Default: if None, defaults to the layout of input.
  • device  (torch.device, optional)– the desired device of returned tensor. Default: if None, defaults to the device of input.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor.Default: False

Example:

  1. >>> input = torch.empty(2, 3)
  2. >>> torch.ones_like(input)
  3. tensor([[ 1., 1., 1.],
  4. [ 1., 1., 1.]])
  1. torch.arange(start=0, end, step=1, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor?

返回大小為 的一維張量,該值具有從<cite>開(kāi)始</cite>開(kāi)始具有公共差step的間隔[start, end)的值。

請(qǐng)注意,與end比較時(shí),非整數(shù)step會(huì)出現(xiàn)浮點(diǎn)舍入錯(cuò)誤; 為了避免不一致,在這種情況下,建議在end中添加一個(gè)小的ε。

Parameters

  • 起始(編號(hào))–點(diǎn)集的起始值。 默認(rèn)值:0
  • 結(jié)束(編號(hào))–點(diǎn)集的結(jié)束值
  • 步驟(編號(hào))–每對(duì)相鄰點(diǎn)之間的間隙。 默認(rèn)值:1。
  • out (Tensor, optional) – the output tensor.
  • dtype (torch.dtype ,可選)–返回張量的所需數(shù)據(jù)類型。 默認(rèn)值:如果None使用全局默認(rèn)值(請(qǐng)參閱 torch.set_default_tensor_type())。 如果未提供 <cite>dtype</cite> ,則從其他輸入?yún)?shù)推斷數(shù)據(jù)類型。 如果<cite>開(kāi)始</cite>,<cite>結(jié)束</cite>或<cite>停止</cite>中的任何一個(gè)是浮點(diǎn),則推斷 <cite>dtype</cite> 為默認(rèn) dtype,請(qǐng)參見(jiàn)[ get_default_dtype() 。 否則,將 <cite>dtype</cite> 推斷為 <cite>torch.int64</cite> 。
  • layout  (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default False.

Example:

  1. >>> torch.arange(5)
  2. tensor([ 0, 1, 2, 3, 4])
  3. >>> torch.arange(1, 4)
  4. tensor([ 1, 2, 3])
  5. >>> torch.arange(1, 2.5, 0.5)
  6. tensor([ 1.0000, 1.5000, 2.0000])
  1. torch.range(start=0, end, step=1, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor?

在步驟step中返回大小為 的一維張量,其值從startend。 階躍是張量中兩個(gè)值之間的差距。

Warning

不推薦使用此功能,而推薦使用 torch.arange() 。

Parameters

  • start (python:float )–點(diǎn)集的起始值。 默認(rèn)值:0。
  • end (python:float )–點(diǎn)集的結(jié)束值
  • 步驟 (python:float )–每對(duì)相鄰點(diǎn)之間的間隙。 默認(rèn)值:1。
  • out (Tensor, optional) – the output tensor.
  • dtype(torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see  torch.set_default_tensor_type() ). If <cite>dtype</cite> is not given, infer the data type from the other input arguments. If any of <cite>start</cite>, <cite>end</cite>, or <cite>stop</cite> are floating-point, the <cite>dtype</cite> is inferred to be the default dtype, see get_default_dtype(). Otherwise, the <cite>dtype</cite> is inferred to be <cite>torch.int64</cite>.
  • layout  (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
  • device  (torch.device, optional) – the desired device of returned tensor. Default:  if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

  1. >>> torch.range(1, 4)
  2. tensor([ 1., 2., 3., 4.])
  3. >>> torch.range(1, 4, 0.5)
  4. tensor([ 1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000])
  1. torch.linspace(start, end, steps=100, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor?

返回startend之間等距點(diǎn)的steps的一維張量。

輸出張量為steps大小的 1-D。

Parameters

  • 開(kāi)始 (python:float )–點(diǎn)集的起始值
  • end (python:float) – the ending value for the set of points
  • 步驟 (python:int )–在startend之間采樣的點(diǎn)數(shù)。 默認(rèn)值:100。
  • out (Tensor, optional) – the output tensor.
  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see  torch.set_default_tensor_type() ).
  • layout  (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

  1. >>> torch.linspace(3, 10, steps=5)
  2. tensor([ 3.0000, 4.7500, 6.5000, 8.2500, 10.0000])
  3. >>> torch.linspace(-10, 10, steps=5)
  4. tensor([-10., -5., 0., 5., 10.])
  5. >>> torch.linspace(start=-10, end=10, steps=5)
  6. tensor([-10., -5., 0., 5., 10.])
  7. >>> torch.linspace(start=-10, end=10, steps=1)
  8. tensor([-10.])
  1. torch.logspace(start, end, steps=100, base=10.0, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor?

返回與 之間的底數(shù)base對(duì)數(shù)間隔的steps點(diǎn)的一維張量。

The output tensor is 1-D of size steps.

Parameters

  • start (python:float) – the starting value for the set of points
  • end (python:float) – the ending value for the set of points
  • steps (python:int) – number of points to sample between start and end. Default: 100.
  • 基數(shù) (python:float )–對(duì)數(shù)函數(shù)的基數(shù)。 默認(rèn)值:10.0。
  • out (Tensor, optional) – the output tensor.
  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see  torch.set_default_tensor_type() ).
  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

  1. >>> torch.logspace(start=-10, end=10, steps=5)
  2. tensor([ 1.0000e-10, 1.0000e-05, 1.0000e+00, 1.0000e+05, 1.0000e+10])
  3. >>> torch.logspace(start=0.1, end=1.0, steps=5)
  4. tensor([ 1.2589, 2.1135, 3.5481, 5.9566, 10.0000])
  5. >>> torch.logspace(start=0.1, end=1.0, steps=1)
  6. tensor([1.2589])
  7. >>> torch.logspace(start=2, end=2, steps=1, base=2)
  8. tensor([4.0])
  1. torch.eye(n, m=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor?

返回一個(gè)二維張量,對(duì)角線上有一個(gè),其他位置為零。

Parameters

  • n (python:int )–行數(shù)
  • m (python:int , 可選)–默認(rèn)為n的列數(shù)
  • out (Tensor, optional) – the output tensor.
  • dtype  (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see  torch.set_default_tensor_type() ).
  • layout  (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.

退貨

二維張量,對(duì)角線上有一個(gè),其他位置為零

返回類型

張量

Example:

  1. >>> torch.eye(3)
  2. tensor([[ 1., 0., 0.],
  3. [ 0., 1., 0.],
  4. [ 0., 0., 1.]])
  1. torch.empty(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) Tensor?

返回填充有未初始化數(shù)據(jù)的張量。 張量的形狀由變量參數(shù)size定義。

Parameters

  • size (python:int...) – a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
  • out (Tensor, optional) – the output tensor.
  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()).
  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.
  • pin_memory (bool__, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

Example:

  1. >>> torch.empty(2, 3)
  2. tensor(1.00000e-08 *
  3. [[ 6.3984, 0.0000, 0.0000],
  4. [ 0.0000, 0.0000, 0.0000]])
  1. torch.empty_like(input, dtype=None, layout=None, device=None, requires_grad=False) Tensor?

返回與input相同大小的未初始化張量。 torch.empty_like(input)等效于torch.empty(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)。

Parameters

  • input (Tensor) – the size  of input will determine size of the output tensor.
  • dtype (torch.dtype, optional) – the desired data type of returned Tensor. Default: if None, defaults to the dtype of input.
  • layout  (torch.layout, optional) – the desired layout of returned tensor. Default: if None, defaults to the layout of input.
  • device(torch.device, optional) – the desired device of returned tensor. Default: if None, defaults to the device of input.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned Default: False.

Example:

  1. >>> torch.empty((2,3), dtype=torch.int64)
  2. tensor([[ 9.4064e+13, 2.8000e+01, 9.3493e+13],
  3. [ 7.5751e+18, 7.1428e+18, 7.5955e+18]])
  1. torch.empty_strided(size, stride, dtype=None, layout=None, device=None, requires_grad=False, pin_memory=False) Tensor?

返回填充有未初始化數(shù)據(jù)的張量。 張量的形狀和步幅分別由變量參數(shù)sizestride定義。 torch.empty_strided(size, stride)等同于torch.empty(size).as_strided(size, stride)

Warning

創(chuàng)建的張量中的一個(gè)以上元素可以引用單個(gè)存儲(chǔ)位置。 結(jié)果,就地操作(尤其是矢量化的操作)可能會(huì)導(dǎo)致錯(cuò)誤的行為。 如果需要寫(xiě)張量,請(qǐng)先克隆它們。

Parameters

  • 大小(python:ints 的元組)–輸出張量的形狀
  • 跨度(python:ints 的元組)–輸出張量的跨度
  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see  torch.set_default_tensor_type() ).
  • layout  (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.
  • pin_memory (bool__, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

Example:

  1. >>> a = torch.empty_strided((2, 3), (1, 2))
  2. >>> a
  3. tensor([[8.9683e-44, 4.4842e-44, 5.1239e+07],
  4. [0.0000e+00, 0.0000e+00, 3.0705e-41]])
  5. >>> a.stride()
  6. (1, 2)
  7. >>> a.size()
  8. torch.Size([2, 3])
  1. torch.full(size, fill_value, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor?

返回大小為size的張量,其中填充了fill_value

Parameters

  • 大小 (python:int ... )–定義輸出張量形狀的整數(shù)列表,元組或torch.Size。
  • fill_value –用來(lái)填充輸出張量的數(shù)字。
  • out (Tenso, optional) – the output tensor.
  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()).
  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

  1. >>> torch.full((2, 3), 3.141592)
  2. tensor([[ 3.1416, 3.1416, 3.1416],
  3. [ 3.1416, 3.1416, 3.1416]])
  1. torch.full_like(input, fill_value, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor?

返回與填充有fill_valueinput大小相同的張量。 torch.full_like(input, fill_value)等同于torch.full(input.size(), fill_value, dtype=input.dtype, layout=input.layout, device=input.device)。

Parameters

  • input (Tensor) – the size of input will determine size of the output tensor.
  • fill_value – the number to fill the output tensor with.
  • dtype  (torch.dtype, optional) – the desired data type of returned Tensor. Default: if None, defaults to the dtype of input.
  • layout (torch.layout, optional) – the desired layout of returned tensor. Default: if None, defaults to the layout of input.
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, defaults to the device of input.
  • requires_grad(bool__, optional) – If autograd should record operations on the returned tensor. Default: False.
  1. torch.quantize_per_tensor(input, scale, zero_point, dtype) Tensor?

將浮點(diǎn)張量轉(zhuǎn)換為具有給定比例和零點(diǎn)的量化張量。

Parameters

  • 輸入 (tensor)–浮點(diǎn)張量進(jìn)行量化
  • 標(biāo)度 (python:float )–適用于量化公式的標(biāo)度
  • zero_point (python:int )–映射為浮點(diǎn)零的整數(shù)值偏移
  • dtype (torch.dtype)–返回張量的所需數(shù)據(jù)類型。 必須是量化的 dtypes 之一:torch.quint8,torch.qint8torch.qint32

Returns

新量化的張量

Return type

Tensor

Example:

  1. >>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, torch.quint8)
  2. tensor([-1., 0., 1., 2.], size=(4,), dtype=torch.quint8,
  3. quantization_scheme=torch.per_tensor_affine, scale=0.1, zero_point=10)
  4. >>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, torch.quint8).int_repr()
  5. tensor([ 0, 10, 20, 30], dtype=torch.uint8)
  1. torch.quantize_per_channel(input, scales, zero_points, axis, dtype) Tensor?

將浮點(diǎn)張量轉(zhuǎn)換為具有給定比例和零點(diǎn)的每通道量化張量。

Parameters

  • input (Tensor) – float tensor to quantize
  • 秤 (tensor)–要使用的一維浮標(biāo)秤,尺寸應(yīng)匹配input.size(axis)
  • zero_points (python:int )–要使用的整數(shù) 1D 張量偏移量,大小應(yīng)與input.size(axis)相匹配
  • 軸 (python:int )–應(yīng)用每個(gè)通道量化的維度
  • dtype (torch.dtype) – the desired data type of returned tensor. Has to be one of the quantized dtypes: torch.quint8torch.qint8torch.qint32

Returns

A newly quantized tensor

Return type

Tensor

Example:

  1. >>> x = torch.tensor([[-1.0, 0.0], [1.0, 2.0]])
  2. >>> torch.quantize_per_channel(x, torch.tensor([0.1, 0.01]), torch.tensor([10, 0]), 0, torch.quint8)
  3. tensor([[-1., 0.],
  4. [ 1., 2.]], size=(2, 2), dtype=torch.quint8,
  5. quantization_scheme=torch.per_channel_affine,
  6. scale=tensor([0.1000, 0.0100], dtype=torch.float64),
  7. zero_point=tensor([10, 0]), axis=0)
  8. >>> torch.quantize_per_channel(x, torch.tensor([0.1, 0.01]), torch.tensor([10, 0]), 0, torch.quint8).int_repr()
  9. tensor([[ 0, 10],
  10. [100, 200]], dtype=torch.uint8)

索引,切片,聯(lián)接,操作變更

  1. torch.cat(tensors, dim=0, out=None) Tensor?

在給定維度上連接seq張量的給定序列。 所有張量必須具有相同的形狀(在連接維中除外)或?yàn)榭铡?/p>

torch.cat() 可以看作是 torch.split() 和  torch.chunk()  的逆運(yùn)算。

通過(guò)示例可以更好地理解 torch.cat() 。

Parameters

  • 張量(張量序列)–同一類型的任何 python 張量序列。 提供的非空張量必須具有相同的形狀,但貓的尺寸除外。
  • 暗淡的 (python:int , 可選)–張量級(jí)聯(lián)的尺寸
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> x = torch.randn(2, 3)
  2. >>> x
  3. tensor([[ 0.6580, -1.0969, -0.4614],
  4. [-0.1034, -0.5790, 0.1497]])
  5. >>> torch.cat((x, x, x), 0)
  6. tensor([[ 0.6580, -1.0969, -0.4614],
  7. [-0.1034, -0.5790, 0.1497],
  8. [ 0.6580, -1.0969, -0.4614],
  9. [-0.1034, -0.5790, 0.1497],
  10. [ 0.6580, -1.0969, -0.4614],
  11. [-0.1034, -0.5790, 0.1497]])
  12. >>> torch.cat((x, x, x), 1)
  13. tensor([[ 0.6580, -1.0969, -0.4614, 0.6580, -1.0969, -0.4614, 0.6580,
  14. -1.0969, -0.4614],
  15. [-0.1034, -0.5790, 0.1497, -0.1034, -0.5790, 0.1497, -0.1034,
  16. -0.5790, 0.1497]])
  1. torch.chunk(input, chunks, dim=0) List of Tensors?

將張量拆分為特定數(shù)量的塊。

如果沿給定維度dim的張量大小不能被chunks整除,則最后一塊將較小。

Parameters

  • 輸入 (tensor)–要分割的張量
  • 塊 (python:int )–要返回的塊數(shù)
  • 暗淡的 (python:int )–沿其張量分裂的尺寸
  1. torch.gather(input, dim, index, out=None, sparse_grad=False) Tensor?

沿<cite>昏暗</cite>指定的軸收集值。

對(duì)于 3-D 張量,輸出指定為:

  1. out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0
  2. out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1
  3. out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2

如果input是大小為 a89ebd4ba33f6e8d4b992302696fcb6bdim = i的 n 維張量,則index必須是大小為  0998ac39fb5e46e656f2261d0af181b6的  5f0051b0454fa75ca446b59b47eff6f6-維張量,其中  5890f3c556cb9391345ace3ce2c49ff9out具有相同的大小 大小為index。

Parameters

  • 輸入 (tensor)–源張量
  • 暗淡的 (python:int )–沿其索引的軸
  • 索引 (LongTensor )–要收集的元素的索引
  • 輸出 (tensor , 可選)–目標(biāo)張量
  • sparse_grad (bool , 可選)–如果True,則梯度 w.r.t. input將是一個(gè)稀疏張量。

Example:

  1. >>> t = torch.tensor([[1,2],[3,4]])
  2. >>> torch.gather(t, 1, torch.tensor([[0,0],[1,0]]))
  3. tensor([[ 1, 1],
  4. [ 4, 3]])
  1. torch.index_select(input, dim, index, out=None) Tensor?

返回一個(gè)新張量,該張量使用index LongTensor 中的index中的條目沿維度dim索引input張量。

返回的張量具有與原始張量(input)相同的維數(shù)。 dim的尺寸與index的長(zhǎng)度相同; 其他尺寸與原始張量中的尺寸相同。

Note

返回的張量不與原始張量使用相同的存儲(chǔ)空間而不是。 如果out的形狀與預(yù)期的形狀不同,我們將默默地將其更改為正確的形狀,并在必要時(shí)重新分配基礎(chǔ)存儲(chǔ)。

Parameters

  • input (Tensor) – the input tensor.
  • 暗淡的 (python:int )–我們索引的維度
  • 索引 (LongTensor )–包含要索引的索引的一維張量
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> x = torch.randn(3, 4)
  2. >>> x
  3. tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],
  4. [-0.4664, 0.2647, -0.1228, -1.1068],
  5. [-1.1734, -0.6571, 0.7230, -0.6004]])
  6. >>> indices = torch.tensor([0, 2])
  7. >>> torch.index_select(x, 0, indices)
  8. tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],
  9. [-1.1734, -0.6571, 0.7230, -0.6004]])
  10. >>> torch.index_select(x, 1, indices)
  11. tensor([[ 0.1427, -0.5414],
  12. [-0.4664, -0.1228],
  13. [-1.1734, 0.7230]])
  1. torch.masked_select(input, mask, out=None) Tensor?

返回一個(gè)新的一維張量,該張量根據(jù)布爾值掩碼mask為其 <cite>BoolTensor</cite> 索引input張量。

mask張量和input張量的形狀不需要匹配,但它們必須是>可廣播的。

Note

返回的張量是否而不是使用與原始張量相同的存儲(chǔ)

Parameters

  • input (Tensor) – the input tensor.
  • 掩碼 (ByteTensor )–包含二進(jìn)制掩碼的張量,以使用
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> x = torch.randn(3, 4)
  2. >>> x
  3. tensor([[ 0.3552, -2.3825, -0.8297, 0.3477],
  4. [-1.2035, 1.2252, 0.5002, 0.6248],
  5. [ 0.1307, -2.0608, 0.1244, 2.0139]])
  6. >>> mask = x.ge(0.5)
  7. >>> mask
  8. tensor([[False, False, False, False],
  9. [False, True, True, True],
  10. [False, False, False, True]])
  11. >>> torch.masked_select(x, mask)
  12. tensor([ 1.2252, 0.5002, 0.6248, 2.0139])
  1. torch.narrow(input, dim, start, length) Tensor?

返回一個(gè)新的張量,該張量是input張量的縮小版本。 尺寸dimstart輸入到start + length。 返回的張量和input張量共享相同的基礎(chǔ)存儲(chǔ)。

Parameters

  • 輸入 (tensor)–張量變窄
  • 暗淡的 (python:int )–縮小范圍
  • 開(kāi)始 (python:int )–起始尺寸
  • 長(zhǎng)度 (python:int )–到最終尺寸的距離

Example:

  1. >>> x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  2. >>> torch.narrow(x, 0, 0, 2)
  3. tensor([[ 1, 2, 3],
  4. [ 4, 5, 6]])
  5. >>> torch.narrow(x, 1, 1, 2)
  6. tensor([[ 2, 3],
  7. [ 5, 6],
  8. [ 8, 9]])
  1. torch.nonzero(input, *, out=None, as_tuple=False) LongTensor or tuple of LongTensors?

Note

torch.nonzero(..., as_tuple=False) (默認(rèn)值)返回一個(gè)二維張量,其中每一行都是非零值的索引。

torch.nonzero(..., as_tuple=True) 返回一維索引張量的元組,允許進(jìn)行高級(jí)索引,因此x[x.nonzero(as_tuple=True)]給出張量x的所有非零值。 在返回的元組中,每個(gè)索引張量都包含特定維度的非零索引。

有關(guān)這兩種行為的更多詳細(xì)信息,請(qǐng)參見(jiàn)下文。

當(dāng) as_tuple “ False”(默認(rèn))時(shí):

返回一個(gè)張量,該張量包含input所有非零元素的索引。 結(jié)果中的每一行都包含input中非零元素的索引。 結(jié)果按字典順序排序,最后一個(gè)索引更改最快(C 樣式)。

如果input具有 5f0051b0454fa75ca446b59b47eff6f6尺寸,則所得索引張量out的大小為 b6080a6ed8a7dd471ec6fd4b1023bc23,其中 e1bf4c09825257a3ffbe6bddb254bcb6input張量中非零元素的總數(shù)。

當(dāng) as_tuple “ True” 時(shí):

返回一維張量的元組,在input中每個(gè)維度一個(gè)張量,每個(gè)張量包含input所有非零元素的索引(在該維度中)。

如果input具有 5f0051b0454fa75ca446b59b47eff6f6尺寸,則生成的元組包含 e1bf4c09825257a3ffbe6bddb254bcb6大小的  5f0051b0454fa75ca446b59b47eff6f6張量,其中  e1bf4c09825257a3ffbe6bddb254bcb6input張量中非零元素的總數(shù)。

作為一種特殊情況,當(dāng)input具有零維和非零標(biāo)量值時(shí),會(huì)將其視為具有一個(gè)元素的一維張量。

Parameters

  • input (Tensor) – the input tensor.
  • out (LongTensor , 可選)–包含索引的輸出張量

Returns

如果as_tupleFalse,則包含索引的輸出張量。 如果as_tupleTrue,則每個(gè)維度都有一個(gè) 1-D 張量,其中包含沿著該維度的每個(gè)非零元素的索引。

Return type

LongTensor 或 LongTensor 的元組

Example:

  1. >>> torch.nonzero(torch.tensor([1, 1, 1, 0, 1]))
  2. tensor([[ 0],
  3. [ 1],
  4. [ 2],
  5. [ 4]])
  6. >>> torch.nonzero(torch.tensor([[0.6, 0.0, 0.0, 0.0],
  7. [0.0, 0.4, 0.0, 0.0],
  8. [0.0, 0.0, 1.2, 0.0],
  9. [0.0, 0.0, 0.0,-0.4]]))
  10. tensor([[ 0, 0],
  11. [ 1, 1],
  12. [ 2, 2],
  13. [ 3, 3]])
  14. >>> torch.nonzero(torch.tensor([1, 1, 1, 0, 1]), as_tuple=True)
  15. (tensor([0, 1, 2, 4]),)
  16. >>> torch.nonzero(torch.tensor([[0.6, 0.0, 0.0, 0.0],
  17. [0.0, 0.4, 0.0, 0.0],
  18. [0.0, 0.0, 1.2, 0.0],
  19. [0.0, 0.0, 0.0,-0.4]]), as_tuple=True)
  20. (tensor([0, 1, 2, 3]), tensor([0, 1, 2, 3]))
  21. >>> torch.nonzero(torch.tensor(5), as_tuple=True)
  22. (tensor([0]),)
  1. torch.reshape(input, shape) Tensor?

返回具有與input相同的數(shù)據(jù)和元素?cái)?shù)量,但具有指定形狀的張量。 如果可能,返回的張量將是input的視圖。 否則,它將是副本。 連續(xù)輸入和具有兼容步幅的輸入可以在不復(fù)制的情況下進(jìn)行重塑,但是您不應(yīng)該依賴復(fù)制與查看行為。

當(dāng)可以返回視圖時(shí),請(qǐng)參見(jiàn) torch.Tensor.view() 。

單個(gè)尺寸可能為-1,在這種情況下,它是根據(jù)input中的其余尺寸和元素?cái)?shù)量推斷出來(lái)的。

Parameters

  • 輸入 (tensor)–要重塑的張量
  • 形狀 (python:ints 的元組)–新形狀

Example:

  1. >>> a = torch.arange(4.)
  2. >>> torch.reshape(a, (2, 2))
  3. tensor([[ 0., 1.],
  4. [ 2., 3.]])
  5. >>> b = torch.tensor([[0, 1], [2, 3]])
  6. >>> torch.reshape(b, (-1,))
  7. tensor([ 0, 1, 2, 3])
  1. torch.split(tensor, split_size_or_sections, dim=0)?

將張量拆分為多個(gè)塊。

如果split_size_or_sections是整數(shù)類型,則 tensor 將被拆分為大小相等的塊(如果可能)。 如果沿給定維度dim的張量大小不能被split_size整除,則最后一個(gè)塊將較小。

如果split_size_or_sections是列表,則根據(jù)split_size_or_sections將 tensor 拆分為dim,大小為dim。

Parameters

  • 張量 (tensor)–張量分裂。
  • split_size_or_sections (python:int )或 ( 列表 ( python :int ))–單個(gè)塊的大小或每個(gè)塊的大小列表
  • 暗淡的 (python:int )–沿其張量分裂的尺寸。
  1. torch.squeeze(input, dim=None, out=None) Tensor?

返回一個(gè)張量,其中所有尺寸為 <cite>1</cite> 的input尺寸均被刪除。

例如,如果<cite>輸入</cite>的形狀為: 1f976021505083151a3b5d3311ab04c2  ,則張量中的<cite>張量將為: 7af8285a40441ae3080550e9267b63f8。</cite>

給定dim時(shí),僅在給定尺寸上執(zhí)行擠壓操作。 如果<cite>輸入</cite>的形狀為: squeeze(input, 0)保持張量不變,但是squeeze(input, 1)會(huì)將張量壓縮為  e48d9b756847043d64d7565153ad4faf形狀。

Note

返回的張量與輸入張量共享存儲(chǔ),因此更改一個(gè)張量的內(nèi)容將更改另一個(gè)張量的內(nèi)容。

Parameters

  • input (Tensor) – the input tensor.
  • 暗淡的 (python:int , 可選)–如果給定,則僅在此維度上壓縮輸入
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> x = torch.zeros(2, 1, 2, 1, 2)
  2. >>> x.size()
  3. torch.Size([2, 1, 2, 1, 2])
  4. >>> y = torch.squeeze(x)
  5. >>> y.size()
  6. torch.Size([2, 2, 2])
  7. >>> y = torch.squeeze(x, 0)
  8. >>> y.size()
  9. torch.Size([2, 1, 2, 1, 2])
  10. >>> y = torch.squeeze(x, 1)
  11. >>> y.size()
  12. torch.Size([2, 2, 1, 2])
  1. torch.stack(tensors, dim=0, out=None) Tensor?

將張量的序列沿新維度連接起來(lái)。

所有張量都必須具有相同的大小。

Parameters

  • 張量(張量序列)–連接的張量序列
  • 暗淡的 (python:int )–插入的尺寸。 必須介于 0 和級(jí)聯(lián)張量的維數(shù)之間(含)
  • out (Tensor, optional) – the output tensor.
  1. torch.t(input) Tensor?

期望input為< = 2-D 張量,并轉(zhuǎn)置尺寸 0 和 1。

將按原樣返回 0-D 和 1-D 張量,并且可以將 2-D 張量視為transpose(input, 0, 1)的簡(jiǎn)寫(xiě)函數(shù)。

Parameters

input (Tensor) – the input tensor.

Example:

  1. >>> x = torch.randn(())
  2. >>> x
  3. tensor(0.1995)
  4. >>> torch.t(x)
  5. tensor(0.1995)
  6. >>> x = torch.randn(3)
  7. >>> x
  8. tensor([ 2.4320, -0.4608, 0.7702])
  9. >>> torch.t(x)
  10. tensor([.2.4320,.-0.4608,..0.7702])
  11. >>> x = torch.randn(2, 3)
  12. >>> x
  13. tensor([[ 0.4875, 0.9158, -0.5872],
  14. [ 0.3938, -0.6929, 0.6932]])
  15. >>> torch.t(x)
  16. tensor([[ 0.4875, 0.3938],
  17. [ 0.9158, -0.6929],
  18. [-0.5872, 0.6932]])
  1. torch.take(input, index) Tensor?

返回給定索引處帶有input元素的新張量。 將輸入張量視為視為一維張量。 結(jié)果采用與索引相同的形狀。

Parameters

  • input (Tensor) – the input tensor.
  • 索引 (LongTensor )–張量索引

Example:

  1. >>> src = torch.tensor([[4, 3, 5],
  2. [6, 7, 8]])
  3. >>> torch.take(src, torch.tensor([0, 2, 5]))
  4. tensor([ 4, 5, 8])
  1. torch.transpose(input, dim0, dim1) Tensor?

返回一個(gè)張量,該張量是input的轉(zhuǎn)置版本。 給定的尺寸dim0dim1被交換。

產(chǎn)生的out張量與input張量共享其基礎(chǔ)存儲(chǔ),因此更改一個(gè)內(nèi)容將更改另一個(gè)內(nèi)容。

Parameters

  • input (Tensor) – the input tensor.
  • dim0 (python:int )–要轉(zhuǎn)置的第一個(gè)維度
  • dim1 (python:int )–要轉(zhuǎn)置的第二維

Example:

  1. >>> x = torch.randn(2, 3)
  2. >>> x
  3. tensor([[ 1.0028, -0.9893, 0.5809],
  4. [-0.1669, 0.7299, 0.4942]])
  5. >>> torch.transpose(x, 0, 1)
  6. tensor([[ 1.0028, -0.1669],
  7. [-0.9893, 0.7299],
  8. [ 0.5809, 0.4942]])
  1. torch.unbind(input, dim=0) seq?

刪除張量尺寸。

返回給定維度上所有切片的元組,已經(jīng)沒(méi)有它。

Parameters

  • 輸入 (tensor)–要解除綁定的張量
  • 暗淡的 (python:int )–要移除的尺寸

Example:

  1. >>> torch.unbind(torch.tensor([[1, 2, 3],
  2. >>> [4, 5, 6],
  3. >>> [7, 8, 9]]))
  4. (tensor([1, 2, 3]), tensor([4, 5, 6]), tensor([7, 8, 9]))
  1. torch.unsqueeze(input, dim, out=None) Tensor?

返回在指定位置插入的尺寸為 1 的新張量。

返回的張量與此張量共享相同的基礎(chǔ)數(shù)據(jù)。

可以使用[-input.dim() - 1, input.dim() + 1)范圍內(nèi)的dim值。 負(fù)的dim對(duì)應(yīng)于dim = dim + input.dim() + 1處應(yīng)用的  unsqueeze()  。

Parameters

  • input (Tensor) – the input tensor.
  • 暗淡的 (python:int )–插入單例尺寸的索引
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> x = torch.tensor([1, 2, 3, 4])
  2. >>> torch.unsqueeze(x, 0)
  3. tensor([[ 1, 2, 3, 4]])
  4. >>> torch.unsqueeze(x, 1)
  5. tensor([[ 1],
  6. [ 2],
  7. [ 3],
  8. [ 4]])
  1. torch.where()?
  1. torch.where(condition, x, y) Tensor

返回從xy中選擇的元素的張量,具體取決于condition

該操作定義為:

12ee903e238f296681b1cef26fef0f8f

Note

張量condition,xy必須是可廣播的。

Parameters

  • 條件 (BoolTensor)–當(dāng)為 True(非零)時(shí),產(chǎn)生 x,否則產(chǎn)生 y
  • x (tensor)–在conditionTrue的索引處選擇的值
  • y (tensor)–在conditionFalse的索引處選擇的值

Returns

形狀張量等于condition,xy的廣播形狀

Return type

Tensor

Example:

  1. >>> x = torch.randn(3, 2)
  2. >>> y = torch.ones(3, 2)
  3. >>> x
  4. tensor([[-0.4620, 0.3139],
  5. [ 0.3898, -0.7197],
  6. [ 0.0478, -0.1657]])
  7. >>> torch.where(x > 0, x, y)
  8. tensor([[ 1.0000, 0.3139],
  9. [ 0.3898, 1.0000],
  10. [ 0.0478, 1.0000]])
  1. torch.where(condition) tuple of LongTensor

torch.where(condition)torch.nonzero(condition, as_tuple=True)相同。

Note

另請(qǐng)參見(jiàn) torch.nonzero() 。

發(fā)電機(jī)

  1. class torch._C.Generator(device='cpu') Generator?

創(chuàng)建并返回一個(gè)生成器對(duì)象,該對(duì)象管理產(chǎn)生偽隨機(jī)數(shù)的算法的狀態(tài)。 在許多就地隨機(jī)采樣函數(shù)中用作關(guān)鍵字參數(shù)。

Parameters

設(shè)備(torch.device,可選)–生成器所需的設(shè)備。

Returns

一個(gè) torch.Generator 對(duì)象。

Return type

生成器

Example:

  1. >>> g_cpu = torch.Generator()
  2. >>> g_cuda = torch.Generator(device='cuda')
  1. device?

Generator.device->設(shè)備

獲取生成器的當(dāng)前設(shè)備。

Example:

  1. >>> g_cpu = torch.Generator()
  2. >>> g_cpu.device
  3. device(type='cpu')
  1. get_state() Tensor?

返回生成器狀態(tài)為torch.ByteTensor。

Returns

一個(gè)torch.ByteTensor,其中包含將生成器還原到特定時(shí)間點(diǎn)的所有必要位。

Return type

Tensor

Example:

  1. >>> g_cpu = torch.Generator()
  2. >>> g_cpu.get_state()
  1. initial_seed() int?

返回用于生成隨機(jī)數(shù)的初始種子。

Example:

  1. >>> g_cpu = torch.Generator()
  2. >>> g_cpu.initial_seed()
  3. 2147483647
  1. manual_seed(seed) Generator?

設(shè)置用于生成隨機(jī)數(shù)的種子。 返回一個(gè)<cite>torch.生成器</cite>對(duì)象。 建議設(shè)置一個(gè)大種子,即一個(gè)具有 0 和 1 位平衡的數(shù)字。 避免在種子中包含許多 0 位。

Parameters

種子 (python:int )–所需的種子。

Returns

An torch.Generator object.

Return type

Generator

Example:

  1. >>> g_cpu = torch.Generator()
  2. >>> g_cpu.manual_seed(2147483647)
  1. seed() int?

從 std :: random_device 或當(dāng)前時(shí)間獲取不確定的隨機(jī)數(shù),并將其用作生成器的種子。

Example:

  1. >>> g_cpu = torch.Generator()
  2. >>> g_cpu.seed()
  3. 1516516984916
  1. set_state(new_state) void?

設(shè)置生成器狀態(tài)。

Parameters

new_state (Torch.ByteTensor )–所需狀態(tài)。

Example:

  1. >>> g_cpu = torch.Generator()
  2. >>> g_cpu_other = torch.Generator()
  3. >>> g_cpu.set_state(g_cpu_other.get_state())

隨機(jī)抽樣

  1. torch.seed()?

將用于生成隨機(jī)數(shù)的種子設(shè)置為不確定的隨機(jī)數(shù)。 返回用于播種 RNG 的 64 位數(shù)字。

  1. torch.manual_seed(seed)?

設(shè)置用于生成隨機(jī)數(shù)的種子。 返回一個(gè)<cite>torch.生成器</cite>對(duì)象。

Parameters

seed (python:int) – The desired seed.

  1. torch.initial_seed()?

返回長(zhǎng)為 Python <cite>long</cite> 的用于生成隨機(jī)數(shù)的初始種子。

  1. torch.get_rng_state()?

以 <cite>torch.ByteTensor</cite> 的形式返回隨機(jī)數(shù)生成器狀態(tài)。

  1. torch.set_rng_state(new_state)?

設(shè)置隨機(jī)數(shù)生成器狀態(tài)。

Parameters

new_state (torch.ByteTensor )–所需狀態(tài)

  1. torch.default_generator Returns the default CPU torch.Generator?
  1. torch.bernoulli(input, *, generator=None, out=None) Tensor?

從伯努利分布中提取二進(jìn)制隨機(jī)數(shù)(0 或 1)。

input張量應(yīng)為包含用于繪制二進(jìn)制隨機(jī)數(shù)的概率的張量。 因此,input中的所有值都必須在以下范圍內(nèi): b96a669c7fd5b0f5ea99f4373d6eab2e  。

輸出張量的  55a50814a1cfd1ab95d71ca3a7f311fb  元素將根據(jù)input中給出的  55a50814a1cfd1ab95d71ca3a7f311fb概率值繪制一個(gè)  21c5bc8d40ee5ab2e18d64aaba8359c7值。

f1fc132b2ecf782642591904c4f9b7de

返回的out張量?jī)H具有值 0 或 1,并且具有與input相同的形狀。

out可以具有整數(shù)dtype,但是input必須具有浮點(diǎn)dtype。

Parameters

  • 輸入 (tensor)–伯努利分布的概率值的輸入張量
  • 生成器(torch.Generator,可選)–用于采樣的偽隨機(jī)數(shù)生成器
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> a = torch.empty(3, 3).uniform_(0, 1) # generate a uniform random matrix with range [0, 1]
  2. >>> a
  3. tensor([[ 0.1737, 0.0950, 0.3609],
  4. [ 0.7148, 0.0289, 0.2676],
  5. [ 0.9456, 0.8937, 0.7202]])
  6. >>> torch.bernoulli(a)
  7. tensor([[ 1., 0., 0.],
  8. [ 0., 0., 0.],
  9. [ 1., 1., 1.]])
  10. >>> a = torch.ones(3, 3) # probability of drawing "1" is 1
  11. >>> torch.bernoulli(a)
  12. tensor([[ 1., 1., 1.],
  13. [ 1., 1., 1.],
  14. [ 1., 1., 1.]])
  15. >>> a = torch.zeros(3, 3) # probability of drawing "1" is 0
  16. >>> torch.bernoulli(a)
  17. tensor([[ 0., 0., 0.],
  18. [ 0., 0., 0.],
  19. [ 0., 0., 0.]])
  1. torch.multinomial(input, num_samples, replacement=False, *, generator=None, out=None) LongTensor?

返回一個(gè)張量,其中每行包含num_samples索引,這些索引是從位于張量input的相應(yīng)行中的多項(xiàng)式概率分布中采樣的。

Note

input的行不需要加總為 1(在這種情況下,我們將這些值用作權(quán)重),但必須為非負(fù)數(shù),有限且總和為非零。

根據(jù)每個(gè)樣本的采樣時(shí)間,索引從左到右排序(第一個(gè)樣本放在第一列中)。

如果input是向量,則out是大小num_samples的向量。

如果input是具有 <cite>m</cite> 行的矩陣,則out是形狀 5d2de3653458e6a14a8a4f0fa87b3ad1的矩陣。

如果替換為True,則抽取樣本進(jìn)行替換。

如果沒(méi)有,則它們將被替換而不會(huì)被繪制,這意味著當(dāng)為一行繪制樣本索引時(shí),無(wú)法為該行再次繪制它。

Note

如果繪制時(shí)不進(jìn)行替換,則num_samples必須小于input中非零元素的數(shù)目(如果是矩陣,則必須小于input每行中非零元素的最小數(shù)目)。

Parameters

  • 輸入 (tensor)–包含概率的輸入張量
  • num_samples (python:int )–要繪制的樣本數(shù)
  • 替換 (bool , 可選)–是否使用替換繪制
  • generator  (torch.Generator, optional) – a pseudorandom number generator for sampling
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> weights = torch.tensor([0, 10, 3, 0], dtype=torch.float) # create a tensor of weights
  2. >>> torch.multinomial(weights, 2)
  3. tensor([1, 2])
  4. >>> torch.multinomial(weights, 4) # ERROR!
  5. RuntimeError: invalid argument 2: invalid multinomial distribution (with replacement=False,
  6. not enough non-negative category to sample) at ../aten/src/TH/generic/THTensorRandom.cpp:320
  7. >>> torch.multinomial(weights, 4, replacement=True)
  8. tensor([ 2, 1, 1, 1])
  1. torch.normal()?
  1. torch.normal(mean, std, *, generator=None, out=None) Tensor

返回從均值和標(biāo)準(zhǔn)差給出的獨(dú)立正態(tài)分布中得出的隨機(jī)數(shù)張量。

mean 是一個(gè)張量,每個(gè)輸出元素的正態(tài)分布均值

std 是一個(gè)張量,每個(gè)輸出元素的正態(tài)分布的標(biāo)準(zhǔn)偏差

mean 和 std 的形狀不需要匹配,但是每個(gè)張量中元素的總數(shù)必須相同。

Note

當(dāng)形狀不匹配時(shí),將 mean 的形狀用作返回的輸出張量的形狀

Parameters

  • 均值 (tensor)–每個(gè)元素均值的張量
  • std (tensor)–每個(gè)元素的標(biāo)準(zhǔn)偏差張量
  • generator  (torch.Generator, optional) – a pseudorandom number generator for sampling
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1))
  2. tensor([ 1.0425, 3.5672, 2.7969, 4.2925, 4.7229, 6.2134,
  3. 8.0505, 8.1408, 9.0563, 10.0566])
  1. torch.normal(mean=0.0, std, out=None) Tensor

與上面的功能相似,但均值在所有繪制的元素之間共享。

Parameters

  • 平均值 (python:float , 可選)–所有分布的平均值
  • std (Tensor) – the tensor of per-element standard deviations
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> torch.normal(mean=0.5, std=torch.arange(1., 6.))
  2. tensor([-1.2793, -1.0732, -2.0687, 5.1177, -1.2303])
  1. torch.normal(mean, std=1.0, out=None) Tensor

與上面的函數(shù)相似,但是標(biāo)準(zhǔn)偏差在所有繪制的元素之間共享。

Parameters

  • mean (Tensor) – the tensor of per-element means
  • std (python:float , 可選)–所有發(fā)行版的標(biāo)準(zhǔn)差
  • out (tensor , 可選)–輸出張量

Example:

  1. >>> torch.normal(mean=torch.arange(1., 6.))
  2. tensor([ 1.1552, 2.6148, 2.6535, 5.8318, 4.2361])
  1. torch.normal(mean, std, size, *, out=None) Tensor

與上述功能相似,但均值和標(biāo)準(zhǔn)差在所有繪制的元素之間共享。  所得張量的大小由size給出。

Parameters

  • 平均值 (python:float )–所有分布的平均值
  • std (python:float )–所有分布的標(biāo)準(zhǔn)偏差
  • 大小 (python:int ... )–定義輸出張量形狀的整數(shù)序列。
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> torch.normal(2, 3, size=(1, 4))
  2. tensor([[-1.3987, -1.9544, 3.6048, 0.7909]])
  1. torch.rand(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor?

從區(qū)間  22e975ba7fcff9173338360452b96179返回均勻分布的隨機(jī)張量

張量的形狀由變量參數(shù)size定義。

Parameters

  • size (python:int...) – a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
  • out (Tensor, optional) – the output tensor.
  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see  torch.set_default_tensor_type() ).
  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
  • device  (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

  1. >>> torch.rand(4)
  2. tensor([ 0.5204, 0.2503, 0.3525, 0.5673])
  3. >>> torch.rand(2, 3)
  4. tensor([[ 0.8237, 0.5781, 0.6879],
  5. [ 0.3816, 0.7249, 0.0998]])
  1. torch.rand_like(input, dtype=None, layout=None, device=None, requires_grad=False) Tensor?

返回與input大小相同的張量,該張量由間隔 上均勻分布的隨機(jī)數(shù)填充。 torch.rand_like(input)等效于torch.rand(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)。

Parameters

  • input (Tensor) – the size of input will determine size of the output tensor.
  • dtype (torch.dtype, optional) – the desired data type of returned Tensor. Default: if None, defaults to the dtype of input.
  • layout (torch.layout, optional) – the desired layout of returned tensor. Default: if None, defaults to the layout of input.
  • device(torch.device, optional) – the desired device of returned tensor. Default: if None, defaults to the device of input.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.
  1. torch.randint(low=0, high, size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor?

返回一個(gè)由在low(包括)和high(不包括)之間均勻生成的隨機(jī)整數(shù)填充的張量。

The shape of the tensor is defined by the variable argument size.

Parameters

  • 低 (python:int , 可選)–從分布中得出的最低整數(shù)。 默認(rèn)值:0
  • 高 (python:int )–從分布中得出的最高整數(shù)之上一個(gè)。
  • 大小(元組)–定義輸出張量形狀的元組。
  • generator (torch.Generator, optional) – a pseudorandom number generator for sampling
  • out (Tensor, optional) – the output tensor.
  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()).
  • layout (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see  torch.set_default_tensor_type() ).
  • device  (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

  1. >>> torch.randint(3, 5, (3,))
  2. tensor([4, 3, 4])
  3. >>> torch.randint(10, (2, 2))
  4. tensor([[0, 2],
  5. [5, 5]])
  6. >>> torch.randint(3, 10, (2, 2))
  7. tensor([[4, 5],
  8. [6, 7]])
  1. torch.randint_like(input, low=0, high, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor?

返回具有與張量input相同形狀的張量,其中填充了在low(包括)和high(排除)之間均勻生成的隨機(jī)整數(shù)。

Parameters

  • input (Tensor) – the size of input will determine size of the output tensor.
  • low (python:int__, optional) – Lowest integer to be drawn from the distribution. Default: 0.
  • high (python:int) – One above the highest integer to be drawn from the distribution.
  • dtype  (torch.dtype, optional) – the desired data type of returned Tensor. Default: if None, defaults to the dtype of input.
  • layout (torch.layout, optional) – the desired layout of returned tensor. Default: if None, defaults to the layout of input.
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, defaults to the device of input.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.
  1. torch.randn(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor?

從平均值為 <cite>0</cite> ,方差為 <cite>1</cite> 的正態(tài)分布中返回一個(gè)填充有隨機(jī)數(shù)的張量(也稱為標(biāo)準(zhǔn)正態(tài)分布)。

6a5aeab1deaf496af03eb65c0690d32b

The shape of the tensor is defined by the variableargument size.

Parameters

  • size (python:int...) – a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.
  • out (Tensor, optional) – the output tensor.
  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see  torch.set_default_tensor_type() ).
  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor.Default: False.

Example:

  1. >>> torch.randn(4)
  2. tensor([-2.1436, 0.9966, 2.3426, -0.6366])
  3. >>> torch.randn(2, 3)
  4. tensor([[ 1.5954, 2.8929, -1.0923],
  5. [ 1.1719, -0.4709, -0.1996]])
  1. torch.randn_like(input, dtype=None, layout=None, device=None, requires_grad=False) Tensor?

返回一個(gè)與input相同大小的張量,該張量由均值 0 和方差 1 的正態(tài)分布的隨機(jī)數(shù)填充。torch.randn_like(input)等效于torch.randn(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)

Parameters

  • input (Tensor) – the size of input will determine size of the output tensor.
  • dtype (torch.dtype,  optional) – the desired data type of returned Tensor. Default: if None, defaults to the dtype of input.
  • layout (torch.layout, optional) – the desired layout of returned tensor. Default: if None, defaults to the layout of input.
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, defaults to the device of input.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.
  1. torch.randperm(n, out=None, dtype=torch.int64, layout=torch.strided, device=None, requires_grad=False) LongTensor?

返回從0n - 1的整數(shù)的隨機(jī)排列。

Parameters

  • n (python:int )–上限(不包括)
  • out (Tenso, optional) – the output tensor.
  • dtype(torch.dtype ,可選)–返回張量的所需數(shù)據(jù)類型。 默認(rèn)值:torch.int64。
  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool__, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

  1. >>> torch.randperm(4)
  2. tensor([2, 1, 0, 3])

就地隨機(jī)抽樣

在 Tensor 上還定義了一些就地隨機(jī)采樣函數(shù)。 單擊以查看其文檔:

  • torch.Tensor.bernoulli_() - torch.bernoulli() 的就地版本
  • torch.Tensor.cauchy_() -從柯西分布中得出的數(shù)字
  • torch.Tensor.exponential_() -從指數(shù)分布中得出的數(shù)字
  • torch.Tensor.geometric_() -從幾何分布中繪制的元素
  • torch.Tensor.log_normal_()  -來(lái)自對(duì)數(shù)正態(tài)分布的樣本
  • torch.Tensor.normal_() - torch.normal() 的就地版本
  • torch.Tensor.random_() -從離散均勻分布中采樣的數(shù)字
  • torch.Tensor.uniform_() -從連續(xù)均勻分布中采樣的數(shù)字

準(zhǔn)隨機(jī)抽樣

  1. class torch.quasirandom.SobolEngine(dimension, scramble=False, seed=None)?

torch.quasirandom.SobolEngine 是用于生成(加擾)Sobol 序列的引擎。 Sobol 序列是低差異準(zhǔn)隨機(jī)序列的一個(gè)示例。

用于 Sobol 序列的引擎的這種實(shí)現(xiàn)方式能夠?qū)ψ畲缶S度為 1111 的序列進(jìn)行采樣。它使用方向編號(hào)生成這些序列,并且這些編號(hào)已從此處改編而來(lái)。

參考文獻(xiàn)

  • Art B. Owen。 爭(zhēng)奪 Sobol 和 Niederreiter-Xing 點(diǎn)。 復(fù)雜性雜志,14(4):466-489,1998 年 12 月。
  • I. M. Sobol。 立方體中點(diǎn)的分布和積分的準(zhǔn)確評(píng)估。 嗯 Vychisl。 墊。 我在。 Phys。,7:784-802,1967。

Parameters

  • 尺寸 (Int )–要繪制的序列的尺寸
  • 擾亂 (bool , 可選)–將其設(shè)置為True將產(chǎn)生擾亂的 Sobol 序列。 加擾能夠產(chǎn)生更好的 Sobol 序列。默認(rèn)值:False。
  • 種子 (Int , 可選)–這是加擾的種子。 如果指定,則將隨機(jī)數(shù)生成器的種子設(shè)置為此。 否則,它將使用隨機(jī)種子。默認(rèn)值:None

例子:

  1. >>> soboleng = torch.quasirandom.SobolEngine(dimension=5)
  2. >>> soboleng.draw(3)
  3. tensor([[0.5000, 0.5000, 0.5000, 0.5000, 0.5000],
  4. [0.7500, 0.2500, 0.7500, 0.2500, 0.7500],
  5. [0.2500, 0.7500, 0.2500, 0.7500, 0.2500]])
  1. draw(n=1, out=None, dtype=torch.float32)?

從 Sobol 序列中繪制n點(diǎn)序列的功能。 請(qǐng)注意,樣本取決于先前的樣本。 結(jié)果的大小為 4a928fac0225cfecb1270d6afba7caf9。

Parameters

  • n (Int , 可選)–繪制點(diǎn)序列的長(zhǎng)度。 默認(rèn)值:1
  • out (tensor , 可選)–輸出張量
  • dtype (torch.dtype,可選)–返回的張量的所需數(shù)據(jù)類型。 默認(rèn)值:torch.float32
  1. fast_forward(n)?

通過(guò)n步驟快速前進(jìn)SobolEngine狀態(tài)的功能。 這等效于不使用樣本繪制n樣本。

Parameters

n (Int )–快進(jìn)的步數(shù)。

  1. reset()?

SobolEngine重置為基本狀態(tài)的功能。

序列化

  1. torch.save(obj, f, pickle_module=<module 'pickle' from '/opt/conda/lib/python3.6/pickle.py'>, pickle_protocol=2, _use_new_zipfile_serialization=False)?

將對(duì)象保存到磁盤文件。

另請(qǐng)參見(jiàn):推薦的模型保存方法

Parameters

  • obj –保存的對(duì)象
  • f –類似于文件的對(duì)象(必須實(shí)現(xiàn)寫(xiě)入和刷新)或包含文件名的字符串
  • pickle_module –用于腌制元數(shù)據(jù)和對(duì)象的模塊
  • pickle_protocol –可以指定為覆蓋默認(rèn)協(xié)議

Warning

如果使用的是 Python 2,則 torch.save() 不支持StringIO.StringIO作為有效的類似文件的對(duì)象。 這是因?yàn)?write 方法應(yīng)返回寫(xiě)入的字節(jié)數(shù); StringIO.write()不這樣做。

請(qǐng)改用io.BytesIO之類的東西。

  1. >>> # Save to file
  2. >>> x = torch.tensor([0, 1, 2, 3, 4])
  3. >>> torch.save(x, 'tensor.pt')
  4. >>> # Save to io.BytesIO buffer
  5. >>> buffer = io.BytesIO()
  6. >>> torch.save(x, buffer)
  1. torch.load(f, map_location=None, pickle_module=<module 'pickle' from '/opt/conda/lib/python3.6/pickle.py'>, **pickle_load_args)?

從文件加載用 torch.save() 保存的對(duì)象。

torch.load() 使用 Python 的解開(kāi)工具,但會(huì)特別處理位于張量之下的存儲(chǔ)。 它們首先在 CPU 上反序列化,然后移到保存它們的設(shè)備上。 如果失敗(例如,因?yàn)檫\(yùn)行時(shí)系統(tǒng)沒(méi)有某些設(shè)備),則會(huì)引發(fā)異常。 但是,可以使用 map_location參數(shù)將存儲(chǔ)動(dòng)態(tài)重新映射到一組備用設(shè)備。

如果map_location是可調(diào)用的,則將為每個(gè)序列化存儲(chǔ)調(diào)用一次,并帶有兩個(gè)參數(shù):storage 和 location。 storage 參數(shù)將是駐留在 CPU 上的存儲(chǔ)的初始反序列化。 每個(gè)序列化存儲(chǔ)都有一個(gè)與之關(guān)聯(lián)的位置標(biāo)簽,該標(biāo)簽標(biāo)識(shí)了從中進(jìn)行保存的設(shè)備,該標(biāo)簽是傳遞給map_location的第二個(gè)參數(shù)。 內(nèi)置位置標(biāo)簽是用于 CPU 張量的'cpu'和用于 CUDA 張量的'cuda:device_id'(例如'cuda:2')。 map_location應(yīng)該返回None或存儲(chǔ)。 如果map_location返回存儲(chǔ),它將用作最終反序列化的對(duì)象,已經(jīng)移至正確的設(shè)備。 否則, torch.load() 將退回到默認(rèn)行為,就像未指定map_location一樣。

如果map_location是 torch.device 對(duì)象或與設(shè)備標(biāo)簽沖突的字符串,則它指示應(yīng)加載所有張量的位置。

否則,如果map_location是字典,它將用于將文件(鍵)中出現(xiàn)的位置標(biāo)簽重新映射到指定將存儲(chǔ)位置(值)放置的位置標(biāo)簽。

用戶擴(kuò)展可以使用torch.serialization.register_package()注冊(cè)自己的位置標(biāo)簽以及標(biāo)記和反序列化方法。

Parameters

  • f –類似于文件的對(duì)象(必須實(shí)現(xiàn)read(),:methreadline,:methtell和:methseek)或包含文件名的字符串
  • map_location –函數(shù), torch.device ,字符串或指定如何重新映射存儲(chǔ)位置的字典
  • pickle_module –用于解開(kāi)元數(shù)據(jù)和對(duì)象的模塊(必須與用于序列化文件的pickle_module匹配)
  • pickle_load_args –(僅適用于 Python 3)可選關(guān)鍵字參數(shù)傳遞給pickle_module.load()pickle_module.Unpickler(),例如errors=...。

Note

當(dāng)您在包含 GPU 張量的文件上調(diào)用 torch.load() 時(shí),這些張量將默認(rèn)加載到 GPU。 您可以先調(diào)用torch.load(.., map_location='cpu'),然后再調(diào)用load_state_dict(),以避免在加載模型檢查點(diǎn)時(shí) GPU RAM 激增。

Note

默認(rèn)情況下,我們將字節(jié)字符串解碼為utf-8。 這是為了避免在 Python 3 中加載 Python 2 保存的文件時(shí)出現(xiàn)常見(jiàn)錯(cuò)誤情況UnicodeDecodeError: 'ascii' codec can't decode byte 0x...。如果此默認(rèn)設(shè)置不正確,則可以使用額外的encoding關(guān)鍵字參數(shù)來(lái)指定應(yīng)如何加載這些對(duì)象,例如encoding='latin1'使用latin1編碼將它們解碼為字符串,encoding='bytes'將它們保留為字節(jié)數(shù)組,以后可以使用byte_array.decode(...)進(jìn)行解碼。

Example

  1. >>> torch.load('tensors.pt')
  2. ## Load all tensors onto the CPU
  3. >>> torch.load('tensors.pt', map_location=torch.device('cpu'))
  4. ## Load all tensors onto the CPU, using a function
  5. >>> torch.load('tensors.pt', map_location=lambda storage, loc: storage)
  6. ## Load all tensors onto GPU 1
  7. >>> torch.load('tensors.pt', map_location=lambda storage, loc: storage.cuda(1))
  8. ## Map tensors from GPU 1 to GPU 0
  9. >>> torch.load('tensors.pt', map_location={'cuda:1':'cuda:0'})
  10. ## Load tensor from io.BytesIO object
  11. >>> with open('tensor.pt', 'rb') as f:
  12. buffer = io.BytesIO(f.read())
  13. >>> torch.load(buffer)
  14. ## Load a module with 'ascii' encoding for unpickling
  15. >>> torch.load('module.pt', encoding='ascii')

平行性

  1. torch.get_num_threads() int?

返回用于并行化 CPU 操作的線程數(shù)

  1. torch.set_num_threads(int)?

設(shè)置用于 CPU 上的內(nèi)部運(yùn)算并行的線程數(shù)。 警告:為確保使用正確的線程數(shù),必須在運(yùn)行 eager,JIT 或 autograd 代碼之前調(diào)用 set_num_threads。

  1. torch.get_num_interop_threads() int?

返回用于 CPU 上的互操作并行的線程數(shù)(例如,在 JIT 解釋器中)

  1. torch.set_num_interop_threads(int)?

設(shè)置用于 CPU 上的互操作并行性(例如,在 JIT 解釋器中)的線程數(shù)。 警告:只能在一次操作間并行工作開(kāi)始之前(例如 JIT 執(zhí)行)調(diào)用一次。

局部禁用梯度計(jì)算

上下文管理器torch.no_grad(),torch.enable_grad()torch.set_grad_enabled()有助于局部禁用和啟用梯度計(jì)算。 有關(guān)其用法的更多詳細(xì)信息,請(qǐng)參見(jiàn)局部禁用梯度計(jì)算。 這些上下文管理器是線程本地的,因此如果您使用threading模塊等將工作發(fā)送到另一個(gè)線程,它們將無(wú)法工作。

Examples:

  1. >>> x = torch.zeros(1, requires_grad=True)
  2. >>> with torch.no_grad():
  3. ... y = x * 2
  4. >>> y.requires_grad
  5. False
  6. >>> is_train = False
  7. >>> with torch.set_grad_enabled(is_train):
  8. ... y = x * 2
  9. >>> y.requires_grad
  10. False
  11. >>> torch.set_grad_enabled(True) # this can also be used as a function
  12. >>> y = x * 2
  13. >>> y.requires_grad
  14. True
  15. >>> torch.set_grad_enabled(False)
  16. >>> y = x * 2
  17. >>> y.requires_grad
  18. False

數(shù)學(xué)運(yùn)算

逐點(diǎn)操作

  1. torch.abs(input, out=None) Tensor?

計(jì)算給定input張量的按元素的絕對(duì)值。

726d369abb9c76e751e626cbfef10220

Parameters

  • input (Tensor) – the input tensor.
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> torch.abs(torch.tensor([-1, -2, 3]))
  2. tensor([ 1, 2, 3])
  1. torch.acos(input, out=None) Tensor?

返回帶有input元素的反余弦的新張量。

25323a5363649a36549d717d5a3cbc3e

Parameters

  • input (Tensor) – the input tensor.
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> a = torch.randn(4)
  2. >>> a
  3. tensor([ 0.3348, -0.5889, 0.2005, -0.1584])
  4. >>> torch.acos(a)
  5. tensor([ 1.2294, 2.2004, 1.3690, 1.7298])
  1. torch.add()?
  1. torch.add(input, other, out=None)

將標(biāo)量other添加到輸入input的每個(gè)元素中,并返回一個(gè)新的結(jié)果張量。

1e18f010799098c127bab9465476d1fb

如果input的類型為 FloatTensor 或 DoubleTensor,則other必須為實(shí)數(shù),否則應(yīng)為整數(shù)。

Parameters

  • input (Tensor) – the input tensor.
  • 值(編號(hào))–要添加到input每個(gè)元素的編號(hào)
  1. Keyword Arguments

out (Tensor, optional) – the output tensor.

Example:

  1. >>> a = torch.randn(4)
  2. >>> a
  3. tensor([ 0.0202, 1.0985, 1.3506, -0.6056])
  4. >>> torch.add(a, 20)
  5. tensor([ 20.0202, 21.0985, 21.3506, 19.3944])
  1. torch.add(input, alpha=1, other, out=None)

張量other的每個(gè)元素乘以標(biāo)量alpha,然后加到張量input的每個(gè)元素上。 返回結(jié)果張量。

inputother的狀必須是可廣播的。

5a5bc06d446342edf134fb0e87f755ec

如果other的類型為 FloatTensor 或 DoubleTensor,則alpha必須為實(shí)數(shù),否則應(yīng)為整數(shù)。

Parameters

  • 輸入 (tensor)–第一個(gè)輸入張量
  • alpha (數(shù)字)– other的標(biāo)量乘法器
  • 其他 (tensor)–第二個(gè)輸入張量
  1. Keyword Arguments

out (Tensor, optional) – the output tensor.

Example:

  1. >>> a = torch.randn(4)
  2. >>> a
  3. tensor([-0.9732, -0.3497, 0.6245, 0.4022])
  4. >>> b = torch.randn(4, 1)
  5. >>> b
  6. tensor([[ 0.3743],
  7. [-1.7724],
  8. [-0.5811],
  9. [-0.8017]])
  10. >>> torch.add(a, 10, b)
  11. tensor([[ 2.7695, 3.3930, 4.3672, 4.1450],
  12. [-18.6971, -18.0736, -17.0994, -17.3216],
  13. [ -6.7845, -6.1610, -5.1868, -5.4090],
  14. [ -8.9902, -8.3667, -7.3925, -7.6147]])
  1. torch.addcdiv(input, value=1, tensor1, tensor2, out=None) Tensor?

執(zhí)行tensor1除以tensor2的元素,將結(jié)果乘以標(biāo)量value并將其加到input上。

69053b1e1ce2faac8aae0ee4370775b9

input,tensor1tensor2的形狀必須是可廣播。

對(duì)于類型為 <cite>FloatTensor</cite> 或 <cite>DoubleTensor</cite> 的輸入,value必須為實(shí)數(shù),否則為整數(shù)。

Parameters

  • 輸入 (tensor)–要添加的張量
  • 值(編號(hào) , 可選)–  的乘數(shù)
  • 張量 1 (tensor)–分子張量
  • 張量 2 (tensor)–分母張量
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> t = torch.randn(1, 3)
  2. >>> t1 = torch.randn(3, 1)
  3. >>> t2 = torch.randn(1, 3)
  4. >>> torch.addcdiv(t, 0.1, t1, t2)
  5. tensor([[-0.2312, -3.6496, 0.1312],
  6. [-1.0428, 3.4292, -0.1030],
  7. [-0.5369, -0.9829, 0.0430]])
  1. torch.addcmul(input, value=1, tensor1, tensor2, out=None) Tensor?

對(duì)tensor1tensor2進(jìn)行元素逐項(xiàng)乘法,將結(jié)果與標(biāo)量value相乘,然后將其與input相加。

6bfdc94f1a8a13ee1a6ebc9af2d74fda

tensor ,tensor1tensor2的形狀必須是可廣播的。

For inputs of type <cite>FloatTensor</cite> or <cite>DoubleTensor</cite>, value must  be a real number, otherwise an integer.

Parameters

  • input (Tensor) – the tensor to be added
  • 值(編號(hào) , 可選)–  的乘數(shù)
  • 張量 1 (tensor)–要相乘的張量
  • 張量 2 (tensor)–要相乘的張量
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> t = torch.randn(1, 3)
  2. >>> t1 = torch.randn(3, 1)
  3. >>> t2 = torch.randn(1, 3)
  4. >>> torch.addcmul(t, 0.1, t1, t2)
  5. tensor([[-0.8635, -0.6391, 1.6174],
  6. [-0.7617, -0.5879, 1.7388],
  7. [-0.8353, -0.6249, 1.6511]])
  1. torch.angle(input, out=None) Tensor?

計(jì)算給定input張量的元素方向角(以弧度為單位)。

5af8c34b3d4c490c508caf11a458d5d6

Parameters

  • input (Tensor) – the input tensor.
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> torch.angle(torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j]))*180/3.14159
  2. tensor([ 135., 135, -45])
  1. torch.asin(input, out=None) Tensor?

返回帶有input元素的反正弦值的新張量。

fa8ed00ae85106eea8e8a11b6b66d898

Parameters

  • input (Tensor) – the input tensor.
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> a = torch.randn(4)
  2. >>> a
  3. tensor([-0.5962, 1.4985, -0.4396, 1.4525])
  4. >>> torch.asin(a)
  5. tensor([-0.6387, nan, -0.4552, nan])
  1. torch.atan(input, out=None) Tensor?

返回帶有input元素的反正切的新張量。

8614369d5e7413ce21e9a5d6bcf786cb

Parameters

  • input (Tensor) – the input tensor.
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> a = torch.randn(4)
  2. >>> a
  3. tensor([ 0.2341, 0.2539, -0.6256, -0.6448])
  4. >>> torch.atan(a)
  5. tensor([ 0.2299, 0.2487, -0.5591, -0.5727])
  1. torch.atan2(input, other, out=None) Tensor?

考慮象限的  c45e59e7ab5c2f7cff1ff37748c0d62b元素逐級(jí)反正切。 返回一個(gè)新的張量,其矢量  2cb0a971340ffd46996a0cf9eb81d376與矢量  8456c6ac83b24dbe917ff5a29a771bd7之間的弧度為符號(hào)角。 (請(qǐng)注意,第二個(gè)參數(shù)  4e409083ee35ea2eeab094ea4f9bf730是 x 坐標(biāo),而第一個(gè)參數(shù)  0c7468da87ed4da7f144e93eb6e7e60e是 y 坐標(biāo)。)

iinputother的形狀必須是可廣播的。

Parameters

  • input (Tenso) – the first input tensor
  • other (Tensor) – the second input tensor
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> a = torch.randn(4)
  2. >>> a
  3. tensor([ 0.9041, 0.0196, -0.3108, -2.4423])
  4. >>> torch.atan2(a, torch.randn(4))
  5. tensor([ 0.9833, 0.0811, -1.9743, -1.4151])
  1. torch.bitwise_not(input, out=None) Tensor?

計(jì)算給定輸入張量的按位非。 輸入張量必須是整數(shù)或布爾類型。 對(duì)于布爾張量,它計(jì)算邏輯非。

Parameters

  • input (Tensor) – the input tensor.
  • out (Tensor, optional) – the output tensor.

Example

  1. >>> torch.bitwise_not(torch.tensor([-1, -2, 3], dtype=torch.int8))
  2. tensor([ 0, 1, -4], dtype=torch.int8)
  1. torch.bitwise_xor(input, other, out=None) Tensor?

計(jì)算inputother的按位 XOR。 輸入張量必須是整數(shù)或布爾類型。 對(duì)于布爾張量,它計(jì)算邏輯 XOR。

Parameters

  • 輸入 –第一個(gè)輸入張量
  • 其他 –第二個(gè)輸入張量
  • out (Tensor, optional) – the output tensor.

Example

  1. >>> torch.bitwise_xor(torch.tensor([-1, -2, 3], dtype=torch.int8), torch.tensor([1, 0, 3], dtype=torch.int8))
  2. tensor([-2, -2, 0], dtype=torch.int8)
  3. >>> torch.bitwise_xor(torch.tensor([True, True, False]), torch.tensor([False, True, False]))
  4. tensor([ True, False, False])
  1. torch.ceil(input, out=None) Tensor?

返回帶有input元素的 ceil 的新張量,該元素大于或等于每個(gè)元素的最小整數(shù)。

3425145dda63005bbc6ff0b2f0331aa7

Parameters

  • input (Tensor) – the input tensor.
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> a = torch.randn(4)
  2. >>> a
  3. tensor([-0.6341, -1.4208, -1.0900, 0.5826])
  4. >>> torch.ceil(a)
  5. tensor([-0., -1., -1., 1.])
  1. torch.clamp(input, min, max, out=None) Tensor?

input中的所有元素限制在 <cite>[</cite> min , max <cite>]</cite> 范圍內(nèi),并返回結(jié)果張量:

597a5259be5b1a330eaa6858b12b8994

如果input的類型為 <cite>FloatTensor</cite> 或 <cite>DoubleTensor</cite> ,則參數(shù) min 和 max 必須為實(shí)數(shù),否則為實(shí)數(shù) 應(yīng)該是整數(shù)。

Parameters

  • input (Tensor) – the input tensor.
  • min (編號(hào))–要鉗制的范圍的下限
  • 最大(編號(hào))–要鉗位的范圍的上限
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> a = torch.randn(4)
  2. >>> a
  3. tensor([-1.7120, 0.1734, -0.0478, -0.0922])
  4. >>> torch.clamp(a, min=-0.5, max=0.5)
  5. tensor([-0.5000, 0.1734, -0.0478, -0.0922])
  1. torch.clamp(input, *, min, out=None) Tensor

input中的所有元素限制為大于或等于 min 。

如果input的類型為 <cite>FloatTensor</cite> 或 <cite>DoubleTensor</cite> ,則value應(yīng)為實(shí)數(shù),否則應(yīng)為整數(shù)。

Parameters

  • input (Tensor) – the input tensor.
  • 值(編號(hào))–輸出中每個(gè)元素的最小值
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> a = torch.randn(4)
  2. >>> a
  3. tensor([-0.0299, -2.3184, 2.1593, -0.8883])
  4. >>> torch.clamp(a, min=0.5)
  5. tensor([ 0.5000, 0.5000, 2.1593, 0.5000])
  1. torch.clamp(input, *, max, out=None) Tensor

input中的所有元素限制為小于或等于 <max。

If input is of type <cite>FloatTensor</cite> or <cite>DoubleTensor</cite>, value should be a real number, otherwise it should be an integer.

Parameters

  • input (Tensor) – the input tensor.
  • 值(編號(hào))–輸出中每個(gè)元素的最大值
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> a = torch.randn(4)
  2. >>> a
  3. tensor([ 0.7753, -0.4702, -0.4599, 1.1899])
  4. >>> torch.clamp(a, max=0.5)
  5. tensor([ 0.5000, -0.4702, -0.4599, 0.5000])
  1. torch.conj(input, out=None) Tensor?

計(jì)算給定input張量的逐元素共軛。

334473ca71a76cc4b45b56e05f9113be

Parameters

  • input (Tensor) – the input tensor.
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> torch.conj(torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j]))
  2. tensor([-1 - 1j, -2 - 2j, 3 + 3j])
  1. torch.cos(input, out=None) Tensor?

返回帶有input元素的余弦的新張量。

a54751040e4e9bbc318ce13170d3575a

Parameters

  • input (Tensor) – the input tensor.
  • out (Tensor, optional) – the output tensor.

Example:

  1. >>> a = torch.randn(4)
  2. >>> a
  3. tensor([ 1.4309, 1.2706, -0.8562, 0.9796])
  4. >>> torch.cos(a)
  5. tensor([ 0.1395, 0.2957, 0.6553, 0.5574])
  1. torch.cosh(input, out=None) Tensor?

返回具有input元素的雙曲余弦的新張量。

01d5c509dd9312bb70bb293dffb6ee74

Parameters

  • input (Tensor) – the input tensor.
  • out (
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)