Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 3e6d808
  • /
  • examples
  • /
  • sparse_parafac.ipynb
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:f4b5eb359d5ac3a4a1af83ebc10bfc0b69885ee4
directory badge
swh:1:dir:861931d15798d78a4b1354cb873598a68ff024f8

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
sparse_parafac.ipynb
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "import tensorly as tl\n",
    "tl.set_backend('pytorch')\n",
    "\n",
    "from tensorly.decomposition import parafac\n",
    "from tensorly.cp_tensor import cp_to_tensor, CPTensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate tensor\n",
    "rank = 70\n",
    "t_shape = (10,  20, 30)\n",
    "\n",
    "factors = [torch.randn(l, rank) for l in t_shape]\n",
    "weights = tl.ones(rank)\n",
    "\n",
    "t = cp_to_tensor(CPTensor((weights, factors)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "approx error: 114.89116668701172, non-zero elements in approx: 4200\n"
     ]
    }
   ],
   "source": [
    "# Parafac\n",
    "r = rank\n",
    "\n",
    "cp = parafac(t, r, cvg_criterion = 'rec_error')\n",
    "approx = cp_to_tensor(cp)\n",
    "\n",
    "approx_error = tl.norm(t - approx)\n",
    "nonzero_elems_count = sum(t_shape)*r\n",
    "\n",
    "print('approx error: {}, non-zero elements in approx: {}'.format(approx_error,\\\n",
    "                                                                 nonzero_elems_count))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "approx error: 112.28209686279297, non-zero elements in approx: 4200.0\n"
     ]
    }
   ],
   "source": [
    "# Sparse parafac\n",
    "r = rank-5\n",
    "sparsity = .05\n",
    "\n",
    "cp, sparse = parafac(t, r, sparsity=sparsity, cvg_criterion = 'rec_error')\n",
    "approx_sp = cp_to_tensor(cp) + sparse\n",
    "\n",
    "approx_error = tl.norm(t - approx_sp)\n",
    "nonzero_elems_count = sum(t_shape)*r + sparsity*np.prod(t_shape)\n",
    "\n",
    "print('approx error: {}, non-zero elements in approx: {}'.format(approx_error,\\\n",
    "                                                                 nonzero_elems_count))"
   ]
  },
  {
   "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API