Skip to content

Commit

Permalink
MAINT: refactor some sklearnex examples (uxlfoundation#1948)
Browse files Browse the repository at this point in the history
* MAINT: refactor some sklearnex examples

* fixed random_forest_regressor_dpnp.py
  • Loading branch information
samir-nasibli authored Dec 11, 2024
1 parent 0b74e74 commit 963298b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
19 changes: 17 additions & 2 deletions examples/sklearnex/incremental_pca_dpctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,29 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

# sklearnex IncrementalPCA example for GPU offloading with DPCtl usm ndarray:
# SKLEARNEX_PREVIEW=YES python ./incremental_pca_dpctl.py

import dpctl
import dpctl.tensor as dpt

from sklearnex.preview.decomposition import IncrementalPCA
# Import estimator via sklearnex's patch mechanism from sklearn
from sklearnex import patch_sklearn, sklearn_is_patched

patch_sklearn()

# Function that can validate current state of patching
sklearn_is_patched()

# Import estimator from the patched sklearn namespace.
from sklearn.decomposition import IncrementalPCA

# Or just directly import estimator from sklearnex namespace.
# from sklearnex.preview.decomposition import IncrementalPCA

# We create GPU SyclQueue and then put data to dpctl tensor using
# the queue. It allows us to do computation on GPU.

queue = dpctl.SyclQueue("gpu")

incpca = IncrementalPCA()
Expand Down
26 changes: 20 additions & 6 deletions examples/sklearnex/random_forest_regressor_dpnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,40 @@
# ==============================================================================

# sklearnex RF example for GPU offloading with DPNP ndarray:
# python ./random_forest_regressor_dpnp_batch.py
# python ./random_forest_regressor_dpnp.py

import dpctl
import dpnp
import numpy as np
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split

# Import estimator via sklearnex's patch mechanism from sklearn
from sklearnex import patch_sklearn, sklearn_is_patched

patch_sklearn()

# Function that can validate current state of patching
sklearn_is_patched()

# Import estimator from the patched sklearn namespace.
from sklearn.ensemble import RandomForestRegressor

# Or just directly import estimator from sklearnex namespace.
from sklearnex.ensemble import RandomForestRegressor

sycl_device = "gpu:0"
# We create GPU SyclQueue and then put data to dpctl tensor using
# the queue. It allows us to do computation on GPU.
queue = dpctl.SyclQueue("gpu")

X, y = make_regression(
n_samples=1000, n_features=4, n_informative=2, random_state=0, shuffle=False
)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

dpnp_X_train = dpnp.asarray(X_train, device=sycl_device)
dpnp_y_train = dpnp.asarray(y_train, device=sycl_device)
dpnp_X_test = dpnp.asarray(X_test, device=sycl_device)
dpnp_X_train = dpnp.asarray(X_train, usm_type="device", sycl_queue=queue)
dpnp_y_train = dpnp.asarray(y_train, usm_type="device", sycl_queue=queue)
dpnp_X_test = dpnp.asarray(X_test, usm_type="device", sycl_queue=queue)

rf = RandomForestRegressor(max_depth=2, random_state=0).fit(dpnp_X_train, dpnp_y_train)

Expand Down

0 comments on commit 963298b

Please sign in to comment.