Skip to content

Commit

Permalink
[FIX] Passing checks (#298)
Browse files Browse the repository at this point in the history
* Initial fix for all tests passing locally py=3.8

* fix bug in tests

* fix bug in test for data

* debugging error in dummy forward pass

* debug try -2

* catch runtime error in ci

* catch runtime error in ci

* add better debug test setup

* debug some more

* run this test only

* remove sum backward

* remove inplace in inception block

* undo silly change

* Enable all tests

* fix flake

* fix bug in test setup

* remove anamoly detection

* minor changes to comments

* Apply suggestions from code review

Co-authored-by: nabenabe0928 <[email protected]>

* Address comments from Shuhei

* revert change leading to bug

* fix flake

* change comment position in feature validator

* Add documentation for _is_datasets_consistent

* address comments from arlind

* case when all nans in test

Co-authored-by: nabenabe0928 <[email protected]>
  • Loading branch information
ravinkohli and nabenabe0928 committed Mar 9, 2022
1 parent 5167742 commit 3e50b27
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 263 deletions.
5 changes: 3 additions & 2 deletions autoPyTorch/api/base_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,7 @@ def fit_ensemble(
Args:
optimize_metric (str): name of the metric that is used to
evaluate a pipeline. if not specified, value passed to search will be used
precision (int), (default=32): Numeric precision used when loading
precision (Optional[int]): Numeric precision used when loading
ensemble data. Can be either 16, 32 or 64.
ensemble_nbest (Optional[int]):
only consider the ensemble_nbest models to build the ensemble.
Expand Down Expand Up @@ -1780,6 +1780,7 @@ def fit_ensemble(
"Please call the `search()` method of {} prior to "
"fit_ensemble().".format(self.__class__.__name__))

precision = precision if precision is not None else self.precision
if precision not in [16, 32, 64]:
raise ValueError("precision must be one of 16, 32, 64 but got {}".format(precision))

Expand Down Expand Up @@ -1830,7 +1831,7 @@ def fit_ensemble(
manager = self._init_ensemble_builder(
time_left_for_ensembles=time_left_for_ensemble,
optimize_metric=self.opt_metric if optimize_metric is None else optimize_metric,
precision=self.precision if precision is None else precision,
precision=precision,
ensemble_size=ensemble_size,
ensemble_nbest=ensemble_nbest,
)
Expand Down
49 changes: 35 additions & 14 deletions autoPyTorch/data/tabular_feature_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def _comparator(cmp1: str, cmp2: str) -> int:
if cmp1 not in choices or cmp2 not in choices:
raise ValueError('The comparator for the column order only accepts {}, '
'but got {} and {}'.format(choices, cmp1, cmp2))

idx1, idx2 = choices.index(cmp1), choices.index(cmp2)
return idx1 - idx2

Expand Down Expand Up @@ -279,13 +280,12 @@ def transform(
# having a value for a categorical column.
# We need to convert the column in test data to
# object otherwise the test column is interpreted as float
if len(self.categorical_columns) > 0:
categorical_columns = self.column_transformer.transformers_[0][-1]
for column in categorical_columns:
if X[column].isna().all():
X[column] = X[column].astype('object')

if self.column_transformer is not None:
if len(self.categorical_columns) > 0:
categorical_columns = self.column_transformer.transformers_[0][-1]
for column in categorical_columns:
if X[column].isna().all():
X[column] = X[column].astype('object')
X = self.column_transformer.transform(X)

# Sparse related transformations
Expand Down Expand Up @@ -371,16 +371,10 @@ def _check_data(

dtypes = [dtype.name for dtype in X.dtypes]

dtypes_diff = [s_dtype != dtype for s_dtype, dtype in zip(self.dtypes, dtypes)]
diff_cols = X.columns[[s_dtype != dtype for s_dtype, dtype in zip(self.dtypes, dtypes)]]
if len(self.dtypes) == 0:
self.dtypes = dtypes
elif (
any(dtypes_diff) # the dtypes of some columns are different in train and test dataset
and self.all_nan_columns is not None # Ignore all_nan_columns is None
and len(set(X.columns[dtypes_diff]).difference(self.all_nan_columns)) != 0
):
# The dtypes can be different if and only if the column belongs
# to all_nan_columns as these columns would be imputed.
elif not self._is_datasets_consistent(diff_cols, X):
raise ValueError("The dtype of the features must not be changed after fit(), but"
" the dtypes of some columns are different between training ({}) and"
" test ({}) datasets.".format(self.dtypes, dtypes))
Expand Down Expand Up @@ -548,6 +542,33 @@ def infer_objects(self, X: pd.DataFrame) -> pd.DataFrame:

return X

def _is_datasets_consistent(self, diff_cols: List[Union[int, str]], X: pd.DataFrame) -> bool:
"""
Check the consistency of dtypes between training and test datasets.
The dtypes can be different if the column belongs to `self.all_nan_columns`
(list of column names with all nans in training data) or if the column is
all nan as these columns would be imputed.
Args:
diff_cols (List[bool]):
The column labels that have different dtypes.
X (pd.DataFrame):
A validation or test dataset to be compared with the training dataset
Returns:
_ (bool): Whether the training and test datasets are consistent.
"""
if self.all_nan_columns is None:
if len(diff_cols) == 0:
return True
else:
return all(X[diff_cols].isna().all())

# dtype is different ==> the column in at least either of train or test datasets must be all NaN
# inconsistent <==> dtype is different and the col in both train and test is not all NaN
inconsistent_cols = list(set(diff_cols) - self.all_nan_columns)

return len(inconsistent_cols) == 0 or all(X[inconsistent_cols].isna().all())


def has_object_columns(
feature_types: pd.Series,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def transform(self, X: Dict[str, Any]) -> Dict[str, Any]:
Returns:
(Dict[str, Any]): the updated 'X' dictionary
"""
X.update({'encoder': self.preprocessor})
# X.update({'encoder': self.preprocessor})
return X

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def transform(self, X: Dict[str, Any]) -> Dict[str, Any]:
Returns:
np.ndarray: Transformed features
"""
X.update({'scaler': self.preprocessor})
# X.update({'scaler': self.preprocessor})
return X

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def fit(self, X: Dict[str, Any], y: Any = None) -> BaseEstimator:

self.embedding = self.build_embedding(
num_input_features=num_input_features,
num_numerical_features=num_numerical_columns)
num_numerical_features=num_numerical_columns) # type: ignore[arg-type]
return self

def transform(self, X: Dict[str, Any]) -> Dict[str, Any]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ def train_step(self, data: np.ndarray, targets: np.ndarray) -> Tuple[float, torc
loss = loss_func(self.criterion, original_outputs, adversarial_outputs)
loss.backward()
self.optimizer.step()
if self.scheduler:
if 'ReduceLROnPlateau' in self.scheduler.__class__.__name__:
self.scheduler.step(loss)
else:
self.scheduler.step()

# only passing the original outputs since we do not care about
# the adversarial performance.
return loss.item(), original_outputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def fit(self, X: Dict[str, Any], y: Any = None, **kwargs: Any) -> autoPyTorchCom
y=y,
**kwargs
)

# Add snapshots to base network to enable
# predicting with snapshot ensemble
self.choice: autoPyTorchComponent = cast(autoPyTorchComponent, self.choice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_search_space_updates():
value_range=['shake-shake'],
default_value='shake-shake')
updates.append(node_name='network_backbone',
hyperparameter='ResNetBackbone:shake_shake_method',
hyperparameter='ResNetBackbone:shake_shake_update_func',
value_range=['M3'],
default_value='M3'
)
Expand Down
Loading

0 comments on commit 3e50b27

Please sign in to comment.