def net_and_mock(self, module, data, train_split, iterator): """Return a NeuralNetClassifier with mocked train and validation step which save the args and kwargs the methods are calld with. """ from skorch import NeuralNetClassifier X, y = data net = NeuralNetClassifier( module, module__input_units=3, max_epochs=1, iterator_train=iterator, iterator_valid=iterator, train_split=train_split ) mock = Mock() def decorator(func): def wrapper(*args, **kwargs): mock(*args, **kwargs) func.__dict__['mock_'] = mock return func(*args[1:], **kwargs) return wrapper import types net.train_step = types.MethodType(decorator(net.train_step), net) net.validation_step = types.MethodType( decorator(net.validation_step), net) return net.fit(X, y), mock
def performance_skorch( X_train, X_test, y_train, y_test, batch_size, device, lr, max_epochs, ): torch.manual_seed(0) net = NeuralNetClassifier( ClassifierModule, batch_size=batch_size, optimizer=torch.optim.Adadelta, lr=lr, device=device, max_epochs=max_epochs, callbacks=[ ('tr_acc', EpochScoring( 'accuracy', lower_is_better=False, on_train=True, name='train_acc', )), ], ) net.fit(X_train, y_train) y_pred = net.predict(X_test) score = accuracy_score(y_test, y_pred) return score
def test_quickstart(self): X, y = make_classification(1000, 20, n_informative=10, random_state=0) X = X.astype(np.float32) y = y.astype(np.int64) class MyModule(nn.Module): def __init__(self, num_units=10, nonlin=F.relu): super(MyModule, self).__init__() self.dense0 = nn.Linear(20, num_units) self.nonlin = nonlin self.dropout = nn.Dropout(0.5) self.dense1 = nn.Linear(num_units, 10) self.output = nn.Linear(10, 2) def forward(self, X, **kwargs): X = self.nonlin(self.dense0(X)) X = self.dropout(X) X = F.relu(self.dense1(X)) X = F.softmax(self.output(X)) return X net = NeuralNetClassifier( MyModule, max_epochs=10, lr=0.1, # Shuffle training data on each epoch iterator_train__shuffle=True, ) net.fit(X, y) y_proba = net.predict_proba(X) pass
def test_subclassing_epoch_scoring(self, classifier_module, classifier_data): # This test's purpose is to check that it is possible to # easily subclass EpochScoring by overriding on_epoch_end to # record 2 scores. from skorch import NeuralNetClassifier from skorch.callbacks import EpochScoring class MyScoring(EpochScoring): def on_epoch_end(self, net, dataset_train, dataset_valid, **kwargs): _, y_test, y_proba = self.get_test_data( dataset_train, dataset_valid) y_pred = np.concatenate(y_proba).argmax(1) # record 2 valid scores score_0_valid = accuracy_score(y_test, y_pred) net.history.record('score_0', score_0_valid) score_1_valid = accuracy_score(y_test, y_pred) + 1 net.history.record('score_1', score_1_valid) X, y = classifier_data net = NeuralNetClassifier( classifier_module, callbacks=[MyScoring(scoring=None)], max_epochs=1, ) net.fit(X, y) row = net.history[-1] keys_history = set(row.keys()) keys_expected = {'score_0', 'score_1'} assert keys_expected.issubset(keys_history) assert np.isclose(row['score_0'], row['score_1'] - 1)
def test_lr_callback_batch_steps_correctly( self, classifier_module, classifier_data, policy, kwargs, ): batch_size = 100 max_epochs = 2 X, y = classifier_data num_examples = len(X) lr_policy = LRScheduler(policy, **kwargs) net = NeuralNetClassifier(classifier_module(), max_epochs=max_epochs, batch_size=batch_size, callbacks=[lr_policy]) net.fit(X, y) total_iterations_per_epoch = num_examples / batch_size # 80% of sample used for training by default total_training_iterations_per_epoch = 0.8 * total_iterations_per_epoch expected = int(total_training_iterations_per_epoch * max_epochs) # pylint: disable=protected-access assert lr_policy.batch_idx_ == expected
def skip_experiment(callbacks, today_str): dict_list = [] for two_receptor_layer in range(1, 10): for iteration in range(100): skorch_sn = NeuralNetClassifier( module=LinearSoupNetwork, module__layer_stock=layer_stock, module__soup_size=30, module__n_blocks=3, module__block_size=10, module__random_input_factor=[[1 for i in range(two_receptor_layer)] + [2] + [1 for i in range(two_receptor_layer+1, 10)] for j in range(3)], module__num_receptors=[[1 for i in range(two_receptor_layer)] + [2] + [1 for i in range(two_receptor_layer+1, 10)] for j in range(3)], max_epochs=50, lr=0.1, iterator_train__shuffle=True, device='cuda', callbacks=callbacks ) skorch_sn.fit(fmnist_train.train_data[:, None, :, :].float(), fmnist_train.train_labels.long()) y_pred = skorch_sn.predict(fmnist_test.test_data[:, None, :, :].float()) accuracy = metrics.accuracy_score(fmnist_test.test_labels.long(), y_pred) dict_list.append({'two_receptor_layer': two_receptor_layer, 'iteration': iteration, 'accuracy': accuracy}) pd.DataFrame(dict_list).to_csv(f'results/{today_str}/skip_experiment.csv') plot_filename = f'results/{today_str}/soup_{two_receptor_layer}' try: skorch_sn.module_.plot_soup(plot_filename) except Exception as e: print('pydot doesnt exist on this system, not plotting') ex.add_artifact(plot_filename)
def get_net_with_mock(self, classifier_data, classifier_module, monitor='train_loss'): """Returns a net with a mocked lr policy that allows to check what it's step method was called with. """ X, y = classifier_data net = NeuralNetClassifier( classifier_module, callbacks=[ ('scheduler', LRScheduler(ReduceLROnPlateau, monitor=monitor)), ], max_epochs=1, ).fit(X, y) # mock the policy policy = dict(net.callbacks_)['scheduler'].lr_scheduler_ mock_step = Mock(side_effect=policy.step) policy.step = mock_step # make sure that mocked policy is set scheduler = dict(net.callbacks_)['scheduler'] # pylint: disable=protected-access scheduler._get_scheduler = lambda *args, **kwargs: policy net.partial_fit(X, y) return net, mock_step
def paths_experiment(callbacks, today_str): dict_list = [] for paths in range(1, 10): for iteration in range(100): skorch_sn = NeuralNetClassifier( module=LinearSoupNetwork, module__layer_stock=layer_stock, module__soup_size=30, module__n_blocks=3, module__block_size=10, module__random_input_factor=[[1 for i in range(10)] for j in range(3)], module__num_receptors=[[1 for i in range(10)] for j in range(3)], module__paths=paths, max_epochs=1, lr=0.1, iterator_train__shuffle=True, device='cuda', callbacks=callbacks ) skorch_sn.fit(fmnist_train.train_data[:, None, :, :].float(), fmnist_train.train_labels.long()) y_pred = skorch_sn.predict(fmnist_test.test_data[:, None, :, :].float()) accuracy = metrics.accuracy_score(fmnist_test.test_labels.long(), y_pred) dict_list.append({'paths': paths, 'iteration': iteration, 'accuracy': accuracy}) pd.DataFrame(dict_list).to_csv(f'results/{today_str}/paths_experiment.csv')
def try_it(): X, y = make_classification(1000, 784, n_informative=10, random_state=0) logging.info('X: {}'.format(X.shape)) X = X.astype(np.float32) y = y.astype(np.int64) net = NeuralNetClassifier( F22SkorchCnn, module__num_features=X.shape[1], max_epochs=10, lr=0.1, ) # kwargs = {'module__num_units': 777} # net.set_params(**kwargs) net.fit(X, y) y_proba = net.predict_proba(X) logging.info('y_proba: {}'.format(y_proba)) scores = cross_val_score(net, X, y, cv=3, scoring='accuracy') logging.info('Score: {}'.format(scores)) logging.info("Accuracy: %.2f (+/- %.2f) [%s]" % (scores.mean(), scores.std(), 'F22SkorchCnn'))
def get_skorch_classifier(): X, y = make_classification(200, 5, n_informative=3, random_state=0) X = X.astype(np.float32) y = y.astype(np.int64) X_df = pd.DataFrame(X, columns=['col' + str(i) for i in range(X.shape[1])]) class MyModule(nn.Module): def __init__(self, input_units=5, num_units=5, nonlin=nn.ReLU()): super(MyModule, self).__init__() self.dense0 = nn.Linear(input_units, num_units) self.nonlin = nonlin self.dense1 = nn.Linear(num_units, num_units) self.output = nn.Linear(num_units, 2) self.softmax = nn.Softmax(dim=-1) def forward(self, X, **kwargs): X = self.nonlin(self.dense0(X)) X = self.nonlin(self.dense1(X)) X = self.softmax(self.output(X)) return X model = NeuralNetClassifier( MyModule, max_epochs=20, lr=0.1, ) model.fit(X_df.values, y) return model, X_df, y
def simple_trainingl(x, y): # Trains the Neural Network with fixed hyperparameters # The Neural Net is initialized with fixed hyperparameters nn = NeuralNetClassifier(NeuralNet, max_epochs=10, lr=0.01, batch_size=12, optimizer=optim.RMSprop) # Training nn.fit(x, y) pass
def __init__(self, path_train, definition, path_test=None, c_model_name=Classifier.cnn, cv_folds=5, num_epochs=100, batch_size=8, learning_rate=0.0003): """ Initializer for CNNClassification class. :param path_train: path to the h5 file for the training dataset (simulated data) :param definition: Type of SSW definition to use. example: "CP07", "U65" :param path_test: path to the h5 file for the test dataset (real data) :param c_model_name: CNN classifier name which is going to be used example: "cnn", "cnn_max_pool" :param cv_folds: number of folds for the cross-validation which is used to evaluate the performance on the training dataset. :param num_epochs: Number of epochs to train the model :param batch_size: Batch size for Adam optimizer :param learning_rate: Learning rate for Adam optimizer """ # Device configuration device = torch.device('cuda:' + os.getenv("CUDA_VISIBLE_DEVICES") if torch.cuda. is_available() else 'cpu') SetSeed().set_seed() self.data_manager_train = DataManager(path_train) if path_test: self.data_manager_test = DataManager(path_test) self.definition = definition self.cv_folds = cv_folds self.metric_txt = ["F1", "ROCAUC", "Accuracy"] self.metrics = [f1_score, roc_auc_score, accuracy_score] self.model_class_name = c_model_name # Number of channels in the CNN - number of features to use num_ts = 3 self.classifier = NeuralNetClassifier( cnn_model.get_cnn_classes()[c_model_name](num_ts), criterion=nn.CrossEntropyLoss, max_epochs=num_epochs, lr=learning_rate, batch_size=batch_size, device=device, optimizer=torch.optim.Adam, )
def test_lr_scheduler_record_epoch_step(self, classifier_module, classifier_data, policy, kwargs): epochs = 3 scheduler = LRScheduler(policy, **kwargs) lrs = scheduler.simulate(epochs, initial_lr=123.) net = NeuralNetClassifier(classifier_module, max_epochs=epochs, lr=123., callbacks=[('scheduler', scheduler)]) net.fit(*classifier_data) assert np.all(net.history[:, 'event_lr'] == lrs)
def test_cyclic_lr_with_epoch_step_warning(self, classifier_module, classifier_data): msg = ("The LRScheduler now makes a step every epoch by default. " "To have the cyclic lr scheduler update " "every batch set step_every='batch'") with pytest.warns(FutureWarning, match=msg) as record: scheduler = LRScheduler(TorchCyclicLR, base_lr=123, max_lr=999) net = NeuralNetClassifier( classifier_module, max_epochs=0, callbacks=[('scheduler', scheduler)], ) net.initialize() assert len(record) == 1
def __init__(self, device): self.n_steer = 17 self.n_vel = 5 torch.cuda.empty_cache() self.total_actions = self.n_steer * self.n_vel self.velocity_range = [0.5, 0.75, 1.0, 1.25, 1.5] #unitL: m/s self.min_steering_angle = -0.4188 #unit: radians. Equivalent to -24 degrees self.steer_range = [ self.min_steering_angle + 0.0523 * i for i in range(self.n_steer) ] self.model_file_name = '/CEMmodel.pkl' self.lrate = 0.1 self.device = device #Environment model self.env_model = Environment_Model_Architecture().to(device) print("Environment instance created.\n") self.load_environment_model() #Deep MLP module # self.net = NeuralNetClassifier( # MLP_module, # max_epochs=20, # lr=self.lrate, # iterator_train__shuffle=False, # ) self.net = None #Deep CNN module self.cnet = NeuralNetClassifier( CNN_ClassifierModule, max_epochs=30, lr=self.lrate, device=self.device, optimizer=torch.optim.SGD, ) self.reset_state_sim = plt.imread("../Data/img2.png") self.cnet.initialize_module() # self.net.initialize_module() #Training related variables self.NN_MODULE_TYPE = 'deepCNN' #'deepMLP' and 'deepCNN self.N_SESSIONS = 150 self.N_TSTEPS_HORIZON = 50 self.ELITE_PERCENTILE = 70 self.N_GENERATIONS = 100 self.log = [] self.FIRST_RUN = False self.train_CEM()
def __init__(self): rospy.init_node("deep_cem") self.n_steer = 17 self.n_vel = 5 self.total_actions = self.n_steer * self.n_vel self.velocity_range = [0.5, 0.75, 1.0, 1.25, 1.5] #unitL: m/s self.min_steering_angle = -0.4188 #unit: radians. Equivalent to -24 degrees self.steer_range = [ self.min_steering_angle + 0.0523 * i for i in range(self.n_steer) ] self.model_file_name = '/CEMmodel.pkl' self.lrate = 0.1 self.device = 'cpu' #Deep MLP module self.net = NeuralNetClassifier( MLP_module, max_epochs=20, lr=self.lrate, iterator_train__shuffle=False, ) #Deep CNN module self.cnet = NeuralNetClassifier( CNN_ClassifierModule, max_epochs=30, lr=self.lrate, device=self.device, optimizer=torch.optim.SGD, ) # Simulator interface variables self.sim_node = SimInterface() rospy.sleep( 2.0) #sleeping for 2 seconds for simulator to start properly self.reset_state_sim = self.sim_node.get_reset_state() self.cnet.initialize_module() self.net.initialize_module() #Training related variables self.NN_MODULE_TYPE = 'deepCNN' #'deepMLP' and 'deepCNN self.N_EPISODES = 50 self.N_TSTEPS_HORIZON = 20 self.ELITE_PERCENTILE = 70 self.N_GENERATIONS = 8 self.log = [] self.FIRST_RUN = True # train CEM when the simulator obj is created. self.train_CEM()
def train_with_skorch(self, model, dataset): if dataset['train'].X.ndim == 3: dataX = deepcopy(dataset['train'].X) dataX = dataX[:, :, :, None] else: dataX = dataset['train'].X net = NeuralNetClassifier( model, max_epochs=global_vars.get('max_epochs'), lr=1e-3, # Shuffle training data on each epoch iterator_train__shuffle=True, ) net.fit(dataX, dataset['train'].y) return net
def test_reduce_lr_monitor_max( self, classifier_data, classifier_module, mode, score): X, y = classifier_data net = NeuralNetClassifier( classifier_module, callbacks=[ ('scheduler', LRScheduler( ReduceLROnPlateau, monitor='train_loss', mode=mode)), ], max_epochs=1, ) net.fit(X, y) policy = dict(net.callbacks_)['scheduler'].lr_scheduler_ assert policy.best == score
def _DDTClassifier(self): if self.dataset == 'ttt': depth = 13 enc = OneHotEncoder(categories='auto', sparse=False) enc = enc.fit(self.x) self.feat_lbl = enc.get_feature_names(self.feat_lbl).tolist() self.x = enc.transform(self.x).astype(np.float32) elif self.dataset == 'cancer': depth = 8 elif self.dataset == 'cesarean': depth = 11 enc = OneHotEncoder( categories='auto', sparse=False, ) enc = enc.fit(self.x) self.feat_lbl = enc.get_feature_names(self.feat_lbl).tolist() self.x = enc.transform(self.x).astype(np.float32) else: raise ValueError('[ERROR] Invalid Dataset.') n_output = len(self.target_lbl) n_input = self.x.shape[1] module = lambda: FDDTN(depth=depth, n_input=n_input, n_output=n_output, continuous=False, labels=self.feat_lbl, param_initer=lambda *x: 0.5 * torch.ones(*x), action_labels=self.target_lbl) NeuralNetClassifier.train_step_single = train_step_single_monkey_patch NeuralNetClassifier.validation_step = validation_step_monkey_patch NeuralNetClassifier._default_callbacks = property( callbacks_monkey_patch) self.classifier = NeuralNetClassifier( module=module, criterion=nn.CrossEntropyLoss, optimizer=Adam, train_split=CVSplit(cv=0.3), # callbacks=[('EarlyStopping', EarlyStopping(patience=20, # threshold=1e-6, # threshold_mode='abs'))], lr=1e-2, max_epochs=600, batch_size=256, device='cuda') self.classifier.encoder = self.enc self._reset_classifier() return self.classifier
def test_grid_search_with_dict_works(self, sldict_cls, data): from sklearn.model_selection import GridSearchCV from skorch import NeuralNetClassifier from torch import nn class MyClassifier(nn.Module): """Simple classification module.""" def __init__(self, num_units=10, nonlin=nn.functional.relu): super(MyClassifier, self).__init__() self.dense0 = nn.Linear(20, num_units) self.nonlin = nonlin self.dropout = nn.Dropout(0.5) self.dense1 = nn.Linear(num_units, 10) self.output = nn.Linear(10, 2) # pylint: disable=arguments-differ def forward(self, X): X = self.nonlin(self.dense0(X)) X = self.dropout(X) X = self.nonlin(self.dense1(X)) X = nn.functional.softmax(self.output(X), dim=-1) return X net = NeuralNetClassifier(MyClassifier) X, y = data X = sldict_cls(X=X) params = { 'lr': [0.01, 0.02], 'max_epochs': [10, 20], } gs = GridSearchCV(net, params, refit=True, cv=3, scoring='accuracy') gs.fit(X, y) print(gs.best_score_, gs.best_params_)
def grid_search_pipeline_training(self): # Through a grid search, the optimal hyperparameters are found # A pipeline is used in order to scale and train the neural net # The grid search module from scikit-learn wraps the pipeline # The Neural Net is instantiated, none hyperparameter is provided nn = NeuralNetClassifier(NeuralNet, verbose=0, train_split=False) # The pipeline is instantiated, it wraps scaling and training phase pipeline = Pipeline([('scale', StandardScaler()), ('nn', nn)]) # The parameters for the grid search are defined # It must be used the prefix "nn__" when setting hyperparamters for the training phase # It must be used the prefix "nn__module__" when setting hyperparameters for the Neural Net params = { 'nn__max_epochs': [10, 20], 'nn__lr': [0.1, 0.01], 'nn__module__num_units': [5, 10], 'nn__module__dropout': [0.1, 0.5], 'nn__optimizer': [optim.Adam, optim.SGD, optim.RMSprop] } # The grid search module is instantiated gs = GridSearchCV(pipeline, params, refit=False, cv=3, scoring='balanced_accuracy', verbose=1) # Initialize grid search gs.fit(self.x, self.y) pass
def classifier_training(crispr_model_classifier, X, y_binary, cv_splitter): split_iter = [ls for ls in cv_splitter] net_classifer = NeuralNetClassifier(crispr_model_classifier, optimizer=torch.optim.Adam, lr=config.start_lr, optimizer__weight_decay=config.lr_decay, optimizer__betas=(0.9, 0.98), optimizer__eps=1e-9, batch_size=config.batch_size, max_epochs=config.n_epochs, device=device2) net_classifer = RandomForestClassifier(n_estimators = 30) cv_results = cross_validate(net_classifer, X, y_binary, scoring=['roc_auc', 'average_precision'], cv=split_iter, return_estimator=True, verbose=0) new_cv_splitter = iter(split_iter) results_dfs = [] last_train = 0 for i in range(5): cur_train, cur_test = next(new_cv_splitter) if i == 0: last_train = cur_train y_true = y_binary[cur_test] y_pred = cv_results['estimator'][i].predict_proba(X[cur_test, :])[:, 1] result_df = pd.DataFrame({'ground_truth': y_true, 'prediction': y_pred, 'fold': i}) results_dfs.append(result_df) results_df = pd.concat(results_dfs, ignore_index=True) results_df.to_csv(config.test_prediction, index=False, mode='a+') logger.debug("{0!r}".format(cv_results['test_roc_auc'])) logger.debug("{0!r}".format(cv_results['test_average_precision'])) logger.debug("{0!r}".format(cv_results.keys())) return cv_results['estimator'][0], last_train
def train(): X, y = make_classification(1000, 20, n_informative=10, random_state=0) X = X.astype(np.float32) y = y.astype(np.int64) module = make_classifier(input_units=20) net = NeuralNetClassifier( module, max_epochs=10, lr=0.1, callbacks=[TriggerKeyError(), PrintMemory()], device='cuda', ) return net.fit(X, y)
def test_reduce_lr_monitor_passes_monitored_loss( self, classifier_data, classifier_module, mode): X, y = classifier_data net = NeuralNetClassifier( classifier_module, callbacks=[ ('scheduler', LRScheduler( ReduceLROnPlateau, monitor='valid_loss', mode=mode)), ], max_epochs=1, ) net.fit(X, y) expected = net.history_[-1, "valid_loss"] policy = dict(net.callbacks_)['scheduler'].lr_scheduler_ assert policy.best == pytest.approx(expected)
def test_reduce_lr_raise_error_when_key_does_not_exist( self, classifier_data, classifier_module): X, y = classifier_data net = NeuralNetClassifier( classifier_module, callbacks=[ ('scheduler', LRScheduler( ReduceLROnPlateau, monitor='bad_key')), ], max_epochs=1, ) msg = ("'bad_key' was not found in history. A Scoring " "callback with name='bad_key' should be placed before the " "LRScheduler callback") with pytest.raises(ValueError, match=msg): net.fit(X, y)
def net(self, module_cls): from skorch import NeuralNetClassifier net = NeuralNetClassifier( module_cls, train_split=None, max_epochs=3, ) return net
def build_estimator(hyperparams, train_data, test=False): device = 'cuda' if torch.cuda.is_available() else 'cpu' # Extract info from training data X, y, *_ = train_data in_features = X.shape[1] n_classes = len(np.unique(y)) n_samples = y.shape[0] bal_weights = torch.from_numpy( n_samples / (n_classes * np.bincount(y))).float().to(device) callbacks = [ ('f1_score_valid', EpochScoring('f1' if n_classes == 2 else 'f1_macro', name='valid_f1', lower_is_better=False)), ('early_stopping', EarlyStopping(monitor='valid_loss', patience=5, lower_is_better=True)), ( 'learning_rate_scheduler', LRScheduler( policy=lr_scheduler.ReduceLROnPlateau, monitor='valid_loss', # Following kargs are passed to the # lr scheduler constructor mode='min', min_lr=1e-5)), ] return NeuralNetClassifier( NNModule, criterion=nn.CrossEntropyLoss, optimizer=torch.optim.SGD, max_epochs=300, iterator_train__shuffle=True, # Shuffle training data on each epoch callbacks=callbacks, device=device, train_split=CVSplit(cv=5, stratified=True, random_state=RANDOM_STATE), lr=hyperparams['lr'], batch_size=hyperparams['batch_size'], module__in_features=in_features, module__n_classes=n_classes, module__n_layers=hyperparams['n_layers'], module__n_neuron_per_layer=hyperparams['n_neuron_per_layer'], module__activation=getattr(F, hyperparams['activation']), module__p_dropout=hyperparams['p_dropout'], criterion__weight=bal_weights if hyperparams['class_weight'] == 'balanced' else None, optimizer__momentum=hyperparams['momentum'], optimizer__weight_decay=hyperparams['weight_decay'], optimizer__nesterov=True, verbose=3, iterator_train__num_workers=4, iterator_valid__num_workers=4)
def predict(model_name, device, save_model, testset, img_path, save_type="csv"): # print("Testing model ", model_name) model, input_size = get_pretrained_models(model_name) model.eval() net = NeuralNetClassifier(model, device=device) net.initialize() # This is important! net.load_params(f_params=save_model) y_preds = net.predict(testset) if save_type == "pickle": SAVE_OUTPUT_NAME = model_name + "_outputs.pkl" with open(os.path.join("outputs", SAVE_OUTPUT_NAME), "wb") as j: pickle.dump({"model_outputs": y_preds}, j) elif save_type == "csv": SAVE_OUTPUT_NAME = model_name + "_outputs.csv" output_df = pd.DataFrame() output_df['path'] = img_path output_df['label'] = testset.labels output_df['output'] = y_preds output_df.to_csv(os.path.join("outputs", SAVE_OUTPUT_NAME), index=False)
def test_lr_callback_steps_correctly( self, classifier_module, classifier_data, policy, kwargs, ): max_epochs = 2 X, y = classifier_data lr_policy = LRScheduler(policy, **kwargs) net = NeuralNetClassifier( classifier_module(), max_epochs=max_epochs, batch_size=16, callbacks=[lr_policy], ) net.fit(X, y) # pylint: disable=protected-access assert lr_policy.lr_scheduler_.last_epoch == max_epochs - 1
def test_lr_callback_init_policies( self, classifier_module, classifier_data, policy, instance, kwargs, ): X, y = classifier_data lr_policy = LRScheduler(policy, **kwargs) net = NeuralNetClassifier(classifier_module, max_epochs=2, callbacks=[lr_policy]) net.fit(X, y) assert any( list( map( lambda x: isinstance(getattr(x[1], 'lr_scheduler_', None), instance), net.callbacks_)))
def test_no_parameter_updates_when_norm_0( self, classifier_module, classifier_data): from copy import deepcopy from skorch import NeuralNetClassifier from skorch.callbacks import GradientNormClipping net = NeuralNetClassifier( classifier_module, callbacks=[('grad_norm', GradientNormClipping(0))], train_split=None, warm_start=True, max_epochs=1, ) net.initialize() params_before = deepcopy(list(net.module_.parameters())) net.fit(*classifier_data) params_after = net.module_.parameters() for p0, p1 in zip(params_before, params_after): p0, p1 = to_numpy(p0), to_numpy(p1) assert np.allclose(p0, p1)
def net_and_mock(self, module, data, train_split, iterator): """Return a NeuralNetClassifier with mocked train and validation step which save the args and kwargs the methods are calld with. """ from skorch import NeuralNetClassifier X, y = data net = NeuralNetClassifier( module, module__input_units=3, max_epochs=1, iterator_train=iterator, iterator_valid=iterator, train_split=train_split ) net.initialize() net.callbacks_ = [] mock = Mock() def decorator(func): def wrapper(*args, **kwargs): mock(*args, **kwargs) func.__dict__['mock_'] = mock return func(*args[1:], **kwargs) return wrapper import types net.get_iterator = types.MethodType(decorator(net.get_iterator), net) return net.partial_fit(X, y), mock