HyukjinKwon commented on code in PR #41383:
URL: https://github.com/apache/spark/pull/41383#discussion_r1236776295


##########
python/pyspark/mlv2/classification.py:
##########
@@ -0,0 +1,306 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from pyspark.mlv2.base import _PredictorParams
+
+from pyspark.ml.param.shared import HasProbabilityCol
+
+from typing import Any, Union, List, Tuple, Callable
+import numpy as np
+import pandas as pd
+import math
+
+from pyspark.sql import DataFrame
+from pyspark.ml.common import inherit_doc
+from pyspark.ml.torch.distributor import TorchDistributor
+from pyspark.ml.param.shared import (
+    HasMaxIter,
+    HasFitIntercept,
+    HasTol,
+    HasWeightCol,
+    HasSeed,
+    HasNumTrainWorkers,
+    HasBatchSize,
+    HasLearningRate,
+    HasMomentum,
+)
+from pyspark.mlv2.base import Predictor, PredictionModel
+from pyspark.sql.functions import lit, count, countDistinct
+
+import torch
+import torch.nn as torch_nn
+
+
+class _LogisticRegressionParams(
+    _PredictorParams,
+    HasMaxIter,
+    HasFitIntercept,
+    HasTol,
+    HasWeightCol,
+    HasNumTrainWorkers,
+    HasBatchSize,
+    HasLearningRate,
+    HasMomentum,
+    HasProbabilityCol,
+    HasSeed,
+):
+    """
+    Params for :py:class:`LogisticRegression` and 
:py:class:`LogisticRegressionModel`.
+
+    .. versionadded:: 3.0.0
+    """
+
+    pass
+
+
+class _LinearNet(torch_nn.Module):
+    def __init__(self, num_features: int, num_classes: int, bias: bool) -> 
None:
+        super(_LinearNet, self).__init__()
+        output_dim = num_classes
+        self.fc = torch_nn.Linear(num_features, output_dim, bias=bias, 
dtype=torch.float32)
+
+    def forward(self, x: Any) -> Any:
+        output = self.fc(x)
+        return output
+
+
+def _train_logistic_regression_model_worker_fn(
+    num_samples_per_worker: int,
+    num_features: int,
+    batch_size: int,
+    max_iter: int,
+    num_classes: int,
+    learning_rate: float,
+    momentum: float,
+    fit_intercept: bool,
+    seed: int,
+) -> Any:
+    from pyspark.ml.torch.distributor import _get_spark_partition_data_loader
+    from torch.nn.parallel import DistributedDataParallel as DDP
+    import torch.distributed
+    import torch.optim as optim
+
+    # TODO: add a setting seed param.
+    torch.manual_seed(seed)
+
+    # TODO: support training on GPU
+    # TODO: support L1 / L2 regularization
+    torch.distributed.init_process_group("gloo")
+
+    ddp_model = DDP(
+        _LinearNet(num_features=num_features, num_classes=num_classes, 
bias=fit_intercept)
+    )
+
+    loss_fn = torch_nn.CrossEntropyLoss()
+
+    optimizer = optim.SGD(ddp_model.parameters(), lr=learning_rate, 
momentum=momentum)
+    data_loader = _get_spark_partition_data_loader(
+        num_samples_per_worker,
+        batch_size,
+        num_workers=0,
+        prefetch_factor=None,

Review Comment:
   @WeichenXu123 is this code being tested? It cannot be `None`, and throw an 
exception like:
   
   ```
   ValueError: prefetch_factor option could only be specified in 
multiprocessing.let num_workers > 0 to enable multiprocessing.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to