Multi-layer convolutional LSTM with Pytorch

Overview

Convolution_LSTM_pytorch

Thanks for your attention. I haven't got time to maintain this repo for a long time. I recommend this repo which provides an excellent implementation.

Usage

A multi-layer convolution LSTM module Pytorch implementation of Convolutional LSTM Network: A Machine Learning Approach for Precipitation Nowcasting

clstm = ConvLSTM(input_channels=512, hidden_channels=[128, 64, 64], kernel_size=5, step=9, effective_step=[2, 4, 8])
lstm_outputs = clstm(cnn_features)
hidden_states = lstm_outputs[0]

Thanks

Thanks to @Jackie-Chou and @chencodeX who provide lots of valuable advice. I apology for the inconvenience.

Comments
  • Why Wci,Wcf,Wco should be initialized at the beginning of  each batch?

    Why Wci,Wcf,Wco should be initialized at the beginning of each batch?

    @automan000 Thanks a lot for you work! I am a little confused that in code:

    41 def init_hidden(self, batch_size, hidden, shape): 42 ¦ self.Wci = Variable(torch.zeros(1, hidden, shape[0], shape[1])).cuda() 43 ¦ self.Wcf = Variable(torch.zeros(1, hidden, shape[0], shape[1])).cuda() 44 ¦ self.Wco = Variable(torch.zeros(1, hidden, shape[0], shape[1])).cuda() 45 ¦ return (Variable(torch.zeros(batch_size, hidden, shape[0], shape[1])).cuda(), 46 ¦ ¦ ¦ Variable(torch.zeros(batch_size, hidden, shape[0], shape[1])).cuda())

    Why Wci, Wcf, Wco should be initialized?

    opened by SampsonKwan 6
  • about concat

    about concat

    https://arxiv.org/pdf/1506.04214.pdf in this parper image the LSTM compute i(t),f(t),o(t) is use x(t),h(t-1) and c(t-1). but in your code convolution_lstm.py line 24: combined = torch.cat((input, h), dim=1)

    you just use x and h, why?

    opened by chencodeX 5
  • RuntimeError: Jacobian mismatch for output 0 with respect to input 0

    RuntimeError: Jacobian mismatch for output 0 with respect to input 0

    Default code snippet ran in pytorch 0.40, it hangs a bit then this error shows up.

    Complete Error:

    `RuntimeError Traceback (most recent call last) in () 101 output = convlstm(input) 102 output = output[0][0] --> 103 res = torch.autograd.gradcheck(loss_fn, (output, target), raise_exception=True) 104 print(res)

    C:\Anaconda3\lib\site-packages\torch\autograd\gradcheck.py in gradcheck(func, inputs, eps, atol, rtol, raise_exception) 190 if not ((a - n).abs() <= (atol + rtol * n.abs())).all(): 191 return fail_test('Jacobian mismatch for output %d with respect to input %d,\n' --> 192 'numerical:%s\nanalytical:%s\n' % (i, j, n, a)) 193 194 if not reentrant:

    C:\Anaconda3\lib\site-packages\torch\autograd\gradcheck.py in fail_test(msg) 170 def fail_test(msg): 171 if raise_exception: --> 172 raise RuntimeError(msg) 173 return False 174

    RuntimeError: Jacobian mismatch for output 0 with respect to input 0, numerical:tensor(1.00000e-02 * [[ 0.0000], [ 0.0000], [ 0.0000], ..., [ 0.0000], [ 0.0000], [ 0.0000]]) analytical:tensor([[-3.6467e-05], [ 2.2621e-06], [-3.9878e-05], ..., [-4.3014e-05], [ 1.2278e-05], [ 3.6285e-06]])`

    opened by inkplay 3
  • 关于input 和 step 的问题

    关于input 和 step 的问题

    代码的输入数据定义为: input = Variable(torch.randn(1, 512, 64, 32)).cuda() 参数分别为(batch_size, channels, height, weight) 在输入convlstm网络进行计算时有下面这个for循环: for step in range(self.step): x= input for i in range(self.num_layers): ...

    请问这个循环每次的输入x都是相同的吗?序列体现在哪里呢?有理解不对的地方希望能得到您的指正,谢谢!

    opened by Fzz123 2
  • What's the shape of input?

    What's the shape of input?

    This is a question rather than an issue, sorry for bothering. > + I have questions about input/output shape, as well as the meaning of "input_channel". According to #gradient check part of the code, the input is in the shape of (1, 512, 64, 32), while the output shrinks to (1, 32, 64, 32). I assume for the input, 1 is batch size, 512 is input_channel, and the image is of size 64*32.

    The questions are: What does these channels mean? Are they filters as in Keras convLSTM library(https://keras.io/layers/recurrent/#convlstm2dcell)? How do we input a sequence of 5 images? And why is output channel smaller than the input?

    opened by mikumeow 1
  • about forward

    about forward

    I'm very happy to see that you have modified the previous question, but I suggest that your ConvLSTMCell forward process take the following calculations: ci = torch.sigmoid(self.Wxi(input) + self.Whi(hidden_state) + c * self.Wci) cf = torch.sigmoid(self.Wxf(input) + self.Whf(hidden_state) + c * self.Wcf) new_c = cf * c + ci * torch.tanh(self.Wxc(input) + self.Whc(hidden_state)) co = torch.sigmoid(self.Wxo(input) + self.Who(hidden_state) + new_c * self.Wco) new_h = co * torch.tanh(new_c)

    opened by chencodeX 1
  • Error in backward

    Error in backward

    Thanks for your implementation of conv-lstm. However, there may exist some bugs in the code. I use the code as a part of my project. The convolutional features are extracted from images and are passed to the conv-lstm. Following the conv-lstm are a fully-connected layer and the loss. But the loss.backward() will report an error, which tells that the 'retain_graph' parameter should be true. However, setting retain_graph=True will consume more and more memory and slow down the program.

    opened by xgniu 1
  • using for custom dataset

    using for custom dataset

    Hi, I'm trying to apply your code to a sequential image dataset.

    However, whenever I tried to input concatenated images (Batch x Timeseries x Channels x Width x Height), it gives following error.

    
    
    <ipython-input-40-c130ba752793> in forward(self, input, h, c)
         29         input = input.cuda()
         30 
    ---> 31         combined = torch.cat((input, h), dim=1)
         32 
         33         A = self.conv(combined)
    
    TypeError: cat received an invalid combination of arguments - got (tuple, dim=int), but expected one of:
     * (sequence[torch.cuda.FloatTensor] seq)
     * (sequence[torch.cuda.FloatTensor] seq, int dim)
          didn't match because some of the arguments have invalid types: (tuple, dim=int)
    
    

    It seems like something is wrong with .cuda() declaration.

    So I looked into those two Variables inputand h

    input has torch.FloatTensor type,

    h has torch.autograd.variable.Variable type.

    From this article, those two Variables have to be changed to same data type.

    My question is,

    i) Have you undergone the same issue like this?

    ii) I've tried to change the data type of h by h.cuda() didn't work. I'm not used to Pytorch. So is there any advice I can get?

    BTW thank you for your update. Gave me a lot of help

    opened by KyonP 1
  • Peephole connections (Wci, Wcf, Wco) gradient update

    Peephole connections (Wci, Wcf, Wco) gradient update

    The LSTM paper defines a specific rule for gradient updates of the 'peephole' connections. Specifically:

    [...] during learning no error signals are propagated back from gates via peephole connections to CEC

    Based on my understanding of the code the way these 3 variables are initialized (as asked in Issue 17) is an attempt at implementing this update rule, but I don't see how does initializing them as Variables helps. From my understanding of the quoted part of the LSTM paper, the peephole connections should be updated but the gradient that updates them should stop there and not flow any further. If that is the case then this implementation is incorrect, although it might be that Pytorch does not support such an operation as .detach() is not suitable for the job.

    opened by Pozimek 1
  • Input shape issue and lack of bias.

    Input shape issue and lack of bias.

    The first problem is that in ConvLSTM.forward, the code is using the same x = input in multiple timesteps. I guess the input shape of forward func. shall be changed to

    [sequence, bsize, channel, x, y] 
    

    instead of the original

    [bsize, channel, x, y]
    

    And, x=input line shall be changed to

    x=input[step]
    

    for different steps. I am still studying if it's appropriate to loop layers within loops of timesteps, but after training your current code(with the change I mentioned above), I can get decent outcomes.

    The second problem is that in ConvLSTMCell, there're no biases. For example in

    ci = torch.sigmoid(self.Wxi(x) + self.Whi(h) + c * self.Wci)
    

    While it should be something like

    ci = torch.sigmoid(self.Wxi(x) + self.Whi(h) + c * self.Wci + self.Bci)
    

    But I don't know if such constants would affect the backward phase.

    P.S. I'm myself a beginner so maybe I'm wrong. Please reply :)

    opened by mikumeow 9
Owner
Zijie Zhuang
PhD@Tsinghua University, Machine Learning & Computer Vision
Zijie Zhuang
PyTorch implementation of ShapeConv: Shape-aware Convolutional Layer for RGB-D Indoor Semantic Segmentation.

Shape-aware Convolutional Layer (ShapeConv) PyTorch implementation of ShapeConv: Shape-aware Convolutional Layer for RGB-D Indoor Semantic Segmentatio

Hanchao Leng 82 Dec 29, 2022
Time-series-deep-learning - Developing Deep learning LSTM, BiLSTM models, and NeuralProphet for multi-step time-series forecasting of stock price.

Stock Price Prediction Using Deep Learning Univariate Time Series Predicting stock price using historical data of a company using Neural networks for

Abdultawwab Safarji 7 Nov 27, 2022
MLP-Numpy - A simple modular implementation of Multi Layer Perceptron in pure Numpy.

MLP-Numpy A simple modular implementation of Multi Layer Perceptron in pure Numpy. I used the Iris dataset from scikit-learn library for the experimen

Soroush Omranpour 1 Jan 1, 2022
This repository implements and evaluates convolutional networks on the Möbius strip as toy model instantiations of Coordinate Independent Convolutional Networks.

Orientation independent Möbius CNNs This repository implements and evaluates convolutional networks on the Möbius strip as toy model instantiations of

Maurice Weiler 59 Dec 9, 2022
CoSMA: Convolutional Semi-Regular Mesh Autoencoder. From Paper "Mesh Convolutional Autoencoder for Semi-Regular Meshes of Different Sizes"

Mesh Convolutional Autoencoder for Semi-Regular Meshes of Different Sizes Implementation of CoSMA: Convolutional Semi-Regular Mesh Autoencoder arXiv p

Fraunhofer SCAI 10 Oct 11, 2022
PyTorch implementation of the Quasi-Recurrent Neural Network - up to 16 times faster than NVIDIA's cuDNN LSTM

Quasi-Recurrent Neural Network (QRNN) for PyTorch Updated to support multi-GPU environments via DataParallel - see the the multigpu_dataparallel.py ex

Salesforce 1.3k Dec 28, 2022
A3C LSTM Atari with Pytorch plus A3G design

NEWLY ADDED A3G A NEW GPU/CPU ARCHITECTURE OF A3C FOR SUBSTANTIALLY ACCELERATED TRAINING!! RL A3C Pytorch NEWLY ADDED A3G!! New implementation of A3C

David Griffis 532 Jan 2, 2023
Tree LSTM implementation in PyTorch

Tree-Structured Long Short-Term Memory Networks This is a PyTorch implementation of Tree-LSTM as described in the paper Improved Semantic Representati

Riddhiman Dasgupta 529 Dec 10, 2022
LSTM and QRNN Language Model Toolkit for PyTorch

LSTM and QRNN Language Model Toolkit This repository contains the code used for two Salesforce Research papers: Regularizing and Optimizing LSTM Langu

Salesforce 1.9k Jan 8, 2023
LSTM model trained on a small dataset of 3000 names written in PyTorch

LSTM model trained on a small dataset of 3000 names. Model generates names from model by selecting one out of top 3 letters suggested by model at a time until an EOS (End Of Sentence) character is not encountered.

Sahil Lamba 1 Dec 20, 2021
A PyTorch implementation of Multi-digit Number Recognition from Street View Imagery using Deep Convolutional Neural Networks

SVHNClassifier-PyTorch A PyTorch implementation of Multi-digit Number Recognition from Street View Imagery using Deep Convolutional Neural Networks If

Potter Hsu 182 Jan 3, 2023
Implementation of the Point Transformer layer, in Pytorch

Point Transformer - Pytorch Implementation of the Point Transformer self-attention layer, in Pytorch. The simple circuit above seemed to have allowed

Phil Wang 501 Jan 3, 2023
A PyTorch implementation of Radio Transformer Networks from the paper "An Introduction to Deep Learning for the Physical Layer".

An Introduction to Deep Learning for the Physical Layer An usable PyTorch implementation of the noisy autoencoder infrastructure in the paper "An Intr

Gram.AI 120 Nov 21, 2022
Pytorch implementation of "Forward Thinking: Building and Training Neural Networks One Layer at a Time"

forward-thinking-pytorch Pytorch implementation of Forward Thinking: Building and Training Neural Networks One Layer at a Time Requirements Python 2.7

Kim Heecheol 65 Oct 6, 2022
Pytorch implementation of the paper "Enhancing Content Preservation in Text Style Transfer Using Reverse Attention and Conditional Layer Normalization"

Pytorch implementation of the paper "Enhancing Content Preservation in Text Style Transfer Using Reverse Attention and Conditional Layer Normalization"

Dongkyu Lee 4 Sep 18, 2022
Sharpened cosine similarity torch - A Sharpened Cosine Similarity layer for PyTorch

Sharpened Cosine Similarity A layer implementation for PyTorch Install At your c

Brandon Rohrer 203 Nov 30, 2022
📦 PyTorch based visualization package for generating layer-wise explanations for CNNs.

Explainable CNNs ?? Flexible visualization package for generating layer-wise explanations for CNNs. It is a common notion that a Deep Learning model i

Ashutosh Hathidara 183 Dec 15, 2022
Code of U2Fusion: a unified unsupervised image fusion network for multiple image fusion tasks, including multi-modal, multi-exposure and multi-focus image fusion.

U2Fusion Code of U2Fusion: a unified unsupervised image fusion network for multiple image fusion tasks, including multi-modal (VIS-IR, medical), multi

Han Xu 129 Dec 11, 2022
Using LSTM write Tang poetry

本教程将通过一个示例对LSTM进行介绍。通过搭建训练LSTM网络,我们将训练一个模型来生成唐诗。本文将对该实现进行详尽的解释,并阐明此模型的工作方式和原因。并不需要过多专业知识,但是可能需要新手花一些时间来理解的模型训练的实际情况。为了节省时间,请尽量选择GPU进行训练。

null 56 Dec 15, 2022