Generators
VAE.generators
Collection of generators for VAE model training.
Generator class for data given as a Numpy array(s).
VAE.generators.FitGenerator
FitGenerator(datasets, input_length, batch_size=32, beta_scheduler=None, condition=None, ensemble_size=None, ensemble_type='random', ensemble_index=None, ensemble_range=None, ensemble_replace=False, ensemble_sync=False, filter_length=None, initial_epoch=0, input_channels=None, latitude=0, longitude=None, prediction_channels=None, prediction_length=None, repeat_samples=1, sample_weights=None, shuffle=True, sph_degree=None, strides=1, time=None, tp_period=None, dtype='float32', **kwargs)
Bases: Sequence
Generator class for model training.
Given an Numpy array of shape (set_size, data_length, channels)
, the generator prepares the inputs and
targetsfor the model training in :func:
keras.Model.fit_generator()`.
Parameters:
-
datasets
(Union[ndarray, list[ndarray]]
) –Dataset used for training. The dataset can be either a single numpy array of shape
(set_size, data_length, channels)
or a list of Numpy arrays. In case of a list of Numpy arrays,set_size
andchannels
must be the same, whiledata_length
can vary. Missing (non-finite) values will be excluded from the samples. -
input_length
(int
) –Length of input to the encoder.
-
batch_size
(int
, default:32
) –Batch size. Note that the effective batch size is
batch_size * repeat_samples
. -
beta_scheduler
–Instance of :class:
BetaScheduler
that returns thebeta
parameters for the KL loss in each epoch. -
condition
(Union[ndarray, list[ndarray], dict]
, default:None
) –Additional data used as condition. The Numpy arrays must be of length
data_length
matching the Numpy array(s) indataset
. If a list is provided, the length of the list must match the length ofdatasets
. If a dict is provided, the keys must matchencoder
anddecoder
. This allows to pass different conditions to the encoder and decoder, provided as the corresponding dict values. -
ensemble_size
(int
, default:None
) –Size for the one-hot encoded ensemble condition.
-
ensemble_type
(str
, default:'random'
) –Whether to use the dataset index (
index
) or random ensemble condition (random
, 'random_full'). Ifindex
, the ensemble condition corresponds to the dataset index. If 'random' orrandom_full
, the ensemble condition is sampled from a uniform distribution in the rangeensemble_range
. The samples are the same for all samples in a batch ('random') or different for each sample in a batch ('random_full'). Defaults torandom
. -
ensemble_index
(int
, default:None
) –Array of indices used as ensemble condition if
ensemble_type
isindex
. Must match the length ofdataset
and must be in range(0, ensemble_size)
. Defaults toNone
meaning the dataset index is used. -
ensemble_range
(tuple[int, int]
, default:None
) –Range of the random ensemble condition. Must be a subrange of
(0, ensemble_size)
. Defaults toNone
and is set to(0, ensemble_size)
. -
ensemble_replace
(bool
, default:False
) –Whether to sample the random ensemble condition with replacement if
repeat_samples > 1
. Defaults toFalse
. -
ensemble_sync
(bool
, default:False
) –Synchronize random ensemble conditions between encoder and decoder. If
True
, the random ensemble conditions of the encoder and decoder are the same. Defaults toFalse
, i.e. the random ensemble conditions of the encoder an decoder are different random samples. Note that the ensemble conditions of the decoder and prediction are always the same. -
filter_length
(Union[int, tuple[int, int]]
, default:None
) –Length of the temporal filter for the inputs and targets. A centered moving average filter of length
2 * filter_length + 1
is applied to the inputs and targets. If a tuple of two ints is given, the first int is the length of the filter for the input to the encoder and the target to the decoder. The second int is the length of the filter for the target to the prediction. Defaults toNone
, i.e. no filter. -
initial_epoch
(int
, default:0
) –Initial epoch at which the generator will start. This will affect the
beta
parameter. -
input_channels
(list[int]
, default:None
) –Range of channels used as input. The items in the tuple refer to start, stop and step in slice notation. Defaults to
None
means all channels are used. -
latitude
(ndarray
, default:0
) –Latitude in degree if the spherical harmonics are used for spatial condition.
-
longitude
(ndarray
, default:None
) –Longitude of the data in degree if the spherical harmonics are used for spatial condition. Length of
longitude
must be equal toset_size
. Defaults toNone
and is set tonp.arange(0, 360, 360/set_size)
. -
prediction_channels
(list[int]
, default:None
) –Range of channels used for prediction. The items in the tuple refer to start, stop and step in slice notation. Defaults to
None
means all channels are used. -
prediction_length
(int
, default:None
) –Length of prediction. Defaults to
None
means no prediction. -
repeat_samples
(int
, default:1
) –Number of times the same sample is repeated in the batch. This will augment the batch size. This options is useful in combination with the ensemble condition, in which the same samples is presented multiple times with different random samples of the ensemble conditions. Defaults to 1.
-
sample_weights
(ndarray
, default:None
) –Sample weights of shape
(nr_datasets, set_size)
. Defaults toNone
. -
shuffle
(bool
, default:True
) –Shuffle samples order.
-
sph_degree
(int
, default:None
) –Number of spherical degrees if the spherical harmonics are used for spatial condition.
-
strides
(int
, default:1
) –Sample strides along second dimension of
data
of sizedata_length
. -
time
(Union[ndarray, list[ndarray]]
, default:None
) –Time of the data if the time-periodic harmonics are used for temporal condition. Must be of length
data_length
. If a list is provided, the length of the list must match the length ofdatasets
. -
tp_period
(float
, default:None
) –Maximal period for the temporal harmonics. See :func:
get_tp_harmonics
. -
dtype
(str
, default:'float32'
) –Dtype of the data that will be returned.
Source code in VAE/generators.py
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 |
|
VAE.generators.FitGenerator.nr_samples
property
nr_samples
Return number of samples.
VAE.generators.FitGenerator.__getitem__
__getitem__(idx)
Return batch of data.
Note that the effective batch size is batch_size * repeat_samples
.
Parameters:
-
idx
(int
) –Batch index.
Returns:
-
tuple[dict, dict]
–Two dicts, one for the inputs and one for the targets.
Source code in VAE/generators.py
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 |
|
VAE.generators.FitGenerator.get_ensemble_condition
get_ensemble_condition(batch_size, idx=None)
Return ensemble condition for given batch.
A one-hot encoded ensemble index is return that is the same for all samples in the batch. The code is
broadcasted along the second dimension of size set_size
.
In case of repeat_samples > 1
, the actual batch size is batch_size * repeat_samples
and a set of
repeat_samples
random indices is sampled.
To alter between sampling with and without replacement, the ensemble_replace
flag can be set.
Parameters:
-
batch_size
(int
) –Batch size.
-
idx
(int
, default:None
) –Required if
ensemble_type='index
. Returns the ensemble condition corresponding to the batch with indexidx
.
Returns:
-
ndarray
–Array of shape
(batch_size * repeat_samples, set_size, ensemble_size)
Source code in VAE/generators.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
|
VAE.generators.FitGenerator.get_index
get_index(idx)
Return array of dataset and time index of samples in batch.
The returned array is of shape (batch_size * repeat_samples, 2)
with the first column containing the dataset
index and the second column the time index of the sample. The time index refers to the first sample of the
target sequence for the prediction.
Parameters:
-
idx
(int
) –Batch index.
Returns:
-
ndarray
–Array of shape
(batch_size * repeat_samples, 2)
.
Source code in VAE/generators.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
|
VAE.generators.FitGenerator.get_sph_harmonics
get_sph_harmonics(latitude, longitude, sph_degree)
Get spherical harmonics.
The returned array is of shape (set_size, 2 * sph_degree + 1)
with the rows containing the spherical harmonics
for the given latitude and longitude values.
Parameters:
-
latitude
–float Latitude value in degree.
-
longitude
–array_like Array of longitude values in degree of shape
(set_size,)
.
Returns:
-
ndarray
–Array of shape
(set_size, 2 * sph_degree + 1)
.
Source code in VAE/generators.py
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
|
VAE.generators.FitGenerator.get_tp_harmonics
get_tp_harmonics(time, tp_period)
Get temporal harmonics.
Parameters:
-
time
(ndarray
) –Array of time values for which the harmonics are calculated.
-
tp_period
(int
) –Maximal period for the temporal harmonics in time units.
Returns:
-
ndarray
–Array of shape
(len(times), tp_period)
.
Source code in VAE/generators.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
|
VAE.generators.FitGenerator.on_epoch_end
on_epoch_end()
Shuffle data after each epoch.
This method is called after each epoch and shuffles the data if shuffle=True
.
Source code in VAE/generators.py
503 504 505 506 507 508 509 510 511 |
|
VAE.generators.FitGenerator.summary
summary()
Print summary.
Source code in VAE/generators.py
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
|
VAE.generators.PredictGenerator
PredictGenerator(datasets, input_length, batch_size=32, beta_scheduler=None, condition=None, ensemble_size=None, ensemble_type='random', ensemble_index=None, ensemble_range=None, ensemble_replace=False, ensemble_sync=False, filter_length=None, initial_epoch=0, input_channels=None, latitude=0, longitude=None, prediction_channels=None, prediction_length=None, repeat_samples=1, sample_weights=None, shuffle=True, sph_degree=None, strides=1, time=None, tp_period=None, dtype='float32', **kwargs)
Bases: FitGenerator
Generator class for model prediction.
The generator prepares the inputs for the model prediction with :func:ks.Model.predict
.
Parameters:
-
**kwargs
–See :class:
FitGenerator
for parameters.
Returns:
-
–
Dictionary containing the inputs for the model prediction.
Source code in VAE/generators.py
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 |
|
VAE.generators.PredictGenerator.__getitem__
__getitem__(idx)
Return inputs to the model for given batch.
Source code in VAE/generators.py
602 603 604 605 |
|
VAE.generators.example_FitGenerator
example_FitGenerator()
Example of :class:FitGenerator
.
This example shows how to use the :class:FitGenerator
class.
Source code in VAE/generators.py
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 |
|