日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pytorch torch.device类(表示在其上或将要分配torch.Tensor的设备)

發(fā)布時(shí)間:2025/3/20 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pytorch torch.device类(表示在其上或将要分配torch.Tensor的设备) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

from https://pytorch.org/docs/1.1.0/tensor_attributes.html?highlight=torch%20device#torch.torch.device

CLASS torch.device

A torch.device is an object representing the device on which a torch.Tensor is or will be allocated.
torch.device是一個(gè)對(duì)象,表示在其上或?qū)⒁峙鋞orch.Tensor的設(shè)備。

The torch.device contains a device type (‘cpu’ or ‘cuda’) and optional device ordinal for the device type. If the device ordinal is not present, this represents the current device for the device type; e.g. a torch.Tensor constructed with device ‘cuda’ is equivalent to ‘cuda:X’ where X is the result of torch.cuda.current_device().
torch.device包含設(shè)備類型(“ cpu”或“ cuda”)和該設(shè)備類型的可選設(shè)備序號(hào)。
如果設(shè)備序號(hào)不存在,則代表設(shè)備類型的當(dāng)前設(shè)備;
例如 用設(shè)備’cuda’構(gòu)造的torch.Tensor等效于’cuda:X’,其中X是torch.cuda.current_device()的結(jié)果。

A torch.Tensor’s device can be accessed via the Tensor.device property.
可以通過Tensor.device屬性訪問torch.Tensor的設(shè)備。

A torch.device can be constructed via a string or via a string and device ordinal
可以通過字符串或通過字符串和設(shè)備序號(hào)構(gòu)造torch.device

Via a string:

>>> torch.device('cuda:0') device(type='cuda', index=0)>>> torch.device('cpu') device(type='cpu')>>> torch.device('cuda') # current cuda device device(type='cuda')

Via a string and device ordinal:

>>> torch.device('cuda', 0) device(type='cuda', index=0)>>> torch.device('cpu', 0) device(type='cpu', index=0)

NOTE
The torch.device argument in functions can generally be substituted with a string. This allows for fast prototyping of code.
函數(shù)中的torch.device參數(shù)通常可以用字符串替換。
這樣可以快速編寫代碼原型。

>>> # Example of a function that takes in a torch.device 帶有torch.device的函數(shù)示例 >>> cuda1 = torch.device('cuda:1') >>> torch.randn((2,3), device=cuda1) >>> # You can substitute the torch.device with a string 您可以用字符串替換torch.device >>> torch.randn((2,3), device='cuda:1')

NOTE

For legacy reasons, a device can be constructed via a single device ordinal, which is treated as a cuda device. This matches Tensor.get_device(), which returns an ordinal for cuda tensors and is not supported for cpu tensors. 出于遺留原因,可以通過單個(gè)設(shè)備序號(hào)(被視為cuda設(shè)備)構(gòu)造設(shè)備。 這與Tensor.get_device()匹配,后者為cuda張量返回序數(shù),而cpu張量不支持該序數(shù)。 >>> torch.device(1) device(type='cuda', index=1)

NOTE

Methods which take a device will generally accept a (properly formatted) string or (legacy) integer device ordinal, i.e. the following are all equivalent: 使用設(shè)備的方法通常會(huì)接受(正確格式化的)字符串或(舊式)整數(shù)設(shè)備序數(shù),即以下所有等效方法: >>> torch.randn((2,3), device=torch.device('cuda:1')) >>> torch.randn((2,3), device='cuda:1') >>> torch.randn((2,3), device=1) # legacy 與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的pytorch torch.device类(表示在其上或将要分配torch.Tensor的设备)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。