Exemple #1
0
def predict_mnist():
    dls = read_data_loaders()
    lr = 1.

    learn = Learner(dls=dls,
                    model=torch.nn.Linear(28 * 28, 1),
                    opt_func=SGD,
                    loss_func=mnist_loss,
                    metrics=batch_accuracy)
    learn.fit(10, lr=lr)
    def objective(trial: optuna.trial.Trial) -> float:
        model = nn.Sequential(nn.Linear(20, 1), nn.Sigmoid())
        learn = Learner(
            data,
            model,
            loss_func=F.nll_loss,
            metrics=[accuracy],
        )
        learn.fit(1, cbs=FastAIV2PruningCallback(trial))

        return 1.0
Exemple #3
0
# %%
learner = Learner(
    mnist_dls,
    ConvNet(),
    loss_func=F.nll_loss,
    metrics=[accuracy, Precision(average="macro"), Recall(average="macro")],
)

# %%[markdown]
#
# These are too many epochs, but we want to see the behavior of the net when it
# is trained for some time.

# %%
learner.fit(n_epoch=20)

# %%
pprint(list(learner.metrics))

# %%
model = learner.model
x, y = mnist_dls.valid.one_batch()
model, type(x), x.shape, type(y), y.shape

# %%
with torch.no_grad():
    pred_proba = model(x)

# %%
pred_proba.shape
Exemple #4
0
print(summary(mlp_model, torch.zeros((1, 1, 28, 28)).to(device), show_input=True))
print(summary(mlp_model, torch.zeros((1, 1, 28, 28)).to(device), show_input=False))

# %%
mlp_learner = Learner(
    mnist_dls,
    mlp_model,
    metrics=[accuracy, Precision(average="macro"), Recall(average="macro")],
)

# %%
mlp_learner.lr_find()

# %%
with mlp_learner.no_bar():
    mlp_learner.fit(n_epoch=4)

# %%
conv_model = nn.Sequential(
    nn.Conv2d(1, 10, kernel_size=5, stride=(2, 2)),
    nn.ReLU(),
    nn.Conv2d(10, 20, kernel_size=5, stride=(2, 2)),
    nn.ReLU(),
    nn.Flatten(),
    nn.Linear(320, 60),
    nn.ReLU(),
    nn.Linear(60, 10),
    nn.LogSoftmax(dim=1),
).to(device)

# %%