from torch.utils.data import TensorDataset, DataLoader


if __name__ == "__main__":
    torch.manual_seed(0)
    n, d = 200, 20
    X = torch.randn(n, d)
    true_w = torch.randn(d)
    y = (X @ true_w > 0).long()

    split = int(0.7 * n)
    train_ds = TensorDataset(X[:split], y[:split])
    val_ds = TensorDataset(X[split:], y[split:])
    train_loader = DataLoader(train_ds, batch_size=16, shuffle=True)
    val_loader = DataLoader(val_ds, batch_size=16)

    overfit_model = RegularizedMLP(d, 256, 2, dropout_p=0.0)
    overfit_model, overfit_val = train_with_early_stopping(
        overfit_model, train_loader, val_loader,
        epochs=200, weight_decay=0.0, patience=200,
    )

    regularized_model = RegularizedMLP(d, 256, 2, dropout_p=0.5)
    regularized_model, reg_val = train_with_early_stopping(
        regularized_model, train_loader, val_loader,
        epochs=200, weight_decay=1e-3, patience=15,
    )

    print(f"Overfit model best val loss:      {overfit_val:.4f}")
    print(f"Regularized model best val loss:  {reg_val:.4f}")
