enumerate用法详解、with torch.no_grad():用法详解、torch.max用法详解、torch.cuda的一些指令、torch.device()的简单用法

目录

1.enumerate用法详解:enumerate

2.with torch.no_grad():用法详解:with torch.no_grad():

3.torch.max用法详解:torch.max

4.torch.cuda的一些指令:torch.cuda指令

5.torch.device()的简单用法:torch.device()

5.1指定设备

5.1.1使用的具体设备

5.1.2使用GPU,但没有具体设备

5.2将构建的张量或者模型分配到相应的设备上


1.enumerate用法详解:enumerate

总结:一般用于列表,既遍历索引又遍历元素。

2.with torch.no_grad():用法详解:with torch.no_grad():

总结:在该模块下,所有计算得出的tensor的requires_grad都自动设置为False。

3.torch.max用法详解:torch.max

总结:按维度dim返回最大值以及索引。

4.torch.cuda的一些指令:torch.cuda指令

5.torch.device()的简单用法:torch.device()

总结:

5.1指定设备

5.1.1使用的具体设备

device = torch.device('cuda', 0)
device = torch.device('cuda:0')

没有显式指定设备序号的话则使用

device = torch.device('cuda', torch.cuda.current_device())

5.1.2使用GPU,但没有具体设备

device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 

5.2将构建的张量或者模型分配到相应的设备上

data = data.to(device)
model = Model(...).to(device)        # 这里Model是一个类

你可能感兴趣的:(#,Pytorch,深度学习)