LeetKernels
ProblemsStart solving
Problems

34. Logistic Regression medium

Solve the logistic regression problem on a GPU. Given a feature matrix X of size n_samples×n_features and a binary target vector y of size n_samples (containing only 0s and 1s), compute the coefficient vector β that maximizes the log-likelihood: βmax​i=1∑n​[yi​log(pi​)+(1−yi​)log(1−pi​)] where pi​=σ(XiT​β) and σ(z)=1+e−z1​ is the sigmoid function.

Implementation Requirements

  • External libraries are not permitted
  • The solve function signature must remain unchanged
  • The final coefficients must be stored in the beta vector
  • The target vector y contains only binary values (0 and 1)

Example:

Input:
X (samples × features): ​2.01.03.01.5−1.0−2.0−1.5−3.0​1.02.03.02.5−2.0−1.0−2.5−3.0​​ y: ​11100010​​ Output:
β: [2.26−1.29​]

Constraints

  • 1 ≤ n_samples ≤ 100,000
  • 1 ≤ n_features ≤ 1,000
  • n_samples ≥ n_features
  • -10.0 ≤ values in X ≤ 10.0
  • y contains only binary values: 0 or 1
  • Solutions are tested with absolute tolerance of 1e-2 and relative tolerance of 1e-2
  • Performance is measured with n_features = 8, n_samples = 16

Starter code (CUDA)

#include <cuda_runtime.h>

// X, y, beta are device pointers
extern "C" void solve(const float* X, const float* y, float* beta, int n_samples, int n_features) {}

Solve it on your GPU

Previous: Ordinary Least SquaresNext: Monte Carlo Integration

From LeetGPU, CC BY-NC-ND 4.0.