-
Notifications
You must be signed in to change notification settings - Fork 89
/
base.py
109 lines (85 loc) · 2.95 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
Abstract base class for the Keras neural network regressors.
The reason for this class between BaseClassifier and deep_learning classifiers is
because we can generalise tags and _predict
"""
__author__ = ["AurumnPegasus", "achieveordie"]
__all__ = ["BaseDeepRegressor"]
from abc import ABC, abstractmethod
import numpy as np
from aeon.regression.base import BaseRegressor
class BaseDeepRegressor(BaseRegressor, ABC):
"""Abstract base class for deep learning time series regression.
The base classifier provides a deep learning default method for
_predict, and provides a new abstract method for building a
model.
Parameters
----------
batch_size : int, default = 40
training batch size for the model
last_file_name : str, default = "last_model"
The name of the file of the last model, used
only if save_last_model_to_file is used
"""
_tags = {
"X_inner_type": "numpy3D",
"capability:multivariate": True,
"algorithm_type": "deeplearning",
"non-deterministic": True,
"cant-pickle": True,
"python_dependencies": "tensorflow",
}
def __init__(self, batch_size=40, last_file_name="last_model"):
super(BaseDeepRegressor, self).__init__()
self.batch_size = batch_size
self.last_file_name = last_file_name
self.model_ = None
@abstractmethod
def build_model(self, input_shape):
"""
Construct a compiled, un-trained, keras model that is ready for training.
Parameters
----------
input_shape : tuple
The shape of the data fed into the input layer
Returns
-------
A compiled Keras Model
"""
...
def summary(self):
"""
Summary function to return the losses/metrics for model fit.
Returns
-------
history: dict or None,
Dictionary containing model's train/validation losses and metrics
"""
return self.history.history if self.history is not None else None
def _predict(self, X):
"""
Find regression estimate for all cases in X.
Parameters
----------
X : an np.ndarray of shape = (n_instances, n_channels, series_length)
The training input samples.
Returns
-------
predictions : 1d numpy array
array of predictions of each instance
"""
X = X.transpose((0, 2, 1))
y_pred = self.model_.predict(X, self.batch_size)
y_pred = np.squeeze(y_pred, axis=-1)
return y_pred
def save_last_model_to_file(self, file_path="./"):
"""Save the last epoch of the trained deep learning model.
Parameters
----------
file_path : str, default = "./"
The directory where the model will be saved
Returns
-------
None
"""
self.model_.save(file_path + self.last_file_name + ".hdf5")