【发布时间】:2020-03-21 05:48:08
【问题描述】:
我正在尝试仅实现卷积 LSTM 的一个单元并在其中传递一个张量 (1,3,128,128)。我收到尺寸不匹配错误。
class ConvLSTMCell(nn.Module):
def __init__(self, input_size, input_dim, hidden_dim, kernel_size, bias):
"""
Parameters
----------
input_size: (int, int)
Height and width of input tensor as (height, width).
input_dim: int
Number of channels of input tensor.
hidden_dim: int
Number of channels of hidden state.
kernel_size: (int, int)
Size of the convolutional kernel.
bias: bool
Whether or not to add the bias.
"""
super(ConvLSTMCell, self).__init__()
self.height, self.width = input_size
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.kernel_size = kernel_size
# self.padding = kernel_size[0] // 2, kernel_size[1] // 2
self.bias = bias
self.conv = nn.Conv2d(in_channels=self.input_dim + self.hidden_dim,
out_channels=4 * self.hidden_dim,
kernel_size=self.kernel_size,
#padding=self.padding,
bias=self.bias)
def forward(self, input, prev_state):
h_prev, c_prev = prev_state
print('x: {}\nh_prev: {}\nc_prev: {}'.format(x.size(), h_prev.size(), c_prev.size()))
combined = torch.cat((input, h_prev), dim=1) # concatenate along channel axis
print('combined: {}'.format(combined.size()))
combined_conv = self.conv(combined)
print('combined_conv: {}'.format(combined_conv.size()))
cc_i, cc_f, cc_o, cc_g = torch.split(combined_conv, self.hidden_dim, dim=1)
print('cc_i: {}\ncc_f: {}\ncc_o: {}\ncc_g: {}'.format(cc_i.size(), cc_f.size(), cc_o.size(), cc_g.size()))
i = torch.sigmoid(cc_i)
f = torch.sigmoid(cc_f)
o = torch.sigmoid(cc_o)
g = torch.tanh(cc_g)
print('i: {}\nf: {}\no: {}\ng: {}'.format(i.size(), f.size(), o.size(), g.size()))
c_cur = f * c_prev + i * g
h_cur = o * F.tanh(c_cur)
print('c_cur: {}\nh_cur: {}'.format(c_cur.size(), h_cur.size()))
return h_cur, c_cur
def init_hidden(self, batch_size):
return (Variable(torch.zeros(batch_size, self.hidden_dim, self.height, self.width)),
Variable(torch.zeros(batch_size, self.hidden_dim, self.height, self.width)))
x = torch.randn(1,3,128,128)
model = ConvLSTMCell(input_size=(128,128), input_dim=3, hidden_dim=3, kernel_size=(5,5),
bias=True)
hc = model.init_hidden(batch_size=1)
if gpu:
x.cuda()
model.cuda()
hc.cuda()
out = model(x, hc)
print(out.size())
我收到以下错误:
x:torch.Size([1, 3, 128, 128])
h_prev: torch.Size([1, 3, 128, 128])
c_prev: torch.Size([1, 3, 128, 128])
组合:torch.Size([1, 6, 128, 128])
combined_conv: torch.Size([1, 12, 124, 124])
cc_i:torch.Size([1, 3, 124, 124])
cc_f: torch.Size([1, 3, 124, 124])
cc_o:torch.Size([1, 3, 124, 124])
cc_g: torch.Size([1, 3, 124, 124])
i:torch.Size([1, 3, 124, 124])
f:torch.Size([1, 3, 124, 124])
o:torch.Size([1, 3, 124, 124])
g:torch.Size([1, 3, 124, 124])
Traceback(最近一次调用最后一次):
文件“trial.py”,第 87 行,在
out = 模型(x, hc)
文件“/Users/abcde/opt/anaconda3/envs/matrix/lib/python3.7/site-packages/torch/nn/modules/module.py”,第 541 行,在 call
result = self.forward(*input, **kwargs)
文件“trial.py”,第 66 行,向前
c_cur = f * c_prev + i * g
RuntimeError: 张量 a (124) 的大小必须匹配张量 b (128) 在非单例>维度 3 的大小
我希望使用它构建一个由 17 个单元组成的网络,并且我想使用每个单元的输出来计算相对于基本事实的损失。基本事实是 18 (3,128,128) 张图像。
如何让我的网络输出相同大小的隐藏状态?
【问题讨论】:
标签: conv-neural-network pytorch convolution