A Comprehensive Guide to Using TFTS for Time Series Prediction

Nov 7, 2020 | Data Science

TFTS (TensorFlow Time Series) is a powerful and user-friendly package designed to aid your time series tasks, such as prediction, classification, or anomaly detection, using TensorFlow or Keras. In this blog, we will explore how to effectively use TFTS, prepare data, customize configurations, and troubleshoot common problems you might encounter.

Installation

To get started with TFTS, you’ll need to install a few prerequisites:

  • Python version: 3.7
  • TensorFlow version: 2.4

To install TFTS, run the command below in your terminal:

pip install tfts

Quick Start

Start by importing the necessary libraries and loading a sample dataset. Here’s how to dive right in:

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

train_length = 24
predict_length = 8
(x_train, y_train), (x_valid, y_valid) = tfts.get_data(sine, train_length, predict_length, test_size=0.2)

model_name_or_path = "seq2seq"
config = AutoConfig.for_model(model_name_or_path)
model = AutoModel.from_config(config, predict_length=predict_length)

trainer = KerasTrainer(model)
trainer.train((x_train, y_train), (x_valid, y_valid), n_epochs=3)

pred = trainer.predict(x_valid)
trainer.plot(history=x_valid, true=y_valid, pred=pred)
plt.show()

In this example, we’re setting up a sequence-to-sequence prediction task using a sine wave dataset. Let’s imagine you’re a chef preparing a recipe:

Analogy: Cooking Up Time Series Predictions

Think of the process as cooking a dish:

  • Ingredients: The training data (x_train, y_train) and validation data (x_valid, y_valid) are like the ingredients you gather.
  • Recipe Instructions: The AutoModel and AutoConfig act like the instructions on how to combine those ingredients effectively to create a delicious dish (the trained model).
  • Cooking Time: Just as you cannot rush cooking, the number of epochs (n_epochs) determines how thoroughly your model has learned before you start testing it.
  • Tasting: After cooking, you taste (training) and adjust the seasoning (tuning). The prediction output shows how well your model performs—just like a chef tasting their dish to evaluate its flavor!

Preparing Your Own Data

You can also train TFTS with your own datasets. You will need to prepare your data in 3D format, whether you’re using numpy arrays or TensorFlow datasets.

Using Numpy Arrays

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)  # inputs
y_train = np.random.rand(1, predict_length, 1)  # target
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)

Using TensorFlow Datasets

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

class FakeReader(object):
    # Implementation omitted for brevity
    pass

train_reader = FakeReader(predict_length=10)
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)

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)

Custom Model Configuration

If you wish to customize your model further, you can easily modify configurations:

config = AutoConfig.for_model(rnn)
config.rnn_hidden_size = 128
model = AutoModel.from_config(config, predict_length=7)

Troubleshooting Tips

If you encounter difficulties while working with TFTS, consider these troubleshooting ideas:

  • Installation Issues: Ensure that the required Python and TensorFlow versions are installed correctly. Use virtual environments to avoid conflicts.
  • Data Shape Confusion: Double-check the dimensions of your input data. Time series data should be structured as (batch, train_length, features).
  • Learning Issues: If the model isn’t converging, try tuning hyperparameters such as learning rates or increasing the number of epochs.
  • Resource Constraints: Be mindful of your hardware’s memory and processing capabilities. Consider simplifying your models or changing your dataset size.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox