convolutionalLSTM(convLSTM)的pytorch版本代码实现
这篇是⾃⼰初学神经⽹络的时候,写着当笔记⽤的,我看有⼈说调⽤不了,⼤家酌情参考吧,我现在也不搞lstm了,细节不太记得了
要是有不同意见可以探讨,懒得和我探讨那就是您对,⿇烦不要上来就喷⼈,谢谢您了
convolutional LSTM(convLSTM)是⼀⽂提出的,⽤于降⽔预测。这⼀⽹络结构,既考虑了输⼊之间的空间关联,也考虑了时序信息,因此,也被⽤于视频分析。心经全文解释
github上已经有了许多个convLSTM的pytorch实现,这⾥选择进⾏调试运⾏。
⽂件中定义了ConvLSTM和ConvLSTMCell两个类,并给出了⼀段调⽤代码。
⽂章⽬录
ConvLSTM
包含__init__和forward两个函数。
__init__:根据输⼊参数定义⼀个多层的convLSTM
def__init__(lf, input_channels, hidden_channels, kernel_size, step=1, effective_step=[1]):
super(ConvLSTM, lf).__init__()
lf.input_channels =[input_channels]+ hidden_channels
lf.hidden_channels = hidden_channels
lf.kernel_size = kernel_size
lf.num_layers =len(hidden_channels)
lf.step = step
lf.effective_step = effective_step
lf._all_layers =[]
for i in range(lf.num_layers):# 定义⼀个多层的convLSTM(即多个convLSTMCell),并存放在_all_layers列表中
name ='cell{}'.format(i)
cell = ConvLSTMCell(lf.input_channels[i], lf.hidden_channels[i], lf.kernel_size)
tattr(lf, name, cell)
lf._all_layers.append(cell)
forward:⼀个多层convLSTM的多时步前向传播
def forward(lf,input):
internal_state =[]
outputs =[]
老君山景区for step in range(lf.step):# 在每⼀个时步进⾏前向运算
善变的谎言x =input
酸辣汤的配料
for i in range(lf.num_layers):# 对多层convLSTM中的每⼀层convLSTMCell,依次进⾏前向运算
# all cells are initialized in the first step
name ='cell{}'.format(i)
if step ==0:# 如果是在第⼀个时步,则需要调⽤init_hidden进⾏convLSTMCell的初始化
bsize, _, height, width = x.size()
(h, c)=getattr(lf, name).init_hidden(batch_size=bsize, hidden=lf.hidden_channels[i],
shape=(height, width))
internal_state.append((h, c))
# do forward
(h, c)= internal_state[i]
x, new_c =getattr(lf, name)(x, h, c)# 调⽤convLSTMCell的forward进⾏前向运算
internal_state[i]=(x, new_c)
# only record effective steps
if step in lf.effective_step:
outputs.append(x)
return outputs,(x, new_c)童年趣事400字作文
ConvLSTMCell
包含__init__、forward和init_hidden三个函数。
__init__:初始化⼀个LSTM单元
def__init__(lf, input_channels, hidden_channels, kernel_size):
super(ConvLSTMCell, lf).__init__()
asrt hidden_channels %2==0读书手抄报简笔画
lf.input_channels = input_channels
lf.hidden_channels = hidden_channels
lf.kernel_size = kernel_size
lf.num_features =4
lf.padding =int((kernel_size -1)/2)
lf.Wxi = nn.Conv2d(lf.input_channels, lf.hidden_channels, lf.kernel_size,1, lf.padding, bias=True)
lf.Whi = nn.Conv2d(lf.hidden_channels, lf.hidden_channels, lf.kernel_size,1, lf.padding, bias=Fal)
lf.Wxf = nn.Conv2d(lf.input_channels, lf.hidden_channels, lf.kernel_size,1, lf.padding, bias=True)
lf.Whf = nn.Conv2d(lf.hidden_channels, lf.hidden_channels, lf.kernel_size,1, lf.padding, bias=Fal)
lf.Wxc = nn.Conv2d(lf.input_channels, lf.hidden_channels, lf.kernel_size,1, lf.padding, bias=True)
lf.Whc = nn.Conv2d(lf.hidden_channels, lf.hidden_channels, lf.kernel_size,1, lf.padding, bias=Fal)
lf.Wxo = nn.Conv2d(lf.input_channels, lf.hidden_channels, lf.kernel_size,1, lf.padding, bias=True)
lf.Who = nn.Conv2d(lf.hidden_channels, lf.hidden_channels, lf.kernel_size,1, lf.padding, bias=Fal)
lf.Wci =None
lf.Wcf =None
2019年高考作文lf.Wco =None
forward:⼀个LSTM单元⾥的前向传播,即convLSTM中最核⼼的5个公式,输出的ch&cc分表代表current hidden_state & current cell_state
def forward(lf, x, h, c):
ci = torch.sigmoid(lf.Wxi(x)+ lf.Whi(h)+ c * lf.Wci)
cf = torch.sigmoid(lf.Wxf(x)+ lf.Whf(h)+ c * lf.Wcf)
航班英文cc = cf * c + ci * torch.tanh(lf.Wxc(x)+ lf.Whc(h))
co = torch.sigmoid(lf.Wxo(x)+ lf.Who(h)+ cc * lf.Wco)
ch = co * torch.tanh(cc)
return ch, cc
init_hidden:convLSTMCell的初始化,返回初始的hidden_state & cell_state
def init_hidden(lf, batch_size, hidden, shape):
if lf.Wci is None:
lf.Wci = s(1, hidden, shape[0], shape[1]))
lf.Wcf = s(1, hidden, shape[0], shape[1]))
lf.Wco = s(1, hidden, shape[0], shape[1]))
el:
asrt shape[0]== lf.Wci.size()[2],'Input Height Mismatched!'
asrt shape[1]== lf.Wci.size()[3],'Input Width Mismatched!'
return(s(batch_size, hidden, shape[0], shape[1])),
s(batch_size, hidden, shape[0], shape[1])))
调⽤
if __name__ =='__main__':
# gradient check
# 定义⼀个5层的convLSTM
convlstm = ConvLSTM(input_channels=512, hidden_channels=[128,64,64,32,32], kernel_size=3, step=5, effective_step=[4])
loss_fn = MSELoss()
input= Variable(torch.randn(1,512,64,32))
target = Variable(torch.randn(1,32,64,32)).double()
output = convlstm(input)
output = output[0][0].double()
res = adcheck(loss_fn,(output, target), eps=1e-6, rai_exception=True)
print(res)
如果需要在其他py⽂件中调⽤此模块,直接导⼊即可