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

[WIP] Distributed NearestNeighbors #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# dask
.lock
4 changes: 4 additions & 0 deletions distributed/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Getting started
1. Install Google Cloud CLI tool https://cloud.google.com/sdk/gcloud
2. Enable Compute Engine API https://console.cloud.google.com/marketplace/product/google/compute.googleapis.com
3. Install RAPIDS https://docs.rapids.ai/deployment/stable/cloud/gcp/gke/
332 changes: 332 additions & 0 deletions distributed/distributed_knn.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,332 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Distributed Nearest Neighbors with cuML"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from dask_cuda import LocalCUDACluster\n",
"from dask.distributed import Client\n",
"\n",
"cluster = LocalCUDACluster()\n",
"client = Client(cluster)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"BlazingContext ready\n"
]
}
],
"source": [
"from blazingsql import BlazingContext\n",
"\n",
"bc = BlazingContext(dask_client=client, network_interface='lo')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import dask_cudf\n",
"\n",
"df = dask_cudf.read_csv('https://github.com/gumdropsteve/datasets/raw/master/iris.csv')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"bc.create_table('iris', df)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# df.tail()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# df.compute().to_pandas().plot(kind='scatter', x='sepal_length', y='petal_width', c='target', cmap=('spring'), sharex=False)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# df.species.compute().unique()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nearest Neighbors\n",
"Nearest Neighbors enables the query of the k-nearest neighbors from a set of input samples."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from cuml.dask.neighbors import NearestNeighbors\n",
"\n",
"\"\"\"Multi-node Multi-GPU NearestNeighbors Model.\"\"\"\n",
"\n",
"nn = NearestNeighbors(client=client)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" <th>sepal_width</th>\n",
" <th>petal_length</th>\n",
" <th>petal_width</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>147</th>\n",
" <td>6.5</td>\n",
" <td>3.0</td>\n",
" <td>5.2</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148</th>\n",
" <td>6.2</td>\n",
" <td>3.4</td>\n",
" <td>5.4</td>\n",
" <td>2.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>149</th>\n",
" <td>5.9</td>\n",
" <td>3.0</td>\n",
" <td>5.1</td>\n",
" <td>1.8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal_length sepal_width petal_length petal_width\n",
"147 6.5 3.0 5.2 2.0\n",
"148 6.2 3.4 5.4 2.3\n",
"149 5.9 3.0 5.1 1.8"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# X = df[['sepal_length', 'sepal_width', 'petal_length', 'petal_width']]\n",
"X = bc.sql('select sepal_length, sepal_width, petal_length, petal_width from iris')\n",
"\n",
"X.tail(3)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<cuml.dask.neighbors.nearest_neighbors.NearestNeighbors at 0x7fdbc80549d0>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# pass features and labels into model\n",
"nn.fit(X)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"NearestNeighbors returns a tuple of distances and indices.\n",
"\n",
"distances: cuDF DataFrame or numpy ndarray\n",
" The distances of the k-nearest neighbors for each column vector\n",
" in X\n",
"\n",
"indices: cuDF DataFrame of numpy ndarray\n",
" The indices of the k-nearest neighbors for each column vector in X"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"distances, indicies = nn.kneighbors(X, n_neighbors=3)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" <th>2</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>147</th>\n",
" <td>0.0</td>\n",
" <td>0.223631</td>\n",
" <td>0.346403</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148</th>\n",
" <td>0.0</td>\n",
" <td>0.244975</td>\n",
" <td>0.300019</td>\n",
" </tr>\n",
" <tr>\n",
" <th>149</th>\n",
" <td>0.0</td>\n",
" <td>0.282832</td>\n",
" <td>0.316213</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 0 1 2\n",
"147 0.0 0.223631 0.346403\n",
"148 0.0 0.244975 0.300019\n",
"149 0.0 0.282832 0.316213"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"distances.tail(3)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# indicies.tail(3)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}