自定义层和读写文件

自定义层

自定义一个没有任何参数的层

python 复制代码
import torch
import torch.nn.functional as F
from torch import nn

class CenteredLayer(nn.Module):
	def __init__(self):
		super().__init__()
	
	def forward(self, X):
		return X - X.mean()


layer = CenteredLayer()
layer(torch.FloatTensor([1, 2, 3, 4, 5]))

将层作为组件和冰岛构建更复杂的模型中

python 复制代码
net = nn.Sequential(nn.Linear(8, 128), CenteredLayer())

Y = net(torch.rand(4m 8))
Y.mean()

带参数的层

python 复制代码
class MyLinear(nn.Module):
	def __init__(self, in_units, units):
		super().__init__()
		self.weight = nn.Parameter(torch.randn(in_units, units))
		self.bias = nn.Parameter(torch.randn(units,))
	
	def forward(self, X):
		linear = torch.matmul(X, self.weight.data) + self.bias.data
		return F.relu(linear)

dense = MyLinear(5, 3)
dense.weight

使用自定义的层执行传播计算

python 复制代码
dense(torch.rand(2, 5))

读写文件

python 复制代码
import torch
from torch import nn
from torch.nn import functional as F

x = torch.arange(4)
torch.save(x, 'x-file')
x2 = torch.load('x-file')
x2 == x

存储一个张量列表

python 复制代码
y = torch.zeros(4)
torch.save([x, y], 'x-files')
x2, y2 = torch.load('x-files')

写入或读取字典

python 复制代码
mydict = {'x': x, 'y': y}
torch.save(mydict, 'mydict')
mydict2 = torch.load('mydict')

加载和保存模型参数

python 复制代码
class MLP(nn.Module):
	def __init__(self):
		super().__init__()
		self.hidden = nn.Linear(20, 256)
		self.output = nn.Linear(256, 10)
	
	def forward(self, x):
		return self.output(F.relu(self.hidden(x)))

net = MLP()
X = torch.randn(size=(2, 20))
Y = net(X)

将模型存储为文件

python 复制代码
torch.save(net.state_dict(), 'mlp.params')

# 保存参数后需要我们自己保存MLP的定义, 需要有定义才能加载
clone = MLP()
clone.load_state_dict(torch.load('mlp.params'))
clone.eval()
相关推荐
wj3055853788 小时前
课程 9:模型测试记录与 Prompt 策略
linux·人工智能·python·comfyui
星寂樱易李8 小时前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
qingfeng154159 小时前
企业微信机器人开发:如何实现自动化与智能运营?
人工智能·python·机器人·自动化·企业微信
初心未改HD9 小时前
深度学习之CNN卷积层详解
人工智能·深度学习·cnn
AI医影跨模态组学10 小时前
EBioMedicine美国佐治亚理工学院与埃默里大学:基于深度学习的放射组学与病理学多模态融合预测HPV相关口咽鳞状细胞癌预后
人工智能·深度学习·论文·医学·医学影像·影像组学
人工智能培训11 小时前
大模型与传统小模型、传统NLP模型的核心差异解析
人工智能·深度学习·神经网络·机器学习·生成对抗网络
彦为君12 小时前
Agent 安全:从权限提示到沙箱隔离
python·ai·ai编程
PILIPALAPENG12 小时前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
Terrence Shen13 小时前
大模型部署工具对比
人工智能·深度学习·计算机视觉