Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def check_data(self, X, y):
if (
(y is None) and
(not is_dataset(X)) and
(self.iterator_train is DataLoader)
):
raise ValueError("No y-values are given (y=None). You must "
"implement your own DataLoader for training "
"(and your validation) and supply it using the "
"``iterator_train`` and ``iterator_valid`` "
"parameters respectively.")
if y is None:
# The user implements its own mechanism for generating y.
return
if get_dim(y) == 1:
msg = (
"The target data shouldn't be 1-dimensional but instead have "
"2 dimensions, with the second dimension having the same size "
"as the number of regression targets (usually 1). Please "
If this doesn't work with your data, you have to pass a
``Dataset`` that can deal with the data.
y : target data, compatible with skorch.dataset.Dataset
The same data types as for ``X`` are supported. If your X is
a Dataset that contains the target, ``y`` may be set to
None.
Returns
-------
dataset
The initialized dataset.
"""
if is_dataset(X):
return X
dataset = self.dataset
is_initialized = not callable(dataset)
kwargs = self._get_params_for('dataset')
if kwargs and is_initialized:
raise TypeError("Trying to pass an initialized Dataset while "
"passing Dataset arguments ({}) is not "
"allowed.".format(kwargs))
if is_initialized:
return dataset
return dataset(X, y, **kwargs)
If this doesn't work with your data, you have to pass a
``Dataset`` that can deal with the data.
y : target data, compatible with skorch.dataset.Dataset
The same data types as for ``X`` are supported. If your X is
a Dataset that contains the target, ``y`` may be set to
None.
Returns
-------
dataset
The initialized dataset.
"""
if is_dataset(X):
return X
dataset = self.dataset
is_initialized = not callable(dataset)
kwargs = self._get_params_for('dataset')
if kwargs and is_initialized:
raise TypeError("Trying to pass an initialized Dataset while "
"passing Dataset arguments ({}) is not "
"allowed.".format(kwargs))
if is_initialized:
return dataset
return dataset(X, y, **kwargs)
Whether to set the module to train mode or not.
device : string (default='cpu')
The device to store each inference result on.
This defaults to CPU memory since there is genereally
more memory available there. For performance reasons
this might be changed to a specific CUDA device,
e.g. 'cuda:0'.
Yields
------
yp : torch tensor
Result from a forward call on an individual batch.
"""
dataset = X if is_dataset(X) else self.get_dataset(X)
iterator = self.get_iterator(dataset, training=training)
for Xi, _ in iterator:
yp = self.evaluation_step(Xi, training=training)
if isinstance(yp, tuple):
yield tuple(n.to(device) for n in yp)
else:
yield yp.to(device)