How to use the skorch.NeuralNetClassifier function in skorch

To help you get started, weā€™ve selected a few skorch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github spring-epfl / mia / tests / test_estimators.py View on Github external
def torch_attack_model_fn():
    model = skorch.NeuralNetClassifier(
        module=AttackNet, max_epochs=5, criterion=nn.BCELoss, train_split=None
    )
    return model
github probml / pyprobml / scripts / mnist_skorch.py View on Github external
dropout=0.5,
    ):
        super(ClassifierModule, self).__init__()
        self.dropout = nn.Dropout(dropout)

        self.hidden = nn.Linear(input_dim, hidden_dim)
        self.output = nn.Linear(hidden_dim, output_dim)

    def forward(self, X, **kwargs):
        X = F.relu(self.hidden(X))
        X = self.dropout(X)
        X = F.softmax(self.output(X), dim=-1)
        return X
      
      
net = NeuralNetClassifier(
    ClassifierModule,
    max_epochs=10,
    lr=0.1,
    device=device,
)

time_start = time()
net.fit(X_train, y_train);
print('time spent training {}'.format(time() - time_start))
github fancompute / wavetorch / study / vowel_train_sklearn.py View on Github external
N_classes, cfg['geom']['px'], cfg['geom']['py'], cfg['geom']['pd'], 
                    cfg['geom']['Nx'], cfg['geom']['Ny'], cfg['geom']['pml']['N']
                    )
source = wavetorch.utils.setup_src_coords(
                    cfg['geom']['src_x'], cfg['geom']['src_y'], cfg['geom']['Nx'],
                    cfg['geom']['Ny'], cfg['geom']['pml']['N']
                    )

design_region = torch.zeros(cfg['geom']['Nx'], cfg['geom']['Ny'], dtype=torch.uint8)
design_region[source[0].x.item()+5:probes[0].x.item()-5] = 1

def my_train_split(ds, y):
    return ds, skorch.dataset.Dataset(corpus.valid[:200], y=None)

### Perform training
net = skorch.NeuralNetClassifier(
    module=wavetorch.WaveCell,

    # Training configuration
    max_epochs=cfg['training']['N_epochs'],
    batch_size=cfg['training']['batch_size'],
    lr=cfg['training']['lr'],
    # train_split=skorch.dataset.CVSplit(cfg['training']['N_folds'], stratified=True, random_state=cfg['seed']),
    optimizer=torch.optim.Adam,
    criterion=torch.nn.CrossEntropyLoss,
    callbacks=[
        ClipDesignRegion,
        skorch.callbacks.EpochScoring('accuracy', lower_is_better=False, on_train=True, name='train_acc'),
        skorch.callbacks.Checkpoint(monitor=None, fn_prefix='1234_', dirname='test', f_params="params_{last_epoch[epoch]}.pt", f_optimizer='optimizer.pt', f_history='history.json')
        ],
    callbacks__print_log__keys_ignored=None,
    train_split=None,
github probml / pyprobml / scripts / skorch_demo.py View on Github external
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), dim=-1)
        return X


net = NeuralNetClassifier(
    MyModule,
    max_epochs=3,
    lr=0.1,
)

net.fit(X, y)
y_proba = net.predict_proba(X) # (1000, 2)
github skorch-dev / skorch / examples / benchmarks / mnist.py View on Github external
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)
github modAL-python / modAL / examples / deep_bayesian_active_learning_pytorch.py View on Github external
if __name__ == '__main__':
    # loading the data
    mnist = MNIST('.', download=True, transform=T.ToTensor())
    # defining training data
    labels = np.array([data[1] for data in mnist])
    train_idx = []
    for digit in range(10):
        digit_sampled = np.random.choice(np.where(labels == digit)[0], 10, replace=False)
        train_idx = np.concatenate((train_idx, digit_sampled)).astype(int)

    X_train = np.vstack((mnist[0][0][None, :] for i in train_idx))
    y_train = np.vstack((mnist[i][1] for i in train_idx)).reshape(-1)

    device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

    model = NeuralNetClassifier(SimpleClassifier,
                                module__in_channels=1, module__n_classes=10,
                                criterion=nn.CrossEntropyLoss,
                                device=device)

    learner = ActiveLearner(estimator=model, query_strategy=max_entropy,
                            X_training=X_train, y_training=y_train)

    X_pool = np.vstack((mnist[0][0][None, :] for i in range(100)))
    y_pool = np.vstack((mnist[i][1] for i in range(100))).reshape(-1)

    learner.query(X_pool, n_instances=10)
github modAL-python / modAL / examples / pytorch_integration.py View on Github external
nn.ReLU(),
                                nn.Dropout(0.5),
                                nn.Linear(128,10),
        )

    def forward(self, x):
        out = x
        out = self.convs(out)
        out = out.view(-1,12*12*64)
        out = self.fcs(out)
        return out


# create the classifier
device = "cuda" if torch.cuda.is_available() else "cpu"
classifier = NeuralNetClassifier(Torch_Model,
                                 # max_epochs=100,
                                 criterion=nn.CrossEntropyLoss,
                                 optimizer=torch.optim.Adam,
                                 train_split=None,
                                 verbose=1,
                                 device=device)

"""
Data wrangling
1. Reading data from torchvision
2. Assembling initial training data for ActiveLearner
3. Generating the pool
"""

mnist_data = MNIST('.', download=True, transform=ToTensor())
dataloader = DataLoader(mnist_data, shuffle=True, batch_size=60000)