分類目錄:《深入淺出Pytorch函數(shù)》總目錄
相關(guān)文章:
· 深入淺出TensorFlow2函數(shù)——tf.constant
· 深入淺出Pytorch函數(shù)——torch.tensor
· 深入淺出Pytorch函數(shù)——torch.as_tensor
· 深入淺出Pytorch函數(shù)——torch.Tensor
· 深入淺出PaddlePaddle函數(shù)——paddle.to_tensor文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-443317.html
基于data
構(gòu)建一個(gè)沒有梯度歷史的張量(葉張量)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-443317.html
語(yǔ)法
torch.tensor(data, *, dtype=None, device=None, requires_grad=False, pin_memory=False)
參數(shù)
-
data
:張量的初始數(shù)據(jù)。可以是列表、元組、numpy.ndarray
、標(biāo)量及其他類型 -
dtype
:[torch.dtype
, 可選] 返回所需張量的數(shù)據(jù)類型。如果為None
,則推斷數(shù)據(jù)類型數(shù)據(jù) -
device
:[torch.device
, 可選] 構(gòu)造張量的設(shè)備。如果為None
且數(shù)據(jù)是張量,則使用數(shù)據(jù)的設(shè)備。如果為None
且數(shù)據(jù)不是張量,則結(jié)果張量在CPU上構(gòu)建。 -
requires_grad
:[bool
, 可選] 標(biāo)識(shí)自動(dòng)微分是否記錄返回張量的操作,默認(rèn)為False
-
pin_memory
:[bool
, 可選] 如果設(shè)置,返回的張量將被分配到固定內(nèi)存中。僅適用于CPU張量。默認(rèn)為False
實(shí)例
>>> 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 double tensor on a CUDA device
tensor([[ 0.1111, 0.2222, 0.3333]], dtype=torch.float64, device='cuda:0')
>>> torch.tensor(3.14159) # Create a zero-dimensional (scalar) tensor
tensor(3.1416)
>>> torch.tensor([]) # Create an empty tensor (of size (0,))
tensor([])
到了這里,關(guān)于深入淺出Pytorch函數(shù)——torch.tensor的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!