Upload 4 files
Browse files- README.md +36 -3
- aunet_model.py +89 -0
- config.json +6 -0
- training_args.json +8 -0
README.md
CHANGED
@@ -1,3 +1,36 @@
|
|
1 |
-
---
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
tags:
|
3 |
+
- time-series
|
4 |
+
- forecasting
|
5 |
+
- attention
|
6 |
+
- tensorflow
|
7 |
+
- keras
|
8 |
+
license: mit
|
9 |
+
---
|
10 |
+
|
11 |
+
# AUNET: Attention-Based Time Series Forecasting
|
12 |
+
|
13 |
+
AUNET is a neural network architecture designed for time series forecasting, combining multi-head self-attention with dense layers to capture temporal patterns in numeric datasets. Developed using TensorFlow/Keras, it supports customizable input windows and forecast horizons.
|
14 |
+
|
15 |
+
## Usage
|
16 |
+
|
17 |
+
```python
|
18 |
+
from aunet_model import AUNET
|
19 |
+
|
20 |
+
model = AUNET(input_length=30, forecast_horizon=7)
|
21 |
+
model.fit(X_train, y_train)
|
22 |
+
y_pred = model.predict(X_test)
|
23 |
+
```
|
24 |
+
|
25 |
+
## Model Details
|
26 |
+
|
27 |
+
- Architecture: Multi-head self-attention + dense layers
|
28 |
+
- Framework: TensorFlow / Keras
|
29 |
+
- Use Case: Multistep forecasting of univariate or multivariate numeric time series
|
30 |
+
- Target Feature: Second column of data (post date-drop)
|
31 |
+
|
32 |
+
## Authors
|
33 |
+
|
34 |
+
- Adria Binte Habib
|
35 |
+
- Dr. Golam Rabiul Alam
|
36 |
+
- Dr. Zia Uddin
|
aunet_model.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# AUNET: Attention-Based Time Series Forecaster (TensorFlow / Keras)
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
from tensorflow.keras import layers, models, regularizers, optimizers, callbacks
|
7 |
+
|
8 |
+
class AUNET:
|
9 |
+
def __init__(self, input_length=30, forecast_horizon=7, feature_dim=None,
|
10 |
+
lr=0.000352, dropout_rate=0.261, hidden_size=128, num_heads=8,
|
11 |
+
patience=10, batch_size=32, epochs=50, verbose=1):
|
12 |
+
self.input_length = input_length
|
13 |
+
self.forecast_horizon = forecast_horizon
|
14 |
+
self.feature_dim = feature_dim
|
15 |
+
self.lr = lr
|
16 |
+
self.dropout_rate = dropout_rate
|
17 |
+
self.hidden_size = hidden_size
|
18 |
+
self.num_heads = num_heads
|
19 |
+
self.patience = patience
|
20 |
+
self.batch_size = batch_size
|
21 |
+
self.epochs = epochs
|
22 |
+
self.verbose = verbose
|
23 |
+
self.model = None
|
24 |
+
self.history = None
|
25 |
+
|
26 |
+
def _build_model(self):
|
27 |
+
inputs = layers.Input(shape=(self.input_length, self.feature_dim))
|
28 |
+
x = layers.MultiHeadAttention(num_heads=self.num_heads, key_dim=self.hidden_size, dropout=self.dropout_rate)(inputs, inputs)
|
29 |
+
for _ in range(2):
|
30 |
+
x = layers.Dense(self.hidden_size, activation="relu", kernel_regularizer=regularizers.l2(1.95e-6))(x)
|
31 |
+
x = layers.BatchNormalization()(x)
|
32 |
+
x = layers.Dropout(self.dropout_rate)(x)
|
33 |
+
x = layers.Dense(self.forecast_horizon)(x)
|
34 |
+
outputs = layers.Flatten()(x)
|
35 |
+
model = models.Model(inputs, outputs)
|
36 |
+
model.compile(optimizer=optimizers.Adam(learning_rate=self.lr), loss='mse')
|
37 |
+
return model
|
38 |
+
|
39 |
+
def fit(self, X, y, validation_split=0.2):
|
40 |
+
if self.feature_dim is None:
|
41 |
+
self.feature_dim = X.shape[2]
|
42 |
+
self.model = self._build_model()
|
43 |
+
es = callbacks.EarlyStopping(monitor='val_loss', patience=self.patience, restore_best_weights=True)
|
44 |
+
self.history = self.model.fit(X, y,
|
45 |
+
epochs=self.epochs,
|
46 |
+
batch_size=self.batch_size,
|
47 |
+
validation_split=validation_split,
|
48 |
+
callbacks=[es],
|
49 |
+
verbose=self.verbose)
|
50 |
+
|
51 |
+
def predict(self, X):
|
52 |
+
return self.model.predict(X)
|
53 |
+
|
54 |
+
def evaluate(self, X, y_true, scaler_y=None):
|
55 |
+
y_pred = self.predict(X)
|
56 |
+
if scaler_y:
|
57 |
+
y_pred = scaler_y.inverse_transform(y_pred)
|
58 |
+
y_true = scaler_y.inverse_transform(y_true)
|
59 |
+
mae = np.mean(np.abs(y_true - y_pred))
|
60 |
+
rmse = np.sqrt(np.mean((y_true - y_pred)**2))
|
61 |
+
r2 = 1 - np.sum((y_true - y_pred)**2) / np.sum((y_true - np.mean(y_true))**2)
|
62 |
+
return {"MAE": mae, "RMSE": rmse, "R2": r2}
|
63 |
+
|
64 |
+
def plot_learning_curve(self):
|
65 |
+
if self.history:
|
66 |
+
plt.figure(figsize=(10, 5))
|
67 |
+
plt.plot(self.history.history['loss'], label='Train Loss')
|
68 |
+
if 'val_loss' in self.history.history:
|
69 |
+
plt.plot(self.history.history['val_loss'], label='Validation Loss')
|
70 |
+
plt.xlabel('Epoch')
|
71 |
+
plt.ylabel('Loss')
|
72 |
+
plt.title('Learning Curve')
|
73 |
+
plt.legend()
|
74 |
+
plt.grid(True)
|
75 |
+
plt.show()
|
76 |
+
|
77 |
+
def plot_predictions(self, y_true, y_pred, scaler_y=None, title='Prediction vs Actual'):
|
78 |
+
if scaler_y:
|
79 |
+
y_pred = scaler_y.inverse_transform(y_pred)
|
80 |
+
y_true = scaler_y.inverse_transform(y_true)
|
81 |
+
plt.figure(figsize=(12, 6))
|
82 |
+
plt.plot(y_true.flatten(), label='Actual')
|
83 |
+
plt.plot(y_pred.flatten(), label='Predicted')
|
84 |
+
plt.title(title)
|
85 |
+
plt.xlabel('Time Step')
|
86 |
+
plt.ylabel('Value')
|
87 |
+
plt.legend()
|
88 |
+
plt.grid(True)
|
89 |
+
plt.show()
|
config.json
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": ["AUNET"],
|
3 |
+
"library_name": "tensorflow",
|
4 |
+
"tags": ["time-series", "forecasting", "attention"],
|
5 |
+
"license": "mit"
|
6 |
+
}
|
training_args.json
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"epochs": 50,
|
3 |
+
"batch_size": 32,
|
4 |
+
"optimizer": "adam",
|
5 |
+
"learning_rate": 0.000352,
|
6 |
+
"input_length": 30,
|
7 |
+
"forecast_horizon": 7
|
8 |
+
}
|