Last active
August 6, 2020 15:41
-
-
Save alberduris/0ec97f16ddd5265ab162d810050f557d to your computer and use it in GitHub Desktop.
[Sparse Tensors on Pytorch & Tensorflow] #Others #JupyterNotebook #CodeSnippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Sparse Tensors\n", | |
"\n", | |
"## Pytorch\n", | |
"\n", | |
"Docs: https://pytorch.org/docs/stable/sparse.html" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import torch" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Sparse matrix:\n", | |
"tensor(indices=tensor([[0, 1, 0, 2, 2, 3],\n", | |
" [0, 0, 1, 0, 1, 0]]),\n", | |
" values=tensor([1., 2., 3., 4., 5., 6.]),\n", | |
" size=(4, 2), nnz=6, layout=torch.sparse_coo)\n", | |
"\n", | |
"To Dense:\n", | |
"tensor([[1., 3.],\n", | |
" [2., 0.],\n", | |
" [4., 5.],\n", | |
" [6., 0.]])\n" | |
] | |
} | |
], | |
"source": [ | |
"i = torch.LongTensor([[0, 1, 0, 2, 2, 3, ],\n", | |
" [0, 0, 1, 0, 1, 0, ]]) # indices tensor\n", | |
"\n", | |
"v = torch.FloatTensor([1, 2, 3, 4, 5, 6]) # values values\n", | |
"\n", | |
"foo = torch.sparse.FloatTensor(i, v, torch.Size([4,2])) # Sparse tensor w/ values @ indices\n", | |
"\n", | |
"print('Sparse matrix:\\n{}\\n\\nTo Dense:\\n{}'.format(foo, foo.to_dense()))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"tensor([[1., 1., 1., 1., 1.],\n", | |
" [1., 1., 1., 1., 1.]])" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"bar = torch.ones(size=(2,5))\n", | |
"bar" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"tensor([[4., 4., 4., 4., 4.],\n", | |
" [2., 2., 2., 2., 2.],\n", | |
" [9., 9., 9., 9., 9.],\n", | |
" [6., 6., 6., 6., 6.]])" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"torch.sparse.mm(foo, bar)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Tensorflow\n", | |
"\n", | |
"Docs: https://www.tensorflow.org/api_docs/python/tf/sparse/SparseTensor" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import tensorflow as tf" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"values = tf.Variable([1., 2., 3., 4., 5., 6.])\n", | |
"foo = tf.sparse.SparseTensor(indices=[[0, 0], [1, 0], [0, 1], [2, 0], [2, 1], [3, 0]], values=values, dense_shape=[4, 2])\n", | |
"foo_dense = tf.sparse.to_dense(foo, validate_indices=False)\n", | |
"\n", | |
"bar = tf.ones(shape=(2,5))\n", | |
"\n", | |
"mult = tf.sparse.matmul(foo, bar)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"SparseTensorValue(indices=array([[0, 0],\n", | |
" [1, 0],\n", | |
" [0, 1],\n", | |
" [2, 0],\n", | |
" [2, 1],\n", | |
" [3, 0]], dtype=int64), values=array([1., 2., 3., 4., 5., 6.], dtype=float32), dense_shape=array([4, 2], dtype=int64))\n", | |
"[[1. 1. 1. 1. 1.]\n", | |
" [1. 1. 1. 1. 1.]]\n" | |
] | |
} | |
], | |
"source": [ | |
"with tf.Session() as sess:\n", | |
" tf.global_variables_initializer().run()\n", | |
" print(foo.eval())\n", | |
" print(bar.eval())\n", | |
" foo_dense_res, mult_res = sess.run([foo_dense, mult])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[1., 3.],\n", | |
" [2., 0.],\n", | |
" [4., 5.],\n", | |
" [6., 0.]], dtype=float32)" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"foo_dense_res" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[4., 4., 4., 4., 4.],\n", | |
" [2., 2., 2., 2., 2.],\n", | |
" [9., 9., 9., 9., 9.],\n", | |
" [6., 6., 6., 6., 6.]], dtype=float32)" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"mult_res" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.6.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment