tensortools

A very simple and barebones tensor decomposition library for CP decomposition a.k.a. PARAFAC a.k.a. TCA

https://github.com/ahwillia/tensortools

Science Score: 23.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
  • .zenodo.json file
  • DOI references
    Found 14 DOI reference(s) in README
  • Academic publication links
  • Committers with academic emails
    1 of 5 committers (20.0%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.1%) to scientific vocabulary

Keywords from Contributors

dimensionality-reduction
Last synced: 11 months ago · JSON representation

Repository

A very simple and barebones tensor decomposition library for CP decomposition a.k.a. PARAFAC a.k.a. TCA

Basic Info
  • Host: GitHub
  • Owner: ahwillia
  • License: mit
  • Default Branch: main
  • Homepage:
  • Size: 4.19 MB
Statistics
  • Stars: 36
  • Watchers: 2
  • Forks: 12
  • Open Issues: 0
  • Releases: 0
Fork of neurostatslab/tensortools
Created over 4 years ago · Last pushed almost 3 years ago
Metadata Files
Readme License

README.md

Tensortools

TensorTools is a bare bones Python package for fitting and visualizing canonical polyadic (CP) tensor decompositions of higher-order data arrays. I originally developed this library for applications in neuroscience (Williams et al., 2018), but the code could be helpful in other domains.

Installation

From the command line run:

pip install git+https://github.com/ahwillia/tensortools

(You will need to have git installed for this command to work.)

Alternatively you can download the source code and install locally by running:

git clone https://github.com/ahwillia/tensortools cd tensortools pip3 install -e .

Quick Start

Here's how to perform a parameter sweep over 1 - 9 components, and plot the reconstruction error and similarity diagnostics as a function of the model rank (these diagnostics are described in Williams et al., 2018). The snippet also uses plot_factors(...) to plot the factors extracted by one of the models in the ensemble.

The method "ncp_hals" fits a nonnegative tensor decomposition, other methods are "ncp_bcd" (also nonnegative) and "cp_als" (unconstrained decomposition). See the tensortools/optimize/ folder for the implementation of these algorithms.

```python import tensortools as tt

data = # ... specify a numpy array holding the tensor you wish to fit

Fit an ensemble of models, 4 random replicates / optimization runs per model rank

ensemble = tt.Ensemble(fitmethod="ncphals") ensemble.fit(data, ranks=range(1, 9), replicates=4)

fig, axes = plt.subplots(1, 2) tt.plotobjective(ensemble, ax=axes[0]) # plot reconstruction error as a function of num components. tt.plotsimilarity(ensemble, ax=axes[1]) # plot model similarity as a function of num components. fig.tight_layout()

Plot the low-d factors for an example model, e.g. rank-2, first optimization run / replicate.

numcomponents = 2 replicate = 0 tt.plotfactors(ensemble.factors(num_components)[replicate]) # plot the low-d factors

plt.show() ```

Check out the scripts in the examples/ folder for other short demos.

Time-Shifted Tensor Decompositions

This repo contains a module tensortools.cpwarp which allows for time-shifted tensor decompositions of 3d-arrays. The motivation behind this model and some of its implementaional details are laid out in the following set of notes.

Alex H. Williams (2020). Combining tensor decomposition and time warping models for multi-neuronal spike train analysis. bioRxiv. 2020.03.02.974014

A very similar model was previously proposed by Mørup et al. (2008). Also see Sorokin et al. (2020) for an application of this model to neural data. To fit this model, check out the script in examples/shift_cpd.py, which should reproduce Figure 4 from the Williams (2020) paper.

The important function to call is fit_shifted_cp(), like below:

```python fitshiftedcp( data, rank, boundary="wrap", nrestarts=5, maxshiftaxis0=0.1, maxshiftaxis1=None, miniter=10, maxiter=100, unonneg=True, v_nonneg=True, tol=1e-4, patience=5, mask=None, )

""" Fits a time-shifted CP decomposition to 3d-array data. The model parameters are three factor matrices {u, v, w} and two sets of shift parameters {us, vs}.

u.shape == (rank, data.shape[0]) v.shape == (rank, data.shape[1]) w.shape == (rank, data.shape[2]) us.shape == (rank, data.shape[0]) vs.shape == (rank, data.shape[1])

The element data[i, j, k] is approximated by:

sumr ( u[r, i] * v[r, j] * w[r, t + us[r, i] + v_s[r, i]] )

Note that if the shift parameters are zero (us == vs == 0), this is the typical CP tensor decomposition.

Parameters

data : ndarray Data tensor. rank : int Number of components. initu : ndarray or None Initial guess for factor matrix u. initv : ndarray or None Initial guess for factor matrix v. initw : ndarray or None Initial guess for factor matrix w. maxshiftaxis0 : float or None Maximum absolute value for us, expressed as a fraction on the interval (0, 0.5]. If None, then all us shifts are set to zero. maxshiftaxis1 : float Maximum absolute value for vs, expressed as a fraction on the interval (0, 0.5]. If None, then all vs shifts are set to zero. unonneg : bool If True, the factor matrix u is constrained to be nonnegative. vnonneg : bool If True, the factor matrix v is constrained to be nonnegative. boundary : str If equal to "wrap" the shifting along axis=2 has a periodic boundary condition. Otherwise the behavior is similar to "edge" mode in the numpy.pad() function. miniter : int Minimum number of iterations before stopping. max_iter : int Maximum number of iterations before giving up. tol : float Convergence tolerance patience : int Number of iterations to wait between convergence checks. mask : ndarray of booleans or None Specifies missing data, and can be used for cross-validation. """ ```

I hope to upload a more detailed tutorial soon; until then, please refer to the papers cited above and reach out to me by email if you are interested in further details.

Citation

If you found this resource useful, please consider citing this paper.

@ARTICLE{Williams2018, title = "Unsupervised Discovery of Demixed, {Low-Dimensional} Neural Dynamics across Multiple Timescales through Tensor Component Analysis", author = "Williams, Alex H and Kim, Tony Hyun and Wang, Forea and Vyas, Saurabh and Ryu, Stephen I and Shenoy, Krishna V and Schnitzer, Mark and Kolda, Tamara G and Ganguli, Surya", journal = "Neuron", volume = 98, number = 6, pages = "1099--1115.e8", month = jun, year = 2018, }

Owner

  • Name: Alex Williams
  • Login: ahwillia
  • Kind: user
  • Location: New York, NY
  • Company: NYU / Flatiron Institute

Asst Prof (NYU Center for Neural Science) and Project Leader (Flatiron Institute Center for Computational Neuroscience)

GitHub Events

Total
  • Watch event: 6
Last Year
  • Watch event: 6

Committers

Last synced: over 3 years ago

All Time
  • Total Commits: 299
  • Total Committers: 5
  • Avg Commits per committer: 59.8
  • Development Distribution Score (DDS): 0.247
Top Committers
Name Email Commits
Alex Williams a****a@g****m 225
Ben Erichson e****n@u****u 38
Jean KOSSAIFI j****i@g****m 34
Ben Erichson 1****n@u****m 1
Alexander Williams a****s@c****l 1
Committer Domains (Top 20 + Academic)
uw.edu: 1

Issues and Pull Requests

Last synced: 11 months ago

All Time
  • Total issues: 0
  • Total pull requests: 1
  • Average time to close issues: N/A
  • Average time to close pull requests: about 18 hours
  • Total issue authors: 0
  • Total pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 0.0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
  • dyballa (1)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 106 last-month
  • Total docker downloads: 8
  • Total dependent packages: 0
  • Total dependent repositories: 1
  • Total versions: 2
  • Total maintainers: 2
pypi.org: tensortools

Tools for Tensor Decomposition.

  • Versions: 2
  • Dependent Packages: 0
  • Dependent Repositories: 1
  • Downloads: 106 Last month
  • Docker Downloads: 8
Rankings
Docker downloads count: 4.2%
Dependent packages count: 10.0%
Forks count: 10.5%
Average: 13.0%
Stargazers count: 13.3%
Downloads: 18.4%
Dependent repos count: 21.7%
Maintainers (2)
Last synced: 11 months ago