Skip to content

Commit

Permalink
fix: remove the plot while testing
Browse files Browse the repository at this point in the history
  • Loading branch information
LongxingTan committed Aug 1, 2024
1 parent 2962509 commit 4fa2278
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 62 deletions.
12 changes: 5 additions & 7 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# TFTS examples

[time series prediction](./run_prediction_simple.py)

## kaggle datasets
- [web traffic prediction]()
- [ventilator]()
- [time series prediction](./run_prediction_simple.py)
- [time series anomaly detection](./run_anomaly.py)


## more time series examples
## More examples

- [time series anomaly detection](./run_anomaly.py)
- [TFTS-Bert](https://github.com/LongxingTan/KDDCup2022-Baidu) wins the **3rd place** in KDD Cup 2022 wind power forecasting
- [TFTS-Seq2seq](https://github.com/LongxingTan/Data-competitions/tree/master/tianchi-enso-prediction) wins the **4th place** in Tianchi ENSO prediction 2021
5 changes: 3 additions & 2 deletions examples/run_anomaly.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ def run_inference(args):

model = AutoModelForAnomaly.from_pretrained(config, args.predict_length, args.output_dir)
det = model.detect(x_test, y_test)
plot(sig, det)
return sig, det


if __name__ == "__main__":
args = parse_args()
run_train(args)
run_inference(args)
sig, det = run_inference(args)
plot(sig, det)
2 changes: 1 addition & 1 deletion examples/run_prediction_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def run_train(args):

pred = trainer.predict(valid[0])
trainer.plot(history=valid[0], true=valid[1], pred=pred)
plt.show()


if __name__ == "__main__":
args = parse_args()
run_train(args)
plt.show()
5 changes: 4 additions & 1 deletion tests/test_examples/test_tfts_inputs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import unittest

import numpy as np
Expand All @@ -6,6 +7,8 @@

from tfts import AutoConfig, AutoModel, KerasTrainer

logger = logging.getLogger(__name__)


class InputsTest(unittest.TestCase):
def setUp(self):
Expand All @@ -21,7 +24,7 @@ def test_encoder_array(self):
y_valid = np.random.rand(1, predict_length, 1)

for m in self.test_models:
print(f"Test model {m}")
logger.info(f"Test model {m}")
config = AutoConfig.for_model(m)
model = AutoModel.from_config(config, predict_length=predict_length)
trainer = KerasTrainer(model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import tensorflow as tf

from tfts.layers.deepar_layer import GaussianLayer
from tfts.layers.auto_task import GaussianHead


class DeepARLayerTest(unittest.TestCase):
def test_gaussian_layer(self):
hidden_size = 32
layer = GaussianLayer(hidden_size)
layer = GaussianHead(hidden_size)

x = tf.random.normal([2, 10, 1])
mu, sig = layer(x)
Expand Down
40 changes: 40 additions & 0 deletions tfts/models/auto_task.py → tfts/layers/auto_task.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Time series task head"""

from typing import Optional, Tuple

import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense, GlobalAveragePooling1D
Expand Down Expand Up @@ -87,3 +89,41 @@ def _mahala_distance(self, x, mean, cov):
d = np.dot(x - mean, np.linalg.inv(cov))
d = np.dot(d, (x - mean).T)
return d


class GaussianHead(tf.keras.layers.Layer):
def __init__(self, units: int):
self.units = units
super().__init__()

def build(self, input_shape: Tuple[Optional[int], ...]):
in_channels = input_shape[2]
self.weight1 = self.add_weight(
name="gauss_w1", shape=(in_channels, self.units), initializer=tf.keras.initializers.GlorotNormal()
)
self.weight2 = self.add_weight(
name="gauss_w2", shape=(in_channels, self.units), initializer=tf.keras.initializers.GlorotNormal()
)
self.bias1 = self.add_weight(name="gauss_b1", shape=(self.units,), initializer=tf.keras.initializers.Zeros())
self.bias2 = self.add_weight(name="gauss_b2", shape=(self.units,), initializer=tf.keras.initializers.Zeros())
super().build(input_shape)

def call(self, x):
"""Returns mean and standard deviation tensors.
Args:
x (tf.Tensor): Input tensor.
Returns:
Tuple[tf.Tensor, tf.Tensor]: Mean and standard deviation tensors.
"""
mu = tf.matmul(x, self.weight1) + self.bias1
sig = tf.matmul(x, self.weight2) + self.bias2
sig_pos = tf.math.log1p(tf.math.exp(sig)) + 1e-7
return mu, sig_pos

def get_config(self):
"""Returns the configuration of the layer."""
config = {"units": self.units}
base_config = super().get_config()
return {**base_config, **config}
45 changes: 0 additions & 45 deletions tfts/layers/deepar_layer.py

This file was deleted.

3 changes: 2 additions & 1 deletion tfts/models/auto_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import pandas as pd
import tensorflow as tf

from tfts.layers.auto_task import AnomalyHead, ClassificationHead, PredictionHead, SegmentationHead

from .auto_config import AutoConfig
from .auto_task import AnomalyHead, ClassificationHead, PredictionHead, SegmentationHead
from .base import BaseConfig, BaseModel

logger = logging.getLogger(__name__)
Expand Down
4 changes: 2 additions & 2 deletions tfts/models/deepar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import tensorflow as tf
from tensorflow.keras.layers import Activation, BatchNormalization, Dense, Dropout

from tfts.layers.deepar_layer import GaussianLayer
from tfts.layers.auto_task import GaussianHead

from .base import BaseConfig, BaseModel

Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(
self.rnn = tf.keras.layers.RNN(cell, return_state=True, return_sequences=True)
self.bn = BatchNormalization()
self.dense = Dense(units=predict_sequence_length, activation="relu")
self.gauss = GaussianLayer(units=1)
self.gauss = GaussianHead(units=1)

def __call__(self, inputs: tf.Tensor, return_dict: Optional[bool] = None):
"""DeepAR
Expand Down
2 changes: 1 addition & 1 deletion tfts/models/rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def __init__(self, predict_sequence_length=3, config=RNNConfig()) -> None:
self.dense2 = Dense(1)

def __call__(self, inputs: tf.Tensor, teacher: Optional[tf.Tensor] = None):
"""_summary_
"""Another RNN model
Parameters
----------
Expand Down

0 comments on commit 4fa2278

Please sign in to comment.