>>> duplicate_items([1, 2], [3])
set()
>>> duplicate_items({1: 'a', 2: 'a'})
set()
>>> duplicate_items(['a', 'b', 'a'])
{'a'}
>>> duplicate_items([1, 2], {3: 'hi', 4: 'ha'}, (2, 3))
{2, 3}
skorch.utils.
freeze_parameter
(param)[source]
Convenience function to freeze a passed torch parameter.
Used by skorch.callbacks.Freezer
skorch.utils.
get_dim
(y)[source]
Return the number of dimensions of a torch tensor or numpy
array-like object.
skorch.utils.
get_map_location
(target_device, fallback_device='cpu')[source]
Determine the location to map loaded data (e.g., weights)
for a given target device (e.g. ‘cuda’).
skorch.utils.
is_skorch_dataset
(ds)[source]
Checks if the supplied dataset is an instance of
skorch.dataset.Dataset
even when it is nested inside
torch.util.data.Subset
.
skorch.utils.
multi_indexing
(data, i, indexing=None)[source]
Perform indexing on multiple data structures.
Currently supported data types:
numpy arrays
torch tensors
pandas NDFrame
a dictionary of the former three
a list/tuple of the former three
i
can be an integer or a slice.
indexing : function/callable or None (default=None)
If not None, use this function for indexing into the data. If
None, try to automatically determine how to index data.
>>> multi_indexing({'a': [1, 2, 3], 'b': [4, 5, 6]}, np.s_[-2:])
{'a': [2, 3], 'b': [5, 6]}
>>> multi_indexing(pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}), [1, 2])
1 2 5
2 3 6
skorch.utils.
noop
(*args, **kwargs)[source]
No-op function that does nothing and returns None
.
This is useful for defining scoring callbacks that do not need a
target extractor.
skorch.utils.
params_for
(prefix, kwargs)[source]
Extract parameters that belong to a given sklearn module prefix from
kwargs
. This is useful to obtain parameters that belong to a
submodule.
Examples
>>> kwargs = {'encoder__a': 3, 'encoder__b': 4, 'decoder__a': 5}
>>> params_for('encoder', kwargs)
{'a': 3, 'b': 4}
skorch.utils.
to_device
(X, device)[source]
Generic function to modify the device type of the tensor(s) or module.
PyTorch distribution objects are left untouched, since they don’t support an
API to move between devices.
device : str, torch.device
The compute device to be used. If device=None, return the input
unmodified
skorch.utils.
to_numpy
(X)[source]
Generic function to convert a pytorch tensor to numpy.
This function tries to unpack the tensor(s) from supported
data structures (e.g., dicts, lists, etc.) but doesn’t go
beyond.
Returns X when it already is a numpy array.
device : str, torch.device
The compute device to be used. If set to ‘cuda’, data in torch
tensors will be pushed to cuda tensors before being sent to the
module.
accept_sparse : bool (default=False)
Whether to accept scipy sparse matrices as input. If False,
passing a sparse matrix raises an error. If True, it is
converted to a torch COO tensor.
skorch.utils.
unfreeze_parameter
(param)[source]
Convenience function to unfreeze a passed torch parameter.
Used by skorch.callbacks.Unfreezer