【问题标题】:How to convert a CNN LSTM form keras to pytorch如何将 CNN LSTM 形式的 keras 转换为 pytorch
【发布时间】:2021-07-01 09:17:58
【问题描述】:

我正在尝试将用于 keras 的 CNN LSTM 转换为 pytorch,但我遇到了麻烦。

ConvNN_model = models.Sequential()
ConvNN_model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 1)))
ConvNN_model.add(layers.MaxPooling2D((2, 2)))
ConvNN_model.add(layers.Conv2D(64, (3, 3), activation='relu'))
ConvNN_model.add(TimeDistributed(LSTM(128, activation='relu')))
ConvNN_model.add(Dropout(0.2))
ConvNN_model.add(LSTM(128, activation='relu'))
ConvNN_model.add(layers.Dense(64, activation='relu'))
ConvNN_model.add(layers.Dropout(0.25))
ConvNN_model.add(layers.Dense(15, activation='softmax'))

如何将上述代码从 Keras 转换为 Pytorch?

【问题讨论】:

  • 你遇到了什么麻烦?你试过什么?
  • 我试图在课堂上写一个class ConvNetLSTM(nn.Module): 和一个__init__ 和一个foward,但我对pytorch 没有太多经验,我不确定我是否在里面写了正确的网络__init__

标签: keras pytorch conv-neural-network lstm


【解决方案1】:

这是您在 Keras 中的 CNN:

ConvNN_model = models.Sequential()
ConvNN_model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 1)))
ConvNN_model.add(layers.MaxPooling2D((2, 2)))
ConvNN_model.add(layers.Conv2D(64, (3, 3), activation='relu'))
ConvNN_model.add(TimeDistributed(LSTM(128, activation='relu')))
ConvNN_model.add(Dropout(0.2))
ConvNN_model.add(LSTM(128, activation='relu'))
ConvNN_model.add(layers.Dense(64, activation='relu'))
ConvNN_model.add(layers.Dropout(0.25))
ConvNN_model.add(layers.Dense(15, activation='softmax'))

这是 PyTorch 中的等效代码:

class ConvNN_model(nn.Module):
    def __init__(self):
        super(ConvNN_model, self).__init__()
        self.layers = nn.Sequential(
                         nn.Conv2d(1, 32, kernel_size=3),
                         nn.ReLU(),
                         nn.MaxPool2d((2, 2)),
                         nn.Conv2d(32, 64, kernel_size=3),
                         nn.ReLU(),
                         TimeDistributed(nn.LSTM(128, 128)),
                         nn.Dropout(0.2),
                         nn.LSTM(128, 128),
                         nn.ReLU(),
                         nn.Linear(128, 64),
                         nn.ReLU(),
                         nn.Dropout(0.25),
                         nn.Linear(64, 15),
                         nn.Softmax()
                         )
    def forward(self, x):
        return self.layers(x)

请记住,PyTorch 中没有 TimeDistributed 类的等效模块,因此您必须自己构建它。这是您可以使用的一个(来自here):

class TimeDistributed(nn.Module):
    def __init__(self, module, batch_first=False):
        super(TimeDistributed, self).__init__()
        self.module = module
        self.batch_first = batch_first

    def forward(self, x):

        if len(x.size()) <= 2:
            return self.module(x)

        # Squash samples and timesteps into a single axis
        x_reshape = x.contiguous().view(-1, x.size(-1))  # (samples * timesteps, input_size)

        y = self.module(x_reshape)

        # We have to reshape Y
        if self.batch_first:
            y = y.contiguous().view(x.size(0), -1, y.size(-1))  # (samples, timesteps, output_size)
        else:
            y = y.view(-1, x.size(1), y.size(-1))  # (timesteps, samples, output_size)

        return y

给猫剥皮的方法有上百万种;您不必像我一样在nn.Sequential 块中创建整个网络。或者,如果您想坚持顺序方法以与 Keras 保持一致,则无需继承 nn.Module 并完全使用顺序层。

【讨论】:

    猜你喜欢
    • 2019-09-28
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2018-06-29
    • 2020-02-09
    • 1970-01-01
    相关资源
    最近更新 更多