{"action":"created","comment":{"url":"https://api.github.com/repos/tensorly/tensorly/pulls/comments/172435519","pull_request_review_id":101448010,"id":172435519,"diff_hunk":"@@ -1,54 +1,87 @@\n import numpy as np\n+\n from .. import backend as T\n from ..random import check_random_state\n from ..base import unfold\n from ..kruskal_tensor import kruskal_to_tensor\n from ..tenalg import khatri_rao\n \n # Author: Jean Kossaifi <jean.kossaifi+tensors@gmail.com>\n+# Author: Chris Swierczewski <csw@amazon.com>\n \n # License: BSD 3 clause\n \n \n-def parafac(tensor, rank, n_iter_max=100, init='svd', tol=10e-7,\n-            random_state=None, verbose=False):\n-    \"\"\"CANDECOMP/PARAFAC decomposition via alternating least squares (ALS)\n+def normalize_factors(factors):\n+    \"\"\"Normalizes factors to unit length and returns factor magnitudes\n \n-        Computes a rank-`rank` decomposition of `tensor` [1]_ such that:\n-        ``tensor = [| factors[0], ..., factors[-1] |]``\n+    Turns ``factors = [|U_1, ... U_n|]`` into ``[weights; |V_1, ... V_n|]``,\n+    where the columns of each `V_k` are normalized to unit Euclidean length\n+    from the columns of `U_k` with the normalizing constants absorbed into\n+    `weights`. In the special case of a symmetric tensor, `weights` holds the\n+    eigenvalues of the tensor.\n+\n+    Parameters\n+    ----------\n+    factors : ndarray list\n+        list of matrices, all with the same number of columns\n+        i.e.::\n+            for u in U:\n+                u[i].shape == (s_i, R)\n+\n+        where `R` is fixed while `s_i` can vary with `i`\n+\n+    Returns\n+    -------\n+    normalized_factors : list of ndarrays\n+        list of matrices with the same shape as `factors`\n+    weights : ndarray\n+        vector of length `R` holding normalizing constants\n+\n+    \"\"\"\n+    # allocate variables for weights, and normalized factors\n+    rank = factors[0].shape[1]\n+    weights = T.ones(rank)\n+    normalized_factors = []\n+\n+    # normalize columns of factor matrices\n+    for factor in factors:\n+        scales = T.norm(factor, axis=0)\n+        weights *= scales\n+        scales_non_zero = T.where(scales==0, T.ones(T.shape(scales)), scales)\n+        normalized_factors.append(factor/scales_non_zero)\n+    return normalized_factors, weights\n+\n+\n+def initialize_factors(tensor, rank, init='svd', random_state=None):\n+    r\"\"\"Initialize factors used in `parafac`.\n+\n+    The type of initialization is set using `init`. If `init == 'random'` then\n+    initialize factor matrices using `random_state`. If `init == 'svd'` then\n+    initialize the `m`th factor matrix using the `rank` left singular vectors\n+    of the `m`th unfolding of the input tensor.\n \n     Parameters\n     ----------\n     tensor : ndarray\n-    rank  : int\n-            number of components\n-    n_iter_max : int\n-                 maximum number of iteration\n+    rank : int\n     init : {'svd', 'random'}, optional\n-    tol : float, optional\n-          tolerance: the algorithm stops when the variation in\n-          the reconstruction error is less than the tolerance\n-    random_state : {None, int, np.random.RandomState}\n-    verbose : int, optional\n-        level of verbosity\n \n     Returns\n     -------\n     factors : ndarray list\n-            list of factors of the CP decomposition\n-            element `i` is of shape (tensor.shape[i], rank)\n+        List of initialized factors of the CP decomposition where element `i`\n+        is of shape (tensor.shape[i], rank)\n \n-    References\n-    ----------\n-    .. [1] T.G.Kolda and B.W.Bader, \"Tensor Decompositions and Applications\",\n-       SIAM REVIEW, vol. 51, n. 3, pp. 455-500, 2009.\n     \"\"\"\n     rng = check_random_state(random_state)\n \n     if init is 'random':\n-        factors = [T.tensor(rng.random_sample((tensor.shape[i], rank)), **T.context(tensor)) for i in range(T.ndim(tensor))]\n+        factors = [T.tensor(rng.random_sample((tensor.shape[i], rank))) for i in range(T.ndim(tensor))]\n+        factors, _ = normalize_factors(factors)\n+        return factors\n \n-    elif init is 'svd':\n+    if init is 'svd':","path":"tensorly/decomposition/candecomp_parafac.py","position":107,"original_position":107,"commit_id":"63a0c873e554519d7cf2e3bb6b758e46e582509a","original_commit_id":"63a0c873e554519d7cf2e3bb6b758e46e582509a","user":{"login":"JeanKossaifi","id":832583,"avatar_url":"https://avatars1.githubusercontent.com/u/832583?v=4","gravatar_id":"","url":"https://api.github.com/users/JeanKossaifi","html_url":"https://github.com/JeanKossaifi","followers_url":"https://api.github.com/users/JeanKossaifi/followers","following_url":"https://api.github.com/users/JeanKossaifi/following{/other_user}","gists_url":"https://api.github.com/users/JeanKossaifi/gists{/gist_id}","starred_url":"https://api.github.com/users/JeanKossaifi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JeanKossaifi/subscriptions","organizations_url":"https://api.github.com/users/JeanKossaifi/orgs","repos_url":"https://api.github.com/users/JeanKossaifi/repos","events_url":"https://api.github.com/users/JeanKossaifi/events{/privacy}","received_events_url":"https://api.github.com/users/JeanKossaifi/received_events","type":"User","site_admin":false},"body":"Why not elif? \r\n(it's the same since we use return inside the statement but might make the logic more immediately obvious?)","created_at":"2018-03-06T08:04:27Z","updated_at":"2018-03-06T08:07:01Z","html_url":"https://github.com/tensorly/tensorly/pull/37#discussion_r172435519","pull_request_url":"https://api.github.com/repos/tensorly/tensorly/pulls/37","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/tensorly/tensorly/pulls/comments/172435519"},"html":{"href":"https://github.com/tensorly/tensorly/pull/37#discussion_r172435519"},"pull_request":{"href":"https://api.github.com/repos/tensorly/tensorly/pulls/37"}}},"pull_request":{"url":"https://api.github.com/repos/tensorly/tensorly/pulls/37","id":169186323,"html_url":"https://github.com/tensorly/tensorly/pull/37","diff_url":"https://github.com/tensorly/tensorly/pull/37.diff","patch_url":"https://github.com/tensorly/tensorly/pull/37.patch","issue_url":"https://api.github.com/repos/tensorly/tensorly/issues/37","number":37,"state":"open","locked":false,"title":"Refactor parafac initialization","user":{"login":"cswiercz","id":915840,"avatar_url":"https://avatars3.githubusercontent.com/u/915840?v=4","gravatar_id":"","url":"https://api.github.com/users/cswiercz","html_url":"https://github.com/cswiercz","followers_url":"https://api.github.com/users/cswiercz/followers","following_url":"https://api.github.com/users/cswiercz/following{/other_user}","gists_url":"https://api.github.com/users/cswiercz/gists{/gist_id}","starred_url":"https://api.github.com/users/cswiercz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cswiercz/subscriptions","organizations_url":"https://api.github.com/users/cswiercz/orgs","repos_url":"https://api.github.com/users/cswiercz/repos","events_url":"https://api.github.com/users/cswiercz/events{/privacy}","received_events_url":"https://api.github.com/users/cswiercz/received_events","type":"User","site_admin":false},"body":"We factor out the factor initialization code from the main parafac algorithm. We\r\ndo this in anticipation of upcoming changes to the main algorithm mentioned in\r\nPR #29. Otherwise, `parafac()` will be too large of a function and, therefore,\r\ndifficult to debug.\r\n\r\nAdded a collection of randomized parafac tests, as well.","created_at":"2018-02-14T18:25:22Z","updated_at":"2018-03-06T08:07:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3a743f29e1d24becd424d6922e8eaf12c11a2357","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/tensorly/tensorly/pulls/37/commits","review_comments_url":"https://api.github.com/repos/tensorly/tensorly/pulls/37/comments","review_comment_url":"https://api.github.com/repos/tensorly/tensorly/pulls/comments{/number}","comments_url":"https://api.github.com/repos/tensorly/tensorly/issues/37/comments","statuses_url":"https://api.github.com/repos/tensorly/tensorly/statuses/63a0c873e554519d7cf2e3bb6b758e46e582509a","head":{"label":"cswiercz:parafac-refactor-initialization","ref":"parafac-refactor-initialization","sha":"63a0c873e554519d7cf2e3bb6b758e46e582509a","user":{"login":"cswiercz","id":915840,"avatar_url":"https://avatars3.githubusercontent.com/u/915840?v=4","gravatar_id":"","url":"https://api.github.com/users/cswiercz","html_url":"https://github.com/cswiercz","followers_url":"https://api.github.com/users/cswiercz/followers","following_url":"https://api.github.com/users/cswiercz/following{/other_user}","gists_url":"https://api.github.com/users/cswiercz/gists{/gist_id}","starred_url":"https://api.github.com/users/cswiercz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cswiercz/subscriptions","organizations_url":"https://api.github.com/users/cswiercz/orgs","repos_url":"https://api.github.com/users/cswiercz/repos","events_url":"https://api.github.com/users/cswiercz/events{/privacy}","received_events_url":"https://api.github.com/users/cswiercz/received_events","type":"User","site_admin":false},"repo":{"id":101897489,"name":"tensorly","full_name":"cswiercz/tensorly","owner":{"login":"cswiercz","id":915840,"avatar_url":"https://avatars3.githubusercontent.com/u/915840?v=4","gravatar_id":"","url":"https://api.github.com/users/cswiercz","html_url":"https://github.com/cswiercz","followers_url":"https://api.github.com/users/cswiercz/followers","following_url":"https://api.github.com/users/cswiercz/following{/other_user}","gists_url":"https://api.github.com/users/cswiercz/gists{/gist_id}","starred_url":"https://api.github.com/users/cswiercz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cswiercz/subscriptions","organizations_url":"https://api.github.com/users/cswiercz/orgs","repos_url":"https://api.github.com/users/cswiercz/repos","events_url":"https://api.github.com/users/cswiercz/events{/privacy}","received_events_url":"https://api.github.com/users/cswiercz/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/cswiercz/tensorly","description":"TensorLy: Tensor Learning in Python.","fork":true,"url":"https://api.github.com/repos/cswiercz/tensorly","forks_url":"https://api.github.com/repos/cswiercz/tensorly/forks","keys_url":"https://api.github.com/repos/cswiercz/tensorly/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cswiercz/tensorly/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cswiercz/tensorly/teams","hooks_url":"https://api.github.com/repos/cswiercz/tensorly/hooks","issue_events_url":"https://api.github.com/repos/cswiercz/tensorly/issues/events{/number}","events_url":"https://api.github.com/repos/cswiercz/tensorly/events","assignees_url":"https://api.github.com/repos/cswiercz/tensorly/assignees{/user}","branches_url":"https://api.github.com/repos/cswiercz/tensorly/branches{/branch}","tags_url":"https://api.github.com/repos/cswiercz/tensorly/tags","blobs_url":"https://api.github.com/repos/cswiercz/tensorly/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cswiercz/tensorly/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cswiercz/tensorly/git/refs{/sha}","trees_url":"https://api.github.com/repos/cswiercz/tensorly/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cswiercz/tensorly/statuses/{sha}","languages_url":"https://api.github.com/repos/cswiercz/tensorly/languages","stargazers_url":"https://api.github.com/repos/cswiercz/tensorly/stargazers","contributors_url":"https://api.github.com/repos/cswiercz/tensorly/contributors","subscribers_url":"https://api.github.com/repos/cswiercz/tensorly/subscribers","subscription_url":"https://api.github.com/repos/cswiercz/tensorly/subscription","commits_url":"https://api.github.com/repos/cswiercz/tensorly/commits{/sha}","git_commits_url":"https://api.github.com/repos/cswiercz/tensorly/git/commits{/sha}","comments_url":"https://api.github.com/repos/cswiercz/tensorly/comments{/number}","issue_comment_url":"https://api.github.com/repos/cswiercz/tensorly/issues/comments{/number}","contents_url":"https://api.github.com/repos/cswiercz/tensorly/contents/{+path}","compare_url":"https://api.github.com/repos/cswiercz/tensorly/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cswiercz/tensorly/merges","archive_url":"https://api.github.com/repos/cswiercz/tensorly/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cswiercz/tensorly/downloads","issues_url":"https://api.github.com/repos/cswiercz/tensorly/issues{/number}","pulls_url":"https://api.github.com/repos/cswiercz/tensorly/pulls{/number}","milestones_url":"https://api.github.com/repos/cswiercz/tensorly/milestones{/number}","notifications_url":"https://api.github.com/repos/cswiercz/tensorly/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cswiercz/tensorly/labels{/name}","releases_url":"https://api.github.com/repos/cswiercz/tensorly/releases{/id}","deployments_url":"https://api.github.com/repos/cswiercz/tensorly/deployments","created_at":"2017-08-30T15:21:12Z","updated_at":"2017-08-30T15:21:14Z","pushed_at":"2018-03-05T20:11:26Z","git_url":"git://github.com/cswiercz/tensorly.git","ssh_url":"git@github.com:cswiercz/tensorly.git","clone_url":"https://github.com/cswiercz/tensorly.git","svn_url":"https://github.com/cswiercz/tensorly","homepage":"https://tensorly.github.io/","size":493,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"tensorly:master","ref":"master","sha":"f41ae3bce45a4cf2f101d610704838d335b1f586","user":{"login":"tensorly","id":22989719,"avatar_url":"https://avatars1.githubusercontent.com/u/22989719?v=4","gravatar_id":"","url":"https://api.github.com/users/tensorly","html_url":"https://github.com/tensorly","followers_url":"https://api.github.com/users/tensorly/followers","following_url":"https://api.github.com/users/tensorly/following{/other_user}","gists_url":"https://api.github.com/users/tensorly/gists{/gist_id}","starred_url":"https://api.github.com/users/tensorly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tensorly/subscriptions","organizations_url":"https://api.github.com/users/tensorly/orgs","repos_url":"https://api.github.com/users/tensorly/repos","events_url":"https://api.github.com/users/tensorly/events{/privacy}","received_events_url":"https://api.github.com/users/tensorly/received_events","type":"Organization","site_admin":false},"repo":{"id":71603727,"name":"tensorly","full_name":"tensorly/tensorly","owner":{"login":"tensorly","id":22989719,"avatar_url":"https://avatars1.githubusercontent.com/u/22989719?v=4","gravatar_id":"","url":"https://api.github.com/users/tensorly","html_url":"https://github.com/tensorly","followers_url":"https://api.github.com/users/tensorly/followers","following_url":"https://api.github.com/users/tensorly/following{/other_user}","gists_url":"https://api.github.com/users/tensorly/gists{/gist_id}","starred_url":"https://api.github.com/users/tensorly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tensorly/subscriptions","organizations_url":"https://api.github.com/users/tensorly/orgs","repos_url":"https://api.github.com/users/tensorly/repos","events_url":"https://api.github.com/users/tensorly/events{/privacy}","received_events_url":"https://api.github.com/users/tensorly/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/tensorly/tensorly","description":"TensorLy: Tensor Learning in Python.","fork":false,"url":"https://api.github.com/repos/tensorly/tensorly","forks_url":"https://api.github.com/repos/tensorly/tensorly/forks","keys_url":"https://api.github.com/repos/tensorly/tensorly/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tensorly/tensorly/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tensorly/tensorly/teams","hooks_url":"https://api.github.com/repos/tensorly/tensorly/hooks","issue_events_url":"https://api.github.com/repos/tensorly/tensorly/issues/events{/number}","events_url":"https://api.github.com/repos/tensorly/tensorly/events","assignees_url":"https://api.github.com/repos/tensorly/tensorly/assignees{/user}","branches_url":"https://api.github.com/repos/tensorly/tensorly/branches{/branch}","tags_url":"https://api.github.com/repos/tensorly/tensorly/tags","blobs_url":"https://api.github.com/repos/tensorly/tensorly/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tensorly/tensorly/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tensorly/tensorly/git/refs{/sha}","trees_url":"https://api.github.com/repos/tensorly/tensorly/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tensorly/tensorly/statuses/{sha}","languages_url":"https://api.github.com/repos/tensorly/tensorly/languages","stargazers_url":"https://api.github.com/repos/tensorly/tensorly/stargazers","contributors_url":"https://api.github.com/repos/tensorly/tensorly/contributors","subscribers_url":"https://api.github.com/repos/tensorly/tensorly/subscribers","subscription_url":"https://api.github.com/repos/tensorly/tensorly/subscription","commits_url":"https://api.github.com/repos/tensorly/tensorly/commits{/sha}","git_commits_url":"https://api.github.com/repos/tensorly/tensorly/git/commits{/sha}","comments_url":"https://api.github.com/repos/tensorly/tensorly/comments{/number}","issue_comment_url":"https://api.github.com/repos/tensorly/tensorly/issues/comments{/number}","contents_url":"https://api.github.com/repos/tensorly/tensorly/contents/{+path}","compare_url":"https://api.github.com/repos/tensorly/tensorly/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tensorly/tensorly/merges","archive_url":"https://api.github.com/repos/tensorly/tensorly/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tensorly/tensorly/downloads","issues_url":"https://api.github.com/repos/tensorly/tensorly/issues{/number}","pulls_url":"https://api.github.com/repos/tensorly/tensorly/pulls{/number}","milestones_url":"https://api.github.com/repos/tensorly/tensorly/milestones{/number}","notifications_url":"https://api.github.com/repos/tensorly/tensorly/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tensorly/tensorly/labels{/name}","releases_url":"https://api.github.com/repos/tensorly/tensorly/releases{/id}","deployments_url":"https://api.github.com/repos/tensorly/tensorly/deployments","created_at":"2016-10-21T23:14:52Z","updated_at":"2018-03-05T21:19:36Z","pushed_at":"2018-03-05T20:11:27Z","git_url":"git://github.com/tensorly/tensorly.git","ssh_url":"git@github.com:tensorly/tensorly.git","clone_url":"https://github.com/tensorly/tensorly.git","svn_url":"https://github.com/tensorly/tensorly","homepage":"https://tensorly.github.io/","size":478,"stargazers_count":280,"watchers_count":280,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":51,"mirror_url":null,"archived":false,"open_issues_count":10,"license":{"key":"other","name":"Other","spdx_id":null,"url":null},"forks":51,"open_issues":10,"watchers":280,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/tensorly/tensorly/pulls/37"},"html":{"href":"https://github.com/tensorly/tensorly/pull/37"},"issue":{"href":"https://api.github.com/repos/tensorly/tensorly/issues/37"},"comments":{"href":"https://api.github.com/repos/tensorly/tensorly/issues/37/comments"},"review_comments":{"href":"https://api.github.com/repos/tensorly/tensorly/pulls/37/comments"},"review_comment":{"href":"https://api.github.com/repos/tensorly/tensorly/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/tensorly/tensorly/pulls/37/commits"},"statuses":{"href":"https://api.github.com/repos/tensorly/tensorly/statuses/63a0c873e554519d7cf2e3bb6b758e46e582509a"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"JeanKossaifi"}}