Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-45434][ML][CONNECT] LogisticRegression checks the training labels #43246

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions python/pyspark/ml/connect/classification.py
Expand Up @@ -41,7 +41,7 @@
)
from pyspark.ml.connect.base import Predictor, PredictionModel
from pyspark.ml.connect.io_utils import ParamsReadWrite, CoreModelReadWrite
from pyspark.sql.functions import lit, count, countDistinct
from pyspark.sql import functions as sf

import torch
import torch.nn as torch_nn
Expand Down Expand Up @@ -232,18 +232,20 @@ def _fit(self, dataset: Union[DataFrame, pd.DataFrame]) -> "LogisticRegressionMo
num_train_workers
)

# TODO: check label values are in range of [0, num_classes)
num_rows, num_classes = dataset.agg(
count(lit(1)), countDistinct(self.getLabelCol())
num_rows, num_features, classes = dataset.select(
sf.count(sf.lit(1)),
sf.first(sf.array_size(self.getFeaturesCol())),
sf.collect_set(self.getLabelCol()),
).head() # type: ignore[misc]

num_batches_per_worker = math.ceil(num_rows / num_train_workers / batch_size)
num_samples_per_worker = num_batches_per_worker * batch_size

num_features = len(dataset.select(self.getFeaturesCol()).head()[0]) # type: ignore[index]

num_classes = len(classes)
if num_classes < 2:
raise ValueError("Training dataset distinct labels must >= 2.")
if any(c not in range(0, num_classes) for c in classes):
raise ValueError("Training labels must be integers in [0, numClasses).")

num_batches_per_worker = math.ceil(num_rows / num_train_workers / batch_size)
num_samples_per_worker = num_batches_per_worker * batch_size

# TODO: support GPU.
distributor = TorchDistributor(
Expand Down