/
_rocket.py
343 lines (268 loc) · 9.93 KB
/
_rocket.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""Rocket transformer."""
__maintainer__ = []
__all__ = ["Rocket"]
import numpy as np
from numba import get_num_threads, njit, prange, set_num_threads
from aeon.transformations.collection import BaseCollectionTransformer
from aeon.utils.validation import check_n_jobs
class Rocket(BaseCollectionTransformer):
"""RandOm Convolutional KErnel Transform (ROCKET).
A kernel (or convolution) is a subseries used to create features that can be used
in machine learning tasks. ROCKET [1]_ generates a large number of random
convolutional kernels in the fit method. The length and dilation of each kernel
are also randomly generated. The kernels are used in the transform stage to
generate a new set of features. A kernel is used to create an activation map for
each series by running it across a time series, including random length and
dilation. It transforms the time series with two features per kernel. The first
feature is global max pooling and the second is proportion of positive values
(or PPV).
Parameters
----------
num_kernels : int, default=10,000
Number of random convolutional kernels.
normalise : bool, default True
Whether or not to normalise the input time series per instance.
n_jobs : int, default=1
The number of jobs to run in parallel for `transform`. ``-1`` means using all
processors.
random_state : None or int, optional, default = None
Seed for random number generation.
See Also
--------
MultiRocketMultivariate, MiniRocket, MiniRocketMultivariate
References
----------
.. [1] Tan, Chang Wei and Dempster, Angus and Bergmeir, Christoph
and Webb, Geoffrey I,
"ROCKET: Exceptionally fast and accurate time series
classification using random convolutional kernels",2020,
https://link.springer.com/article/10.1007/s10618-020-00701-z,
https://arxiv.org/abs/1910.13051
Examples
--------
>>> from aeon.transformations.collection.convolution_based import Rocket
>>> from aeon.datasets import load_unit_test
>>> X_train, y_train = load_unit_test(split="train")
>>> X_test, y_test = load_unit_test(split="test")
>>> trf = Rocket(num_kernels=512)
>>> trf.fit(X_train)
Rocket(num_kernels=512)
>>> X_train = trf.transform(X_train)
>>> X_test = trf.transform(X_test)
"""
_tags = {
"output_data_type": "Tabular",
"capability:multivariate": True,
"algorithm_type": "convolution",
}
def __init__(
self,
num_kernels=10_000,
normalise=True,
n_jobs=1,
random_state=None,
):
self.num_kernels = num_kernels
self.normalise = normalise
self.n_jobs = n_jobs
self.random_state = random_state
super().__init__()
def _fit(self, X, y=None):
"""Generate random kernels adjusted to time series shape.
Infers time series length and number of channels from input numpy array,
and generates random kernels.
Parameters
----------
X : 3D np.ndarray of shape = (n_cases, n_channels, n_timepoints)
collection of time series to transform
y : ignored argument for interface compatibility
Returns
-------
self
"""
if isinstance(self.random_state, int):
self._random_state = self.random_state
else:
self._random_state = None
_, n_channels, n_timepoints = X.shape
self.kernels = _generate_kernels(
n_timepoints, self.num_kernels, n_channels, self._random_state
)
return self
def _transform(self, X, y=None):
"""Transform input time series using random convolutional kernels.
Parameters
----------
X : 3D np.ndarray of shape = [n_cases, n_channels, n_timepoints]
collection of time series to transform
y : ignored argument for interface compatibility
Returns
-------
np.ndarray [n_cases, num_kernels], transformed features
"""
if self.normalise:
X = (X - X.mean(axis=-1, keepdims=True)) / (
X.std(axis=-1, keepdims=True) + 1e-8
)
prev_threads = get_num_threads()
n_jobs = check_n_jobs(self.n_jobs)
set_num_threads(n_jobs)
X_ = _apply_kernels(X.astype(np.float32), self.kernels)
set_num_threads(prev_threads)
return X_
@njit(
"Tuple((float32[:],int32[:],float32[:],int32[:],int32[:],int32[:],"
"int32[:]))(int32,int32,int32,optional(int32))",
cache=True,
)
def _generate_kernels(n_timepoints, num_kernels, n_channels, seed):
if seed is not None:
np.random.seed(seed)
candidate_lengths = np.array((7, 9, 11), dtype=np.int32)
lengths = np.random.choice(candidate_lengths, num_kernels).astype(np.int32)
num_channel_indices = np.zeros(num_kernels, dtype=np.int32)
for i in range(num_kernels):
limit = min(n_channels, lengths[i])
num_channel_indices[i] = 2 ** np.random.uniform(0, np.log2(limit + 1))
channel_indices = np.zeros(num_channel_indices.sum(), dtype=np.int32)
weights = np.zeros(
np.int32(
np.dot(lengths.astype(np.float32), num_channel_indices.astype(np.float32))
),
dtype=np.float32,
)
biases = np.zeros(num_kernels, dtype=np.float32)
dilations = np.zeros(num_kernels, dtype=np.int32)
paddings = np.zeros(num_kernels, dtype=np.int32)
a1 = 0 # for weights
a2 = 0 # for channel_indices
for i in range(num_kernels):
_length = lengths[i]
_num_channel_indices = num_channel_indices[i]
_weights = np.random.normal(0, 1, _num_channel_indices * _length).astype(
np.float32
)
b1 = a1 + (_num_channel_indices * _length)
b2 = a2 + _num_channel_indices
a3 = 0 # for weights (per channel)
for _ in range(_num_channel_indices):
b3 = a3 + _length
_weights[a3:b3] = _weights[a3:b3] - _weights[a3:b3].mean()
a3 = b3
weights[a1:b1] = _weights
channel_indices[a2:b2] = np.random.choice(
np.arange(0, n_channels), _num_channel_indices, replace=False
)
biases[i] = np.random.uniform(-1, 1)
dilation = 2 ** np.random.uniform(
0, np.log2((n_timepoints - 1) / (_length - 1))
)
dilation = np.int32(dilation)
dilations[i] = dilation
padding = ((_length - 1) * dilation) // 2 if np.random.randint(2) == 1 else 0
paddings[i] = padding
a1 = b1
a2 = b2
return (
weights,
lengths,
biases,
dilations,
paddings,
num_channel_indices,
channel_indices,
)
@njit(fastmath=True, cache=True)
def _apply_kernel_univariate(X, weights, length, bias, dilation, padding):
n_timepoints = len(X)
output_length = (n_timepoints + (2 * padding)) - ((length - 1) * dilation)
_ppv = 0
_max = np.NINF
end = (n_timepoints + padding) - ((length - 1) * dilation)
for i in range(-padding, end):
_sum = bias
index = i
for j in range(length):
if index > -1 and index < n_timepoints:
_sum = _sum + weights[j] * X[index]
index = index + dilation
if _sum > _max:
_max = _sum
if _sum > 0:
_ppv += 1
return np.float32(_ppv / output_length), np.float32(_max)
@njit(fastmath=True, cache=True)
def _apply_kernel_multivariate(
X, weights, length, bias, dilation, padding, num_channel_indices, channel_indices
):
n_columns, n_timepoints = X.shape
output_length = (n_timepoints + (2 * padding)) - ((length - 1) * dilation)
_ppv = 0
_max = np.NINF
end = (n_timepoints + padding) - ((length - 1) * dilation)
for i in range(-padding, end):
_sum = bias
index = i
for j in range(length):
if index > -1 and index < n_timepoints:
for k in range(num_channel_indices):
_sum = _sum + weights[k, j] * X[channel_indices[k], index]
index = index + dilation
if _sum > _max:
_max = _sum
if _sum > 0:
_ppv += 1
return np.float32(_ppv / output_length), np.float32(_max)
@njit(
"float32[:,:](float32[:,:,:],Tuple((float32[::1],int32[:],float32[:],"
"int32[:],int32[:],int32[:],int32[:])))",
parallel=True,
fastmath=True,
cache=True,
)
def _apply_kernels(X, kernels):
(
weights,
lengths,
biases,
dilations,
paddings,
num_channel_indices,
channel_indices,
) = kernels
n_cases, n_channels, _ = X.shape
num_kernels = len(lengths)
_X = np.zeros((n_cases, num_kernels * 2), dtype=np.float32) # 2 features per kernel
for i in prange(n_cases):
a1 = 0 # for weights
a2 = 0 # for channel_indices
a3 = 0 # for features
for j in range(num_kernels):
b1 = a1 + num_channel_indices[j] * lengths[j]
b2 = a2 + num_channel_indices[j]
b3 = a3 + 2
if num_channel_indices[j] == 1:
_X[i, a3:b3] = _apply_kernel_univariate(
X[i, channel_indices[a2]],
weights[a1:b1],
lengths[j],
biases[j],
dilations[j],
paddings[j],
)
else:
_weights = weights[a1:b1].reshape((num_channel_indices[j], lengths[j]))
_X[i, a3:b3] = _apply_kernel_multivariate(
X[i],
_weights,
lengths[j],
biases[j],
dilations[j],
paddings[j],
num_channel_indices[j],
channel_indices[a2:b2],
)
a1 = b1
a2 = b2
a3 = b3
return _X.astype(np.float32)