譯者:https : //pytorch.org/docs/stable/torch.html
此外,它提供了許多實(shí)用程序,可有效地序列化張量和任意類型,以及其他有用的實(shí)用程序。
它具有CUDA對應(yīng)項(xiàng),使您能夠具有計(jì)算能力> = 3.0的NVIDIA GPU上運(yùn)行張量計(jì)算。
torch.is_tensor(obj)?
如果<cite> obj </ cite>是PyTorch張量,則返回True。
參數(shù)
obj(對象)–要測試的對象
torch.is_storage(obj)?
如果<cite> obj </ cite>是PyTorch存儲對象,則返回True。
參量
obj(對象)–要測試的對象
torch.is_floating_point(input) -> (bool)?
如果input
的數(shù)據(jù)類型是浮點(diǎn)數(shù)據(jù)類型,即torch.float64
,torch.float32
和torch.float16
之一,則返回True。
參量
輸入(tensor
)–要測試的PyTorch張量
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>
例:
>>> torch.tensor([1.2, 3]).dtype # initial default for floating point is torch.float32
torch.float32
>>> torch.set_default_dtype(torch.float64)
>>> torch.tensor([1.2, 3]).dtype # a new floating point tensor
torch.float64
torch.get_default_dtype() → torch.dtype?
獲取當(dāng)前的默認(rèn)浮點(diǎn)數(shù) torch.dtype
。
例:
>>> torch.get_default_dtype() # initial default for floating point is torch.float32
torch.float32
>>> torch.set_default_dtype(torch.float64)
>>> torch.get_default_dtype() # default is now changed to torch.float64
torch.float64
>>> torch.set_default_tensor_type(torch.FloatTensor) # setting tensor type also affects this
>>> torch.get_default_dtype() # changed to torch.float32, the dtype for torch.FloatTensor
torch.float32
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)張量類型類型名稱
例:
>>> torch.tensor([1.2, 3]).dtype # initial default for floating point is torch.float32
torch.float32
>>> torch.set_default_tensor_type(torch.DoubleTensor)
>>> torch.tensor([1.2, 3]).dtype # a new floating point tensor
torch.float64
torch.numel(input) → int?
返回input
張量中的元素總數(shù)。
參量
輸入(張量)–輸入張量。
例:
>>> a = torch.randn(1, 2, 3, 4, 5)
>>> torch.numel(a)
120
>>> a = torch.zeros(4,4)
>>> torch.numel(a)
16
torch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None, sci_mode=None)?
設(shè)置打印選項(xiàng)。從NumPy無恥地拿走的物品
參量
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:
>>> torch.set_flush_denormal(True)
True
>>> torch.tensor([1e-323], dtype=torch.float64)
tensor([ 0.], dtype=torch.float64)
>>> torch.set_flush_denormal(False)
True
>>> torch.tensor([1e-323], dtype=torch.float64)
tensor(9.88131e-324 *
[ 1.0000], dtype=torch.float64)
注意
隨機(jī)抽樣創(chuàng)建操作列在隨機(jī)抽樣下,包括: torch.rand()
torch.rand_like()
torch.randn()
torch.randn_like()
torch.randint()
torch.randint_like()
torch.randperm()
您也可以將 torch.empty()
與輸入一起使用
位隨機(jī)抽樣方法來創(chuàng)建 torch.Tensor
,并從更廣泛的分布范圍內(nèi)采樣值。
torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False) → Tensor?
用data
構(gòu)造一個張量。
警告
torch.tensor()
始終復(fù)制data
。 如果您具有張量data
并希望避免復(fù)制,請使用 torch.Tensor.requires_grad_()
或 torch.Tensor.detach()
。 如果您有 NumPy ndarray
并想避免復(fù)制,請使用 torch.as_tensor()
。
Warning
當(dāng)數(shù)據(jù)是張量 <cite>x</cite> 時, torch.tensor()
從傳遞的任何數(shù)據(jù)中讀出“數(shù)據(jù)”,并構(gòu)造一個葉子變量。 因此,torch.tensor(x)
等同于x.clone().detach()
,torch.tensor(x, requires_grad=True)
等同于x.clone().detach().requires_grad_(True)
。
建議使用clone()
和detach()
的等效項(xiàng)。
Parameters
ndarray
,標(biāo)量和其他類型。torch.dtype
,可選)–返回張量的所需數(shù)據(jù)類型。 默認(rèn)值:如果None
,則從data
推斷數(shù)據(jù)類型。torch.device
,可選)–返回張量的所需設(shè)備。 默認(rèn)值:如果None
,則使用當(dāng)前設(shè)備作為默認(rèn)張量類型(請參見 torch.set_default_tensor_type()
)。 device
將是用于 CPU 張量類型的 CPU,并且是用于 CUDA 張量類型的當(dāng)前 CUDA 設(shè)備。False
。False
。Example:
>>> torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])
tensor([[ 0.1000, 1.2000],
[ 2.2000, 3.1000],
[ 4.9000, 5.2000]])
>>> torch.tensor([0, 1]) # Type inference on data
tensor([ 0, 1])
>>> torch.tensor([[0.11111, 0.222222, 0.3333333]],
dtype=torch.float64,
device=torch.device('cuda:0')) # creates a torch.cuda.DoubleTensor
tensor([[ 0.1111, 0.2222, 0.3333]], dtype=torch.float64, device='cuda:0')
>>> torch.tensor(3.14159) # Create a scalar (zero-dimensional tensor)
tensor(3.1416)
>>> torch.tensor([]) # Create an empty tensor (of size (0,))
tensor([])
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
ndarray
,標(biāo)量和其他類型。 將在內(nèi)部強(qiáng)制轉(zhuǎn)換為torch.LongTensor
。 索引是矩陣中非零值的坐標(biāo),因此應(yīng)為二維,其中第一維是張量維數(shù),第二維是非零值數(shù)。ndarray
,標(biāo)量和其他類型。torch.Size
,可選)–稀疏張量的大小。 如果未提供,則將推斷大小為足以容納所有非零元素的最小大小。values
推斷數(shù)據(jù)類型。device
將是用于 CPU 張量類型的 CPU,是用于 CUDA 張量類型的當(dāng)前 CUDA 設(shè)備。False
.Example:
>>> i = torch.tensor([[0, 1, 1],
[2, 0, 2]])
>>> v = torch.tensor([3, 4, 5], dtype=torch.float32)
>>> torch.sparse_coo_tensor(i, v, [2, 4])
tensor(indices=tensor([[0, 1, 1],
[2, 0, 2]]),
values=tensor([3., 4., 5.]),
size=(2, 4), nnz=3, layout=torch.sparse_coo)
>>> torch.sparse_coo_tensor(i, v) # Shape inference
tensor(indices=tensor([[0, 1, 1],
[2, 0, 2]]),
values=tensor([3., 4., 5.]),
size=(2, 3), nnz=3, layout=torch.sparse_coo)
>>> torch.sparse_coo_tensor(i, v, [2, 4],
dtype=torch.float64,
device=torch.device('cuda:0'))
tensor(indices=tensor([[0, 1, 1],
[2, 0, 2]]),
values=tensor([3., 4., 5.]),
device='cuda:0', size=(2, 4), nnz=3, dtype=torch.float64,
layout=torch.sparse_coo)
## Create an empty sparse tensor with the following invariants:
## 1\. sparse_dim + dense_dim = len(SparseTensor.shape)
## 2\. SparseTensor._indices().shape = (sparse_dim, nnz)
## 3\. SparseTensor._values().shape = (nnz, SparseTensor.shape[sparse_dim:])
#
## For instance, to create an empty sparse tensor with nnz = 0, dense_dim = 0 and
## sparse_dim = 1 (hence indices is a 2D tensor of shape = (1, 0))
>>> S = torch.sparse_coo_tensor(torch.empty([1, 0]), [], [1])
tensor(indices=tensor([], size=(1, 0)),
values=tensor([], size=(0,)),
size=(1,), nnz=0, layout=torch.sparse_coo)
## and to create an empty sparse tensor with nnz = 0, dense_dim = 1 and
## sparse_dim = 1
>>> S = torch.sparse_coo_tensor(torch.empty([1, 0]), torch.empty([0, 2]), [1, 2])
tensor(indices=tensor([], size=(1, 0)),
values=tensor([], size=(0, 2)),
size=(1, 2), nnz=0, layout=torch.sparse_coo)
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>,則不會執(zhí)行任何復(fù)制,否則將使用新的<cite>張量</cite>。 如果數(shù)據(jù)<cite>張量</cite>>具有requires_grad=True
,則返回保留計(jì)算圖的計(jì)算圖。
同樣,如果數(shù)據(jù)是對應(yīng)的 <cite>dtype</cite> 的ndarray
,并且<cite>設(shè)備</cite>是 cpu,則不會執(zhí)行任何復(fù)制。
Parameters
ndarray
,scalar, and other types.torch.dtype
, optional) – the desired data type of returned tensor. Default: if None
, infers data type from data
.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:
>>> a = numpy.array([1, 2, 3])
>>> t = torch.as_tensor(a)
>>> t
tensor([ 1, 2, 3])
>>> t[0] = -1
>>> a
array([-1, 2, 3])
>>> a = numpy.array([1, 2, 3])
>>> t = torch.as_tensor(a, device=torch.device('cuda'))
>>> t
tensor([ 1, 2, 3])
>>> t[0] = -1
>>> a
array([1, 2, 3])
torch.as_strided(input, size, stride, storage_offset=0) → Tensor?
創(chuàng)建具有指定size
,stride
和storage_offset
的現(xiàn)有<cite>炬管</cite> input
的視圖。
Warning
創(chuàng)建的張量中的一個以上元素可以引用單個存儲位置。 結(jié)果,就地操作(尤其是矢量化的操作)可能會導(dǎo)致錯誤的行為。 如果需要寫張量,請先克隆它們。
許多 PyTorch 函數(shù)可返回張量視圖,并在此函數(shù)內(nèi)部實(shí)現(xiàn)。 這些功能,例如 torch.Tensor.expand()
,更易于閱讀,因此更可取。
Parameters
Example:
>>> x = torch.randn(3, 3)
>>> x
tensor([[ 0.9039, 0.6291, 1.0795],
[ 0.1586, 2.1939, -0.4900],
[-0.1909, -0.7503, 1.9355]])
>>> t = torch.as_strided(x, (2, 2), (1, 2))
>>> t
tensor([[0.9039, 1.0795],
[0.6291, 0.1586]])
>>> t = torch.as_strided(x, (2, 2), (1, 2), 1)
tensor([[0.6291, 0.1586],
[1.0795, 2.1939]])
torch.from_numpy(ndarray) → Tensor?
從numpy.ndarray
創(chuàng)建 Tensor
。
返回的張量和ndarray
共享相同的內(nèi)存。 對張量的修改將反映在ndarray
中,反之亦然。 返回的張量不可調(diào)整大小。
當(dāng)前它接受具有numpy.float64
,numpy.float32
,numpy.float16
,numpy.int64
,numpy.int32
,
numpy.int16
,numpy.int8
,numpy.uint8
和numpy.bool
d 類型的ndarray
。
Example:
>>> a = numpy.array([1, 2, 3])
>>> t = torch.from_numpy(a)
>>> t
tensor([ 1, 2, 3])
>>> t[0] = -1
>>> a
array([-1, 2, 3])
torch.zeros(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor?
返回一個由標(biāo)量值 <cite>0</cite> 填充的張量,其形狀由變量參數(shù)size
定義。
Parameters
torch.dtype
,可選)–返回張量的所需數(shù)據(jù)類型。 默認(rèn)值:如果None
使用全局默認(rèn)值(請參見 torch.set_default_tensor_type()
)。torch.layout
,可選)–返回的 Tensor 所需的布局。 默認(rèn)值:torch.strided
。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.False
.Example:
>>> torch.zeros(2, 3)
tensor([[ 0., 0., 0.],
[ 0., 0., 0.]])
>>> torch.zeros(5)
tensor([ 0., 0., 0., 0., 0.])
torch.zeros_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor?
返回一個由標(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 開始,此功能不支持out
關(guān)鍵字。 作為替代,舊的torch.zeros_like(input, out=output)
等效于torch.zeros(input.size(), out=output)
。
Parameters
input
的大小將確定輸出張量的大小。torch.dtype
,可選)–返回的 Tensor 的所需數(shù)據(jù)類型。 默認(rèn)值:如果為None
,則默認(rèn)為input
的dtype。torch.layout
,可選)–返回張量的所需布局。 默認(rèn)值:如果為None
,則默認(rèn)為input
的布局。torch.device
,可選)–返回張量的所需設(shè)備。 默認(rèn)值:如果為None
,則默認(rèn)為input
的設(shè)備。False
.Example:
>>> input = torch.empty(2, 3)
>>> torch.zeros_like(input)
tensor([[ 0., 0., 0.],
[ 0., 0., 0.]])
torch.ones(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor?
返回一個由標(biāo)量值 <cite>1</cite> 填充的張量,其形狀由變量自變量size
定義。
Parameters
torch.dtype
, optional) – the desired data type of returned tensor. Default: if None
, uses a global default (see torch.set_default_tensor_type()
).torch.layout
, optional) – the desired layout of returned Tensor. Default: torch.strided
.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.False
.Example:
>>> torch.ones(2, 3)
tensor([[ 1., 1., 1.],
[ 1., 1., 1.]])
>>> torch.ones(5)
tensor([ 1., 1., 1., 1., 1.])
torch.ones_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor?
返回一個由標(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 開始,此功能不支持out
關(guān)鍵字。 作為替代,舊的torch.ones_like(input, out=output)
等效于torch.ones(input.size(), out=output)
。
Parameters
input
will determine size of the output tensor.torch.dtype
, optional) – the desired data type of returned Tensor. Default: if None
, defaults to the dtype of input
.torch.layout
, optional)– the desired layout of returned tensor. Default: if None
, defaults to the layout of input
.torch.device
, optional)– the desired device of returned tensor. Default: if None
, defaults to the device of input
.False
Example:
>>> input = torch.empty(2, 3)
>>> torch.ones_like(input)
tensor([[ 1., 1., 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>開始</cite>開始具有公共差
step
的間隔[start, end)
的值。
請注意,與end
比較時,非整數(shù)step
會出現(xiàn)浮點(diǎn)舍入錯誤; 為了避免不一致,在這種情況下,建議在end
中添加一個小的ε。
Parameters
0
。1
。torch.dtype
,可選)–返回張量的所需數(shù)據(jù)類型。 默認(rèn)值:如果None
使用全局默認(rèn)值(請參閱 torch.set_default_tensor_type()
)。 如果未提供 <cite>dtype</cite> ,則從其他輸入?yún)?shù)推斷數(shù)據(jù)類型。 如果<cite>開始</cite>,<cite>結(jié)束</cite>或<cite>停止</cite>中的任何一個是浮點(diǎn),則推斷
<cite>dtype</cite> 為默認(rèn) dtype,請參見[ get_default_dtype()
。 否則,將 <cite>dtype</cite> 推斷為 <cite>torch.int64</cite> 。torch.layout
, optional) – the desired layout of returned Tensor. Default: torch.strided.
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.False
.Example:
>>> torch.arange(5)
tensor([ 0, 1, 2, 3, 4])
>>> torch.arange(1, 4)
tensor([ 1, 2, 3])
>>> torch.arange(1, 2.5, 0.5)
tensor([ 1.0000, 1.5000, 2.0000])
torch.range(start=0, end, step=1, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor?
在步驟step
中返回大小為
的一維張量,其值從
start
到end
。 階躍是張量中兩個值之間的差距。
Warning
不推薦使用此功能,而推薦使用 torch.arange()
。
Parameters
0
。1
。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>.torch.layout
, optional) – the desired layout of returned Tensor. Default: torch.strided.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.False
.Example:
>>> torch.range(1, 4)
tensor([ 1., 2., 3., 4.])
>>> torch.range(1, 4, 0.5)
tensor([ 1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000])
torch.linspace(start, end, steps=100, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor?
返回start
和end
之間等距點(diǎn)的steps
的一維張量。
輸出張量為steps
大小的 1-D。
Parameters
start
和end
之間采樣的點(diǎn)數(shù)。 默認(rèn)值:100。torch.dtype
, optional) – the desired data type of returned tensor. Default: if None
, uses a global default (see torch.set_default_tensor_type()
).torch.layout
, optional) – the desired layout of returned Tensor. Default: torch.strided
.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.False
.Example:
>>> 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.])
torch.logspace(start, end, steps=100, base=10.0, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor?
返回與
和
之間的底數(shù)
base
對數(shù)間隔的steps
點(diǎn)的一維張量。
The output tensor is 1-D of size steps
.
Parameters
start
and end
. Default: 100
.10.0
。torch.dtype
, optional) – the desired data type of returned tensor. Default: if None
, uses a global default (see torch.set_default_tensor_type()
).torch.layout
, optional) – the desired layout of returned Tensor. Default: torch.strided
.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.False
.Example:
>>> torch.logspace(start=-10, end=10, steps=5)
tensor([ 1.0000e-10, 1.0000e-05, 1.0000e+00, 1.0000e+05, 1.0000e+10])
>>> torch.logspace(start=0.1, end=1.0, steps=5)
tensor([ 1.2589, 2.1135, 3.5481, 5.9566, 10.0000])
>>> torch.logspace(start=0.1, end=1.0, steps=1)
tensor([1.2589])
>>> torch.logspace(start=2, end=2, steps=1, base=2)
tensor([4.0])
torch.eye(n, m=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor?
返回一個二維張量,對角線上有一個,其他位置為零。
Parameters
torch.dtype
, optional) – the desired data type of returned tensor. Default: if None
, uses a global default (see torch.set_default_tensor_type()
).torch.layout
, optional) – the desired layout of returned Tensor. Default: torch.strided
.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.False
.退貨
二維張量,對角線上有一個,其他位置為零
返回類型
張量
Example:
>>> torch.eye(3)
tensor([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 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
torch.dtype
, optional) – the desired data type of returned tensor. Default: if None
, uses a global default (see torch.set_default_tensor_type()
).
torch.layout
, optional) – the desired layout of returned Tensor. Default: torch.strided
.
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.False
.False
.Example:
>>> torch.empty(2, 3)
tensor(1.00000e-08 *
[[ 6.3984, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000]])
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
will determine size of the output tensor.torch.layout
, optional) – the desired layout of returned tensor. Default: if None
, defaults to the layout of input
.torch.device
, optional) – the desired device of returned tensor. Default: if None
, defaults to the device of input
.False
.Example:
>>> torch.empty((2,3), dtype=torch.int64)
tensor([[ 9.4064e+13, 2.8000e+01, 9.3493e+13],
[ 7.5751e+18, 7.1428e+18, 7.5955e+18]])
torch.empty_strided(size, stride, dtype=None, layout=None, device=None, requires_grad=False, pin_memory=False) → Tensor?
返回填充有未初始化數(shù)據(jù)的張量。 張量的形狀和步幅分別由變量參數(shù)size
和stride
定義。 torch.empty_strided(size, stride)
等同于torch.empty(size).as_strided(size, stride)
。
Warning
創(chuàng)建的張量中的一個以上元素可以引用單個存儲位置。 結(jié)果,就地操作(尤其是矢量化的操作)可能會導(dǎo)致錯誤的行為。 如果需要寫張量,請先克隆它們。
Parameters
torch.dtype
, optional) – the desired data type of returned tensor. Default: if None
, uses a global default (see torch.set_default_tensor_type()
).torch.layout
, optional) – the desired layout of returned Tensor. Default: torch.strided
.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.False
.False
.Example:
>>> a = torch.empty_strided((2, 3), (1, 2))
>>> a
tensor([[8.9683e-44, 4.4842e-44, 5.1239e+07],
[0.0000e+00, 0.0000e+00, 3.0705e-41]])
>>> a.stride()
(1, 2)
>>> a.size()
torch.Size([2, 3])
torch.full(size, fill_value, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor?
返回大小為size
的張量,其中填充了fill_value
。
Parameters
torch.Size
。torch.dtype
, optional) – the desired data type of returned tensor. Default: if None
, uses a global default (see torch.set_default_tensor_type()
).
torch.layout
, optional) – the desired layout of returned Tensor. Default: torch.strided
.
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.
False
.
Example:
>>> torch.full((2, 3), 3.141592)
tensor([[ 3.1416, 3.1416, 3.1416],
[ 3.1416, 3.1416, 3.1416]])
torch.full_like(input, fill_value, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor?
返回與填充有fill_value
的input
大小相同的張量。 torch.full_like(input, fill_value)
等同于torch.full(input.size(), fill_value, dtype=input.dtype, layout=input.layout, device=input.device)
。
Parameters
torch.dtype
, optional) – the desired data type of returned Tensor. Default: if None
, defaults to the dtype of input
.torch.layout
, optional) – the desired layout of returned tensor. Default: if None
, defaults to the layout of input
.torch.device
, optional) – the desired device of returned tensor. Default: if None
, defaults to the device of input
.False
.torch.quantize_per_tensor(input, scale, zero_point, dtype) → Tensor?
將浮點(diǎn)張量轉(zhuǎn)換為具有給定比例和零點(diǎn)的量化張量。
Parameters
torch.dtype
)–返回張量的所需數(shù)據(jù)類型。 必須是量化的 dtypes 之一:torch.quint8
,torch.qint8
和torch.qint32
Returns
新量化的張量
Return type
Tensor
Example:
>>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, torch.quint8)
tensor([-1., 0., 1., 2.], size=(4,), dtype=torch.quint8,
quantization_scheme=torch.per_tensor_affine, scale=0.1, zero_point=10)
>>> torch.quantize_per_tensor(torch.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, torch.quint8).int_repr()
tensor([ 0, 10, 20, 30], dtype=torch.uint8)
torch.quantize_per_channel(input, scales, zero_points, axis, dtype) → Tensor?
將浮點(diǎn)張量轉(zhuǎn)換為具有給定比例和零點(diǎn)的每通道量化張量。
Parameters
input.size(axis)
input.size(axis)
相匹配torch.dtype
) – the desired data type of returned tensor. Has to be one of the quantized dtypes: torch.quint8
, torch.qint8
, torch.qint32
Returns
A newly quantized tensor
Return type
Tensor
Example:
>>> x = torch.tensor([[-1.0, 0.0], [1.0, 2.0]])
>>> torch.quantize_per_channel(x, torch.tensor([0.1, 0.01]), torch.tensor([10, 0]), 0, torch.quint8)
tensor([[-1., 0.],
[ 1., 2.]], size=(2, 2), dtype=torch.quint8,
quantization_scheme=torch.per_channel_affine,
scale=tensor([0.1000, 0.0100], dtype=torch.float64),
zero_point=tensor([10, 0]), axis=0)
>>> torch.quantize_per_channel(x, torch.tensor([0.1, 0.01]), torch.tensor([10, 0]), 0, torch.quint8).int_repr()
tensor([[ 0, 10],
[100, 200]], dtype=torch.uint8)
torch.cat(tensors, dim=0, out=None) → Tensor?
在給定維度上連接seq
張量的給定序列。 所有張量必須具有相同的形狀(在連接維中除外)或?yàn)榭铡?/p>
torch.cat()
可以看作是 torch.split()
和 torch.chunk()
的逆運(yùn)算。
通過示例可以更好地理解 torch.cat()
。
Parameters
Example:
>>> x = torch.randn(2, 3)
>>> x
tensor([[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497]])
>>> torch.cat((x, x, x), 0)
tensor([[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497],
[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497],
[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497]])
>>> torch.cat((x, x, x), 1)
tensor([[ 0.6580, -1.0969, -0.4614, 0.6580, -1.0969, -0.4614, 0.6580,
-1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497, -0.1034, -0.5790, 0.1497, -0.1034,
-0.5790, 0.1497]])
torch.chunk(input, chunks, dim=0) → List of Tensors?
將張量拆分為特定數(shù)量的塊。
如果沿給定維度dim
的張量大小不能被chunks
整除,則最后一塊將較小。
Parameters
torch.gather(input, dim, index, out=None, sparse_grad=False) → Tensor?
沿<cite>昏暗</cite>指定的軸收集值。
對于 3-D 張量,輸出指定為:
out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0
out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1
out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2
如果input
是大小為
和
dim = i
的 n 維張量,則index
必須是大小為
的
-維張量,其中
和
out
具有相同的大小 大小為index
。
Parameters
True
,則梯度 w.r.t. input
將是一個稀疏張量。Example:
>>> t = torch.tensor([[1,2],[3,4]])
>>> torch.gather(t, 1, torch.tensor([[0,0],[1,0]]))
tensor([[ 1, 1],
[ 4, 3]])
torch.index_select(input, dim, index, out=None) → Tensor?
返回一個新張量,該張量使用index
LongTensor 中的index
中的條目沿維度dim
索引input
張量。
返回的張量具有與原始張量(input
)相同的維數(shù)。 dim
的尺寸與index
的長度相同; 其他尺寸與原始張量中的尺寸相同。
Note
返回的張量不與原始張量使用相同的存儲空間而不是。 如果out
的形狀與預(yù)期的形狀不同,我們將默默地將其更改為正確的形狀,并在必要時重新分配基礎(chǔ)存儲。
Parameters
Example:
>>> x = torch.randn(3, 4)
>>> x
tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],
[-0.4664, 0.2647, -0.1228, -1.1068],
[-1.1734, -0.6571, 0.7230, -0.6004]])
>>> indices = torch.tensor([0, 2])
>>> torch.index_select(x, 0, indices)
tensor([[ 0.1427, 0.0231, -0.5414, -1.0009],
[-1.1734, -0.6571, 0.7230, -0.6004]])
>>> torch.index_select(x, 1, indices)
tensor([[ 0.1427, -0.5414],
[-0.4664, -0.1228],
[-1.1734, 0.7230]])
torch.masked_select(input, mask, out=None) → Tensor?
返回一個新的一維張量,該張量根據(jù)布爾值掩碼mask
為其 <cite>BoolTensor</cite> 索引input
張量。
mask
張量和input
張量的形狀不需要匹配,但它們必須是>可廣播的。
Note
返回的張量是否而不是使用與原始張量相同的存儲
Parameters
Example:
>>> x = torch.randn(3, 4)
>>> x
tensor([[ 0.3552, -2.3825, -0.8297, 0.3477],
[-1.2035, 1.2252, 0.5002, 0.6248],
[ 0.1307, -2.0608, 0.1244, 2.0139]])
>>> mask = x.ge(0.5)
>>> mask
tensor([[False, False, False, False],
[False, True, True, True],
[False, False, False, True]])
>>> torch.masked_select(x, mask)
tensor([ 1.2252, 0.5002, 0.6248, 2.0139])
torch.narrow(input, dim, start, length) → Tensor?
返回一個新的張量,該張量是input
張量的縮小版本。 尺寸dim
從start
輸入到start + length
。 返回的張量和input
張量共享相同的基礎(chǔ)存儲。
Parameters
Example:
>>> x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> torch.narrow(x, 0, 0, 2)
tensor([[ 1, 2, 3],
[ 4, 5, 6]])
>>> torch.narrow(x, 1, 1, 2)
tensor([[ 2, 3],
[ 5, 6],
[ 8, 9]])
torch.nonzero(input, *, out=None, as_tuple=False) → LongTensor or tuple of LongTensors?
Note
torch.nonzero(..., as_tuple=False)
(默認(rèn)值)返回一個二維張量,其中每一行都是非零值的索引。
torch.nonzero(..., as_tuple=True)
返回一維索引張量的元組,允許進(jìn)行高級索引,因此x[x.nonzero(as_tuple=True)]
給出張量x
的所有非零值。 在返回的元組中,每個索引張量都包含特定維度的非零索引。
有關(guān)這兩種行為的更多詳細(xì)信息,請參見下文。
當(dāng) as_tuple
為“ False”(默認(rèn))時:
返回一個張量,該張量包含input
所有非零元素的索引。 結(jié)果中的每一行都包含input
中非零元素的索引。 結(jié)果按字典順序排序,最后一個索引更改最快(C 樣式)。
如果input
具有
尺寸,則所得索引張量
out
的大小為
,其中
是
input
張量中非零元素的總數(shù)。
當(dāng) as_tuple
為“ True” 時:
返回一維張量的元組,在input
中每個維度一個張量,每個張量包含input
所有非零元素的索引(在該維度中)。
如果input具有
尺寸,則生成的元組包含
大小的
張量,其中
是
input
張量中非零元素的總數(shù)。
作為一種特殊情況,當(dāng)input
具有零維和非零標(biāo)量值時,會將其視為具有一個元素的一維張量。
Parameters
Returns
如果as_tuple
為False
,則包含索引的輸出張量。 如果as_tuple
為True
,則每個維度都有一個 1-D 張量,其中包含沿著該維度的每個非零元素的索引。
Return type
LongTensor 或 LongTensor 的元組
Example:
>>> torch.nonzero(torch.tensor([1, 1, 1, 0, 1]))
tensor([[ 0],
[ 1],
[ 2],
[ 4]])
>>> torch.nonzero(torch.tensor([[0.6, 0.0, 0.0, 0.0],
[0.0, 0.4, 0.0, 0.0],
[0.0, 0.0, 1.2, 0.0],
[0.0, 0.0, 0.0,-0.4]]))
tensor([[ 0, 0],
[ 1, 1],
[ 2, 2],
[ 3, 3]])
>>> torch.nonzero(torch.tensor([1, 1, 1, 0, 1]), as_tuple=True)
(tensor([0, 1, 2, 4]),)
>>> torch.nonzero(torch.tensor([[0.6, 0.0, 0.0, 0.0],
[0.0, 0.4, 0.0, 0.0],
[0.0, 0.0, 1.2, 0.0],
[0.0, 0.0, 0.0,-0.4]]), as_tuple=True)
(tensor([0, 1, 2, 3]), tensor([0, 1, 2, 3]))
>>> torch.nonzero(torch.tensor(5), as_tuple=True)
(tensor([0]),)
torch.reshape(input, shape) → Tensor?
返回具有與input
相同的數(shù)據(jù)和元素?cái)?shù)量,但具有指定形狀的張量。 如果可能,返回的張量將是input
的視圖。 否則,它將是副本。 連續(xù)輸入和具有兼容步幅的輸入可以在不復(fù)制的情況下進(jìn)行重塑,但是您不應(yīng)該依賴復(fù)制與查看行為。
當(dāng)可以返回視圖時,請參見 torch.Tensor.view()
。
單個尺寸可能為-1,在這種情況下,它是根據(jù)input
中的其余尺寸和元素?cái)?shù)量推斷出來的。
Parameters
Example:
>>> a = torch.arange(4.)
>>> torch.reshape(a, (2, 2))
tensor([[ 0., 1.],
[ 2., 3.]])
>>> b = torch.tensor([[0, 1], [2, 3]])
>>> torch.reshape(b, (-1,))
tensor([ 0, 1, 2, 3])
torch.split(tensor, split_size_or_sections, dim=0)?
將張量拆分為多個塊。
如果split_size_or_sections
是整數(shù)類型,則 tensor
將被拆分為大小相等的塊(如果可能)。 如果沿給定維度dim
的張量大小不能被split_size
整除,則最后一個塊將較小。
如果split_size_or_sections
是列表,則根據(jù)split_size_or_sections
將 tensor
拆分為dim
,大小為dim
。
Parameters
torch.squeeze(input, dim=None, out=None) → Tensor?
返回一個張量,其中所有尺寸為 <cite>1</cite> 的input
尺寸均被刪除。
例如,如果<cite>輸入</cite>的形狀為:
,則張量中的<cite>張量將為:
。</cite>
給定dim
時,僅在給定尺寸上執(zhí)行擠壓操作。 如果<cite>輸入</cite>的形狀為:
,
squeeze(input, 0)
保持張量不變,但是squeeze(input, 1)
會將張量壓縮為
形狀。
Note
返回的張量與輸入張量共享存儲,因此更改一個張量的內(nèi)容將更改另一個張量的內(nèi)容。
Parameters
Example:
>>> x = torch.zeros(2, 1, 2, 1, 2)
>>> x.size()
torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x)
>>> y.size()
torch.Size([2, 2, 2])
>>> y = torch.squeeze(x, 0)
>>> y.size()
torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x, 1)
>>> y.size()
torch.Size([2, 2, 1, 2])
torch.stack(tensors, dim=0, out=None) → Tensor?
將張量的序列沿新維度連接起來。
所有張量都必須具有相同的大小。
Parameters
torch.t(input) → Tensor?
期望input
為< = 2-D 張量,并轉(zhuǎn)置尺寸 0 和 1。
將按原樣返回 0-D 和 1-D 張量,并且可以將 2-D 張量視為transpose(input, 0, 1)
的簡寫函數(shù)。
Parameters
input (Tensor) – the input tensor.
Example:
>>> x = torch.randn(())
>>> x
tensor(0.1995)
>>> torch.t(x)
tensor(0.1995)
>>> x = torch.randn(3)
>>> x
tensor([ 2.4320, -0.4608, 0.7702])
>>> torch.t(x)
tensor([.2.4320,.-0.4608,..0.7702])
>>> x = torch.randn(2, 3)
>>> x
tensor([[ 0.4875, 0.9158, -0.5872],
[ 0.3938, -0.6929, 0.6932]])
>>> torch.t(x)
tensor([[ 0.4875, 0.3938],
[ 0.9158, -0.6929],
[-0.5872, 0.6932]])
torch.take(input, index) → Tensor?
返回給定索引處帶有input
元素的新張量。 將輸入張量視為視為一維張量。 結(jié)果采用與索引相同的形狀。
Parameters
Example:
>>> src = torch.tensor([[4, 3, 5],
[6, 7, 8]])
>>> torch.take(src, torch.tensor([0, 2, 5]))
tensor([ 4, 5, 8])
torch.transpose(input, dim0, dim1) → Tensor?
返回一個張量,該張量是input
的轉(zhuǎn)置版本。 給定的尺寸dim0
和dim1
被交換。
產(chǎn)生的out
張量與input
張量共享其基礎(chǔ)存儲,因此更改一個內(nèi)容將更改另一個內(nèi)容。
Parameters
Example:
>>> x = torch.randn(2, 3)
>>> x
tensor([[ 1.0028, -0.9893, 0.5809],
[-0.1669, 0.7299, 0.4942]])
>>> torch.transpose(x, 0, 1)
tensor([[ 1.0028, -0.1669],
[-0.9893, 0.7299],
[ 0.5809, 0.4942]])
torch.unbind(input, dim=0) → seq?
刪除張量尺寸。
返回給定維度上所有切片的元組,已經(jīng)沒有它。
Parameters
Example:
>>> torch.unbind(torch.tensor([[1, 2, 3],
>>> [4, 5, 6],
>>> [7, 8, 9]]))
(tensor([1, 2, 3]), tensor([4, 5, 6]), tensor([7, 8, 9]))
torch.unsqueeze(input, dim, out=None) → Tensor?
返回在指定位置插入的尺寸為 1 的新張量。
返回的張量與此張量共享相同的基礎(chǔ)數(shù)據(jù)。
可以使用[-input.dim() - 1, input.dim() + 1)
范圍內(nèi)的dim
值。 負(fù)的dim
對應(yīng)于dim
= dim + input.dim() + 1
處應(yīng)用的 unsqueeze()
。
Parameters
Example:
>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, 0)
tensor([[ 1, 2, 3, 4]])
>>> torch.unsqueeze(x, 1)
tensor([[ 1],
[ 2],
[ 3],
[ 4]])
torch.where()?
torch.where(condition, x, y) → Tensor
返回從x
或y
中選擇的元素的張量,具體取決于condition
。
該操作定義為:
Note
張量condition
,x
和y
必須是可廣播的。
Parameters
condition
為True
的索引處選擇的值condition
為False
的索引處選擇的值Returns
形狀張量等于condition
,x
,y
的廣播形狀
Return type
Tensor
Example:
>>> x = torch.randn(3, 2)
>>> y = torch.ones(3, 2)
>>> x
tensor([[-0.4620, 0.3139],
[ 0.3898, -0.7197],
[ 0.0478, -0.1657]])
>>> torch.where(x > 0, x, y)
tensor([[ 1.0000, 0.3139],
[ 0.3898, 1.0000],
[ 0.0478, 1.0000]])
torch.where(condition) → tuple of LongTensor
torch.where(condition)
與torch.nonzero(condition, as_tuple=True)
相同。
Note
另請參見 torch.nonzero()
。
class torch._C.Generator(device='cpu') → Generator?
創(chuàng)建并返回一個生成器對象,該對象管理產(chǎn)生偽隨機(jī)數(shù)的算法的狀態(tài)。 在許多就地隨機(jī)采樣函數(shù)中用作關(guān)鍵字參數(shù)。
Parameters
設(shè)備(torch.device
,可選)–生成器所需的設(shè)備。
Returns
一個 torch.Generator 對象。
Return type
生成器
Example:
>>> g_cpu = torch.Generator()
>>> g_cuda = torch.Generator(device='cuda')
device?
Generator.device->設(shè)備
獲取生成器的當(dāng)前設(shè)備。
Example:
>>> g_cpu = torch.Generator()
>>> g_cpu.device
device(type='cpu')
get_state() → Tensor?
返回生成器狀態(tài)為torch.ByteTensor
。
Returns
一個torch.ByteTensor
,其中包含將生成器還原到特定時間點(diǎn)的所有必要位。
Return type
Tensor
Example:
>>> g_cpu = torch.Generator()
>>> g_cpu.get_state()
initial_seed() → int?
返回用于生成隨機(jī)數(shù)的初始種子。
Example:
>>> g_cpu = torch.Generator()
>>> g_cpu.initial_seed()
2147483647
manual_seed(seed) → Generator?
設(shè)置用于生成隨機(jī)數(shù)的種子。 返回一個<cite>torch.生成器</cite>對象。 建議設(shè)置一個大種子,即一個具有 0 和 1 位平衡的數(shù)字。 避免在種子中包含許多 0 位。
Parameters
種子 (python:int )–所需的種子。
Returns
An torch.Generator object.
Return type
Generator
Example:
>>> g_cpu = torch.Generator()
>>> g_cpu.manual_seed(2147483647)
seed() → int?
從 std :: random_device 或當(dāng)前時間獲取不確定的隨機(jī)數(shù),并將其用作生成器的種子。
Example:
>>> g_cpu = torch.Generator()
>>> g_cpu.seed()
1516516984916
set_state(new_state) → void?
設(shè)置生成器狀態(tài)。
Parameters
new_state (Torch.ByteTensor )–所需狀態(tài)。
Example:
>>> g_cpu = torch.Generator()
>>> g_cpu_other = torch.Generator()
>>> g_cpu.set_state(g_cpu_other.get_state())
torch.seed()?
將用于生成隨機(jī)數(shù)的種子設(shè)置為不確定的隨機(jī)數(shù)。 返回用于播種 RNG 的 64 位數(shù)字。
torch.manual_seed(seed)?
設(shè)置用于生成隨機(jī)數(shù)的種子。 返回一個<cite>torch.生成器</cite>對象。
Parameters
seed (python:int) – The desired seed.
torch.initial_seed()?
返回長為 Python <cite>long</cite> 的用于生成隨機(jī)數(shù)的初始種子。
torch.get_rng_state()?
以 <cite>torch.ByteTensor</cite> 的形式返回隨機(jī)數(shù)生成器狀態(tài)。
torch.set_rng_state(new_state)?
設(shè)置隨機(jī)數(shù)生成器狀態(tài)。
Parameters
new_state (torch.ByteTensor )–所需狀態(tài)
torch.default_generator Returns the default CPU torch.Generator?
torch.bernoulli(input, *, generator=None, out=None) → Tensor?
從伯努利分布中提取二進(jìn)制隨機(jī)數(shù)(0 或 1)。
input
張量應(yīng)為包含用于繪制二進(jìn)制隨機(jī)數(shù)的概率的張量。 因此,input
中的所有值都必須在以下范圍內(nèi):
。
輸出張量的
元素將根據(jù)
input
中給出的
概率值繪制一個
值。
返回的out
張量僅具有值 0 或 1,并且具有與input
相同的形狀。
out
可以具有整數(shù)dtype
,但是input
必須具有浮點(diǎn)dtype
。
Parameters
torch.Generator
,可選)–用于采樣的偽隨機(jī)數(shù)生成器Example:
>>> a = torch.empty(3, 3).uniform_(0, 1) # generate a uniform random matrix with range [0, 1]
>>> a
tensor([[ 0.1737, 0.0950, 0.3609],
[ 0.7148, 0.0289, 0.2676],
[ 0.9456, 0.8937, 0.7202]])
>>> torch.bernoulli(a)
tensor([[ 1., 0., 0.],
[ 0., 0., 0.],
[ 1., 1., 1.]])
>>> a = torch.ones(3, 3) # probability of drawing "1" is 1
>>> torch.bernoulli(a)
tensor([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]])
>>> a = torch.zeros(3, 3) # probability of drawing "1" is 0
>>> torch.bernoulli(a)
tensor([[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]])
torch.multinomial(input, num_samples, replacement=False, *, generator=None, out=None) → LongTensor?
返回一個張量,其中每行包含num_samples
索引,這些索引是從位于張量input
的相應(yīng)行中的多項(xiàng)式概率分布中采樣的。
Note
input
的行不需要加總為 1(在這種情況下,我們將這些值用作權(quán)重),但必須為非負(fù)數(shù),有限且總和為非零。
根據(jù)每個樣本的采樣時間,索引從左到右排序(第一個樣本放在第一列中)。
如果input
是向量,則out
是大小num_samples
的向量。
如果input
是具有 <cite>m</cite> 行的矩陣,則out
是形狀
的矩陣。
如果替換為True
,則抽取樣本進(jìn)行替換。
如果沒有,則它們將被替換而不會被繪制,這意味著當(dāng)為一行繪制樣本索引時,無法為該行再次繪制它。
Note
如果繪制時不進(jìn)行替換,則num_samples
必須小于input
中非零元素的數(shù)目(如果是矩陣,則必須小于input
每行中非零元素的最小數(shù)目)。
Parameters
torch.Generator
, optional) – a pseudorandom number generator for samplingExample:
>>> weights = torch.tensor([0, 10, 3, 0], dtype=torch.float) # create a tensor of weights
>>> torch.multinomial(weights, 2)
tensor([1, 2])
>>> torch.multinomial(weights, 4) # ERROR!
RuntimeError: invalid argument 2: invalid multinomial distribution (with replacement=False,
not enough non-negative category to sample) at ../aten/src/TH/generic/THTensorRandom.cpp:320
>>> torch.multinomial(weights, 4, replacement=True)
tensor([ 2, 1, 1, 1])
torch.normal()?
torch.normal(mean, std, *, generator=None, out=None) → Tensor
返回從均值和標(biāo)準(zhǔn)差給出的獨(dú)立正態(tài)分布中得出的隨機(jī)數(shù)張量。
mean
是一個張量,每個輸出元素的正態(tài)分布均值
std
是一個張量,每個輸出元素的正態(tài)分布的標(biāo)準(zhǔn)偏差
mean
和 std
的形狀不需要匹配,但是每個張量中元素的總數(shù)必須相同。
Note
當(dāng)形狀不匹配時,將 mean
的形狀用作返回的輸出張量的形狀
Parameters
torch.Generator
, optional) – a pseudorandom number generator for samplingExample:
>>> torch.normal(mean=torch.arange(1., 11.), std=torch.arange(1, 0, -0.1))
tensor([ 1.0425, 3.5672, 2.7969, 4.2925, 4.7229, 6.2134,
8.0505, 8.1408, 9.0563, 10.0566])
torch.normal(mean=0.0, std, out=None) → Tensor
與上面的功能相似,但均值在所有繪制的元素之間共享。
Parameters
Example:
>>> torch.normal(mean=0.5, std=torch.arange(1., 6.))
tensor([-1.2793, -1.0732, -2.0687, 5.1177, -1.2303])
torch.normal(mean, std=1.0, out=None) → Tensor
與上面的函數(shù)相似,但是標(biāo)準(zhǔn)偏差在所有繪制的元素之間共享。
Parameters
Example:
>>> torch.normal(mean=torch.arange(1., 6.))
tensor([ 1.1552, 2.6148, 2.6535, 5.8318, 4.2361])
torch.normal(mean, std, size, *, out=None) → Tensor
與上述功能相似,但均值和標(biāo)準(zhǔn)差在所有繪制的元素之間共享。 所得張量的大小由size
給出。
Parameters
Example:
>>> torch.normal(2, 3, size=(1, 4))
tensor([[-1.3987, -1.9544, 3.6048, 0.7909]])
torch.rand(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor?
從區(qū)間
返回均勻分布的隨機(jī)張量
張量的形狀由變量參數(shù)size
定義。
Parameters
torch.dtype
, optional) – the desired data type of returned tensor. Default: if None
, uses a global default (see torch.set_default_tensor_type()
).torch.layout
, optional) – the desired layout of returned Tensor. Default: torch.strided
.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.False
.Example:
>>> torch.rand(4)
tensor([ 0.5204, 0.2503, 0.3525, 0.5673])
>>> torch.rand(2, 3)
tensor([[ 0.8237, 0.5781, 0.6879],
[ 0.3816, 0.7249, 0.0998]])
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
torch.dtype
, optional) – the desired data type of returned Tensor. Default: if None
, defaults to the dtype of input
.torch.layout
, optional) – the desired layout of returned tensor. Default: if None
, defaults to the layout of input
.torch.device
, optional) – the desired device of returned tensor. Default: if None
, defaults to the device of input
.False
.torch.randint(low=0, high, size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor?
返回一個由在low
(包括)和high
(不包括)之間均勻生成的隨機(jī)整數(shù)填充的張量。
The shape of the tensor is defined by the variable argument size.
Parameters
torch.Generator
, optional) – a pseudorandom number generator for samplingtorch.dtype
, optional) – the desired data type of returned tensor. Default: if None
, uses a global default (see torch.set_default_tensor_type()
).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.False
.Example:
>>> torch.randint(3, 5, (3,))
tensor([4, 3, 4])
>>> torch.randint(10, (2, 2))
tensor([[0, 2],
[5, 5]])
>>> torch.randint(3, 10, (2, 2))
tensor([[4, 5],
[6, 7]])
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
will determine size of the output tensor.torch.dtype
, optional) – the desired data type of returned Tensor. Default: if None
, defaults to the dtype of input
.torch.layout
, optional) – the desired layout of returned tensor. Default: if None
, defaults to the layout of input
.torch.device
, optional) – the desired device of returned tensor. Default: if None
, defaults to the device of input
.False
.torch.randn(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor?
從平均值為 <cite>0</cite> ,方差為 <cite>1</cite> 的正態(tài)分布中返回一個填充有隨機(jī)數(shù)的張量(也稱為標(biāo)準(zhǔn)正態(tài)分布)。
The shape of the tensor is defined by the variableargument size
.
Parameters
torch.dtype
, optional) – the desired data type of returned tensor. Default: if None
, uses a global default (see torch.set_default_tensor_type()
).torch.layout
, optional) – the desired layout of returned Tensor. Default: torch.strided
.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.False
.Example:
>>> torch.randn(4)
tensor([-2.1436, 0.9966, 2.3426, -0.6366])
>>> torch.randn(2, 3)
tensor([[ 1.5954, 2.8929, -1.0923],
[ 1.1719, -0.4709, -0.1996]])
torch.randn_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor?
返回一個與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
torch.dtype
, optional) – the desired data type of returned Tensor. Default: if None
, defaults to the dtype of input
.torch.layout
, optional) – the desired layout of returned tensor. Default: if None
, defaults to the layout of input
.torch.device
, optional) – the desired device of returned tensor. Default: if None
, defaults to the device of input
.False
.torch.randperm(n, out=None, dtype=torch.int64, layout=torch.strided, device=None, requires_grad=False) → LongTensor?
返回從0
到n - 1
的整數(shù)的隨機(jī)排列。
Parameters
torch.dtype
,可選)–返回張量的所需數(shù)據(jù)類型。 默認(rèn)值:torch.int64
。torch.layout
, optional) – the desired layout of returned Tensor. Default: torch.strided
.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.False
.Example:
>>> torch.randperm(4)
tensor([2, 1, 0, 3])
在 Tensor 上還定義了一些就地隨機(jī)采樣函數(shù)。 單擊以查看其文檔:
torch.Tensor.bernoulli_()
- torch.bernoulli()
的就地版本torch.Tensor.cauchy_()
-從柯西分布中得出的數(shù)字torch.Tensor.exponential_()
-從指數(shù)分布中得出的數(shù)字torch.Tensor.geometric_()
-從幾何分布中繪制的元素torch.Tensor.log_normal_()
-來自對數(shù)正態(tài)分布的樣本torch.Tensor.normal_()
- torch.normal()
的就地版本torch.Tensor.random_()
-從離散均勻分布中采樣的數(shù)字torch.Tensor.uniform_()
-從連續(xù)均勻分布中采樣的數(shù)字class torch.quasirandom.SobolEngine(dimension, scramble=False, seed=None)?
torch.quasirandom.SobolEngine
是用于生成(加擾)Sobol 序列的引擎。 Sobol 序列是低差異準(zhǔn)隨機(jī)序列的一個示例。
用于 Sobol 序列的引擎的這種實(shí)現(xiàn)方式能夠?qū)ψ畲缶S度為 1111 的序列進(jìn)行采樣。它使用方向編號生成這些序列,并且這些編號已從此處改編而來。
參考文獻(xiàn)
Parameters
False
。None
例子:
>>> soboleng = torch.quasirandom.SobolEngine(dimension=5)
>>> soboleng.draw(3)
tensor([[0.5000, 0.5000, 0.5000, 0.5000, 0.5000],
[0.7500, 0.2500, 0.7500, 0.2500, 0.7500],
[0.2500, 0.7500, 0.2500, 0.7500, 0.2500]])
draw(n=1, out=None, dtype=torch.float32)?
從 Sobol 序列中繪制n
點(diǎn)序列的功能。 請注意,樣本取決于先前的樣本。 結(jié)果的大小為
。
Parameters
torch.dtype
,可選)–返回的張量的所需數(shù)據(jù)類型。 默認(rèn)值:torch.float32
fast_forward(n)?
通過n
步驟快速前進(jìn)SobolEngine
狀態(tài)的功能。 這等效于不使用樣本繪制n
樣本。
Parameters
n (Int )–快進(jìn)的步數(shù)。
reset()?
將SobolEngine
重置為基本狀態(tài)的功能。
torch.save(obj, f, pickle_module=<module 'pickle' from '/opt/conda/lib/python3.6/pickle.py'>, pickle_protocol=2, _use_new_zipfile_serialization=False)?
將對象保存到磁盤文件。
另請參見:推薦的模型保存方法
Parameters
Warning
如果使用的是 Python 2,則 torch.save()
不支持StringIO.StringIO
作為有效的類似文件的對象。 這是因?yàn)?write 方法應(yīng)返回寫入的字節(jié)數(shù); StringIO.write()
不這樣做。
請改用io.BytesIO
之類的東西。
例
>>> # Save to file
>>> x = torch.tensor([0, 1, 2, 3, 4])
>>> torch.save(x, 'tensor.pt')
>>> # Save to io.BytesIO buffer
>>> buffer = io.BytesIO()
>>> torch.save(x, buffer)
torch.load(f, map_location=None, pickle_module=<module 'pickle' from '/opt/conda/lib/python3.6/pickle.py'>, **pickle_load_args)?
從文件加載用 torch.save()
保存的對象。
torch.load()
使用 Python 的解開工具,但會特別處理位于張量之下的存儲。 它們首先在 CPU 上反序列化,然后移到保存它們的設(shè)備上。 如果失敗(例如,因?yàn)檫\(yùn)行時系統(tǒng)沒有某些設(shè)備),則會引發(fā)異常。 但是,可以使用 map_location
參數(shù)將存儲動態(tài)重新映射到一組備用設(shè)備。
如果map_location
是可調(diào)用的,則將為每個序列化存儲調(diào)用一次,并帶有兩個參數(shù):storage 和 location。 storage 參數(shù)將是駐留在 CPU 上的存儲的初始反序列化。 每個序列化存儲都有一個與之關(guān)聯(lián)的位置標(biāo)簽,該標(biāo)簽標(biāo)識了從中進(jìn)行保存的設(shè)備,該標(biāo)簽是傳遞給map_location
的第二個參數(shù)。 內(nèi)置位置標(biāo)簽是用于 CPU 張量的'cpu'
和用于 CUDA 張量的'cuda:device_id'
(例如'cuda:2'
)。 map_location
應(yīng)該返回None
或存儲。
如果map_location
返回存儲,它將用作最終反序列化的對象,已經(jīng)移至正確的設(shè)備。 否則, torch.load()
將退回到默認(rèn)行為,就像未指定map_location
一樣。
如果map_location
是 torch.device
對象或與設(shè)備標(biāo)簽沖突的字符串,則它指示應(yīng)加載所有張量的位置。
否則,如果map_location
是字典,它將用于將文件(鍵)中出現(xiàn)的位置標(biāo)簽重新映射到指定將存儲位置(值)放置的位置標(biāo)簽。
用戶擴(kuò)展可以使用torch.serialization.register_package()
注冊自己的位置標(biāo)簽以及標(biāo)記和反序列化方法。
Parameters
read()
,:methreadline
,:methtell
和:methseek
)或包含文件名的字符串torch.device
,字符串或指定如何重新映射存儲位置的字典pickle_module
匹配)pickle_module.load()
和pickle_module.Unpickler()
,例如errors=...
。Note
當(dāng)您在包含 GPU 張量的文件上調(diào)用 torch.load()
時,這些張量將默認(rèn)加載到 GPU。 您可以先調(diào)用torch.load(.., map_location='cpu')
,然后再調(diào)用load_state_dict()
,以避免在加載模型檢查點(diǎn)時 GPU RAM 激增。
Note
默認(rèn)情況下,我們將字節(jié)字符串解碼為utf-8
。 這是為了避免在 Python 3 中加載 Python 2 保存的文件時出現(xiàn)常見錯誤情況UnicodeDecodeError: 'ascii' codec can't decode byte 0x...
。如果此默認(rèn)設(shè)置不正確,則可以使用額外的encoding
關(guān)鍵字參數(shù)來指定應(yīng)如何加載這些對象,例如encoding='latin1'
使用latin1
編碼將它們解碼為字符串,encoding='bytes'
將它們保留為字節(jié)數(shù)組,以后可以使用byte_array.decode(...)
進(jìn)行解碼。
Example
>>> torch.load('tensors.pt')
## Load all tensors onto the CPU
>>> torch.load('tensors.pt', map_location=torch.device('cpu'))
## Load all tensors onto the CPU, using a function
>>> torch.load('tensors.pt', map_location=lambda storage, loc: storage)
## Load all tensors onto GPU 1
>>> torch.load('tensors.pt', map_location=lambda storage, loc: storage.cuda(1))
## Map tensors from GPU 1 to GPU 0
>>> torch.load('tensors.pt', map_location={'cuda:1':'cuda:0'})
## Load tensor from io.BytesIO object
>>> with open('tensor.pt', 'rb') as f:
buffer = io.BytesIO(f.read())
>>> torch.load(buffer)
## Load a module with 'ascii' encoding for unpickling
>>> torch.load('module.pt', encoding='ascii')
torch.get_num_threads() → int?
返回用于并行化 CPU 操作的線程數(shù)
torch.set_num_threads(int)?
設(shè)置用于 CPU 上的內(nèi)部運(yùn)算并行的線程數(shù)。 警告:為確保使用正確的線程數(shù),必須在運(yùn)行 eager,JIT 或 autograd 代碼之前調(diào)用 set_num_threads。
torch.get_num_interop_threads() → int?
返回用于 CPU 上的互操作并行的線程數(shù)(例如,在 JIT 解釋器中)
torch.set_num_interop_threads(int)?
設(shè)置用于 CPU 上的互操作并行性(例如,在 JIT 解釋器中)的線程數(shù)。 警告:只能在一次操作間并行工作開始之前(例如 JIT 執(zhí)行)調(diào)用一次。
上下文管理器torch.no_grad()
,torch.enable_grad()
和torch.set_grad_enabled()
有助于局部禁用和啟用梯度計(jì)算。 有關(guān)其用法的更多詳細(xì)信息,請參見局部禁用梯度計(jì)算。 這些上下文管理器是線程本地的,因此如果您使用threading
模塊等將工作發(fā)送到另一個線程,它們將無法工作。
Examples:
>>> x = torch.zeros(1, requires_grad=True)
>>> with torch.no_grad():
... y = x * 2
>>> y.requires_grad
False
>>> is_train = False
>>> with torch.set_grad_enabled(is_train):
... y = x * 2
>>> y.requires_grad
False
>>> torch.set_grad_enabled(True) # this can also be used as a function
>>> y = x * 2
>>> y.requires_grad
True
>>> torch.set_grad_enabled(False)
>>> y = x * 2
>>> y.requires_grad
False
torch.abs(input, out=None) → Tensor?
計(jì)算給定input張量的按元素的絕對值。
Parameters
Example:
>>> torch.abs(torch.tensor([-1, -2, 3]))
tensor([ 1, 2, 3])
torch.acos(input, out=None) → Tensor?
返回帶有input元素的反余弦的新張量。
Parameters
Example:
>>> a = torch.randn(4)
>>> a
tensor([ 0.3348, -0.5889, 0.2005, -0.1584])
>>> torch.acos(a)
tensor([ 1.2294, 2.2004, 1.3690, 1.7298])
torch.add()?
torch.add(input, other, out=None)
將標(biāo)量other
添加到輸入input
的每個元素中,并返回一個新的結(jié)果張量。
如果input
的類型為 FloatTensor 或 DoubleTensor,則other
必須為實(shí)數(shù),否則應(yīng)為整數(shù)。
Parameters
input
每個元素的編號Keyword Arguments
out (Tensor, optional) – the output tensor.
Example:
>>> a = torch.randn(4)
>>> a
tensor([ 0.0202, 1.0985, 1.3506, -0.6056])
>>> torch.add(a, 20)
tensor([ 20.0202, 21.0985, 21.3506, 19.3944])
torch.add(input, alpha=1, other, out=None)
張量other
的每個元素乘以標(biāo)量alpha
,然后加到張量input
的每個元素上。 返回結(jié)果張量。
input
和other
的狀必須是可廣播的。
如果other
的類型為 FloatTensor 或 DoubleTensor,則alpha
必須為實(shí)數(shù),否則應(yīng)為整數(shù)。
Parameters
other
的標(biāo)量乘法器Keyword Arguments
out (Tensor, optional) – the output tensor.
Example:
>>> a = torch.randn(4)
>>> a
tensor([-0.9732, -0.3497, 0.6245, 0.4022])
>>> b = torch.randn(4, 1)
>>> b
tensor([[ 0.3743],
[-1.7724],
[-0.5811],
[-0.8017]])
>>> torch.add(a, 10, b)
tensor([[ 2.7695, 3.3930, 4.3672, 4.1450],
[-18.6971, -18.0736, -17.0994, -17.3216],
[ -6.7845, -6.1610, -5.1868, -5.4090],
[ -8.9902, -8.3667, -7.3925, -7.6147]])
torch.addcdiv(input, value=1, tensor1, tensor2, out=None) → Tensor?
執(zhí)行tensor1
除以tensor2
的元素,將結(jié)果乘以標(biāo)量value
并將其加到input
上。
input
,tensor1
和tensor2
的形狀必須是可廣播。
對于類型為 <cite>FloatTensor</cite> 或 <cite>DoubleTensor</cite> 的輸入,value
必須為實(shí)數(shù),否則為整數(shù)。
Parameters
Example:
>>> t = torch.randn(1, 3)
>>> t1 = torch.randn(3, 1)
>>> t2 = torch.randn(1, 3)
>>> torch.addcdiv(t, 0.1, t1, t2)
tensor([[-0.2312, -3.6496, 0.1312],
[-1.0428, 3.4292, -0.1030],
[-0.5369, -0.9829, 0.0430]])
torch.addcmul(input, value=1, tensor1, tensor2, out=None) → Tensor?
對tensor1
與tensor2
進(jìn)行元素逐項(xiàng)乘法,將結(jié)果與標(biāo)量value
相乘,然后將其與input
相加。
tensor
,tensor1
和tensor2
的形狀必須是可廣播的。
For inputs of type <cite>FloatTensor</cite> or <cite>DoubleTensor</cite>, value
must be a real number, otherwise an integer.
Parameters
Example:
>>> t = torch.randn(1, 3)
>>> t1 = torch.randn(3, 1)
>>> t2 = torch.randn(1, 3)
>>> torch.addcmul(t, 0.1, t1, t2)
tensor([[-0.8635, -0.6391, 1.6174],
[-0.7617, -0.5879, 1.7388],
[-0.8353, -0.6249, 1.6511]])
torch.angle(input, out=None) → Tensor?
計(jì)算給定input張量的元素方向角(以弧度為單位)。
Parameters
Example:
>>> torch.angle(torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j]))*180/3.14159
tensor([ 135., 135, -45])
torch.asin(input, out=None) → Tensor?
返回帶有input
元素的反正弦值的新張量。
Parameters
Example:
>>> a = torch.randn(4)
>>> a
tensor([-0.5962, 1.4985, -0.4396, 1.4525])
>>> torch.asin(a)
tensor([-0.6387, nan, -0.4552, nan])
torch.atan(input, out=None) → Tensor?
返回帶有input元素的反正切的新張量。
Parameters
Example:
>>> a = torch.randn(4)
>>> a
tensor([ 0.2341, 0.2539, -0.6256, -0.6448])
>>> torch.atan(a)
tensor([ 0.2299, 0.2487, -0.5591, -0.5727])
torch.atan2(input, other, out=None) → Tensor?
考慮象限的
元素逐級反正切。 返回一個新的張量,其矢量
與矢量
之間的弧度為符號角。 (請注意,第二個參數(shù)
是 x 坐標(biāo),而第一個參數(shù)
是 y 坐標(biāo)。)
iinput
和other
的形狀必須是可廣播的。
Parameters
Example:
>>> a = torch.randn(4)
>>> a
tensor([ 0.9041, 0.0196, -0.3108, -2.4423])
>>> torch.atan2(a, torch.randn(4))
tensor([ 0.9833, 0.0811, -1.9743, -1.4151])
torch.bitwise_not(input, out=None) → Tensor?
計(jì)算給定輸入張量的按位非。 輸入張量必須是整數(shù)或布爾類型。 對于布爾張量,它計(jì)算邏輯非。
Parameters
Example
>>> torch.bitwise_not(torch.tensor([-1, -2, 3], dtype=torch.int8))
tensor([ 0, 1, -4], dtype=torch.int8)
torch.bitwise_xor(input, other, out=None) → Tensor?
計(jì)算input
和other
的按位 XOR。 輸入張量必須是整數(shù)或布爾類型。 對于布爾張量,它計(jì)算邏輯 XOR。
Parameters
Example
>>> torch.bitwise_xor(torch.tensor([-1, -2, 3], dtype=torch.int8), torch.tensor([1, 0, 3], dtype=torch.int8))
tensor([-2, -2, 0], dtype=torch.int8)
>>> torch.bitwise_xor(torch.tensor([True, True, False]), torch.tensor([False, True, False]))
tensor([ True, False, False])
torch.ceil(input, out=None) → Tensor?
返回帶有input元素的 ceil 的新張量,該元素大于或等于每個元素的最小整數(shù)。
Parameters
Example:
>>> a = torch.randn(4)
>>> a
tensor([-0.6341, -1.4208, -1.0900, 0.5826])
>>> torch.ceil(a)
tensor([-0., -1., -1., 1.])
torch.clamp(input, min, max, out=None) → Tensor?
將input
中的所有元素限制在 <cite>[</cite> min
, max
<cite>]</cite> 范圍內(nèi),并返回結(jié)果張量:
如果input
的類型為 <cite>FloatTensor</cite> 或 <cite>DoubleTensor</cite> ,則參數(shù) min
和 max
必須為實(shí)數(shù),否則為實(shí)數(shù) 應(yīng)該是整數(shù)。
Parameters
Example:
>>> a = torch.randn(4)
>>> a
tensor([-1.7120, 0.1734, -0.0478, -0.0922])
>>> torch.clamp(a, min=-0.5, max=0.5)
tensor([-0.5000, 0.1734, -0.0478, -0.0922])
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
Example:
>>> a = torch.randn(4)
>>> a
tensor([-0.0299, -2.3184, 2.1593, -0.8883])
>>> torch.clamp(a, min=0.5)
tensor([ 0.5000, 0.5000, 2.1593, 0.5000])
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
Example:
>>> a = torch.randn(4)
>>> a
tensor([ 0.7753, -0.4702, -0.4599, 1.1899])
>>> torch.clamp(a, max=0.5)
tensor([ 0.5000, -0.4702, -0.4599, 0.5000])
torch.conj(input, out=None) → Tensor?
計(jì)算給定input張量的逐元素共軛。
Parameters
Example:
>>> torch.conj(torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j]))
tensor([-1 - 1j, -2 - 2j, 3 + 3j])
torch.cos(input, out=None) → Tensor?
返回帶有input元素的余弦的新張量。
Parameters
Example:
>>> a = torch.randn(4)
>>> a
tensor([ 1.4309, 1.2706, -0.8562, 0.9796])
>>> torch.cos(a)
tensor([ 0.1395, 0.2957, 0.6553, 0.5574])
torch.cosh(input, out=None) → Tensor?
返回具有input
元素的雙曲余弦的新張量。
Parameters
更多建議: