{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CP-APR: Alternating Poisson Regression for fitting CP to sparse count data\n", "\n", "```\n", "Copyright 2025 National Technology & Engineering Solutions of Sandia,\n", "LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the\n", "U.S. Government retains certain rights in this software.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Set up a sample problem\n", "We follow the general procedure outlined by E. C. Chi and T. G. Kolda, On Tensors, Sparsity, and Nonnegative Factorizations, arXiv:1112.2414 [math.NA], December 2011 (http://arxiv.org/abs/1112.2414)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import sys\n", "import pyttb as ttb\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Pick the shape and rank\n", "sz = (10, 8, 6)\n", "R = 5\n", "\n", "# Generate factor matrices with a few large entries in each column\n", "# this will be the basis of our solution.\n", "np.random.seed(0) # Set seed for reproducibility\n", "A = []\n", "for n in range(len(sz)):\n", " A.append(np.random.uniform(size=(sz[n], R)))\n", " for r in range(R):\n", " p = np.random.permutation(sz[n])\n", " nbig = round((1 / R) * sz[n])\n", " A[-1][p[0:nbig], r] *= 100\n", "weights = np.random.uniform(size=(R,))\n", "S = ttb.ktensor(A, weights)\n", "S.normalize(sort=True, normtype=1)\n", "\n", "X = S.to_tensor()\n", "X.data = np.floor(np.abs(X.data))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Call CP-APR" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Compute a solution\n", "short_tutorial = 2 # Cut off solve early for demo\n", "M = ttb.cp_apr(X, R, printitn=10, stoptime=short_tutorial);" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 2 }