【问题标题】:tensorflow's Timedistributed equivalent in pyTorchpyTorch 中 tensorflow 的时间分布等价物
【发布时间】:2020-11-04 19:07:07
【问题描述】:

是否有 tensorflow.keras.layers.Timedistributed for pytorch 的等效实现?

我正在尝试构建类似的东西 时间分布(Resnet50())。

【问题讨论】:

    标签: tensorflow pytorch


    【解决方案1】:

    感谢 this topic 上的 miguelvr。

    您可以使用此代码,它是一个 PyTorch 模块,用于模仿 Timeditributed 包装器。

    import torch.nn as nn
    
    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
    

    【讨论】:

      猜你喜欢
      • 2019-11-11
      • 2022-08-21
      • 2022-06-22
      • 2020-03-26
      • 2022-01-19
      • 2020-01-06
      • 2022-11-28
      • 2020-12-28
      • 1970-01-01
      相关资源
      最近更新 更多