Skip to content

Commit

Permalink
Moved doc to stable and dev.
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanKossaifi committed Mar 10, 2017
1 parent a0f4b11 commit 3137639
Show file tree
Hide file tree
Showing 418 changed files with 44,712 additions and 245 deletions.
Binary file added dev/_downloads/auto_examples_jupyter.zip
Binary file not shown.
Binary file added dev/_downloads/auto_examples_python.zip
Binary file not shown.
54 changes: 54 additions & 0 deletions dev/_downloads/plot_kruskal_regression.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"cells": [
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"%matplotlib inline"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"\nKruskal tensor regression\n=========================\n\nExample on how to use :class:`tensorly.regression.kruskal_regression.KruskalRegressor` to perform tensor regression.\n\n"
]
},
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"import matplotlib.pyplot as plt\nfrom tensorly.base import tensor_to_vec, partial_tensor_to_vec\nfrom tensorly.datasets.synthetic import gen_image\nfrom tensorly.utils import check_random_state\nfrom tensorly.regression.kruskal_regression import KruskalRegressor\n\n# Parameter of the experiment\nimage_height = 25\nimage_width = 25\n# shape of the images\npatterns = ['rectangle', 'swiss', 'circle']\n# ranks to test\nranks = [1, 2, 3, 4, 5]\n\n# Generate random samples\nrng = check_random_state(1)\nX = rng.normal(size=(1000, image_height, image_width), loc=0, scale=1)\n\n\n# Paramters of the plot, deduced from the data\nn_rows = len(patterns)\nn_columns = len(ranks) + 1\n# Plot the three images\nfig = plt.figure()\n\nfor i, pattern in enumerate(patterns):\n\n # Generate the original image\n weight_img = gen_image(region=pattern, image_height=image_height, image_width=image_width)\n\n # Generate the labels\n y = partial_tensor_to_vec(X, skip_begin=1).dot(tensor_to_vec(weight_img))\n\n # Plot the original weights\n ax = fig.add_subplot(n_rows, n_columns, i*n_columns + 1)\n ax.imshow(weight_img, cmap=plt.cm.OrRd, interpolation='nearest')\n ax.set_axis_off()\n if i == 0:\n ax.set_title('Original\\nweights')\n\n for j, rank in enumerate(ranks):\n\n # Create a tensor Regressor estimator\n estimator = KruskalRegressor(weight_rank=rank, tol=10e-7, n_iter_max=100, reg_W=1, verbose=0)\n\n # Fit the estimator to the data\n estimator.fit(X, y)\n\n ax = fig.add_subplot(n_rows, n_columns, i*n_columns + j + 2)\n ax.imshow(estimator.weight_tensor_, cmap=plt.cm.OrRd, interpolation='nearest')\n ax.set_axis_off()\n\n if i == 0:\n ax.set_title('Learned\\nrank = {}'.format(rank))\n\nplt.suptitle(\"Kruskal tensor regression\")\nplt.show()"
]
}
],
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"name": "python3",
"language": "python"
},
"language_info": {
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"version": "3.5.2",
"mimetype": "text/x-python",
"name": "python",
"file_extension": ".py"
}
},
"nbformat": 4
}
File renamed without changes.
108 changes: 108 additions & 0 deletions dev/_downloads/plot_tensor.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"cells": [
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"%matplotlib inline"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"\nBasic tensor operations\n=======================\n\nExample on how to use :mod:`tensorly.base` to perform basic tensor operations.\n\n\n"
]
},
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"import matplotlib.pyplot as plt\nfrom tensorly.base import unfold, fold\nimport numpy as np"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"A tensor is simply a numpy array\n\n"
]
},
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"tensor = np.arange(24).reshape((3, 4, 2))\nprint('* original tensor:\\n{}'.format(tensor))"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"Unfolding a tensor is easy\n\n"
]
},
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"for mode in range(tensor.ndim):\n print('* mode-{} unfolding:\\n{}'.format(mode, unfold(tensor, mode)))"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"Re-folding the tensor is as easy:\n\n"
]
},
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"for mode in range(tensor.ndim):\n unfolding = unfold(tensor, mode)\n folded = fold(unfolding, mode, tensor.shape)\n print(np.all(folded == tensor))"
]
}
],
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"name": "python3",
"language": "python"
},
"language_info": {
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"version": "3.5.2",
"mimetype": "text/x-python",
"name": "python",
"file_extension": ".py"
}
},
"nbformat": 4
}
File renamed without changes.
54 changes: 54 additions & 0 deletions dev/_downloads/plot_tucker_regression.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"cells": [
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"%matplotlib inline"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"\nTucker tensor regression\n========================\n\nExample on how to use :class:`tensorly.regression.tucker_regression.TuckerRegressor` to perform tensor regression.\n\n"
]
},
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"import matplotlib.pyplot as plt\nfrom tensorly.base import tensor_to_vec, partial_tensor_to_vec\nfrom tensorly.datasets.synthetic import gen_image\nfrom tensorly.utils import check_random_state\nfrom tensorly.regression.tucker_regression import TuckerRegressor\n\n# Parameter of the experiment\nimage_height = 25\nimage_width = 25\n# shape of the images\npatterns = ['rectangle', 'swiss', 'circle']\n# ranks to test\nranks = [1, 2, 3, 4, 5]\n\n# Generate random samples\nrng = check_random_state(1)\nX = rng.normal(size=(1000, image_height, image_width), loc=0, scale=1)\n\n\n# Paramters of the plot, deduced from the data\nn_rows = len(patterns)\nn_columns = len(ranks) + 1\n# Plot the three images\nfig = plt.figure()\n\nfor i, pattern in enumerate(patterns):\n\n # Generate the original image\n weight_img = gen_image(region=pattern, image_height=image_height, image_width=image_width)\n\n # Generate the labels\n y = partial_tensor_to_vec(X, skip_begin=1).dot(tensor_to_vec(weight_img))\n\n # Plot the original weights\n ax = fig.add_subplot(n_rows, n_columns, i*n_columns + 1)\n ax.imshow(weight_img, cmap=plt.cm.OrRd, interpolation='nearest')\n ax.set_axis_off()\n if i == 0:\n ax.set_title('Original\\nweights')\n\n for j, rank in enumerate(ranks):\n\n # Create a tensor Regressor estimator\n estimator = TuckerRegressor(weight_ranks=[rank, rank], tol=10e-7, n_iter_max=100, reg_W=1, verbose=0)\n\n # Fit the estimator to the data\n estimator.fit(X, y)\n\n ax = fig.add_subplot(n_rows, n_columns, i*n_columns + j + 2)\n ax.imshow(estimator.weight_tensor_, cmap=plt.cm.OrRd, interpolation='nearest')\n ax.set_axis_off()\n\n if i == 0:\n ax.set_title('Learned\\nrank = {}'.format(rank))\n\nplt.suptitle(\"Tucker tensor regression\")\nplt.show()"
]
}
],
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"name": "python3",
"language": "python"
},
"language_info": {
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"version": "3.5.2",
"mimetype": "text/x-python",
"name": "python",
"file_extension": ".py"
}
},
"nbformat": 4
}
File renamed without changes.
162 changes: 162 additions & 0 deletions dev/_downloads/rpca.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
{
"cells": [
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"%matplotlib inline"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"\nRobust Tensor Robust PCA\n========================\n\nExample on how to use :mod:`tensorly.decomposition.robust_pca` to perform Robust Tensor PCA.\n\n\n"
]
},
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"import matplotlib.pyplot as plt\nfrom tensorly.datasets.yaleb import fetch_cropped_yaleb\n\ndataset_path = '/data/tensorly_data/'\ndata = fetch_cropped_yaleb(dataset_path, zooming=0.3, max_n_subjects=5)"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"Accumulate a tensor containing all the data\n\n"
]
},
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"X = np.concatenate([data[key]['images'] for key in data], axis=0)\nprint(X.shape)"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"Convert to float\n\n"
]
},
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"X = X.astype(np.float64)\nX -= X.mean()"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"Visualise the data\n\n"
]
},
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"def visualise_images(X, n_images, n_columns, randomise=True):\n indices = np.arange(X.shape[0])\n np.random.shuffle(indices)\n indices = indices[:n_images]\n cmap = plt.cm.Greys_r\n n_rows = np.ceil(n_images / n_columns)\n fig = plt.figure(figsize=(2*n_columns, 2*n_rows))\n fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)\n\n # plot the digits: each image is 8x8 pixels\n for i, e in enumerate(indices):\n ax = fig.add_subplot(n_rows, n_columns, i + 1, xticks=[], yticks=[])\n ax.imshow(X[e], cmap=cmap, interpolation='nearest')\n\nvisualise_images(X, 12, 4)\nplt.show()"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"Add noise\n\n"
]
},
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"from tensorly.random.noise import add_noise\n\nX = add_noise(X, noise='salt_pepper', percent=0.15, inplace=True, random_state=random_state)\nvisualise_images(X, 12, 4)\nplt.show()"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"Apply robust pca\n\n"
]
},
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"from tensorly.decomposition import robust_pca\nlow_rank_part, sparse_part = robust_pca(X, reg_E=0.04, learning_rate=1.2, n_iter_max=20)"
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"Check the results\n\n"
]
},
{
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"cell_type": "code",
"source": [
"def visualise_rpca(X, low_rank_part, sparse_part, n_images=10):\n \"\"\"A little helper function to visualise the result of tensor RPCA\n \"\"\"\n indices = np.arange(X.shape[0])\n np.random.shuffle(indices)\n indices = indices[:n_images]\n\n fig = plt.figure(figsize=(10, 2*n_images))\n fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)\n\n # plot the digits: each image is 8x8 pixels\n for i, e in enumerate(indices):\n cmap = plt.cm.Greys_r\n\n ax = fig.add_subplot(n_images, 4, 4*i + 1, xticks=[], yticks=[])\n ax.imshow(X[e], cmap=cmap, interpolation='nearest')\n if not i:\n ax.set_title('Original')\n\n ax = fig.add_subplot(n_images, 4, 4*i + 2, xticks=[], yticks=[])\n ax.imshow(low_rank_part[e], cmap=cmap, interpolation='nearest')\n if not i:\n ax.set_title('Low-rank')\n\n ax = fig.add_subplot(n_images, 4, 4*i + 3, xticks=[], yticks=[])\n ax.imshow(sparse_part[e], cmap=cmap, interpolation='nearest')\n if not i:\n ax.set_title('Sparse')\n\n ax = fig.add_subplot(n_images, 4, 4*i + 4, xticks=[], yticks=[])\n ax.imshow(low_rank_part[e]+sparse_part[e], cmap=cmap, interpolation='nearest')\n if not i:\n ax.set_title('Reconstruction')\n\n return fig\n\nvisualise_rpca(X, low_rank_part, sparse_part, 6)\nplt.show()"
]
}
],
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"name": "python3",
"language": "python"
},
"language_info": {
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"version": "3.5.2",
"mimetype": "text/x-python",
"name": "python",
"file_extension": ".py"
}
},
"nbformat": 4
}
Loading

0 comments on commit 3137639

Please sign in to comment.