• Stars
    star
    417
  • Rank 103,829 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created about 5 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Library of autoencoders for sequential data

sequitur

sequitur is a library that lets you create and train an autoencoder for sequential data in just two lines of code. It implements three different autoencoder architectures in PyTorch, and a predefined training loop. sequitur is ideal for working with sequential data ranging from single and multivariate time series to videos, and is geared for those who want to get started quickly with autoencoders.

import torch
from sequitur.models import LINEAR_AE
from sequitur import quick_train

train_seqs = [torch.randn(4) for _ in range(100)] # 100 sequences of length 4
encoder, decoder, _, _ = quick_train(LINEAR_AE, train_seqs, encoding_dim=2, denoise=True)

encoder(torch.randn(4)) # => torch.tensor([0.19, 0.84])

Each autoencoder learns to represent input sequences as lower-dimensional, fixed-size vectors. This can be useful for finding patterns among sequences, clustering sequences, or converting sequences into inputs for other algorithms.

Installation

Requires Python 3.X and PyTorch 1.2.X

You can install sequitur with pip:

$ pip install sequitur

Getting Started

1. Prepare your data

First, you need to prepare a set of example sequences to train an autoencoder on. This training set should be a list of torch.Tensors, where each tensor has shape [num_elements, *num_features]. So, if each example in your training set is a sequence of 10 5x5 matrices, then each example would be a tensor with shape [10, 5, 5].

2. Choose an autoencoder

Next, you need to choose an autoencoder model. If you're working with sequences of numbers (e.g. time series) or 1D vectors (e.g. word vectors), then you should use the LINEAR_AE or LSTM_AE model. For sequences of 2D matrices (e.g. videos) or 3D matrices (e.g. fMRI scans), you'll want to use CONV_LSTM_AE. Each model is a PyTorch module, and can be imported like so:

from sequitur.models import CONV_LSTM_AE

More details about each model are in the "Models" section below.

3. Train the autoencoder

From here, you can either initialize the model yourself and write your own training loop, or import the quick_train function and plug in the model, training set, and desired encoding size, like so:

import torch
from sequitur.models import CONV_LSTM_AE
from sequitur import quick_train

train_set = [torch.randn(10, 5, 5) for _ in range(100)]
encoder, decoder, _, _ = quick_train(CONV_LSTM_AE, train_set, encoding_dim=4)

After training, quick_train returns the encoder and decoder models, which are PyTorch modules that can encode and decode new sequences. These can be used like so:

x = torch.randn(10, 5, 5)
z = encoder(x) # Tensor with shape [4]
x_prime = decoder(z) # Tensor with shape [10, 5, 5]

API

Training your Model

quick_train(model, train_set, encoding_dim, verbose=False, lr=1e-3, epochs=50, denoise=False, **kwargs)

Lets you train an autoencoder with just one line of code. Useful if you don't want to create your own training loop. Training involves learning a vector encoding of each input sequence, reconstructing the original sequence from the encoding, and calculating the loss (mean-squared error) between the reconstructed input and the original input. The autoencoder weights are updated using the Adam optimizer.

Parameters:

  • model (torch.nn.Module): Autoencoder model to train (imported from sequitur.models)
  • train_set (list): List of sequences (each a torch.Tensor) to train the model on; has shape [num_examples, seq_len, *num_features]
  • encoding_dim (int): Desired size of the vector encoding
  • verbose (bool, optional (default=False)): Whether or not to print the loss at each epoch
  • lr (float, optional (default=1e-3)): Learning rate
  • epochs (int, optional (default=50)): Number of epochs to train for
  • **kwargs: Parameters to pass into model when it's instantiated

Returns:

  • encoder (torch.nn.Module): Trained encoder model; takes a sequence (as a tensor) as input and returns an encoding of the sequence as a tensor of shape [encoding_dim]
  • decoder (torch.nn.Module): Trained decoder model; takes an encoding (as a tensor) and returns a decoded sequence
  • encodings (list): List of tensors corresponding to the final vector encodings of each sequence in the training set
  • losses (list): List of average MSE values at each epoch

Models

Every autoencoder inherits from torch.nn.Module and has an encoder attribute and a decoder attribute, both of which also inherit from torch.nn.Module.

Sequences of Numbers

LINEAR_AE(input_dim, encoding_dim, h_dims=[], h_activ=torch.nn.Sigmoid(), out_activ=torch.nn.Tanh())

Consists of fully-connected layers stacked on top of each other. Can only be used if you're dealing with sequences of numbers, not vectors or matrices.

Parameters:

  • input_dim (int): Size of each input sequence
  • encoding_dim (int): Size of the vector encoding
  • h_dims (list, optional (default=[])): List of hidden layer sizes for the encoder
  • h_activ (torch.nn.Module or None, optional (default=torch.nn.Sigmoid())): Activation function to use for hidden layers; if None, no activation function is used
  • out_activ (torch.nn.Module or None, optional (default=torch.nn.Tanh())): Activation function to use for the output layer in the encoder; if None, no activation function is used

Example:

To create the autoencoder shown in the diagram above, use the following arguments:

from sequitur.models import LINEAR_AE

model = LINEAR_AE(
  input_dim=10,
  encoding_dim=4,
  h_dims=[8, 6],
  h_activ=None,
  out_activ=None
)

x = torch.randn(10) # Sequence of 10 numbers
z = model.encoder(x) # z.shape = [4]
x_prime = model.decoder(z) # x_prime.shape = [10]

Sequences of 1D Vectors

LSTM_AE(input_dim, encoding_dim, h_dims=[], h_activ=torch.nn.Sigmoid(), out_activ=torch.nn.Tanh())

Autoencoder for sequences of vectors which consists of stacked LSTMs. Can be trained on sequences of varying length.

Parameters:

  • input_dim (int): Size of each sequence element (vector)
  • encoding_dim (int): Size of the vector encoding
  • h_dims (list, optional (default=[])): List of hidden layer sizes for the encoder
  • h_activ (torch.nn.Module or None, optional (default=torch.nn.Sigmoid())): Activation function to use for hidden layers; if None, no activation function is used
  • out_activ (torch.nn.Module or None, optional (default=torch.nn.Tanh())): Activation function to use for the output layer in the encoder; if None, no activation function is used

Example:

To create the autoencoder shown in the diagram above, use the following arguments:

from sequitur.models import LSTM_AE

model = LSTM_AE(
  input_dim=3,
  encoding_dim=7,
  h_dims=[64],
  h_activ=None,
  out_activ=None
)

x = torch.randn(10, 3) # Sequence of 10 3D vectors
z = model.encoder(x) # z.shape = [7]
x_prime = model.decoder(z, seq_len=10) # x_prime.shape = [10, 3]

Sequences of 2D/3D Matrices

CONV_LSTM_AE(input_dims, encoding_dim, kernel, stride=1, h_conv_channels=[1], h_lstm_channels=[])

Autoencoder for sequences of 2D or 3D matrices/images, loosely based on the CNN-LSTM architecture described in Beyond Short Snippets: Deep Networks for Video Classification. Uses a CNN to create vector encodings of each image in an input sequence, and then an LSTM to create encodings of the sequence of vectors.

Parameters:

  • input_dims (tuple): Shape of each 2D or 3D image in the input sequences
  • encoding_dim (int): Size of the vector encoding
  • kernel (int or tuple): Size of the convolving kernel; use tuple to specify a different size for each dimension
  • stride (int or tuple, optional (default=1)): Stride of the convolution; use tuple to specify a different stride for each dimension
  • h_conv_channels (list, optional (default=[1])): List of hidden channel sizes for the convolutional layers
  • h_lstm_channels (list, optional (default=[])): List of hidden channel sizes for the LSTM layers

Example:

from sequitur.models import CONV_LSTM_AE

model = CONV_LSTM_AE(
  input_dims=(50, 100),
  encoding_dim=16,
  kernel=(5, 8),
  stride=(3, 5),
  h_conv_channels=[4, 8],
  h_lstm_channels=[32, 64]
)

x = torch.randn(22, 50, 100) # Sequence of 22 50x100 images
z = model.encoder(x) # z.shape = [16]
x_prime = model.decoder(z, seq_len=22) # x_prime.shape = [22, 50, 100]

More Repositories

1

rebound

Command-line tool that instantly fetches Stack Overflow results when an exception is thrown
Python
4,094
star
2

adrenaline

Instant answers to any programming question
3,771
star
3

BitVision

Terminal dashboard for trading Bitcoin, predicting price movements, and losing all your money
JavaScript
1,197
star
4

communities

Library of community detection algorithms and visualization tools
Python
714
star
5

stackexplain

Explain your error message with ChatGPT
Python
515
star
6

ChatOverflow

AI-generated answers to every coding question
JavaScript
330
star
7

statcode

Man pages for HTTP status codes
Python
311
star
8

openlimit

Maximize your usage of OpenAI models without hitting rate limits
Python
131
star
9

SmarterReply

Chrome extension for creating custom Smart Replies in Gmail
JavaScript
43
star
10

densify

Data augmentation algorithm for point clouds
Python
19
star
11

git-pull

Parallelized web scraper for Github
Python
17
star
12

CommunityNet

Hierarchical GNN for graph datasets with community structure
Python
13
star
13

saplings

Analyze usage patterns of imported modules in a Python program
Python
12
star
14

SeqConv

Graph convolutional operator that uses a LSTM as a filter
Python
9
star
15

BTC-Mining-Calculator

Simple command-line tool for predicting the amount of Bitcoin your device can mine in the next 24hrs
Python
8
star
16

gnn-dtsp

MATH 490 Final Project: Approximating solutions to the decision variant of the TSP with Graph Neural Networks
HTML
7
star
17

overcast

Desktop app that employs end-to-end encryption with forward secrecy for FB Messenger
JavaScript
7
star
18

DeepFCN

Deep learning tool for predicting individual differences (e.g. diagnostic status, IQ, etc.) from brain networks
Python
7
star
19

TypeSense

Chrome extension that analyzes a Messenger conversation's sentiment in real-time
JavaScript
7
star
20

neuropipe

Easy scaffolding for machine learning pipelines in Scikit-Learn
Python
6
star
21

MatrixConv

PyTorch implementation of a GNN with a CNN filter
Python
6
star
22

mvpa

Multivoxel pattern analysis (MVPA) tool for fMRI data
Python
4
star
23

tabber

Chrome extension for saving (and organizing) interesting FB messages, i.e. Pocket for Messenger.com
JavaScript
4
star
24

topigraph

A simple graph-based topic modeling algorithm
Python
3
star
25

PyReserve

Generate a project template and reserve a name on PyPi with one command
Python
3
star
26

dasher-landing-page

Dasher Software's prelaunch landing page
CSS
2
star
27

Equaliser

Automated unit testing for IEquatable objects
C#
2
star
28

excusabot

Mobile app that auto-notifies your company's Slack channel when you're running late for work (made for the ROSS hackathon)
JavaScript
2
star
29

adrenaline-vscode

2
star
30

outgraph

Outlier detection tool for graph datasets
Python
1
star
31

Mirror

Front-end for a chrome extension built at MHacks VI
CSS
1
star
32

enumerast

Algorithm that enumerates all possible execution paths in a Python AST
1
star
33

Course-Checker

A script that scrapes the status of any given UIUC course
Python
1
star
34

Sediment

Tutorial project that uses linear regression to predict a wine's quality given its chemical properties
Python
1
star
35

personal-site

CSS
1
star
36

test-repository

This is for testing the reindexing process on Adrenaline
1
star
37

University-Infographic

Infographic website demonstrating the growing college tuition bubble
CSS
1
star
38

Overcast-Website

Coming soon page for Overcast, an encrypted messaging app
CSS
1
star
39

coinhopper.io

An FPGA mining interface that dynamically mines cryptocurrencies based on each coin's predicted yield
Python
1
star
40

Vacation-Site

Landing page for a vacation rental located in Sanibel, Florida
JavaScript
1
star
41

ChatOverflow-site

Website for the ChatOverflow browser plugin
JavaScript
1
star