We are hiring ! See our job offers.
Revision 1295ccb09626f89f20d0c0183d618f96b4833bf1 authored by Jean Kossaifi on 08 May 2018, 21:04:53 UTC, committed by Jean Kossaifi on 08 May 2018, 22:15:23 UTC
1 parent c729db7
tucker_tensor.py
``````"""
Core operations on Tucker tensors.
"""

from .base import unfold, tensor_to_vec
from .tenalg import multi_mode_dot
from .tenalg import kronecker

# Author: Jean Kossaifi <jean.kossaifi+tensors@gmail.com>

# License: BSD 3 clause

def tucker_to_tensor(core, factors, skip_factor=None, transpose_factors=False):
"""Converts the Tucker tensor into a full tensor

Parameters
----------
core : ndarray
core tensor
factors : ndarray list
list of matrices of shape ``(s_i, core.shape[i])``
skip_factor : None or int, optional, default is None
if not None, index of a matrix to skip
Note that in any case, `modes`, if provided, should have a lengh of ``tensor.ndim``
transpose_factors : bool, optional, default is False
if True, the matrices or vectors in in the list are transposed

Returns
-------
2D-array
full tensor of shape ``(factors.shape, ..., factors[-1].shape)``
"""
return multi_mode_dot(core, factors, skip=skip_factor, transpose=transpose_factors)

def tucker_to_unfolded(core, factors, mode=0, skip_factor=None, transpose_factors=False):
"""Converts the Tucker decomposition into an unfolded tensor (i.e. a matrix)

Parameters
----------
core : ndarray
core tensor
factors : ndarray list
list of matrices
mode : None or int list, optional, default is None
skip_factor : None or int, optional, default is None
if not None, index of a matrix to skip
Note that in any case, `modes`, if provided, should have a lengh of ``tensor.ndim``
transpose_factors : bool, optional, default is False
if True, the matrices or vectors in in the list are transposed

Returns
-------
2D-array
unfolded tensor
"""
return unfold(tucker_to_tensor(core, factors, skip_factor=skip_factor, transpose_factors=transpose_factors), mode)

def tucker_to_vec(core, factors, skip_factor=None, transpose_factors=False):
"""Converts a Tucker decomposition into a vectorised tensor

Parameters
----------
core : ndarray
core tensor
factors : ndarray list
list of factor matrices
skip_factor : None or int, optional, default is None
if not None, index of a matrix to skip
Note that in any case, `modes`, if provided, should have a lengh of ``tensor.ndim``
transpose_factors : bool, optional, default is False
if True, the matrices or vectors in in the list are transposed

Returns
-------
1D-array
vectorised tensor

Notes
-----
Mathematically equivalent but much slower,
you can obtain the same result using:

>>> def tucker_to_vec(core, factors):
...     return kronecker(factors).dot(tensor_to_vec(core))
"""
return tensor_to_vec(tucker_to_tensor(core, factors, skip_factor=skip_factor, transpose_factors=transpose_factors))

`````` Computing file changes ...
{"ft_lang_label":"__label__en","ft_lang_prob":0.5744184,"math_prob":0.5039053,"size":2593,"snap":"2022-40-2023-06","text_gpt3_token_len":624,"char_repetition_ratio":0.17844728,"word_repetition_ratio":0.53687316,"special_character_ratio":0.25607404,"punctuation_ratio":0.21145375,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98396385,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-09-30T10:19:33Z\",\"WARC-Record-ID\":\"<urn:uuid:03c0135c-091a-48df-9420-0d68ed3a399b>\",\"Content-Length\":\"33221\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e1a87a55-4f4d-43e7-916b-936249048bf1>\",\"WARC-Concurrent-To\":\"<urn:uuid:e2f94261-ec63-480a-be26-31e78947017f>\",\"WARC-IP-Address\":\"128.93.166.15\",\"WARC-Target-URI\":\"https://archive.softwareheritage.org/browse/revision/1295ccb09626f89f20d0c0183d618f96b4833bf1/?path=tensorly/tucker_tensor.py\",\"WARC-Payload-Digest\":\"sha1:SJF26ZPEZAHFWJAYFR6K5LMOC2XCRJQP\",\"WARC-Block-Digest\":\"sha1:FIMKNHJPCMZJL3A3RS7U56L54XGI7GYX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030335448.34_warc_CC-MAIN-20220930082656-20220930112656-00579.warc.gz\"}"}