Skip to content

Commit d3066fb

Browse files
add tutorial
1 parent a61fd22 commit d3066fb

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,82 @@ Efficient and Friendly Sparse Matrix Library for TensorFlow
44
tf_sparse is designed to provide sparse matrix operations for [tf_geometric](https://github.com/CrawlScript/tf_geometric), which is an efficient and friendly Graph Neural Network (GNN) library.
55

66

7+
## Tutorial
8+
9+
```python
10+
# coding=utf-8
11+
import os
12+
13+
# Enable GPU 0
14+
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
15+
16+
import tf_sparse as tfs
17+
import tensorflow as tf
18+
import numpy as np
19+
20+
# ==================================== SparseMatrix Creation ====================================
21+
22+
# create a SparseMatrix with index, value, and shape
23+
x = tfs.SparseMatrix(
24+
index=[[0, 0, 1, 3],
25+
[1, 2, 2, 1]],
26+
value=[0.9, 0.8, 0.1, 0.2],
27+
shape=[4, 4]
28+
)
29+
30+
print(x)
31+
32+
# create an identity SparseMatrix
33+
print(tfs.eye(5))
34+
35+
# create a diagonal SparseMatrix
36+
print(tfs.SparseMatrix.from_diagonals([0.1, 0.5, 0.2, 0.4]))
37+
38+
# ==================================== Operations ====================================
39+
40+
41+
# SparseMatrix * Scalar
42+
print(x * 5.0)
43+
print(5.0 * x)
44+
45+
# row/column-level softmax (empty elements are not considered)
46+
print(x.segment_softmax(axis=-1))
47+
48+
# Applying dropout operation on SparseMatrix
49+
print(x.dropout(0.5, training=True))
50+
51+
# convert a SparseMatrix into a dense Tensor
52+
tf_dense_x = x.to_dense()
53+
54+
# convert a SparseMatrix into tf.sparse.SparseTensor
55+
tf_sparse_x = x.to_sparse_tensor()
56+
print(tf_sparse_x)
57+
58+
# create a SparseMatrix with a tf.sparse.SparseTensor
59+
print(tfs.SparseMatrix.from_sparse_tensor(tf_sparse_x))
60+
61+
# create a dense matrix
62+
dense_a = np.random.randn(4, 4).astype(np.float32)
63+
64+
# SparseMatrix @ DenseMatrix (@ denotes matrix multiplication)
65+
print(x @ dense_a)
66+
67+
# DenseMatrix @ SparseMatrix (@ denotes matrix multiplication)
68+
print(dense_a.T @ x.transpose())
69+
70+
# create another SparseMatrix
71+
y = tfs.SparseMatrix(
72+
index=[[1, 2, 2, 3],
73+
[0, 1, 3, 0]],
74+
value=[0.1, 0.4, 0.5, 0.0],
75+
shape=[4, 4]
76+
)
77+
78+
print(y)
79+
80+
# Element-wise SparseMatrix addition
81+
print(x + y)
82+
83+
# Element-wise SparseMatrix subtraction
84+
print(x - y)
85+
```

tutorial_intro.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# coding=utf-8
2+
import os
3+
4+
# Enable GPU 0
5+
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
6+
7+
import tf_sparse as tfs
8+
import tensorflow as tf
9+
import numpy as np
10+
11+
# ==================================== SparseMatrix Creation ====================================
12+
13+
# create a SparseMatrix with index, value, and shape
14+
x = tfs.SparseMatrix(
15+
index=[[0, 0, 1, 3],
16+
[1, 2, 2, 1]],
17+
value=[0.9, 0.8, 0.1, 0.2],
18+
shape=[4, 4]
19+
)
20+
21+
print(x)
22+
23+
# create an identity SparseMatrix
24+
print(tfs.eye(5))
25+
26+
# create a diagonal SparseMatrix
27+
print(tfs.SparseMatrix.from_diagonals([0.1, 0.5, 0.2, 0.4]))
28+
29+
# ==================================== Operations ====================================
30+
31+
32+
# SparseMatrix * Scalar
33+
print(x * 5.0)
34+
print(5.0 * x)
35+
36+
# row/column-level softmax (empty elements are not considered)
37+
print(x.segment_softmax(axis=-1))
38+
39+
# Applying dropout operation on SparseMatrix
40+
print(x.dropout(0.5, training=True))
41+
42+
# convert a SparseMatrix into a dense Tensor
43+
tf_dense_x = x.to_dense()
44+
45+
# convert a SparseMatrix into tf.sparse.SparseTensor
46+
tf_sparse_x = x.to_sparse_tensor()
47+
print(tf_sparse_x)
48+
49+
# create a SparseMatrix with a tf.sparse.SparseTensor
50+
print(tfs.SparseMatrix.from_sparse_tensor(tf_sparse_x))
51+
52+
# create a dense matrix
53+
dense_a = np.random.randn(4, 4).astype(np.float32)
54+
55+
# SparseMatrix @ DenseMatrix (@ denotes matrix multiplication)
56+
print(x @ dense_a)
57+
58+
# DenseMatrix @ SparseMatrix (@ denotes matrix multiplication)
59+
print(dense_a.T @ x.transpose())
60+
61+
# create another SparseMatrix
62+
y = tfs.SparseMatrix(
63+
index=[[1, 2, 2, 3],
64+
[0, 1, 3, 0]],
65+
value=[0.1, 0.4, 0.5, 0.0],
66+
shape=[4, 4]
67+
)
68+
69+
print(y)
70+
71+
# Element-wise SparseMatrix addition
72+
print(x + y)
73+
74+
# Element-wise SparseMatrix subtraction
75+
print(x - y)

0 commit comments

Comments
 (0)