def test_dataloader(): seed_everything() X = np.random.rand(5, 2) y = np.random.rand(5) x_val = np.random.rand(2, 2) y_val = np.random.rand(2) x_test = np.random.rand(1, 2) y_test = np.random.rand(1) shuffled_X, shuffled_y = sk_shuffle(X, y, random_state=1234) # ----------------------------- # train # ----------------------------- loaders = SklearnDataModule(X=X, y=y, val_split=0.2, test_split=0.2, random_state=1234, drop_last=True) train_loader = loaders.train_dataloader() val_loader = loaders.val_dataloader() test_loader = loaders.test_dataloader() assert np.all(train_loader.dataset.X == shuffled_X[2:]) assert np.all(val_loader.dataset.X == shuffled_X[0]) assert np.all(test_loader.dataset.X == shuffled_X[1]) assert np.all(train_loader.dataset.Y == shuffled_y[2:]) # ----------------------------- # train + val # ----------------------------- loaders = SklearnDataModule(X=X, y=y, x_val=x_val, y_val=y_val, test_split=0.2, random_state=1234, drop_last=True) train_loader = loaders.train_dataloader() val_loader = loaders.val_dataloader() test_loader = loaders.test_dataloader() assert np.all(train_loader.dataset.X == shuffled_X[1:]) assert np.all(val_loader.dataset.X == x_val) assert np.all(test_loader.dataset.X == shuffled_X[0]) # ----------------------------- # train + test # ----------------------------- loaders = SklearnDataModule( X=X, y=y, x_test=x_test, y_test=y_test, val_split=0.2, random_state=1234, drop_last=True ) train_loader = loaders.train_dataloader() val_loader = loaders.val_dataloader() test_loader = loaders.test_dataloader() assert np.all(train_loader.dataset.X == shuffled_X[1:]) assert np.all(val_loader.dataset.X == shuffled_X[0]) assert np.all(test_loader.dataset.X == x_test) # ----------------------------- # train + val + test # ----------------------------- loaders = SklearnDataModule(X, y, x_val, y_val, x_test, y_test, random_state=1234, drop_last=True) train_loader = loaders.train_dataloader() val_loader = loaders.val_dataloader() test_loader = loaders.test_dataloader() assert np.all(train_loader.dataset.X == shuffled_X) assert np.all(val_loader.dataset.X == x_val) assert np.all(test_loader.dataset.X == x_test)
def cli_main(): from warnings import warn pl.seed_everything(1234) # Example: Iris dataset in Sklearn (4 features, 3 class labels) try: from sklearn.datasets import load_iris except ImportError: warn('You want to use `sklearn` which is not installed yet,' # pragma: no-cover ' install it with `pip install sklearn`.') X, y = load_iris(return_X_y=True) loaders = SklearnDataModule(X, y) # args parser = ArgumentParser() parser = LogisticRegression.add_model_specific_args(parser) parser = pl.Trainer.add_argparse_args(parser) args = parser.parse_args() # model # model = LogisticRegression(**vars(args)) model = LogisticRegression(input_dim=4, num_classes=3, l1_strength=0.01, learning_rate=0.01) # train trainer = pl.Trainer.from_argparse_args(args) trainer.fit(model, loaders.train_dataloader(args.batch_size), loaders.val_dataloader(args.batch_size))
def cli_main(): from pl_bolts.datamodules.sklearn_datamodule import SklearnDataModule pl.seed_everything(1234) # create dataset try: from sklearn.datasets import load_boston except ModuleNotFoundError as err: raise ModuleNotFoundError( # pragma: no-cover 'You want to use `sklearn` which is not installed yet, install it with `pip install sklearn`.' ) from err X, y = load_boston(return_X_y=True) # these are numpy arrays loaders = SklearnDataModule(X, y) # args parser = ArgumentParser() parser = LinearRegression.add_model_specific_args(parser) parser = pl.Trainer.add_argparse_args(parser) args = parser.parse_args() # model model = LinearRegression(input_dim=13, l1_strength=1, l2_strength=1) # model = LinearRegression(**vars(args)) # train trainer = pl.Trainer.from_argparse_args(args) trainer.fit(model, loaders.train_dataloader(args.batch_size), loaders.val_dataloader(args.batch_size))
def cli_main(): from pl_bolts.datamodules.sklearn_datamodule import SklearnDataModule from pl_bolts.utils import _SKLEARN_AVAILABLE pl.seed_everything(1234) # create dataset if _SKLEARN_AVAILABLE: from sklearn.datasets import load_diabetes else: # pragma: no cover raise ModuleNotFoundError( 'You want to use `sklearn` which is not installed yet, install it with `pip install sklearn`.' ) # args parser = ArgumentParser() parser = LinearRegression.add_model_specific_args(parser) parser = pl.Trainer.add_argparse_args(parser) args = parser.parse_args() # model model = LinearRegression(input_dim=10, l1_strength=1, l2_strength=1) # model = LinearRegression(**vars(args)) # data X, y = load_diabetes(return_X_y=True) # these are numpy arrays loaders = SklearnDataModule(X, y, batch_size=args.batch_size) # train trainer = pl.Trainer.from_argparse_args(args) trainer.fit(model, train_dataloader=loaders.train_dataloader(), val_dataloaders=loaders.val_dataloader())
def cli_main(): from pl_bolts.datamodules.sklearn_datamodule import SklearnDataModule from pl_bolts.utils import _SKLEARN_AVAILABLE pl.seed_everything(1234) # Example: Iris dataset in Sklearn (4 features, 3 class labels) if _SKLEARN_AVAILABLE: from sklearn.datasets import load_iris else: # pragma: no cover raise ModuleNotFoundError( 'You want to use `sklearn` which is not installed yet, install it with `pip install sklearn`.' ) # args parser = ArgumentParser() parser = LogisticRegression.add_model_specific_args(parser) parser = pl.Trainer.add_argparse_args(parser) args = parser.parse_args() # model # model = LogisticRegression(**vars(args)) model = LogisticRegression(input_dim=4, num_classes=3, l1_strength=0.01, learning_rate=0.01) # data X, y = load_iris(return_X_y=True) loaders = SklearnDataModule(X, y, batch_size=args.batch_size, num_workers=0) # train trainer = pl.Trainer.from_argparse_args(args) trainer.fit(model, train_dataloader=loaders.train_dataloader(), val_dataloaders=loaders.val_dataloader())
def cli_main(): pl.seed_everything(1234) # Example: Iris dataset in Sklearn (4 features, 3 class labels) from sklearn.datasets import load_iris X, y = load_iris(return_X_y=True) loaders = SklearnDataModule(X, y) # args parser = ArgumentParser() parser = LogisticRegression.add_model_specific_args(parser) parser = pl.Trainer.add_argparse_args(parser) args = parser.parse_args() # model # model = LogisticRegression(**vars(args)) model = LogisticRegression(input_dim=4, num_classes=3, l1_strength=0.01, learning_rate=0.01) # train trainer = pl.Trainer.from_argparse_args(args) trainer.fit(model, loaders.train_dataloader(args.batch_size), loaders.val_dataloader(args.batch_size))
def cli_main(): pl.seed_everything(1234) # create dataset from sklearn.datasets import load_boston X, y = load_boston(return_X_y=True) # these are numpy arrays loaders = SklearnDataModule(X, y) # args parser = ArgumentParser() parser = LinearRegression.add_model_specific_args(parser) parser = pl.Trainer.add_argparse_args(parser) args = parser.parse_args() # model model = LinearRegression(input_dim=13, l1_strength=1, l2_strength=1) # model = LinearRegression(**vars(args)) # train trainer = pl.Trainer.from_argparse_args(args) trainer.fit(model, loaders.train_dataloader(args.batch_size), loaders.val_dataloader(args.batch_size))
parser.add_argument('--input_dim', type=int, default=None) parser.add_argument('--num_classes', type=int, default=None) parser.add_argument('--bias', default='store_true') parser.add_argument('--batch_size', type=int, default=16) return parser # todo: covert to CLI func and add test if __name__ == '__main__': # pragma: no cover from argparse import ArgumentParser pl.seed_everything(1234) # Example: Iris dataset in Sklearn (4 features, 3 class labels) from sklearn.datasets import load_iris X, y = load_iris(return_X_y=True) loaders = SklearnDataModule(X, y) # args parser = ArgumentParser() parser = LogisticRegression.add_model_specific_args(parser) parser = pl.Trainer.add_argparse_args(parser) args = parser.parse_args() # model # model = LogisticRegression(**vars(args)) model = LogisticRegression(input_dim=4, num_classes=3, l1_strength=0.01, learning_rate=0.01) # train trainer = pl.Trainer.from_argparse_args(args) trainer.fit(model, loaders.train_dataloader(args.batch_size), loaders.val_dataloader(args.batch_size))