Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,3 @@ This repository contains code for a method for clustering multivariate time seri
The use of the method is not restricted to clinical data. It can generally be used for multivariate time series data.

In addition to variational autoencoders with gaussian mixture priors, the code allows to train ordinary variational autoencoders (multivariate gaussian prior) and ordinary autoencoders (without prior), for all available time series models (LSTM, GRUs and Transformers).

The code was written using

(1) Python 3.6 and Tensorflow 1.10.1 (directory tensorflow1), and

(2) Python 3.8 and Tensorflow 2.4.0 (directory tensorflow2).

Note that only the Tensorflow 2.4.0 version gives the option for training transformer networks in addition to LSTMs/GRUs.
Empty file added VaDER/__init__.py
Empty file.
Binary file added VaDER/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file added VaDER/__pycache__/layers.cpython-39.pyc
Binary file not shown.
Binary file added VaDER/__pycache__/utils.cpython-39.pyc
Binary file not shown.
Binary file added VaDER/__pycache__/vader.cpython-39.pyc
Binary file not shown.
Binary file added VaDER/__pycache__/vadermodel.cpython-39.pyc
Binary file not shown.
14 changes: 10 additions & 4 deletions tensorflow2/vader/layers.py → VaDER/layers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import tensorflow as tf
import numpy as np
from utils import positional_encoding, scaled_dot_product_attention
from scipy.optimize import linear_sum_assignment as linear_assignment
from sklearn import metrics
from scipy.stats import multivariate_normal
import warnings
from sklearn.mixture import GaussianMixture
import tensorflow_addons as tfa
import abc

from VaDER.utils import get_angles, positional_encoding, create_padding_mask, create_look_ahead_mask, \
scaled_dot_product_attention, create_masks

class ImputationLayer(tf.keras.layers.Layer):
def __init__(self, A_init):
Expand Down Expand Up @@ -112,7 +121,6 @@ def call(self, x, training, mask):

return out2


class TransformerDecoderLayer(tf.keras.layers.Layer):
def __init__(self, d_model, num_heads, dff, rate=0.1):
super(TransformerDecoderLayer, self).__init__()
Expand Down Expand Up @@ -146,7 +154,6 @@ def call(self, x, enc_output, training,

return out3, attn_weights_block1, attn_weights_block2


class TransformerEncoder(tf.keras.layers.Layer):
def __init__(self, num_layers, D, d_model, num_heads, dff, maximum_position_encoding, rate=0.1):
super(TransformerEncoder, self).__init__()
Expand Down Expand Up @@ -174,7 +181,6 @@ def call(self, x, training, mask):

return x


class TransformerDecoder(tf.keras.layers.Layer):
def __init__(self, num_layers, D, d_model, num_heads, dff, maximum_position_encoding, rate=0.1):
super(TransformerDecoder, self).__init__()
Expand Down
8 changes: 7 additions & 1 deletion tensorflow2/vader/utils.py → VaDER/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import tensorflow as tf
import numpy as np
from scipy.optimize import linear_sum_assignment as linear_assignment
from sklearn import metrics
from scipy.stats import multivariate_normal
import warnings
from sklearn.mixture import GaussianMixture
import tensorflow_addons as tfa
import abc

def get_angles(pos, i, d_model):
angle_rates = 1 / np.power(10000, (2 * (i//2)) / np.float32(d_model))
Expand Down Expand Up @@ -64,7 +71,6 @@ def scaled_dot_product_attention(q, k, v, mask):

return output, attention_weights


def create_masks(inp):
# does not matter which i we take for inp[:,:,i], so just take i=0
tar = inp[:, :-1, 0]
Expand Down
7 changes: 5 additions & 2 deletions tensorflow2/vader/vader.py → VaDER/vader.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import tensorflow as tf
import numpy as np
from scipy.optimize import linear_sum_assignment as linear_assignment
from sklearn import metrics
from scipy.stats import multivariate_normal
import numpy as np
import warnings
from sklearn.mixture import GaussianMixture
from vadermodel import VaderRNN, VaderFFN, VaderTransformer
import tensorflow_addons as tfa
import abc

from VaDER.vadermodel import VaderModel, VaderRNN, VaderFFN, VaderTransformer

class VADER:
'''
Expand Down
16 changes: 13 additions & 3 deletions tensorflow2/vader/vadermodel.py → VaDER/vadermodel.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import tensorflow as tf
import numpy as np
from scipy.optimize import linear_sum_assignment as linear_assignment
from sklearn import metrics
from scipy.stats import multivariate_normal
import warnings
from sklearn.mixture import GaussianMixture
import tensorflow_addons as tfa
import abc
import numpy as np
from layers import ImputationLayer, RnnDecodeTransformLayer, GmmLayer, TransformerEncoder, TransformerDecoder
from utils import create_masks

from VaDER.layers import ImputationLayer, RnnDecodeTransformLayer, GmmLayer, MultiHeadAttention, \
point_wise_feed_forward_network, TransformerEncoderLayer, TransformerDecoderLayer, \
TransformerEncoder, TransformerDecoder
from VaDER.utils import get_angles, positional_encoding, create_padding_mask, create_look_ahead_mask, \
scaled_dot_product_attention, create_masks


class VaderModel(tf.keras.Model):
def __init__(self, X, W, D, K, I, cell_type, n_hidden, recurrent, output_activation, cell_params=None):
Expand Down
8 changes: 3 additions & 5 deletions tensorflow2/test_vader.py → main.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import numpy as np
import time
import os
import sys
import cProfile
import tensorflow as tf
tf.config.run_functions_eagerly(False)
from tensorflow2.vader.vader import VADER
# from tensorflow2.prog import Transformer
from VaDER.vader import VADER

save_path = os.path.join('test_vader', 'vader.ckpt')

np.random.seed(123)

# generating some simple random data [ns * 2 samples, nt - 1 time points, 2 variables]
nt = int(8)
ns = int(5e2 )
ns = int(5e2)
sigma = 0.5
mu1 = -2
mu2 = 2
Expand Down Expand Up @@ -60,6 +57,7 @@
# "cell_params". These are hyperparameters to the transformer architecture, and are interpreted as in
# https://www.tensorflow.org/tutorials/text/transformer
# The use of dropout can be debated, due to the regularizing properties of the variational layer
# Also note that a Transformer is not a great model choice for this data...
vader = VADER(X_train=X_train, W_train=W_train, y_train=y_train, save_path=save_path, n_hidden=[12, 2], k=4,
learning_rate=1e-3, output_activation=None, recurrent=True, cell_type="Transformer", batch_size=64,
cell_params={'d_model': 4, 'num_layers': 1, 'num_heads': 1, 'dff': 16, 'rate': 0.0})
Expand Down
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
numpy==1.21.0
scikit_learn==1.0.2
scipy==1.7.3
setuptools==57.0.0
tensorflow==2.7.1
tensorflow_addons==0.16.1
23 changes: 23 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from setuptools import setup

setup(
name='VaDER',
version='0.1.0',
author='Johann de Jong',
author_email='johanndejong@gmail.com',
packages=['VaDER'],
# package_dir={'': '.'},
# scripts=['bin/script1', 'bin/script2'],
url='',
license='LICENSE',
description='Clustering multivariate time series with missing values',
long_description=open('README.md').read(),
install_requires=[
"numpy >= 1.19.5",
"scikit_learn >= 1.0.2",
"scipy >= 1.7.3",
"setuptools >= 57.0.0",
"tensorflow >= 2.7.1",
"tensorflow_addons >= 0.16.1"
]
)
4 changes: 0 additions & 4 deletions tensorflow1/requirements.txt

This file was deleted.

92 changes: 0 additions & 92 deletions tensorflow1/test_vader.py

This file was deleted.

6 changes: 0 additions & 6 deletions tensorflow1/vader/__init__.py

This file was deleted.

Loading