Unverified Commit 15419685 authored by czkkkkkk's avatar czkkkkkk Committed by GitHub
Browse files

[Tutotrial] Update sparse quickstart tutorial. (#5835)

parent ec428409
......@@ -5,7 +5,8 @@
"colab": {
"provenance": [],
"private_outputs": true,
"toc_visible": true
"toc_visible": true,
"gpuType": "T4"
},
"kernelspec": {
"name": "python3",
......@@ -13,9 +14,7 @@
},
"language_info": {
"name": "python"
},
"gpuClass": "standard",
"accelerator": "GPU"
}
},
"cells": [
{
......@@ -37,14 +36,13 @@
"# Install the required packages.\n",
"\n",
"import os\n",
"# Uncomment following commands to download Pytorch and DGL\n",
"# !pip install torch==2.0.0+cpu torchvision==0.15.1+cpu torchaudio==2.0.1 --index-url https://download.pytorch.org/whl/cpu > /dev/null\n",
"# !pip install dgl==1.1.0 -f https://data.dgl.ai/wheels/repo.html > /dev/null\n",
"import torch\n",
"os.environ['TORCH'] = torch.__version__\n",
"os.environ['DGLBACKEND'] = \"pytorch\"\n",
"\n",
"# Uncomment below to install required packages. If the CUDA version is not 11.8,\n",
"# check the https://www.dgl.ai/pages/start.html to find the supported CUDA\n",
"# version and corresponding command to install DGL.\n",
"#!pip install dgl -f https://data.dgl.ai/wheels/cu118/repo.html > /dev/null\n",
"\n",
"try:\n",
" import dgl.sparse as dglsp\n",
......@@ -372,10 +370,10 @@
{
"cell_type": "markdown",
"source": [
"### Initializing a DGL Diagonal Matrix\n",
"A DGL Diagonal Matrix can be initiate by `dglsp.diag()`.\n",
"### Initializing a DGL Diagonal Sparse Matrix\n",
"A DGL Diagonal Sparse Matrix can be initiate by `dglsp.diag()`.\n",
"\n",
"Identity Matrix is a special type of Diagonal Matrix, in which all the value on the diagonal are 1.0. Use `dglsp.identity()` to initiate a Diagonal Matrix."
"Identity Matrix is a special type of Diagonal Sparse Matrix, in which all the value on the diagonal are 1.0. Use `dglsp.identity()` to initiate a Diagonal Sparse Matrix."
],
"metadata": {
"id": "1CeCoE2Fgl_x"
......@@ -400,40 +398,15 @@
{
"cell_type": "markdown",
"source": [
"### Attributes and methods of a DGL Diagonal Matrix"
],
"metadata": {
"id": "s-JpSHGLhWlm"
}
},
{
"cell_type": "code",
"source": [
"val = torch.tensor([1., 2., 3., 4.])\n",
"D = dglsp.diag(val)\n",
"\n",
"print(f\"Shape of sparse matrix: {D.shape}\")\n",
"print(f\"The number of nonzero elements of sparse matrix: {D.nnz}\")\n",
"print(f\"Datatype of sparse matrix: {D.dtype}\")\n",
"print(f\"Device sparse matrix is stored on: {D.device}\")\n",
"print(f\"Get the values of the nonzero elements: {D.val}\")"
],
"metadata": {
"id": "QMV0u-kQWsWd"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Operations on Sparse Matrix and Diagonal Matrix\n",
"## Operations on Sparse Matrix\n",
"* Elementwise operations\n",
" * `A + B`\n",
" * `A - B`\n",
" * `A * B`\n",
" * `A / B`\n",
" * `A ** scalar`\n",
"* Broadcast operations\n",
" * `sp_<op>_v()`\n",
"* Reduce operations\n",
" * `reduce()`\n",
" * `sum()`\n",
......@@ -443,9 +416,7 @@
"* Matrix transformations\n",
" * `SparseMatrix.transpose()` or `SparseMatrix.T`\n",
" * `SparseMatrix.neg()`\n",
" * `DiagMatrix.transpose()` or `DiagMatrix.T`\n",
" * `DiagMatrix.neg()`\n",
" * `DiagMatrix.inv()`\n",
" * `SparseMatrix.inv()`\n",
"* Matrix multiplication\n",
" * `matmul()`\n",
" * `sddmm()`\n",
......@@ -471,13 +442,7 @@
"source": [
"**add(A, B), equivalent to A + B**\n",
"\n",
"The supported combinations are shown as follows.\n",
"\n",
"A \\\\ B | **DiagMatrix**|**SparseMatrix**|**scalar**\n",
"----------------|---------------|----------------|----------\n",
"**DiagMatrix** |Y |Y |N\n",
"**SparseMatrix**|Y |Y |N\n",
"**scalar** |N |N |N"
"Element-wise addition on two sparse matrices, returning a sparse matrix."
],
"metadata": {
"id": "39YJitpW-K9v"
......@@ -530,13 +495,7 @@
"source": [
"**sub(A, B), equivalent to A - B**\n",
"\n",
"The supported combinations are shown as follows.\n",
"\n",
"A \\\\ B | **DiagMatrix**|**SparseMatrix**|**scalar**\n",
"----------------|---------------|----------------|----------\n",
"**DiagMatrix** |Y |Y |N\n",
"**SparseMatrix**|Y |Y |N\n",
"**scalar** |N |N |N"
"Element-wise substraction on two sparse matrices, returning a sparse matrix."
],
"metadata": {
"id": "i25N0JHUTUX9"
......@@ -592,13 +551,7 @@
"source": [
"**mul(A, B), equivalent to A * B**\n",
"\n",
"The supported combinations are shown as follows.\n",
"\n",
"A \\\\ B | **DiagMatrix**|**SparseMatrix**|**scalar**\n",
"----------------|---------------|----------------|----------\n",
"**DiagMatrix** |Y |N |Y\n",
"**SparseMatrix**|N |N |Y\n",
"**scalar** |Y |Y |N"
"Element-wise multiplication on two sparse matrices or on a sparse matrix and a scalar, returning a sparse matrix."
],
"metadata": {
"id": "bg45jnq8T9EJ"
......@@ -610,20 +563,34 @@
"i = torch.tensor([[1, 1, 2],\n",
" [0, 2, 0]])\n",
"val = torch.tensor([1., 2., 3.])\n",
"A = dglsp.spmatrix(i, val, shape=(3, 3))\n",
"print(\"A:\")\n",
"print(A.to_dense())\n",
"A1 = dglsp.spmatrix(i, val, shape=(3, 3))\n",
"print(\"A1:\")\n",
"print(A1.to_dense())\n",
"\n",
"i = torch.tensor([[0, 1, 2, 2],\n",
" [0, 2, 0, 1]])\n",
"val = torch.tensor([1., 2., 3., 4.])\n",
"A2 = dglsp.spmatrix(i, val, shape=(3, 3))\n",
"\n",
"print(\"A2:\")\n",
"print(A2.to_dense())\n",
"\n",
"print(\"A1 * 3:\")\n",
"print((A1 * 3).to_dense())\n",
"print(\"3 * A1:\")\n",
"print((3 * A1).to_dense())\n",
"\n",
"print(\"A * 3:\")\n",
"print((A * 3).to_dense())\n",
"print(\"3 * A:\")\n",
"print((3 * A).to_dense())\n",
"print(\"A1 * A2\")\n",
"print((A1 * A2).to_dense())\n",
"\n",
"val = torch.tensor([-1., -2., -3.])\n",
"D1 = dglsp.diag(val)\n",
"print(\"D1:\")\n",
"print(D1.to_dense())\n",
"\n",
"print(\"D1 * A2\")\n",
"print((D1 * A2).to_dense())\n",
"\n",
"val = torch.tensor([-4., -5., -6.])\n",
"D2 = dglsp.diag(val)\n",
"print(\"D2:\")\n",
......@@ -648,13 +615,7 @@
"source": [
"**div(A, B), equivalent to A / B**\n",
"\n",
"The supported combinations are shown as follows.\n",
"\n",
"A \\\\ B | **DiagMatrix**|**SparseMatrix**|**scalar**\n",
"----------------|---------------|----------------|----------\n",
"**DiagMatrix** |Y |N |Y\n",
"**SparseMatrix**|N |N |Y\n",
"**scalar** |N |N |N"
"Element-wise multiplication on two sparse matrices or on a sparse matrix and a scalar, returning a sparse matrix. If both `A` and `B` are sparse matrices, both of them must have the same sparsity. And the returned matrix has the same order of non-zero entries as `A`."
],
"metadata": {
"id": "Xb2RU6H4UBCs"
......@@ -666,12 +627,20 @@
"i = torch.tensor([[1, 1, 2],\n",
" [0, 2, 0]])\n",
"val = torch.tensor([1., 2., 3.])\n",
"A = dglsp.spmatrix(i, val, shape=(3, 3))\n",
"print(\"A:\")\n",
"print(A.to_dense())\n",
"A1 = dglsp.spmatrix(i, val, shape=(3, 3))\n",
"print(\"A1:\")\n",
"print(A1.to_dense())\n",
"\n",
"print(\"A / 2:\")\n",
"print((A / 2).to_dense())\n",
"i = torch.tensor([[1, 2, 1],\n",
" [0, 0, 2]])\n",
"val = torch.tensor([1., 3., 2.])\n",
"A2 = dglsp.spmatrix(i, val, shape=(3, 3))\n",
"\n",
"print(\"A1 / 2:\")\n",
"print((A1 / 2).to_dense())\n",
"\n",
"print(\"A1 / A2\")\n",
"print((A1 / A2).to_dense())\n",
"\n",
"val = torch.tensor([-1., -2., -3.])\n",
"D1 = dglsp.diag(val)\n",
......@@ -700,13 +669,7 @@
"source": [
"**power(A, B), equivalent to A \\*\\* B**\n",
"\n",
"The supported combinations are shown as follows.\n",
"\n",
"A \\\\ B | **DiagMatrix**|**SparseMatrix**|**scalar**\n",
"----------------|---------------|----------------|----------\n",
"**DiagMatrix** |N |N |Y\n",
"**SparseMatrix**|N |N |Y\n",
"**scalar** |N |N |N"
"Element-wise power of a sparse matrix and a scalar, returning a sparse matrix."
],
"metadata": {
"id": "2lZbyTYUUgSi"
......@@ -739,6 +702,69 @@
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### *Broadcast operations*"
],
"metadata": {
"id": "VXBz4j5x_wQ4"
}
},
{
"cell_type": "markdown",
"source": [
"**sp_\\<op\\>_v(A, v)**\n",
"\n",
"Broadcast operations on a sparse matrix and a vector, returning a sparse matrix. `v` is broadcasted to the shape of `A` and then the operator is applied on the non-zero values of `A`. `<op>` can be add, sub, mul, and div. \n",
"\n",
"There are two cases regarding the shape of `v`:\n",
"\n",
"1. `v` is a vector of shape `(1, A.shape[1])` or `(A.shape[1])`. In this case, `v` is broadcasted on the row dimension of `A`.\n",
"\n",
"2. `v` is a vector of shape `(A.shape[0], 1)`. In this case, `v` is broadcasted on the column dimension of `A`."
],
"metadata": {
"id": "PtnyZdXHAZ6Z"
}
},
{
"cell_type": "code",
"source": [
"i = torch.tensor([[1, 0, 2], [0, 3, 2]])\n",
"val = torch.tensor([10, 20, 30])\n",
"A = dglsp.spmatrix(i, val, shape=(3, 4))\n",
"\n",
"v1 = torch.tensor([1, 2, 3, 4])\n",
"print(\"A:\")\n",
"print(A.to_dense())\n",
"\n",
"print(\"v1:\")\n",
"print(v1)\n",
"\n",
"print(\"sp_add_v(A, v1)\")\n",
"print(dglsp.sp_add_v(A, v1).to_dense())\n",
"\n",
"v2 = v1.reshape(1, -1)\n",
"print(\"v2:\")\n",
"print(v2)\n",
"\n",
"print(\"sp_add_v(A, v2)\")\n",
"print(dglsp.sp_add_v(A, v2).to_dense())\n",
"\n",
"v3 = torch.tensor([1, 2, 3]).reshape(-1, 1)\n",
"print(\"v3:\")\n",
"print(v3)\n",
"\n",
"print(\"sp_add_v(A, v3)\")\n",
"print(dglsp.sp_add_v(A, v3).to_dense())"
],
"metadata": {
"id": "xxf3s-uWBRR7"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
......@@ -839,37 +865,6 @@
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"*Diagonal Matrix*"
],
"metadata": {
"id": "iE3ANjFolIJu"
}
},
{
"cell_type": "code",
"source": [
"val = torch.tensor([1., 2., 3., 4.])\n",
"D = dglsp.diag(val)\n",
"print(D.to_dense())\n",
"print(\"\")\n",
"\n",
"print(\"Get inverse of diagonal matrix:\")\n",
"print(D.inv().to_dense())\n",
"print(\"\")\n",
"\n",
"print(\"Get a diagonal matrix with the negation of the original nonzero values.\")\n",
"print(D.neg().to_dense())\n",
"print(\"\")"
],
"metadata": {
"id": "j9kjY9RdlGXx"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
......@@ -884,13 +879,7 @@
"source": [
"**matmul(A, B), equivalent to A @ B**\n",
"\n",
"The supported combinations are shown as follows.\n",
"\n",
"A \\\\ B | **Tensor**|**DiagMatrix**|**SparseMatrix**\n",
"----------------|-----------|--------------|----------\n",
"**Tensor** |Y |N |N\n",
"**DiagMatrix** |Y |Y |Y\n",
"**SparseMatrix**|Y |Y |Y"
"Matrix multiplication on sparse matrices and/or dense matrix. There are two cases as follows."
],
"metadata": {
"id": "THWE30v6WpAk"
......@@ -899,7 +888,7 @@
{
"cell_type": "markdown",
"source": [
"**Union[DiagMatrix, SparseMatrix] @ Union[DiagMatrix, SparseMatrix] -> Union[SparseMatrix, DiagMatrix]:**\n",
"**SparseMatrix @ SparseMatrix -> SparseMatrix:**\n",
"\n",
"For a $L \\times M$ sparse matrix A and a $M \\times N$ sparse matrix B, the shape of `A @ B` will be $L \\times N$ sparse matrix."
],
......@@ -955,7 +944,7 @@
{
"cell_type": "markdown",
"source": [
"**Union[DiagMatrix, SparseMatrix] @ Tensor -> Tensor:**\n",
"**SparseMatrix @ Tensor -> Tensor:**\n",
"\n",
"For a $L \\times M$ sparse matrix A and a $M \\times N$ dense matrix B, the shape of `A @ B` will be $L \\times N$ dense matrix."
],
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment