keras - 从 Keras 转换为 Pytorch - conv2d
问题描述
我正在尝试将以下 Keras 代码转换为 PyTorch。
tf.keras.Sequential([
Conv2D(128, 1, activation=tf.nn.relu),
Conv2D(self.channel_n, 1, activation=None),
])
使用 self.channels=16 创建模型摘要时,我得到以下摘要。
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (1, 3, 3, 128) 6272
_________________________________________________________________
conv2d_1 (Conv2D) (1, 3, 3, 16) 2064
=================================================================
Total params: 8,336
Trainable params: 8,336
Non-trainable params: 0
一个人将如何转换?
我已经尝试过这样的:
import torch
from torch import nn
class CellCA(nn.Module):
def __init__(self, channels, dim=128):
super().__init__()
self.net = nn.Sequential(
nn.Conv2d(in_channels=channels,out_channels=dim, kernel_size=1),
nn.ReLU(),
nn.Conv2d(in_channels=dim, out_channels=channels, kernel_size=1),
)
def forward(self, x):
return self.net(x)
但是,我得到 4240 参数
解决方案
如果您正确配置了初始通道(在本例中为 48),则上述尝试是正确的。
推荐阅读
- java - 如何使用 Java 8 流按列表过滤 Map?
- yii2 - Yii2如何在codemix url manager中设置默认语言
- javascript - 使用循环对数组进行索引时出错
- python - 如何使 PyQt5 应用程序响应
- soa - 从 BPEL Process-SOA SUITE 12.2.1.0.0 调用 https REST 服务时的证书问题
- typescript - 无法读取未定义的属性“标题”
- java - 将 App Engine 后端服务从 java 7 运行时迁移到 java 8 运行时
- python-3.x - Multiprocessing vs Concurrent.futures library python(不适用于Google Compute Engine)
- java - Java:找出非 Maven 的依赖版本
- groovy - Lliferay 脚本控制台:“没有这样的属性:类的包:Script1”