import torch
from torch import nn
from torch.nn import functional as F
net = nn.Sequential(nn.Linear(20,256),nn.ReLU(),nn.Linear(256,10))
X = torch.rand(2,20)
net(X)classMLP(nn.module):def__init__(self):super().__init__()#父类初始化函数
self.hidden = nn.Linear(20,256)
self.out = nn.Linear(256,10)defforward(self,X):return self.out(F.ReLU(self.hidden(X)))
net = MLP()
net(X)
clas MySequential(nn.Module):def__init__(self,*args):super().__init__()for block in args:
self._modules[block]= block
defforward(self,X):for block in self._modules.values():
X = block(X)return X
net = MySequential(nn.Linear(20,256),nn.ReLU(),nn.Linear(256,10))
net(X)classFixedHiddenMLP(nn.Module):def__init__(self):super().__init__()
self.rand_weight = torch.rand((20,20), requires_grad=False)
self.linear = nn.Linear(20,20)defforward(self,X):
X = self.linear(X)
X = F.relu(torch.mm(X,self.rand_weight)+1)
X = self.linear(X)while X.abs().sum()>1:
X /=2return X.sum()
net = FixedHiddenMLP()
net(X)
读写文件
import torch
from torch import nn
from torch.nn import function as F
x = torch.arange(4)
torch.save(x,'x-file')#----------------
y = torch.zeros(4)
torch.save([x,y],'x-files')#-------------------
mydict ={'x'= x,'y'= y }
torch.save(mydict,'mydict')
x2 = torch.load('x-file')
x2
#----存储模型的参数-------classMLP(nn.Module):def__init__(self):super().__init__()
self.hidden = nn.Linear(20,256)
self.output = nn.Linear(256,10)defforward(self,x):return self.output(F.relu(self.hidden(x)))
net = MLP()
X = torch.randn(size=(2,20))
Y = net(X)
torch.save(net.state_dict(),'mlp.params')#实例化原始多层感知机模型的一个备份,直接读取文件中存储的参数
clone = MLP()
clone.load_state_dict(torch.load('mlp.params'))
clone.eval()
Y_clone = clone(X)
Y_clone == Y
关于GPU
!nvidia-smi
import torch
from torch import nn
torch.device('cpu'),torch.cuda.device('cuda'),torch.cuda.device('cuda:1')#--------查看可用gpu的数量--------------------
torch.cuda.device_count()#-----允许我们在请求的GPU不存在的情况下运行代码---------deftry_gpu(i=0):'''如果存在,则返回gpu(i),否则返回cpu()'''if torch.cuda.device_count >= i+1:return torch.device(f'cuda:{i}')return torch.device('cpu')deftry_all_gpus():'''返回所有可用的GPU,如果没有GPU,则返回[cpu(),]'''
devices =[
torch.device(f'cuda:{i}')for i inrange(torch.cuda.device_count())return device if devices else[torch.device('cpu')]
try_gpu(),try_gpu(10),try_all_gpus()
查询张量所在的设备
x = torch.tensor([1,2,3])
x.device
存储在GPU上,在同一个GPU上进行发操作
x = torch.ones(2,3,device = try_gpu())
z = x.cuda(1)
net = nn.Sequential(nn.Linear(3,1))
net = net.to(device=try_gpu())
net(x)#------确认模型参数存储在同一个GPU上-------
net[0].weight.data.device