pytorch - 用函数包装 Pytorch 模型的所有参数
问题描述
我希望能够用一个函数包装我的 PyTorch 模型的所有卷积参数(权重和偏差),例如 softmax(权重)、sigmoid(权重)或 alpha*weight 等。有没有办法让这个函数成为使用钩子等的图表?
例如:假设我有一个名为 net 的模型,它具有我感兴趣的可训练参数,仅在卷积层中:
for module in net.modules():
if is_conv_layer(module):
module.weight # This is the weight I am interested in.
因此,替换权重的值很容易:
for module in net.modules():
if is_conv_layer(module):
module.weight.data = sigmoid(module.weight.data)
但是,我还想在图中添加 sigmoid,以便在计算反向传播时也考虑 sigmoid。
解决方案
推荐阅读
- excel - VBA Compile Error: Can't assign to read-only property
- php - Laravel switch case
- django - Django 并发:无法在异步函数中创建用于测试的数据?
- javascript - 如何使用 Nest.js 动态连接到 MongoDB 数据库
- reactjs - 如何检查中间件(thunk)是否正确加载?
- java - 在 Java 中组合 2 个 MongoDB 集合
- java - 如何解决 Apache POI 不支持设置错误?
- php - 在我的 Google Search Console 中添加了不必要的 URL 并出现重定向错误
- python - 如何在使用 asyncio 编写的多客户端聊天应用程序中加密消息
- html - 在固定 div 上滚动无法正常工作