torchnet.utils

MultiTaskDataLoader

class torchnet.utils.MultiTaskDataLoader(datasets, batch_size=1, use_all=False, **loading_kwargs)[source]

Bases: object

Loads batches simultaneously from multiple datasets.

The MultiTaskDataLoader is designed to make multi-task learning simpler. It is ideal for jointly training a model for multiple tasks or multiple datasets. MultiTaskDataLoader is initialzes with an iterable of Dataset objects, and provides an iterator which will return one batch that contains an equal number of samples from each of the Dataset s.

Specifically, it returns batches of [(B_0, 0), (B_1, 1), ..., (B_k, k)] from datasets (D_0, ..., D_k), where each B_i has batch_size samples

Parameters:
  • datasets – A list of Dataset objects to serve batches from
  • batch_size – Each batch from each Dataset will have this many samples
  • use_all (bool) – If True, then the iterator will return batches until all datasets are exhausted. If False, then iteration stops as soon as one dataset runs out
  • loading_kwargs – These are passed to the children dataloaders

Example

>>> train_loader = MultiTaskDataLoader([dataset1, dataset2], batch_size=3)
>>> for ((datas1, labels1), task1), (datas2, labels2), task2) in train_loader:
>>>     print(task1, task2)
0 1
0 1
...
0 1

ResultsWriter

class torchnet.utils.ResultsWriter(filepath, overwrite=False)[source]

Bases: object

Logs results to a file.

The ResultsWriter provides a convenient interface for periodically writing results to a file. It is designed to capture all information for a given experiment, which may have a sequence of distinct tasks. Therefore, it writes results in the format:

{
    'tasks': [...]
    'results': [...]
}

The ResultsWriter class chooses to use a top-level list instead of a dictionary to preserve temporal order of tasks (by default).

Parameters:
  • filepath (str) – Path to write results to
  • overwrite (bool) – whether to clobber a file if it exists

Example

>>> result_writer = ResultWriter(path)
>>> for task in ['CIFAR-10', 'SVHN']:
>>>    train_results = train_model()
>>>    test_results = test_model()
>>>    result_writer.update(task, {'Train': train_results, 'Test': test_results})
update(task_name, result)[source]

Update the results file with new information.

Parameters:
  • task_name (str) – Name of the currently running task. A previously unseen task_name will create a new entry in both tasks and results.
  • result – This will be appended to the list in results which corresponds to the task_name in task_nametasks.

Table module

torchnet.utils.table.canmergetensor(tbl)[source]
torchnet.utils.table.mergetensor(tbl)[source]