runn.models.ensemble_dnn
warning_manager = WarningManager()
module-attribute
#
EnsembleDNN(attributes=None, n_alt=None, n_ensembles=5, layers_dim=[25, 25], activation='relu', regularizer=None, regularization_rate=0.001, dropout=0.0, batch_norm=False, learning_rate=0.001, optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'], n_jobs=1, filename=None, warnings=True)
#
Bases: DNN
Ensemble of deep neural network models for choice modeling.
PARAMETER | DESCRIPTION |
---|---|
attributes |
List with the attributes names in the model, in the same order as in the input data. If None, the model cannot be initialized unless it is loaded from a file. Default: None. |
n_alt |
Number of alternatives in the choice set. If None, the model cannot be initialized unless it is loaded from a file. Default: None. |
n_ensembles |
Number of base DNN models in the ensemble. This value should be greater than 1. Default: 5.
TYPE:
|
layers_dim |
List with the number of neurons in each hidden layer, the length of the list is the number of hidden layers. Default: [25, 25].
TYPE:
|
activation |
Activation function to use in the hidden layers. Can be either a string or a list of strings. See https://keras.io/api/layers/activations/ for the available activations. Default: 'relu'. |
regularizer |
Type of regularization to apply. Possible values: 'l1', 'l2' or 'l1_l2'. Default: None. |
regularization_rate |
Regularization rate if regularizer is not None. Default: 0.001.
TYPE:
|
learning_rate |
Learning rate of the optimizer. Default: 0.001.
TYPE:
|
dropout |
Dropout rate to use in the hidden layers. Can be either a float or a list of floats. If a float is provided, the same dropout rate will be used in all the hidden layers. Default: 0.0. |
batch_norm |
Whether to use batch normalization or not. Default: False.
TYPE:
|
optimizer |
Optimizer to use. Can be either a string or a tf.keras.optimizers.Optimizer. Default: 'adam'. |
loss |
Loss function to use. Can be either a string or a tf.keras.losses.Loss. Default: 'categorical_crossentropy'. |
metrics |
List of metrics to be evaluated by the model during training and testing. Each of this can be either a string or a tf.keras.metrics.Metric. Default: ['accuracy'].
TYPE:
|
n_jobs |
Number of parallel jobs to run. If -1, all CPUs are used. If 1 is given, no parallel computing code is used at all, which is useful for debugging. For n_jobs below -1, (n_cpus + 1 + n_jobs) are used.
TYPE:
|
filename |
Load a previously trained model from a file. If None, a new model will be initialized. When loading a model from a file, the previous parameters will be ignored. Default: None. |
warnings |
Whether to show warnings or not. Default: True.
TYPE:
|
Source code in runn/models/ensemble_dnn.py
fit(x, y, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, bagging=None, **kwargs)
#
Train the ensemble model.
PARAMETER | DESCRIPTION |
---|---|
x |
Input data. It can be a tf.Tensor, np.ndarray or pd.DataFrame.
TYPE:
|
y |
The alternative selected by each decision maker in the sample x. Can be either a tf.Tensor or np.ndarray. It should be a 1D array with integers in the range [0, n_alt-1] or a 2D array with one-hot encoded alternatives.
TYPE:
|
batch_size |
Number of samples per gradient update. If unspecified, batch_size will default to 32. |
epochs |
Number of epochs to train the model. An epoch is an iteration over the entire x and y data provided. Default: 1.
TYPE:
|
verbose |
Verbosity mode. 0 = silent, 1 = ensemble progress bar, 2 = one progress bar per individual model. 3 = for each individual model, show one line per epoch. Default: 1.
TYPE:
|
callbacks |
List of tf.keras.callbacks.Callback instances. List of callbacks to apply during training. See tf.keras.callbacks for details. Default: None. |
validation_split |
Float between 0 and 1. Fraction of the training data to be used as validation data. The model will set apart this fraction of the training data, will not train on it, and will evaluate the loss and any model metrics on this data at the end of each epoch. The validation data is selected from the last samples in the x and y data provided, before shuffling. Default: 0.0.
TYPE:
|
validation_data |
Data on which to evaluate the loss and any model metrics at the end of each epoch. The model will not be trained on this data. This could be a tuple (x_val, y_val) or a tuple (x_val, y_val, val_sample_weights). Default: None. |
bagging |
Whether to use bagging or not. If None, bagging will not be used. If a float is provided, it indicates the percentage of samples to use in each bootstrap sample. The value should be between 0.0 and 1.0. Default: None. |
**kwargs |
Additional arguments passed to the keras model. See tf.keras.Model.fit() for details.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
History
|
A list of tf.keras.callbacks.History objects, one for each individual DNN model. Each History object is a record of training loss values and metrics values at successive epochs, as well as validation loss values and validation metrics values (if applicable). |
Source code in runn/models/ensemble_dnn.py
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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
|
get_history()
#
Return the history of the model training for each individual DNN model.
RETURNS | DESCRIPTION |
---|---|
list[dict]
|
List of dictionaries with the history of the training of each individual DNN model. |
Source code in runn/models/ensemble_dnn.py
get_utility(x, name='EnsembleDNN_Utility')
#
Get the utility of each alternative for a given set of observations.
PARAMETER | DESCRIPTION |
---|---|
x |
The input data. It can be a tf.Tensor, np.ndarray or pd.DataFrame.
TYPE:
|
name |
Name of the utility model. Default: 'EnsembleDNN_Utility'.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ndarray
|
Numpy array with the utility of each alternative for each observation in the input data. |
Source code in runn/models/ensemble_dnn.py
load(path)
#
Load the model from a file.
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the file where the model is saved.
TYPE:
|
Source code in runn/models/ensemble_dnn.py
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 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 |
|
plot_model(ensemble=True, **kwargs)
#
Generate a graphical representation of the ensemble model.
PARAMETER | DESCRIPTION |
---|---|
ensemble |
Whether to plot the ensemble model or the individual DNN models. Default: True.
TYPE:
|
**kwargs |
Additional arguments passed to the 'plot_model' function. See the documentation of the base class for more details.
DEFAULT:
|
Source code in runn/models/ensemble_dnn.py
save(path='model.zip')
#
Save the model to a file. The model must be fitted before saving it.
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the file where the model will be saved. Default: 'model.zip'.
TYPE:
|
Source code in runn/models/ensemble_dnn.py
summary(ensemble=True, line_length=100, **kwargs)
#
Print a summary of the ensemble model.
PARAMETER | DESCRIPTION |
---|---|
ensemble |
If True, print the summary of the ensemble model. If False, print the summary of an individual DNN model. Default: True.
TYPE:
|
line_length |
Total length of printed lines. Default: 100.
TYPE:
|
**kwargs |
Additional arguments passed to the keras model. See tf.keras.Model.summary() for details.
DEFAULT:
|