Implementation of Convolutional LSTM in PyTorch.

Overview

ConvLSTM_pytorch

This file contains the implementation of Convolutional LSTM in PyTorch made by me and DavideA.

We started from this implementation and heavily refactored it add added features to match our needs.

Please note that in this repository we implement the following dynamics: CLSTM_dynamics

which is a bit different from the one in the original paper.

How to Use

The ConvLSTM module derives from nn.Module so it can be used as any other PyTorch module.

The ConvLSTM class supports an arbitrary number of layers. In this case, it can be specified the hidden dimension (that is, the number of channels) and the kernel size of each layer. In the case more layers are present but a single value is provided, this is replicated for all the layers. For example, in the following snippet each of the three layers has a different hidden dimension but the same kernel size.

Example usage:

model = ConvLSTM(input_dim=channels,
                 hidden_dim=[64, 64, 128],
                 kernel_size=(3, 3),
                 num_layers=3,
                 batch_first=True
                 bias=True,
                 return_all_layers=False)

TODO (in progress...)

  • Comment code
  • Add docs
  • Add example usage on a toy problem
  • Implement stateful mechanism
  • ...

Disclaimer

This is still a work in progress and is far from being perfect: if you find any bug please don't hesitate to open an issue.

Comments
  • hidden state raises NotImplementedError()

    hidden state raises NotImplementedError()

    Hey, I am trying to integrate your convLSTM cell into an existing model I have. I did this in the following way:

        def forward(self,x): 
            out = my_model(x)
            out = out.unsqueeze(1) #make sure dimensions fit
            out, self.hidden = self.convlstm(out, self.hidden)
            return out[-1].squeeze(1)
    

    self.hidden is none in the first run, but not none in the second, leading to:

        # Implement stateful ConvLSTM
        if hidden_state is not None:
            raise NotImplementedError()
    

    which is in your ConvLSTM module (line 141, 142)

    would this be implemented just by:

        # Implement stateful ConvLSTM
        if hidden_state is not None:
            hidden_state=hidden_state`
    

    or am I misunderstanding something? What is supposed to happen here?

    Thanks for any help :-D

    opened by SvenGroen 8
  • A question

    A question

    I have a question regarding your implementation: As I understood the original convolutional lstm formulation is as follows: Screen Shot 2019-06-12 at 2 24 27 PM

    But in your implementation, u used only one convolution layer. I don't understand how these 2 correspond with each other. because in the formulation, c is only used in the Hadamard product and not in convolutions, but here c and h are both used in convolutions. in fact, all weights are shared for all 4 formulas, although there are 11 weights in the original formula.

    opened by Melika-Ayoughi 7
  • kernel_size = (2, 2) causes dimension issues

    kernel_size = (2, 2) causes dimension issues

    This code snippet taken and changed slightly from the docstring fails for me:

    import torch
    from CONVLSTM_Implementation import ConvLSTM
    x = torch.rand((32, 10, 64, 128, 128))
    convlstm = ConvLSTM(64, 16, (2, 2), 1, True, True, False)
    _, last_states = convlstm(x)
    

    with the following Error:

    c_next = f * c_cur + i * g
    RuntimeError: The size of tensor a (129) must match the size of tensor b (128) at non-singleton dimension 3
    

    But I don't really know why. I guess I will use a different kernel_size for now.

    opened by NicholasKiefer 2
  • Missing Hadamard Products in Forward pass

    Missing Hadamard Products in Forward pass

    First off thank you for the implementation. Are Hadamard products in the paper missing? Please see the image below and paper that I am referring to for clarification.

    Image for equations Paper

    opened by bkoyuncu 2
  • torch split into the four gates

    torch split into the four gates

    Hey I was not sure how the logic in line 49 works

    cc_i, cc_f, cc_o, cc_g = torch.split(combined_conv, self.hidden_dim, dim=1)

    Since each of the 4 gates has operations of weights with inputs, how is the order of split determined? Why not something like cc_g, cc_f, cc_i, cc_go = torch.split(combined_conv, self.hidden_dim, dim=1)? I am a bit confused how the LSTM equations in https://pytorch.org/docs/stable/nn.html#torch.nn.LSTMCell are implemented here.

    Thanks in advance.

    opened by arvindmohan 2
  • split_size_or_sections

    split_size_or_sections

    Sorry, my English is not very good. Why split_size_or_sections is self.hidden_dim? There are only 4 variables to receive the result, I think self.hidden_dim should be changed to 4

    44 cc_i, cc_f, cc_o, cc_g = torch.split(combined_conv, self.hidden_dim, dim=1)

    opened by geoyee 1
  • Better docstring. Image size and device found in forward.

    Better docstring. Image size and device found in forward.

    Mods:

    • Removed Variable calls (old pytorch version)
    • Removed dependency on image size (now inferred by the batch size)
    • Clarified output and docstring
    • Fixed device hardcoded for hidden states
    • Some PEP8 stuff
    opened by arroqc 0
  • "RuntimeError: Jacobian mismatch for output 0 with respect to input 0"

    If you run the example toy data in the script you will get the above error in pytorch 0.40. I am not sure if the eailer version will cause this issue. How can you make sure this is working?

    opened by inkplay 0
  • What is the difference between hidden_state and hidden_dim?

    What is the difference between hidden_state and hidden_dim?

    I saw that in the code, hidden_state is not implemented:

        def forward(self, input_tensor, hidden_state=None):
            """
    
            Parameters
            ----------
            input_tensor: todo
                5-D Tensor either of shape (t, b, c, h, w) or (b, t, c, h, w)
            hidden_state: todo
                None. todo implement stateful
    

    meanwhile, hidden_dim is given. What is the difference between those two variables?

    opened by yustiks 2
  • ReadMe example missing a comma

    ReadMe example missing a comma

    model = ConvLSTM(input_dim=channels, hidden_dim=[64, 64, 128], kernel_size=(3, 3), num_layers=3, batch_first=True[missing comma here] bias=True, return_all_layers=False)

    opened by MattWittbrodt 1
  • output shape probelm

    output shape probelm

    sorry, i didn't really understand conLSTM when i use keras layers ConvLSTM2D(filters = 128, kernel_size=(3, 3), padding='same', return_sequences = False, go_backwards = True,kernel_initializer = 'he_normal' ),such as input shape is (batch,2,h,w,channel),2 i guess is time,and the output is (batch,h,w,128) but in your code ,i didn't get same shape, can you help me to get the shape like keras convLSTM,thx

    opened by ljc1231 1
  • summary[m_key][

    summary[m_key]["input_shape"] = list(input[0].size()) with empty input

    when i build my model with convlstm in the function def forward(self, x): summary[m_key]["input_shape"] = list(input[0].size()) in torchsummary.py the input is empty tuple, so the program error with '{IndexError}tuple index out of range'

    opened by nmvbxcz 3
Owner
Andrea Palazzi
Senior Deep Learning Engineer @ Nomitri - Computer Vision PhD
Andrea Palazzi
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
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
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
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
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
Using LSTM write Tang poetry

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

null 56 Dec 15, 2022
OHLC Average Prediction of Apple Inc. Using LSTM Recurrent Neural Network

Stock Price Prediction of Apple Inc. Using Recurrent Neural Network OHLC Average Prediction of Apple Inc. Using LSTM Recurrent Neural Network Dataset:

Nouroz Rahman 410 Jan 5, 2023
A resource for learning about deep learning techniques from regression to LSTM and Reinforcement Learning using financial data and the fitness functions of algorithmic trading

A tour through tensorflow with financial data I present several models ranging in complexity from simple regression to LSTM and policy networks. The s

null 195 Dec 7, 2022
Using multidimensional LSTM neural networks to create a forecast for Bitcoin price

Multidimensional LSTM BitCoin Time Series Using multidimensional LSTM neural networks to create a forecast for Bitcoin price. For notes around this co

Jakob Aungiers 318 Dec 14, 2022
Incorporating Transformer and LSTM to Kalman Filter with EM algorithm

Deep learning based state estimation: incorporating Transformer and LSTM to Kalman Filter with EM algorithm Overview Kalman Filter requires the true p

zshicode 57 Dec 27, 2022
Forecasting directional movements of stock prices for intraday trading using LSTM and random forest

Forecasting directional movements of stock-prices for intraday trading using LSTM and random-forest https://arxiv.org/abs/2004.10178 Pushpendu Ghosh,

Pushpendu Ghosh 270 Dec 24, 2022
Deep learning based hand gesture recognition using LSTM and MediaPipie.

Hand Gesture Recognition Deep learning based hand gesture recognition using LSTM and MediaPipie. Demo video using PingPong Robot Files Pretrained mode

Brad 24 Nov 11, 2022
Sign Language is detected in realtime using video sequences. Our approach involves MediaPipe Holistic for keypoints extraction and LSTM Model for prediction.

RealTime Sign Language Detection using Action Recognition Approach Real-Time Sign Language is commonly predicted using models whose architecture consi

Rishikesh S 15 Aug 20, 2022
a reccurrent neural netowrk that when trained on a peice of text and fed a starting prompt will write its on 250 character text using LSTM layers

RNN-Playwrite a reccurrent neural netowrk that when trained on a peice of text and fed a starting prompt will write its on 250 character text using LS

Arno Barton 1 Oct 29, 2021
LSTM Neural Networks for Spectroscopic Studies of Type Ia Supernovae

Package Description The difficulties in acquiring spectroscopic data have been a major challenge for supernova surveys. snlstm is developed to provide

null 7 Oct 11, 2022
A Simple LSTM-Based Solution for "Heartbeat Signal Classification and Prediction" in Tianchi

LSTM-Time-Series-Prediction A Simple LSTM-Based Solution for "Heartbeat Signal Classification and Prediction" in Tianchi Contest. The Link of the Cont

KevinCHEN 1 Jun 13, 2022
Using LSTM to detect spoofing attacks in an Air-Ground network

Using LSTM to detect spoofing attacks in an Air-Ground network Specifications IDE: Spider Packages: Tensorflow 2.1.0 Keras NumPy Scikit-learn Matplotl

Tiep M. H. 1 Nov 20, 2021