Skip to content

Latest commit

 

History

History
272 lines (215 loc) · 8.99 KB

README_CN.md

File metadata and controls

272 lines (215 loc) · 8.99 KB


LICENSE PyPI Version Build Status Lint Status Docs Status Code Coverage Contributing

文档 | 教程 | 发布日志 | English

青山遮不住,毕竟东流去。江晚正愁余,山深闻鹧鸪。
东流TFTS (TensorFlow Time Series) 是一个高效易用的时间序列框架,基于TensorFlow/ Keras。

  • 为多种时间序列任务提供SOTA的深度学习模型,预测、分类、异常检测
  • 提供经典与前沿的深度学习模型,用于工业、科研、竞赛
  • 查阅英文文档,快速入门。欢迎移步时序讨论区

快速使用

Open In Colab Open in Kaggle

安装

  • python >= 3.7
  • tensorflow >= 2.4
pip install tfts

入门使用

import matplotlib.pyplot as plt
import tfts
from tfts import AutoModel, KerasTrainer, Trainer, AutoConfig

train_length = 24
predict_length = 8

# 其中,train是包含(x_train, y_train)的tuple, valid包含(x_valid, y_valid)
train, valid = tfts.get_data('sine', train_length, predict_length, test_size=0.2)
config = AutoConfig.for_model("seq2seq")
model = AutoModel.from_config(config, predict_length)

trainer = KerasTrainer(model)
trainer.train(train, valid)

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

训练自己的数据

为方便使用,将数据转化为三维作为tfts的输入

  • 选项1 np.ndarray
  • 选项2 tf.data.Dataset

编码类模型输入

import numpy as np
from tfts import AutoConfig, AutoModel, KerasTrainer

train_length = 49
predict_length = 10
n_feature = 2

x_train = np.random.rand(1, train_length, n_feature)
y_train = np.random.rand(1, predict_length, 1)
x_valid = np.random.rand(1, train_length, n_feature)
y_valid = np.random.rand(1, predict_length, 1)

config = AutoConfig.for_model("rnn")
model = AutoModel.from_config(config, predict_length=predict_length)
trainer = KerasTrainer(model)
trainer.train(train_dataset=(x_train, y_train), valid_dataset=(x_valid, y_valid), n_epochs=1)

编码-解码类模型输入

# option1
import numpy as np
from tfts import AutoConfig, AutoModel, KerasTrainer

train_length = 49
predict_length = 10
n_encoder_feature = 2
n_decoder_feature = 3

x_train = (
    np.random.rand(1, train_length, 1),
    np.random.rand(1, train_length, n_encoder_feature),
    np.random.rand(1, predict_length, n_decoder_feature),
)
y_train = np.random.rand(1, predict_length, 1)
x_valid = (
    np.random.rand(1, train_length, 1),
    np.random.rand(1, train_length, n_encoder_feature),
    np.random.rand(1, predict_length, n_decoder_feature),
)
y_valid = np.random.rand(1, predict_length, 1)

config = AutoConfig.for_model("seq2seq")
model = AutoModel.from_config(config, predict_length=predict_length)
trainer = KerasTrainer(model)
trainer.train((x_train, y_train), (x_valid, y_valid), n_epochs=1)
# option2
import tensorflow as tf
from tfts import AutoConfig, AutoModel, KerasTrainer

class FakeReader(object):
    def __init__(self, predict_length):
        train_length = 49
        n_encoder_feature = 2
        n_decoder_feature = 3
        self.x = np.random.rand(15, train_length, 1)
        self.encoder_feature = np.random.rand(15, train_length, n_encoder_feature)
        self.decoder_feature = np.random.rand(15, predict_length, n_decoder_feature)
        self.target = np.random.rand(15, predict_length, 1)

    def __len__(self):
        return len(self.x)

    def __getitem__(self, idx):
        return {
            "x": self.x[idx],
            "encoder_feature": self.encoder_feature[idx],
            "decoder_feature": self.decoder_feature[idx],
        }, self.target[idx]

    def iter(self):
        for i in range(len(self.x)):
            yield self[i]

predict_length = 10
train_reader = FakeReader(predict_length=predict_length)
train_loader = tf.data.Dataset.from_generator(
    train_reader.iter,
    ({"x": tf.float32, "encoder_feature": tf.float32, "decoder_feature": tf.float32}, tf.float32),
)
train_loader = train_loader.batch(batch_size=1)
valid_reader = FakeReader(predict_length=predict_length)
valid_loader = tf.data.Dataset.from_generator(
    valid_reader.iter,
    ({"x": tf.float32, "encoder_feature": tf.float32, "decoder_feature": tf.float32}, tf.float32),
)
valid_loader = valid_loader.batch(batch_size=1)

config = AutoConfig.for_model("seq2seq")
model = AutoModel.from_config(config, predict_length=predict_length)
trainer = KerasTrainer(model)
trainer.train(train_dataset=train_loader, valid_dataset=valid_loader, n_epochs=1)

修改模型配置参数

import tensorflow as tf
import tfts
from tfts import AutoModel, AutoConfig

config = AutoConfig.for_model('rnn')
print(config)
config.rnn_hidden_size = 128

model = AutoModel.from_config(config, predict_length=7, )

搭建自己的模型

检查tfts AutoModel已支持的模型
  • rnn
  • tcn
  • bert
  • nbeats
  • seq2seq
  • wavenet
  • transformer
  • informer
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense
from tfts import AutoModel, AutoConfig

def build_model():
    train_length = 24
    train_features = 15
    predict_length = 16

    inputs = Input([train_length, train_features])
    config = AutoConfig.for_model("seq2seq")
    backbone = AutoModel.from_config(config, predict_length=predict_length)
    outputs = backbone(inputs)
    outputs = Dense(1, activation="sigmoid")(outputs)
    model = tf.keras.Model(inputs=inputs, outputs=outputs)
    model.compile(loss="mse", optimizer="rmsprop")
    return model

示例

东流tfts专注业界领先的深度模型

  • Bert模型 获得KDD CUP2022-百度风机功率预测第3名
  • Seq2seq模型 获得阿里天池-AI earth人工智能气象挑战赛第4名

引用

@misc{tfts2020,
  author = {Longxing Tan},
  title = {Time series prediction},
  year = {2020},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/longxingtan/time-series-prediction}},
}