【发布时间】:2020-11-04 19:07:07
【问题描述】:
是否有 tensorflow.keras.layers.Timedistributed for pytorch 的等效实现?
我正在尝试构建类似的东西 时间分布(Resnet50())。
【问题讨论】:
标签: tensorflow pytorch
是否有 tensorflow.keras.layers.Timedistributed for pytorch 的等效实现?
我正在尝试构建类似的东西 时间分布(Resnet50())。
【问题讨论】:
标签: tensorflow pytorch
感谢 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
【讨论】: