Git Product home page Git Product logo

rnn-time-series-anomaly-detection's Introduction

RNN-Time-series-Anomaly-Detection

RNN based Time-series Anomaly detector model implemented in Pytorch.

This is an implementation of RNN based time-series anomaly detector, which consists of two-stage strategy of time-series prediction and anomaly score calculation.

Requirements

  • Ubuntu 16.04+ (Errors reported on Windows 10. see issue. Suggesstions are welcomed.)
  • Python 3.5+
  • Pytorch 0.4.0+
  • Numpy
  • Matplotlib
  • Scikit-learn

Dataset

1. NYC taxi passenger count

2. Electrocardiograms (ECGs)

  • The ECG dataset containing a single anomaly corresponding to a pre-ventricular contraction

3. 2D gesture (video surveilance)

  • X Y coordinate of hand gesture in a video

4. Respiration

  • A patients respiration (measured by thorax extension, sampling rate 10Hz)

5. Space shuttle

  • Space Shuttle Marotta Valve time-series

6. Power demand

  • One years power demand at a Dutch research facility

The Time-series 2~6 are provided by E. Keogh et al. in "HOT SAX: Efficiently Finding the Most Unusual Time Series Subsequence." In The Fifth IEEE International Conference on Data Mining. (2005) , dataset

DISCLAIMER:

The labels provided on this repository are unofficial and have not been verified. Labels were unofficially created by non-experts (annotated without any domain knowledge of the dataset or access to out-of-band data that could confirm the labels) and may contain mislabeled points (both false negatives, and false posatives). We referred to other time-series anomaly detection papers using the datasets (Malhotra et al., 2015., Malhotra et al., 2016.) and the author's dataset presentation slides to label anomaly points in this time series as accurately as possible. If you need accurate label information, you should refer to the official dataset description or contact the authors.

RNN-based Multi-Step Prediction Model

0. Architecture

arch

When the value of x_i is known from i=0 to i=t, the model recursively predicts the value of x_i from i=t+1 to i=T. In this figure, t=3, T=8. We first train this model with a trainset which contains no anomalies, then we use the trained model to detect anomalies in a testset, where anomalies are included.

1. How to train this model

Recursive multi-step prediction using RNNs is a rather difficult problem. As the prediction progresses, the prediction errors are accumulated and the predictions rapidly become inaccurate. To solve this problem, we need a model that is robust to input noise.

1steploss

TODO

msteploss

TODO

pfloss

TODO

RNN-based Multi-Step Prediction Model

TODO

Example of usage

0. Download the dataset: Download the five kinds of multivariate time-series dataset (ecg, gesture,power_demand, respiration, space_shuttle), and Label all the abnormality points in the dataset.

    python 0_download_dataset.py

1. Time-series prediction: Train and save RNN based time-series prediction model on a single time-series trainset

    python 1_train_predictor.py --data ecg --filename chfdb_chf14_45590.pkl
    python 1_train_predictor.py --data nyc_taxi --filename nyc_taxi.pkl

Train multiple models using bash script

    ./1_train_predictor_all.sh

2. Anomaly detection: Fit multivariate gaussian distribution and calculate anomaly scores on a single time-series testset

    python 2_anomaly_detection.py --data ecg --filename chfdb_chf14_45590.pkl --prediction_window 10
    python 2_anomaly_detection.py --data nyc_taxi --filename nyc_taxi.pkl --prediction_window 10

Test multiple models using bash script

    ./2_anomaly_detection_all.sh

Result

1. Time-series prediction: Predictions from the stacked RNN model

prediction1

prediction2

2. Anomaly detection:

Anomaly scores from the Multivariate Gaussian Distribution model

equation1

  • NYC taxi passenger count

scores1

  • Electrocardiograms (ECGs) (filename: chfdb_chf14_45590)

scores3

scores4

Evaluation

Model performance was evaluated by comparing the model output with the pre-labeled ground-truth. Note that the labels are only used for model evaluation. The anomaly score threshold was increased from 0 to some maximum value to plot the change of precision, recall, and f1 score. Here we show only the results for the ECG dataset. Execute the code yourself and see more results.

1. Precision, recall, and F1 score:

  • Electrocardiograms (ECGs) (filename: chfdb_chf14_45590)

a. channel 0

f1ecg1

b. channel 1

f1ecg2

Citations

Please consider citing this project in your publications if it helps your research. The following is a BibTeX reference. The BibTeX entry requires the url LaTeX package.

@misc{park2018anomaly,
author = {Park, Jinman},
title = {{RNN based Time-series Anomaly Detector Model Implemented in Pytorch}},
year = {2018},
howpublished = {\url{https://github.com/chickenbestlover/RNN-Time-series-Anomaly-Detection}},
note = {Accessed: [Insert date here]}
}

References

Contact

If you have any questions, please open an issue.

rnn-time-series-anomaly-detection's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rnn-time-series-anomaly-detection's Issues

prediction_window_size and meaning of graph

Hi

just to make sure, prediction_window_size has no meaning right now hasn't it?

and,
can you please clarify the meaning of the blue and green part in the output graphs?

thanks ahead

Batch size and sequence length

Hello,
I am trying to use your code for implementing a project dealing with sensor data that has repeated step functions with roughly fixed length. Your code has greatly save my implementation time. However, using your code, we got high anomaly scores at the beginning or the end of every step functions, due to the difficulty to learn and predict abrupt jumps or downs. I am wondering whether batch size and sequence length will influence the prediction quality? Or do you have suggestion for improving the prediction quality for the step-like time-series data? Thanks.

Question about the code of the model

I see the following lines in the model construction and I wonder if you would be kind enough to comment on them:

if self.res_connection:
            decoded = decoded + input
        if return_hiddens:
            return decoded,hidden,output

        return decoded, hidden

res_connection: Is this basically a skip/residual connection?

return_hiddens: Should this be better renamed to return_outputs? I am guessing this is optionally returning the weighted output of each hidden layer

Also, the decoder is simply a linear layer. From what I see from looking at RNN autoencoder like tutorials, the decoder is usually also a RNN. Could you comment on the architecture?

1_train_predictor --save_fig error

When i use:

!python /content/drive/My\ Drive/PlasmaSimAI/RNN/1_train_predictor.py --data ecg --filename chfdbchf15.pkl --save_fig

in Google colab the following error occurs:

=> Start training from scratch


Namespace(augment=True, batch_size=64, bptt=50, clip=10, data='ecg', device='cuda', dropout=0.2, emsize=32, epochs=50, eval_batch_size=64, filename='chfdbchf15.pkl', log_interval=10, lr=0.0002, model='LSTM', nhid=32, nlayers=2, prediction_window_size=10, pretrained=False, res_connection=False, resume=False, save_fig=True, save_interval=10, seed=1111, teacher_forcing_ratio=0.7, tied=False, weight_decay=0.0001)

| epoch 1 | 10/ 31 batches | ms/batch 133.2217 | loss 2.27
| epoch 1 | 20/ 31 batches | ms/batch 117.8518 | loss 1.95
| epoch 1 | 30/ 31 batches | ms/batch 117.6507 | loss 2.02

| end of epoch 1 | time: 4.70s | valid loss 1.6075 |

Traceback (most recent call last):
File "/content/drive/My Drive/PlasmaSimAI/RNN/1_train_predictor.py", line 326, in
generate_output(args,epoch,model,gen_dataset,startPoint=1500)
File "/content/drive/My Drive/PlasmaSimAI/RNN/1_train_predictor.py", line 141, in generate_output
target= preprocess_data.reconstruct(gen_dataset.cpu().numpy(), TimeseriesData.mean, TimeseriesData.std)
File "/content/drive/My Drive/PlasmaSimAI/RNN/preprocess_data.py", line 18, in reconstruct
return seqData*std+mean
TypeError: mul(): argument 'other' (position 1) must be Tensor, not numpy.ndarray

When you remove --save_fig the code works as expected. Is this a bug or am i doing something wrong?

Running in Docker

I'm trying to run the code on CPU inside a docker container, I believe all my dependencies are in place but I'm receiving the following error.

File "1_train_predictor.py", line 77, in
train_dataset = TimeseriesData.batchify(args,TimeseriesData.trainData, args.batch_size)
File "/home/RNN-Time-series-Anomaly-Detection/preprocess_data.py", line 65, in batchify
batched_data = batched_data.to(device(args.device))
RuntimeError: Cannot initialize CUDA without ATen_cuda library. PyTorch splits its backend into two shared libraries: a CPU library and a CUDA library; this error has occurred because you are trying to use some CUDA functionality, but the CUDA library has not been loaded by the dynamic linker for some reason. The CUDA library MUST be loaded, EVEN IF you don't directly use any symbols from the CUDA library!

This issue appears to be tied into the Pytorch version I am using:
pip3 install http://download.pytorch.org/whl/cpu/torch-0.4.1-cp35-cp35m-linux_x86_64.whl
After rebuilding the Docker container for my desktop system with GPU using libnccl-dev=2.2.13-1+cuda9.0 my error changed:

RuntimeError: cuda runtime error (35) : CUDA driver version is insufficient for CUDA runtime version at /pytorch/aten/src/THC/THCGeneral.cpp:74

Both of these errors appear to be a result of trying to build the Docker container with a Windows or Mac host OS. Nvidia Docker requires a Linux host.

I was able to run the model using p2.xlarge instance on AWS with Deep Learning AMI using a GPU but the same instance failed to run with a CPU flag.

About the result

To chickenbestlover
I have a question about how to get the gragh after the detection ,the result file I got was .pkl, I don't know how to change or use it .

resume problem

when i use resume=True to keep training model, but the parameters don't change, the loss don't go down. How can i figure it out ?

anomaly scores for multivariate analysis

Hi I am trying to use your code for multivariate time series analysis. The anomaly scores calculated from different channels should be the same correct? I thought the anomaly scores aggregate the prediction errors from all the channels. In you ecg example, the anomaly score for the two channels is the same. However, in my own example, it is not the same. As for different channel, they have different anomaly score. Is there any reason?

cuda_functional is required

Hi there

When I tried to follow the example, it seems that the package "cuda_functional" is required. However, this package requires GPU and CUDA.

Is there a way that I can perform your code on CPU only?

Thanks

problem with tensor's view?

I am attempting to run the following:

<python 1_train_predictor.py --data nyc_taxi --filename nyc_taxi.pkl
Here is the traceback:

Traceback (most recent call last): File "1_train_predictor.py", line 322, in <module> train(args,model,train_dataset,epoch) File "1_train_predictor.py", line 230, in train loss1 = criterion(outSeq1.view(args.batch_size,-1), targetSeq.view(args.batch_size,-1)) RuntimeError: invalid argument 2: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Call .contiguous() before .view(). at /Users/soumith/code/builder/wheel/pytorch-src/aten/src/TH/generic/THTensor.cpp:280

Any ideas?

How to use trained model on personal ecg measurements

I was wondering how I would be able to test my own personal set of ecg data against the model. Say that I train the model on the all the ecg datasets that are provided and now I collect my own set of ecg data from a device and I want to tell to see if it can detect an anomaly or not. How would I go about it? Thank you.

DLL problem

Problem description:
Traceback (most recent call last):
File "D:\Python35\R_anomalyDetector.py", line 1, in
from torch.autograd import Variable
File "C:\Users\Dell\AppData\Roaming\Python\Python35\site-packages\torch_init_.py", line 80, in
from torch._C import *
ImportError: DLL load failed: The specified program cannot be found.

What can I do?

UnicodeDecodeError on Windows10

Thank you sincerely for your work. I got a stuck in running the code, which is as follow :

My env is:
win10, anaconda, pytorch=1.1.0 with cuda auto installed by conda

An UnicodeDecodeError happened when I running:
(ad) λ p python 1_train_predictor.py --data ecg --filename chfdb_chf01_275.pkl

The error is:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 93: invalid continuation byte

The detail is:
`> building the model
The using device is : cuda
Traceback (most recent call last):

File "1_train_predictor.py", line 115, in
model = model.to(device(args.device))

File "E:\ProgramSoftW\Anaconda\Installed\envs\ad\lib\site-packages\torch\nn\modules\module.py", line 386, in to
return self._apply(convert)

File "E:\ProgramSoftW\Anaconda\Installed\envs\ad\lib\site-packages\torch\nn\modules\module.py", line 193, in _apply
module._apply(fn)

File "E:\ProgramSoftW\Anaconda\Installed\envs\ad\lib\site-packages\torch\nn\modules\rnn.py", line 127, in _apply self.flatten_parameters()

File "E:\ProgramSoftW\Anaconda\Installed\envs\ad\lib\site-packages\torch\nn\modules\rnn.py", line 100, in flatten_parameters if not any_param.is_cuda or not torch.backends.cudnn.is_acceptable(any_param):

File "E:\ProgramSoftW\Anaconda\Installed\envs\ad\lib\site-packages\torch\backends\cudnn_init_.py", line 105, in is_acceptable
if _libcudnn() is None:

File "E:\ProgramSoftW\Anaconda\Installed\envs\ad\lib\site-packages\torch\backends\cudnn_init_.py", line 48, in _libcudnn
lib = find_cudnn_windows_lib()

File "E:\ProgramSoftW\Anaconda\Installed\envs\ad\lib\site-packages\torch\backends\cudnn_init_.py", line 32, in find_cudnn_windows_lib
out = out.decode().strip()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 93: invalid continuation byte`

The solutions I had try are:

  • I tried to use pytorch=0.4.0 and pytorch=1.1.0 seperately in python 3.5 and 3.6 3.7,but it doesn't work

  • I tried to run it use cpu. It works. But It takes too long.

  • I tried to change the encoding way, encoding='utf-8'. It worked partly

I cann't figure out what's happened in cuda or pytorch with the error. Looking for Ur reply~

loss function

Why don't you let the gradient pass through your modified professor function?

Anomaly detection without labels

The anomaly detector is really awesome!
I want to know, if there is a way to adapt it, to detect anomalies without labeling the data, like the unsupervised learning.
Thanks!

how to generate the plots and gif as you posted in the website?

Dear there,
Thanks for your fantastic work on the time series anomaly detection using RNN techniques.
I have several questions for you:

  1. As the title said, how to generate the plots and gif as you posted in the website?
  2. If the proposed method can used to the multivariate time series cases (with multiple sensor channels).
  3. Whether this implementation can be applied online, which means synchronously detecting anomalies after training the models during robot manipulation task?

Operation does not have an identity.

Running anomaly_detector.py seems to generate the following error. Could someone help?

RuntimeError                              Traceback (most recent call last)
<ipython-input-11-1f043cf1d658> in <module>()
    110                                                          label=TimeseriesData.testLabel.to(args.device))
    111         print('data: ',args.data,' filename: ',args.filename,
--> 112               ' f-beta (no compensation): ', f_beta.max().item(),' beta: ',args.beta)
    113         if args.compensate:
    114             precision, recall, f_beta = get_precision_recall(args, score, num_samples=1000, beta=args.beta,

RuntimeError: operation does not have an identity.

PS: I'm using a custom dataset.

the problem of low precision

I am very appreciated that the authors have contributed the codes.
However, when I test the codes, I can only get about f-Beta=0.3 on these datasets. and I find out that is because the precision is very low, not bigger than 0.5.
And I want to know if there are some tricks in the experiments?

repackage_hidden의 역할이 뭔가요?

1_train_predictor.py 안에 있는 train 함수에서 아래 부분의 코드가 나와있는데

    hidden = model.repackage_hidden(hidden)
    hidden_ = model.repackage_hidden(hidden)

이 부분에서 repackage_hidden의 역할이 뭔가요?
밑에 나와있는 함수인데
제가 실력이 부족해서,
튜플에 담겨있는 h(t),c(t)를
다시 튜플에 담아서 return해주는 이유를 잘 이해 되지 않아서
질문합니다.

def repackage_hidden(self,h):
    """Wraps hidden states in new Variables, to detach them from their history."""
    if type(h) == tuple:
        return tuple(self.repackage_hidden(v) for v in h) # 이중 tuple을 푸는 역할
    else:
        return h.detach()

training loop

In the training loop, I see the following code which I have trouble understanding:

for i in range(inputSeq.size(0)):
    outVal, hidden_, hid = model.forward(outVal, hidden_,return_hiddens=True)
    outVals.append(outVal)

So, first off, the variable i is used in an outer loop, so I am not sure if this has any detrimental or some side effect. I am guessing not but might be a good idea to change the variable names.

Most importantly, I was wondering the loop does not seem to go through the sequence. I was expecting that the model is going to see the sequence one point/token at a time but thatis not what seems to be happening. Could you kindly elaborate on this?

error changes

I think in 2_anomaly_detection.py 135 rows
scores.append(score), targets.append(targets), predicted_scores.append(predicted_score)
should change to
scores.append(score), targets.append(target), predicted_scores.append(predicted_score)

Labels of the datasets

Hello, I was wondering on what basis you had marked the ground truth anomalies. Have you used any thresholds to decide what is an anomaly and what is not or have you simply followed what is explained in the papers you reference? Thanks in advance.

How to use this code for my robot anomaly detection?

After some change, the code has successfully been run on my computer.
Then,.........
If I want use this code for my robot anomaly detection, how should I prepare my data.

I have a basic idea. I want to do the anomaly detection based on multi-sensor data of the joint motors ( ie,. rotate speed, torque, and temperature).
How should I prepare my data according to the code you provide?

Then, I have tried to understand the data of the nyc_taxi.csv data,can you tell me what each column of the table means?
default

paper

안녕하세요!
좋은 코드 감사합니다.
prediction 부분에서 궁금한게 있어서 관련 논문을 읽어보고싶은데
혹시 어떤 paper를 베이스로 코드를 작성하셨는지 여쭤봐도 될까요?

TypeError: invalid file: WindowsPath('save/ecg/checkpoint/chfdb_chf13_45590.pth')

=> saving checkpoint ..
Traceback (most recent call last):
File "train_predictor.py", line 338, in
model.save_checkpoint(model_dictionary, is_best)
File "C:\Users\Dell\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.5\model\model.py", line 103, in save_checkpoint
torch.save(state, checkpoint)
File "C:\Users\Dell\AppData\Roaming\Python\Python35\site-packages\torch\serialization.py", line 209, in save
return _with_file_like(f, "wb", lambda f: _save(obj, f, pickle_module, pickle_protocol))
File "C:\Users\Dell\AppData\Roaming\Python\Python35\site-packages\torch\serialization.py", line 132, in _with_file_like
f = open(f, mode)
TypeError: invalid file: WindowsPath('save/ecg/checkpoint/chfdb_chf13_45590.pth')

C:\Users\Dell\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.5>

2 Layer LSTM

In the article "LSTM-based Encoder-Decoder for Multi-sensor Anomaly Detection" from Malhotra are two LSTM layers (encoder and decoder) mentioned. You refered to that article and you wanted to implement that concept. But I can't see in your RNNPredictor-model that you implement two separate LSTM layer. Do you deviate from that concept intentionally or have I missed something?

[BUG]: Model saving logic is wrong

The code seems to be doing the wrong thing:

is_best = val_loss > best_val_loss
best_val_loss = max(val_loss, best_val_loss)

best_val_loss is initialized to 1.0 and once the validation loss drops below 1.0, any further decrease will not be taken into account due to the use of max. I am not sure about the logic here but perhaps best_val_loss should be set to None or inf initially and min should be used instead of max.

调通代码

请问调通代码需要做哪些准别工作啊

Runtime error running example

Just trying one of the ECG examples and getting a runtime error. I'm running pytorch 1.5.

$ python 1_train_predictor.py --data ecg --filename chfdb_chf01_275.pkl --emsize 128 --nhid 128 --save_fig --epoch 600
=> Start training from scratch
-----------------------------------------------------------------------------------------
Namespace(augment=True, batch_size=64, bptt=50, clip=10, data='ecg', device='cuda', dropout=0.2, emsize=128, epochs=600, eval_batch_size=64, filename='chfdb_chf01_275.pkl', log_interval=10, lr=0.0002, model='LSTM', nhid=128, nlayers=2, prediction_window_size=10, pretrained=False, res_connection=False, resume=False, save_fig=True, save_interval=10, seed=1111, teacher_forcing_ratio=0.7, tied=False, weight_decay=0.0001)
-----------------------------------------------------------------------------------------
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/home/marc/dev/RNN-Time-series-Anomaly-Detection/1_train_predictor.py in <module>
    318 
    319             epoch_start_time = time.time()
--> 320             train(args,model,train_dataset,epoch)
    321             val_loss = evaluate(args,model,test_dataset)
    322             print('-' * 89)

/home/marc/dev/RNN-Time-series-Anomaly-Detection/1_train_predictor.py in train(args, model, train_dataset, epoch)
    226             outSeq1 = torch.cat(outVals,dim=0)
    227             hids1 = torch.cat(hids1,dim=0)
--> 228             loss1 = criterion(outSeq1.view(args.batch_size,-1), targetSeq.view(args.batch_size,-1))
    229 
    230             '''Loss2: Teacher forcing loss'''

RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.

Run time error on Mac

I get the following error running on Mac

[sscmf1jsg8wl /opt/LMCO/git/notebooks/no-git/RNN-Time-series-Anomaly-Detection] python 1_train_predictor.py --data ecg
=> Start training from scratch

Namespace(augment=True, batch_size=64, bptt=50, clip=10, data='ecg', device='cpu', dropout=0.2, emsize=32, epochs=400, eval_batch_size=64, filename='chfdb_chf13_45590.pkl', log_interval=10, lr=0.0002, model='LSTM', nhid=32, nlayers=2, prediction_window_size=10, pretrained=False, res_connection=False, resume=False, save_fig=False, save_interval=10, seed=1111, teacher_forcing_ratio=0.7, tied=False, weight_decay=0.0001)

Traceback (most recent call last):
File "1_train_predictor.py", line 322, in
train(args,model,train_dataset,epoch)
File "1_train_predictor.py", line 228, in train
loss1 = criterion(outSeq1.view(args.batch_size,-1), targetSeq.view(args.batch_size,-1))
RuntimeError: invalid argument 2: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Call .contiguous() before .view(). at /Users/soumith/code/builder/wheel/pytorch-src/aten/src/TH/generic/THTensor.cpp:237

RuntimeError: view size is not compatible with input tensor's size and stride

Thank you for your repo.
When I run the script python 1_train_predictor.py --data nyc_taxi --filename nyc_taxi.pkl .
It raises an Error

Traceback (most recent call last):
  File "1_train_predictor.py", line 320, in <module>
    train(args,model,train_dataset,epoch)
  File "1_train_predictor.py", line 228, in train
    loss1 = criterion(outSeq1.view(args.batch_size,-1), targetSeq.view(args.batch_size,-1))
RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.

Could you please review it. Thank you

N-Predictions

Hi!
I read "Long Short Term Memory Networks for Anomaly Detection in Time Series" article. Their prediction model learns to predict the next 'm' values. so it means each time-step(every point of time series) has 'm' error prediction values and the output would be a t*m sequence, 't' depicts the length of input. am I right? in your code the N-prediction file is a 1-D sequence.what's different? and why?

Bests

What if you have taxi data from multiple states?

What if you have taxi data not just from NYC but also from several different states?
You end up with redundant time stamps and probably similar or different passenger counts. How can I teach this to the model? The model should understand the variation between passenger counts from each state. Therefore the model should be able to capture the anomaly that is specific to the state. Is this possible here? Thanks.

train on normal data?

Hi, thank you for sharing the anomaly detection code. I notice that author Pankaj Malhotra uses only the normal sequences for training in paper LSTM-based Encoder-Decoder for Multi-sensor Anomaly Detection. But I found you use not only normal sequences but anomaly sequences for training.
in code 0_download_dataset.py, you tag label of anomaly data as 1, label of normal data as 0.

tokens.append(1) if 150 < i < 250 or   \
                            5970 < i < 6050 or \
                            8500 < i < 8650 or \
                            8750 < i < 8890 or \
                            10000 < i < 10200 or \
                            14700 < i < 14800 \
                          else tokens.append(0)

train data is labeled_data[:13104], which including normal sequences and anomaly sequences, could you explain that, thank you so much~

Training on Custom Dataset

Hey @chickenbestlover, I actually wanted to use your code on a different dataset that has 7 columns in total. The thing is I have around 100 sequence having around 1500 timestamps each. How do I split this data into training and testing so that I can train on your model. Furthermore, what changed would be required in the model architecture.

PS: I tried concatenating all 100 files and then dividing the total length in 80/20 ratio. I fed this to your model architecture without changing anything and got an error:

RuntimeError                              Traceback (most recent call last)
<ipython-input-8-f9d48947f2f5> in <module>()
    222     for epoch in range(start_epoch, args.epochs+1):
    223       epoch_start_time = time.time()
--> 224       train(args,model,train_dataset,epoch)
    225       val_loss = evaluate(args,model,test_dataset)
    226       print('-' * 89)

5 frames
<ipython-input-8-f9d48947f2f5> in train(args, model, train_dataset, epoch)
    132       hids1 = []
    133       for i in range(inputSeq.size(0)):
--> 134         outVal, hidden_, hid = model.forward(outVal, hidden_,return_hiddens=True)
    135         outVals.append(outVal)
    136         hids1.append(hid)

/content/drive/My Drive/Colab Notebooks/YahooDataset/model/model.py in forward(self, input, hidden, return_hiddens, noise)
     60 
     61     #emb = self.layerNorm1(emb)
---> 62     output, hidden = self.rnn(emb, hidden)
     63     #output = self.layerNorm2(output)
     64 

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--> 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/rnn.py in forward(self, input, hx)
    565             hx = self.permute_hidden(hx, sorted_indices)
    566 
--> 567         self.check_forward_args(input, hx, batch_sizes)
    568         if batch_sizes is None:
    569             result = _VF.lstm(input, hx, self._flat_weights, self.bias, self.num_layers,

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/rnn.py in check_forward_args(self, input, hidden, batch_sizes)
    521 
    522         self.check_hidden_size(hidden[0], expected_hidden_size,
--> 523                                'Expected hidden[0] size {}, got {}')
    524         self.check_hidden_size(hidden[1], expected_hidden_size,
    525                                'Expected hidden[1] size {}, got {}')

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/rnn.py in check_hidden_size(self, hx, expected_hidden_size, msg)
    185         # type: (Tensor, Tuple[int, int, int], str) -> None
    186         if hx.size() != expected_hidden_size:
--> 187             raise RuntimeError(msg.format(expected_hidden_size, tuple(hx.size())))
    188 
    189     def check_forward_args(self, input, hidden, batch_sizes):

RuntimeError: Expected hidden[0] size (2, 1837, 32), got (2, 64, 32)

Help would be appreciated. Thanks

[BUG]: Computing normal stats is wrong

The following code:

organized[t].append(predictions[step+t-args.prediction_window_size][args.prediction_window_size-1-step])

Should this not be simply:

organized[t].append(predictions[step+t-args.prediction_window_size][step])

Otherwise, I think it is reversing the vector.

Also, I think you should use the absolute value for computing the error vector.

How to improve the recall

The predicting results are generally good.
But we can see that the prediction can not fit good on the lowest value.
Can we imporve it?
how many ways that we can do to improve the recall?
can u Give me a little hint? or we can discuss about it?
thanks a lot.

training loss function

Hi
Could you explain the three training loss you defined, free running, teacher forcing, and simplified professor force? or point out related papers? Thanks

Is it unsupervised learning? Why is the label needed?

I can't understand why the label is needed.
default

I think the train data does not contain anomalies and the test data contains anomalies. So, I process my data as follow, the train data is the normal historical data while the test data contains man-made anomalies, but the result is disappointing. What can I do?
1

Isseu running - does not work on Mac

When I run I get the following error - could you help me how to fixe this
It uses default ecg and

python3 1_train_predictor.py --device cpu --data ecg --filename chfdb_chf13_45590.pkl
=> Start training from scratch

Namespace(augment=True, batch_size=64, bptt=50, clip=10, data='ecg', device='cpu', dropout=0.2, emsize=32, epochs=400, eval_batch_size=64, filename='chfdb_chf13_45590.pkl', log_interval=10, lr=0.0002, model='LSTM', nhid=32, nlayers=2, prediction_window_size=10, pretrained=False, res_connection=False, resume=False, save_fig=False, save_interval=10, seed=1111, teacher_forcing_ratio=0.7, tied=False, weight_decay=0.0001)

Traceback (most recent call last):
File "1_train_predictor.py", line 320, in
train(args,model,train_dataset,epoch)
File "1_train_predictor.py", line 228, in train
loss1 = criterion(outSeq1.view(args.batch_size,-1), targetSeq.view(args.batch_size,-1))
RuntimeError: invalid argument 2: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Call .contiguous() before .view(). at /Users/soumith/code/builder/wheel/pytorch-src/aten/src/TH/generic/THTensor.cpp:237

RuntimeError: The expanded size of the tensor (32) must match the existing size (2) at non-singleton dimension 1

The expanded size of the tensor (32) must match the existing size (2) at non-singleton dimension 1

When I tried to run your code via Notebook, this runtime error came.

--> 135 outVal, hidden_, hid = predictor_model.forward(outVal, hidden_,return_hiddens=True)
136 outVals.append(outVal)
137 hids1.append(hid)

in forward(self, input, hidden, return_hiddens, noise)
71 print("*"*50)
72 # decoded = output.view(output.size(0)*output.size(1), output.size(2))
---> 73 decoded = self.decoder(output.view(output.size(0)*output.size(1), output.size(2))) # [(seq_len x batch_size) * feature_size]
74 print(type(decoded))
75 print(decoded.size())

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
--> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/linear.py in forward(self, input)
53
54 def forward(self, input):
---> 55 return F.linear(input, self.weight, self.bias)
56
57 def extra_repr(self):

~/anaconda3/lib/python3.6/site-packages/torch/nn/functional.py in linear(input, weight, bias)
990 if input.dim() == 2 and bias is not None:
991 # fused op is marginally faster
--> 992 return torch.addmm(bias, input, weight.t())
993
994 output = input.matmul(weight.t())

RuntimeError: The expanded size of the tensor (32) must match the existing size (2) at non-singleton dimension 1

RuntimeError when using tie_weights=True

it had RuntimeError when using tie_weights=True. The error was "he expanded size of the tensor (32) must match the existing size (2) at non-singleton dimension 1. Target sizes: [64, 32]. Tensor sizes: [2]".
After checking the code again, i found there was code "self.decoder.weight = self.encoder.weight" in class RNNPredictor() init. decoder.weight was assigned encoder.weight's size. it caused error when using decoder function

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.