From ad755bde320bab8600d2c07a9f99578304068d27 Mon Sep 17 00:00:00 2001 From: alexmul1114 <96952477+alexmul1114@users.noreply.github.com> Date: Mon, 2 Oct 2023 19:17:00 -0400 Subject: [PATCH 01/18] Add LossFunctions.jl to environment --- Project.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Project.toml b/Project.toml index 3d5ca18..d2c7fcf 100644 --- a/Project.toml +++ b/Project.toml @@ -7,8 +7,10 @@ version = "0.1.0" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" LBFGSB = "5be7bae1-8223-5378-bac3-9e7378a2f6e6" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +LossFunctions = "30fc2ffe-d236-52d8-8643-a9d8f7c094a7" [compat] ForwardDiff = "0.10.36" LBFGSB = "0.4.1" +LossFunctions = "0.11.1" julia = "1.6" From ad90c2d393a37dbc39f1418a30a77b5badf1bb4a Mon Sep 17 00:00:00 2001 From: alexmul1114 <96952477+alexmul1114@users.noreply.github.com> Date: Mon, 2 Oct 2023 20:12:51 -0400 Subject: [PATCH 02/18] Add compatibility with LossFunctions.jl and test --- Project.toml | 2 +- src/GCPDecompositions.jl | 1 + src/gcp-opt.jl | 2 ++ test/Project.toml | 2 ++ test/items/gcp-opt.jl | 4 ++++ 5 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index d2c7fcf..28ec04a 100644 --- a/Project.toml +++ b/Project.toml @@ -12,5 +12,5 @@ LossFunctions = "30fc2ffe-d236-52d8-8643-a9d8f7c094a7" [compat] ForwardDiff = "0.10.36" LBFGSB = "0.4.1" -LossFunctions = "0.11.1" +LossFunctions = "0.10.1" julia = "1.6" diff --git a/src/GCPDecompositions.jl b/src/GCPDecompositions.jl index 065e67d..9751f4a 100644 --- a/src/GCPDecompositions.jl +++ b/src/GCPDecompositions.jl @@ -6,6 +6,7 @@ import Base: require_one_based_indexing import Base: ndims, size, show, summary import Base: getindex import ForwardDiff +import LossFunctions using LinearAlgebra: mul!, rmul!, Diagonal using LBFGSB: lbfgsb diff --git a/src/gcp-opt.jl b/src/gcp-opt.jl index 22efbb1..968895f 100644 --- a/src/gcp-opt.jl +++ b/src/gcp-opt.jl @@ -17,6 +17,8 @@ with respect to a general loss and return a `CPD` object. gcp(X::Array, r, func=(x, m) -> (m - x)^2, grad=(x, m) -> ForwardDiff.derivative(m -> func(x, m), m), lower=-Inf) = _gcp(X, r, func, grad, lower, (;)) +gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower=-Inf) = _gcp(X, r, (x, m) -> loss(x,m), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) + function _gcp(X::Array{TX,N}, r, func, grad, lower, lbfgsopts) where {TX,N} T = nonmissingtype(TX) diff --git a/test/Project.toml b/test/Project.toml index 2857e4c..938c789 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,9 +1,11 @@ [deps] +LossFunctions = "30fc2ffe-d236-52d8-8643-a9d8f7c094a7" OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a" [compat] +LossFunctions = "0.10.1" OffsetArrays = "1.12.7" TestItemRunner = "0.2.1" diff --git a/test/items/gcp-opt.jl b/test/items/gcp-opt.jl index 11a43cc..c41ede4 100644 --- a/test/items/gcp-opt.jl +++ b/test/items/gcp-opt.jl @@ -2,6 +2,7 @@ @testitem "least squares" begin using Random + using LossFunctions @testset "size(X)=$sz, rank(X)=$r" for sz in [(15, 20, 25), (30, 40, 50)], r in 1:2 Random.seed!(0) @@ -17,5 +18,8 @@ Mh = gcp(X, r) # test default (least-squares) loss @test maximum(I -> abs(Mh[I] - X[I]), CartesianIndices(X)) <= 1e-5 + + Mh = gcp(X, r, L2DistLoss()) # test with loss function from LossFunctions.jl (least squares) + @test maximum(I -> abs(Mh[I] - X[I]), CartesianIndices(X)) <= 1e-5 end end From 1593236d925ef322f54c65197f0ef336f736a457 Mon Sep 17 00:00:00 2001 From: alexmul1114 <96952477+alexmul1114@users.noreply.github.com> Date: Tue, 3 Oct 2023 08:49:06 -0400 Subject: [PATCH 03/18] Change LossFunctions required version --- Project.toml | 2 +- test/Project.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 28ec04a..d2c7fcf 100644 --- a/Project.toml +++ b/Project.toml @@ -12,5 +12,5 @@ LossFunctions = "30fc2ffe-d236-52d8-8643-a9d8f7c094a7" [compat] ForwardDiff = "0.10.36" LBFGSB = "0.4.1" -LossFunctions = "0.10.1" +LossFunctions = "0.11.1" julia = "1.6" diff --git a/test/Project.toml b/test/Project.toml index 938c789..b74a5dc 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -6,6 +6,6 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a" [compat] -LossFunctions = "0.10.1" +LossFunctions = "0.11.1" OffsetArrays = "1.12.7" TestItemRunner = "0.2.1" From 55af2bb6cb2c3ee06a35e49b09d790cd093304b3 Mon Sep 17 00:00:00 2001 From: David Hong Date: Wed, 4 Oct 2023 16:38:25 -0400 Subject: [PATCH 04/18] Format --- src/gcp-opt.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/gcp-opt.jl b/src/gcp-opt.jl index 968895f..657fcfb 100644 --- a/src/gcp-opt.jl +++ b/src/gcp-opt.jl @@ -17,7 +17,8 @@ with respect to a general loss and return a `CPD` object. gcp(X::Array, r, func=(x, m) -> (m - x)^2, grad=(x, m) -> ForwardDiff.derivative(m -> func(x, m), m), lower=-Inf) = _gcp(X, r, func, grad, lower, (;)) -gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower=-Inf) = _gcp(X, r, (x, m) -> loss(x,m), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) +gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower=-Inf) = + _gcp(X, r, (x, m) -> loss(x, m), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) function _gcp(X::Array{TX,N}, r, func, grad, lower, lbfgsopts) where {TX,N} T = nonmissingtype(TX) From de7bb0c5af4ae9e86f80a34c5d9f1544895fd816 Mon Sep 17 00:00:00 2001 From: David Hong Date: Wed, 4 Oct 2023 16:40:19 -0400 Subject: [PATCH 05/18] Add docstring --- src/gcp-opt.jl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/gcp-opt.jl b/src/gcp-opt.jl index 657fcfb..5891757 100644 --- a/src/gcp-opt.jl +++ b/src/gcp-opt.jl @@ -17,6 +17,18 @@ with respect to a general loss and return a `CPD` object. gcp(X::Array, r, func=(x, m) -> (m - x)^2, grad=(x, m) -> ForwardDiff.derivative(m -> func(x, m), m), lower=-Inf) = _gcp(X, r, func, grad, lower, (;)) +""" + gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower]) -> CPD + +Compute an approximate rank-`r` CP decomposition of the tensor `X` +with respect to the loss function `loss` and return a `CPD` object. + +# Inputs ++ `X` : multi-dimensional tensor/array to approximate/decompose ++ `r` : number of components for the CPD ++ `loss` : loss function from LossFunctions.jl ++ `lower` : lower bound for factor matrix entries, `default = -Inf` +""" gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower=-Inf) = _gcp(X, r, (x, m) -> loss(x, m), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) From cb91409838a26438137009606ae046a5bca9eb53 Mon Sep 17 00:00:00 2001 From: alexmul1114 <96952477+alexmul1114@users.noreply.github.com> Date: Mon, 9 Oct 2023 13:22:28 -0400 Subject: [PATCH 06/18] Add extensions functionality for LossFunctions --- Project.toml | 6 ++++++ ext/LossFunctionsExt.jl | 8 ++++++++ src/GCPDecompositions.jl | 5 ++++- src/gcp-opt.jl | 4 ++-- 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 ext/LossFunctionsExt.jl diff --git a/Project.toml b/Project.toml index d2c7fcf..b640e2d 100644 --- a/Project.toml +++ b/Project.toml @@ -3,6 +3,9 @@ uuid = "f59fb95b-1bc8-443b-b347-5e445a549f37" authors = ["David Hong and contributors"] version = "0.1.0" +[weakdeps] +LossFunctions = "30fc2ffe-d236-52d8-8643-a9d8f7c094a7" + [deps] ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" LBFGSB = "5be7bae1-8223-5378-bac3-9e7378a2f6e6" @@ -14,3 +17,6 @@ ForwardDiff = "0.10.36" LBFGSB = "0.4.1" LossFunctions = "0.11.1" julia = "1.6" + +[extensions] +LossFunctionsExt = "LossFunctions" \ No newline at end of file diff --git a/ext/LossFunctionsExt.jl b/ext/LossFunctionsExt.jl new file mode 100644 index 0000000..b1e10e2 --- /dev/null +++ b/ext/LossFunctionsExt.jl @@ -0,0 +1,8 @@ +module LossFunctinonsExt + +using GCPDecompositions + +gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower=-Inf) = + _gcp(X, r, (x, m) -> loss(x, m), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) + +end \ No newline at end of file diff --git a/src/GCPDecompositions.jl b/src/GCPDecompositions.jl index 9751f4a..209136e 100644 --- a/src/GCPDecompositions.jl +++ b/src/GCPDecompositions.jl @@ -6,7 +6,6 @@ import Base: require_one_based_indexing import Base: ndims, size, show, summary import Base: getindex import ForwardDiff -import LossFunctions using LinearAlgebra: mul!, rmul!, Diagonal using LBFGSB: lbfgsb @@ -18,4 +17,8 @@ export gcp include("type-cpd.jl") include("gcp-opt.jl") +if !isdefined(Base, :get_extension) + include("ext/LossFunctinonsExt.jl") + end + end diff --git a/src/gcp-opt.jl b/src/gcp-opt.jl index 5891757..a55d2b1 100644 --- a/src/gcp-opt.jl +++ b/src/gcp-opt.jl @@ -29,8 +29,8 @@ with respect to the loss function `loss` and return a `CPD` object. + `loss` : loss function from LossFunctions.jl + `lower` : lower bound for factor matrix entries, `default = -Inf` """ -gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower=-Inf) = - _gcp(X, r, (x, m) -> loss(x, m), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) +#gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower=-Inf) = +# _gcp(X, r, (x, m) -> loss(x, m), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) function _gcp(X::Array{TX,N}, r, func, grad, lower, lbfgsopts) where {TX,N} T = nonmissingtype(TX) From f7b11c7c24a526d8e238ad226b895002af86f822 Mon Sep 17 00:00:00 2001 From: alexmul1114 <96952477+alexmul1114@users.noreply.github.com> Date: Mon, 9 Oct 2023 13:26:36 -0400 Subject: [PATCH 07/18] Fix extension name --- src/GCPDecompositions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GCPDecompositions.jl b/src/GCPDecompositions.jl index 209136e..60dfc0f 100644 --- a/src/GCPDecompositions.jl +++ b/src/GCPDecompositions.jl @@ -18,7 +18,7 @@ include("type-cpd.jl") include("gcp-opt.jl") if !isdefined(Base, :get_extension) - include("ext/LossFunctinonsExt.jl") + include("../ext/LossFunctionsExt.jl") end end From de298d6116b983f69ee299e0c4dcce90309bcb0e Mon Sep 17 00:00:00 2001 From: alexmul1114 <96952477+alexmul1114@users.noreply.github.com> Date: Mon, 9 Oct 2023 14:19:22 -0400 Subject: [PATCH 08/18] Add LossFunctions under extras --- Project.toml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index b640e2d..bf9faea 100644 --- a/Project.toml +++ b/Project.toml @@ -19,4 +19,7 @@ LossFunctions = "0.11.1" julia = "1.6" [extensions] -LossFunctionsExt = "LossFunctions" \ No newline at end of file +LossFunctionsExt = "LossFunctions" + +[extras] +LossFunctions = "30fc2ffe-d236-52d8-8643-a9d8f7c094a7" \ No newline at end of file From 243c9d6ed7954773b1a7647e063281703254926e Mon Sep 17 00:00:00 2001 From: David Hong Date: Mon, 9 Oct 2023 15:17:20 -0400 Subject: [PATCH 09/18] Fix precompile error --- ext/LossFunctionsExt.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/ext/LossFunctionsExt.jl b/ext/LossFunctionsExt.jl index b1e10e2..a5b1d53 100644 --- a/ext/LossFunctionsExt.jl +++ b/ext/LossFunctionsExt.jl @@ -1,6 +1,7 @@ module LossFunctinonsExt using GCPDecompositions +using LossFunctions gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower=-Inf) = _gcp(X, r, (x, m) -> loss(x, m), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) From 5735a2730edcfbfa7994224c2f5e681e70260eca Mon Sep 17 00:00:00 2001 From: David Hong Date: Mon, 9 Oct 2023 15:22:22 -0400 Subject: [PATCH 10/18] Drop LossFunctions.jl from extras --- Project.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/Project.toml b/Project.toml index bf9faea..ed932b2 100644 --- a/Project.toml +++ b/Project.toml @@ -20,6 +20,3 @@ julia = "1.6" [extensions] LossFunctionsExt = "LossFunctions" - -[extras] -LossFunctions = "30fc2ffe-d236-52d8-8643-a9d8f7c094a7" \ No newline at end of file From 0529a798fc6588f5896ccdc6ec2d399832bd9d02 Mon Sep 17 00:00:00 2001 From: David Hong Date: Mon, 9 Oct 2023 15:30:56 -0400 Subject: [PATCH 11/18] Update test manifest --- test/Manifest.toml | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/test/Manifest.toml b/test/Manifest.toml index 923e9cf..bad76a5 100644 --- a/test/Manifest.toml +++ b/test/Manifest.toml @@ -2,7 +2,7 @@ julia_version = "1.8.2" manifest_format = "2.0" -project_hash = "3b0347c951b114bc9938540028de5bc8d1fc29c5" +project_hash = "5b46ab02fc1fd08dc4db50c5af3027ccd5dc095b" [[deps.Adapt]] deps = ["LinearAlgebra"] @@ -39,6 +39,12 @@ uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" [[deps.Logging]] uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" +[[deps.LossFunctions]] +deps = ["Markdown", "Requires", "Statistics"] +git-tree-sha1 = "df9da07efb9b05ca7ef701acec891ee8f73c99e2" +uuid = "30fc2ffe-d236-52d8-8643-a9d8f7c094a7" +version = "0.11.1" + [[deps.Markdown]] deps = ["Base64"] uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" @@ -62,6 +68,12 @@ uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" deps = ["SHA", "Serialization"] uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +[[deps.Requires]] +deps = ["UUIDs"] +git-tree-sha1 = "838a3a4188e2ded87a4f9f184b4b0d78a1e91cb7" +uuid = "ae029012-a4dd-5104-9daa-d747884805df" +version = "1.3.0" + [[deps.SHA]] uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" version = "0.7.0" @@ -69,6 +81,14 @@ version = "0.7.0" [[deps.Serialization]] uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" +[[deps.SparseArrays]] +deps = ["LinearAlgebra", "Random"] +uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" + +[[deps.Statistics]] +deps = ["LinearAlgebra", "SparseArrays"] +uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" + [[deps.TOML]] deps = ["Dates"] uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76" @@ -89,6 +109,10 @@ git-tree-sha1 = "8621ba2637b49748e2dc43ba3d84340be2938022" uuid = "1c621080-faea-4a02-84b6-bbd5e436b8fe" version = "0.1.1" +[[deps.UUIDs]] +deps = ["Random", "SHA"] +uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" + [[deps.Unicode]] uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" From 7ba208834eb5443ec39f718794b27f325c439b20 Mon Sep 17 00:00:00 2001 From: David Hong Date: Mon, 9 Oct 2023 16:44:29 -0400 Subject: [PATCH 12/18] Formatting --- Project.toml | 6 +++--- src/GCPDecompositions.jl | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index ed932b2..18a2f46 100644 --- a/Project.toml +++ b/Project.toml @@ -3,9 +3,6 @@ uuid = "f59fb95b-1bc8-443b-b347-5e445a549f37" authors = ["David Hong and contributors"] version = "0.1.0" -[weakdeps] -LossFunctions = "30fc2ffe-d236-52d8-8643-a9d8f7c094a7" - [deps] ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" LBFGSB = "5be7bae1-8223-5378-bac3-9e7378a2f6e6" @@ -20,3 +17,6 @@ julia = "1.6" [extensions] LossFunctionsExt = "LossFunctions" + +[weakdeps] +LossFunctions = "30fc2ffe-d236-52d8-8643-a9d8f7c094a7" diff --git a/src/GCPDecompositions.jl b/src/GCPDecompositions.jl index 60dfc0f..356fa60 100644 --- a/src/GCPDecompositions.jl +++ b/src/GCPDecompositions.jl @@ -18,7 +18,7 @@ include("type-cpd.jl") include("gcp-opt.jl") if !isdefined(Base, :get_extension) - include("../ext/LossFunctionsExt.jl") - end + include("../ext/LossFunctionsExt.jl") +end end From af1010c2213c3b064c904cf6f91ebd3d31dfb837 Mon Sep 17 00:00:00 2001 From: David Hong Date: Mon, 9 Oct 2023 16:49:18 -0400 Subject: [PATCH 13/18] Bug fixes --- ext/LossFunctionsExt.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/LossFunctionsExt.jl b/ext/LossFunctionsExt.jl index a5b1d53..f1af5d5 100644 --- a/ext/LossFunctionsExt.jl +++ b/ext/LossFunctionsExt.jl @@ -1,9 +1,9 @@ -module LossFunctinonsExt +module LossFunctionsExt using GCPDecompositions using LossFunctions -gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower=-Inf) = - _gcp(X, r, (x, m) -> loss(x, m), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) +GCPDecompositions.gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower=-Inf) = + GCPDecompositions._gcp(X, r, (x, m) -> loss(x, m), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) end \ No newline at end of file From 3737b8fd52ddd58c4024619729e966e112239fbc Mon Sep 17 00:00:00 2001 From: David Hong Date: Mon, 9 Oct 2023 16:51:07 -0400 Subject: [PATCH 14/18] Move docstring, delete old implementation --- ext/LossFunctionsExt.jl | 12 ++++++++++++ src/gcp-opt.jl | 15 --------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/ext/LossFunctionsExt.jl b/ext/LossFunctionsExt.jl index f1af5d5..cb51e40 100644 --- a/ext/LossFunctionsExt.jl +++ b/ext/LossFunctionsExt.jl @@ -3,6 +3,18 @@ module LossFunctionsExt using GCPDecompositions using LossFunctions +""" + gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower]) -> CPD + +Compute an approximate rank-`r` CP decomposition of the tensor `X` +with respect to the loss function `loss` and return a `CPD` object. + +# Inputs ++ `X` : multi-dimensional tensor/array to approximate/decompose ++ `r` : number of components for the CPD ++ `loss` : loss function from LossFunctions.jl ++ `lower` : lower bound for factor matrix entries, `default = -Inf` +""" GCPDecompositions.gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower=-Inf) = GCPDecompositions._gcp(X, r, (x, m) -> loss(x, m), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) diff --git a/src/gcp-opt.jl b/src/gcp-opt.jl index a55d2b1..22efbb1 100644 --- a/src/gcp-opt.jl +++ b/src/gcp-opt.jl @@ -17,21 +17,6 @@ with respect to a general loss and return a `CPD` object. gcp(X::Array, r, func=(x, m) -> (m - x)^2, grad=(x, m) -> ForwardDiff.derivative(m -> func(x, m), m), lower=-Inf) = _gcp(X, r, func, grad, lower, (;)) -""" - gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower]) -> CPD - -Compute an approximate rank-`r` CP decomposition of the tensor `X` -with respect to the loss function `loss` and return a `CPD` object. - -# Inputs -+ `X` : multi-dimensional tensor/array to approximate/decompose -+ `r` : number of components for the CPD -+ `loss` : loss function from LossFunctions.jl -+ `lower` : lower bound for factor matrix entries, `default = -Inf` -""" -#gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower=-Inf) = -# _gcp(X, r, (x, m) -> loss(x, m), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) - function _gcp(X::Array{TX,N}, r, func, grad, lower, lbfgsopts) where {TX,N} T = nonmissingtype(TX) From 3fdeccf8b256ad927282ae7f9a38b88d11353b6e Mon Sep 17 00:00:00 2001 From: David Hong Date: Mon, 9 Oct 2023 17:14:20 -0400 Subject: [PATCH 15/18] Bug fix: arguments to loss were in wrong order --- ext/LossFunctionsExt.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/LossFunctionsExt.jl b/ext/LossFunctionsExt.jl index cb51e40..22417be 100644 --- a/ext/LossFunctionsExt.jl +++ b/ext/LossFunctionsExt.jl @@ -16,6 +16,6 @@ with respect to the loss function `loss` and return a `CPD` object. + `lower` : lower bound for factor matrix entries, `default = -Inf` """ GCPDecompositions.gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower=-Inf) = - GCPDecompositions._gcp(X, r, (x, m) -> loss(x, m), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) + GCPDecompositions._gcp(X, r, (x, m) -> loss(m, x), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) end \ No newline at end of file From 242f88b4cd72be9a3ae65fcfe35ef437b1479bc4 Mon Sep 17 00:00:00 2001 From: David Hong Date: Mon, 9 Oct 2023 17:14:58 -0400 Subject: [PATCH 16/18] Allow any supervised loss --- ext/LossFunctionsExt.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/LossFunctionsExt.jl b/ext/LossFunctionsExt.jl index 22417be..664f74b 100644 --- a/ext/LossFunctionsExt.jl +++ b/ext/LossFunctionsExt.jl @@ -4,7 +4,7 @@ using GCPDecompositions using LossFunctions """ - gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower]) -> CPD + gcp(X::Array, r, loss::LossFunctions.SupervisedLoss, lower]) -> CPD Compute an approximate rank-`r` CP decomposition of the tensor `X` with respect to the loss function `loss` and return a `CPD` object. @@ -15,7 +15,7 @@ with respect to the loss function `loss` and return a `CPD` object. + `loss` : loss function from LossFunctions.jl + `lower` : lower bound for factor matrix entries, `default = -Inf` """ -GCPDecompositions.gcp(X::Array, r, loss::LossFunctions.DistanceLoss, lower=-Inf) = +GCPDecompositions.gcp(X::Array, r, loss::LossFunctions.SupervisedLoss, lower=-Inf) = GCPDecompositions._gcp(X, r, (x, m) -> loss(m, x), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) end \ No newline at end of file From 3b56b99a95b03b1a8033b3e6949196a6ab1c38ce Mon Sep 17 00:00:00 2001 From: David Hong Date: Mon, 9 Oct 2023 17:18:38 -0400 Subject: [PATCH 17/18] Tweak formatting --- ext/LossFunctionsExt.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ext/LossFunctionsExt.jl b/ext/LossFunctionsExt.jl index 664f74b..d4eecc0 100644 --- a/ext/LossFunctionsExt.jl +++ b/ext/LossFunctionsExt.jl @@ -1,7 +1,6 @@ module LossFunctionsExt -using GCPDecompositions -using LossFunctions +using GCPDecompositions, LossFunctions """ gcp(X::Array, r, loss::LossFunctions.SupervisedLoss, lower]) -> CPD From 3ca2ec5b5069c37b997b805e28cc577e25e51ae9 Mon Sep 17 00:00:00 2001 From: David Hong Date: Mon, 9 Oct 2023 17:26:45 -0400 Subject: [PATCH 18/18] Some whitespace formatting --- ext/LossFunctionsExt.jl | 2 +- src/GCPDecompositions.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/LossFunctionsExt.jl b/ext/LossFunctionsExt.jl index d4eecc0..46bdbd8 100644 --- a/ext/LossFunctionsExt.jl +++ b/ext/LossFunctionsExt.jl @@ -17,4 +17,4 @@ with respect to the loss function `loss` and return a `CPD` object. GCPDecompositions.gcp(X::Array, r, loss::LossFunctions.SupervisedLoss, lower=-Inf) = GCPDecompositions._gcp(X, r, (x, m) -> loss(m, x), (x, m) -> LossFunctions.deriv(loss, m, x), lower, (;)) -end \ No newline at end of file +end diff --git a/src/GCPDecompositions.jl b/src/GCPDecompositions.jl index 356fa60..b4aad5a 100644 --- a/src/GCPDecompositions.jl +++ b/src/GCPDecompositions.jl @@ -18,7 +18,7 @@ include("type-cpd.jl") include("gcp-opt.jl") if !isdefined(Base, :get_extension) - include("../ext/LossFunctionsExt.jl") + include("../ext/LossFunctionsExt.jl") end end