Ejemplo n.º 1
0
def cli_main():
    pl.seed_everything(1234)

    # ------------
    # args
    # ------------
    parser = ArgumentParser()
    parser.add_argument('--batch_size', type=int, default=32)
    parser.add_argument('--hidden_dim', type=int, default=128)
    parser.add_argument('--learning_rate', type=float, default=0.0001)
    parser = pl.Trainer.add_argparse_args(parser)
    args = parser.parse_args()

    # init neptune experiment
    import os
    from utils import init_neptune
    api_key = os.environ['NEPTUNE_API_TOKEN']
    project_name = 'zzragida/examples-mnist'
    experiment_name = 'pytorch-lightning-mnist'
    experiment_tags = ['lightning', 'mnist']

    neptune_logger = init_neptune(args, api_key, project_name, experiment_name, experiment_tags)

    # ------------
    # data
    # ------------
    dataset = MNIST('', train=True, download=True, transform=transforms.ToTensor())
    mnist_test = MNIST('', train=False, download=True, transform=transforms.ToTensor())
    mnist_train, mnist_val = random_split(dataset, [55000, 5000])

    train_loader = DataLoader(mnist_train, batch_size=args.batch_size)
    val_loader = DataLoader(mnist_val, batch_size=args.batch_size)
    test_loader = DataLoader(mnist_test, batch_size=args.batch_size)

    # ------------
    # model
    # ------------
    model = LitClassifier(args.hidden_dim, args.learning_rate)

    # ------------
    # training
    # ------------
    trainer = pl.Trainer.from_argparse_args(args, max_epochs=10, logger=neptune_logger)
    trainer.fit(model, train_loader, val_loader)

    # ------------
    # testing
    # ------------
    trainer.test(test_dataloaders=test_loader)
Ejemplo n.º 2
0
def cli_main():
    pl.seed_everything(1234)

    # ------------
    # args
    # ------------
    parser = ArgumentParser()
    parser.add_argument('--batch_size', default=32, type=int)
    parser.add_argument('--lr', default=0.0002, type=float)
    parser.add_argument('--b1', default=0.5, type=float)
    parser.add_argument('--b2', default=0.999, type=float)
    parser.add_argument('--latent_dim', default=100, type=int)
    parser = pl.Trainer.add_argparse_args(parser)
    args = parser.parse_args()

    # init neptune experiment
    import os
    from utils import init_neptune
    api_key = os.environ['NEPTUNE_API_TOKEN']
    project_name = 'zzragida/examples-lit-gan'
    experiment_name = 'pytorch-lightning-gan'
    experiment_tags = ['lightning', 'gan']

    neptune_logger = init_neptune(args, api_key, project_name, experiment_name,
                                  experiment_tags)

    # ------------
    # model
    # ------------
    model = GAN(args)

    # ------------
    # training
    # ------------
    trainer = pl.Trainer.from_argparse_args(args, logger=neptune_logger)
    trainer.fit(model)

    # ------------
    # testing
    # ------------
    trainer.test()
Ejemplo n.º 3
0
def cli_main():
    pl.seed_everything(1234)

    # ------------
    # args
    # ------------
    parser = ArgumentParser()
    parser = pl.Trainer.add_argparse_args(parser)
    args = parser.parse_args()

    # init neptune experiment
    import os
    from utils import init_neptune
    api_key = os.environ['NEPTUNE_API_TOKEN']
    project_name = 'zzragida/examples-lit-bert'
    experiment_name = 'pytorch-lightning-bert'
    experiment_tags = ['lightning', 'bert']

    neptune_logger = init_neptune(args, api_key, project_name, experiment_name,
                                  experiment_tags)

    # ------------
    # model
    # ------------
    model = BertMNLIFinetuner()

    # ------------
    # training
    # ------------
    trainer = pl.Trainer.from_argparse_args(args, logger=neptune_logger)
    trainer.fit(model)

    # ------------
    # testing
    # ------------
    trainer.test()
Ejemplo n.º 4
0
def cli_main():
    pl.seed_everything(1234)

    # ------------
    # args
    # ------------
    parser = ArgumentParser()
    parser.add_argument('--batch_size', default=32, type=int)
    parser = pl.Trainer.add_argparse_args(parser)
    args = parser.parse_args()

    # init neptune experiment
    import os
    from utils import init_neptune
    api_key = os.environ['NEPTUNE_API_TOKEN']
    project_name = 'zzragida/examples-lit-sklearn'
    experiment_name = 'pytorch-lightning-sklearn'
    experiment_tags = ['lightning', 'sklearn']

    neptune_logger = init_neptune(args, api_key, project_name, experiment_name,
                                  experiment_tags)

    # ------------
    # data
    # ------------
    iris = load_iris()
    df = pd.DataFrame(iris.data, columns=iris.feature_names)
    df['target'] = iris.target

    # train/test split
    Y_col = ['target']
    X_col = [col for col in df.columns if not col in Y_col]
    X_train, X_test, Y_train, Y_test = train_test_split(df[X_col],
                                                        df[Y_col],
                                                        test_size=0.2,
                                                        random_state=0)
    X_train, X_val, Y_train, Y_val = train_test_split(X_train[X_col],
                                                      Y_train[Y_col],
                                                      test_size=0.2,
                                                      random_state=0)

    train_ds = DataFrameDataset(X_train, Y_train)
    val_ds = DataFrameDataset(X_val, Y_val)
    test_ds = DataFrameDataset(X_test, Y_test)

    train_loader = DataLoader(train_ds, batch_size=args.batch_size)
    val_loader = DataLoader(val_ds, batch_size=args.batch_size)
    test_loader = DataLoader(test_ds, batch_size=args.batch_size)

    # ------------
    # model
    # ------------
    from sklearn.ensemble import RandomForestClassifier

    clf = RandomForestClassifier(n_estimators=100, random_state=0)
    clf.fit(X_train, Y_train)
    print(accuracy_score(Y_test, clf.predict(X_test).ravel()))
    model = SklearnClassifier(clf)

    # ------------
    # training
    # ------------
    trainer = pl.Trainer.from_argparse_args(args,
                                            max_epochs=1,
                                            logger=neptune_logger)
    trainer.fit(model, train_loader, val_loader)

    # ------------
    # testing
    # ------------
    trainer.test(test_dataloaders=test_loader)
Ejemplo n.º 5
0
def cli_main():
    pl.seed_everything(1234)

    # ------------
    # args
    # ------------
    parser = ArgumentParser()
    parser.add_argument("--batch_size",
                        type=int,
                        default=16,
                        help="size of the batches")
    parser.add_argument("--lr", type=float, default=1e-2, help="learning rate")
    parser.add_argument("--env",
                        type=str,
                        default="CartPole-v0",
                        help="gym environment tag")
    parser.add_argument("--gamma",
                        type=float,
                        default=0.99,
                        help="discount factor")
    parser.add_argument("--sync_rate",
                        type=int,
                        default=10,
                        help="how many frames do we update the target network")
    parser.add_argument("--replay_size",
                        type=int,
                        default=1000,
                        help="capacity of the replay buffer")
    parser.add_argument(
        "--warm_start_size",
        type=int,
        default=1000,
        help=
        "how many samples do we use to fill our buffer at the start of training"
    )
    parser.add_argument("--eps_last_frame",
                        type=int,
                        default=1000,
                        help="what frame should epsilon stop decaying")
    parser.add_argument("--eps_start",
                        type=float,
                        default=1.0,
                        help="starting value of epsilon")
    parser.add_argument("--eps_end",
                        type=float,
                        default=0.01,
                        help="final value of epsilon")
    parser.add_argument("--episode_length",
                        type=int,
                        default=200,
                        help="max length of an episode")
    parser.add_argument("--max_episode_reward",
                        type=int,
                        default=200,
                        help="max episode reward in the environment")
    parser.add_argument("--warm_start_steps",
                        type=int,
                        default=1000,
                        help="max episode reward in the environment")
    parser = pl.Trainer.add_argparse_args(parser)
    args = parser.parse_args()

    # init neptune experiment
    import os
    from utils import init_neptune
    api_key = os.environ['NEPTUNE_API_TOKEN']
    project_name = 'zzragida/examples-lit-dqn'
    experiment_name = 'pytorch-lightning-dqn'
    experiment_tags = ['lightning', 'dqn']

    neptune_logger = init_neptune(args, api_key, project_name, experiment_name,
                                  experiment_tags)

    # ------------
    # model
    # ------------
    model = DQNLightning(args)

    # ------------
    # training
    # ------------
    trainer = pl.Trainer.from_argparse_args(args,
                                            max_epochs=10,
                                            logger=neptune_logger)
    trainer.fit(model)