From 1fd926a3d9e35fd505761d6450b03aacab234a6f Mon Sep 17 00:00:00 2001 From: Richard Townsend Date: Sun, 16 Jun 2019 16:26:37 +0100 Subject: [PATCH 1/4] linear_models: update to use new mat64 API Fixes #232 --- linear_models/linear_regression.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linear_models/linear_regression.go b/linear_models/linear_regression.go index b726539a..eb52c4e5 100644 --- a/linear_models/linear_regression.go +++ b/linear_models/linear_regression.go @@ -87,7 +87,7 @@ func (lr *LinearRegression) Fit(inst base.FixedDataGrid) error { qr.RTo(®) var transposed, qty mat.Dense - transposed.Clone(q.T()) + transposed.CloneFrom(q.T()) qty.Mul(&transposed, observed) regressionCoefficients := make([]float64, n) From 864198dc6c6f9afd5925575d9c9e16498851eaba Mon Sep 17 00:00:00 2001 From: Richard Townsend Date: Sun, 16 Jun 2019 16:40:09 +0100 Subject: [PATCH 2/4] clustering: update to new mat64 API Fixes: #229 --- clustering/em.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/clustering/em.go b/clustering/em.go index 658ac99a..70894df5 100644 --- a/clustering/em.go +++ b/clustering/em.go @@ -26,7 +26,7 @@ type ExpectationMaximization struct { type Params struct { Means *mat.Dense - Covs []mat.Symmetric + Covs []*mat.SymDense } // Number of Gaussians to fit in the mixture @@ -131,27 +131,25 @@ func maximization(X *mat.Dense, y mat.Vector, p Params, n_comps int) Params { // Initialize the new parameters var p_new Params p_new.Means = mat.NewDense(n_comps, n_feats, nil) - p_new.Covs = make([]mat.Symmetric, n_comps) + p_new.Covs = make([]*mat.SymDense, n_comps) - //Update the parameters + // Update the parameters for k := 0; k < n_comps; k++ { X_yk := where(X, y, k) n_obs, _ := X_yk.Dims() - var covs_k mat.Symmetric + covs_k_reg := mat.NewSymDense(n_feats, nil) if n_obs <= 1 { p_new.Means.SetRow(k, p.Means.RawRowView(k)) - covs_k = p.Covs[k] + covs_k_reg = p.Covs[k] } else if n_obs < n_feats { p_new.Means.SetRow(k, means(X_yk)) - covs_k = shrunkCovariance(X_yk) + covs_k_reg = shrunkCovariance(X_yk) } else { p_new.Means.SetRow(k, means(X_yk)) - covs_k = stat.CovarianceMatrix(nil, X_yk, nil) + stat.CovarianceMatrix(covs_k_reg, X_yk, nil) } - reg := mat.NewSymDense(n_feats, symVals(n_feats, 0.000001)) - covs_k_reg := mat.NewSymDense(n_feats, nil) - covs_k_reg.AddSym(covs_k, reg) + p_new.Covs[k] = covs_k_reg } @@ -228,8 +226,8 @@ func initMeans(X *mat.Dense, n_comps, n_feats int) *mat.Dense { } // Creates a n_comps array of n_feats x n_feats mat.Symmetrics -func initCovariance(n_comps, n_feats int) []mat.Symmetric { - var result []mat.Symmetric +func initCovariance(n_comps, n_feats int) []*mat.SymDense { + var result []*mat.SymDense floats := identity(n_feats) for k := 0; k < n_comps; k++ { matrix := mat.NewSymDense(n_feats, floats) From 495bb91a481b51b08b6e6cfed7c47ae71df1653a Mon Sep 17 00:00:00 2001 From: Richard Townsend Date: Tue, 18 Jun 2019 10:28:07 +0100 Subject: [PATCH 3/4] neural: update to new mat64 API --- neural/network.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neural/network.go b/neural/network.go index 2d282feb..51191330 100644 --- a/neural/network.go +++ b/neural/network.go @@ -115,7 +115,7 @@ func (n *Network) Activate(with *mat.Dense, maxIterations int) { } tmp := new(mat.Dense) - tmp.Clone(with) + tmp.CloneFrom(with) // Main loop for i := 0; i < maxIterations; i++ { @@ -197,7 +197,7 @@ func (n *Network) Error(outArg, errArg *mat.Dense, maxIterations int) *mat.Dense // Transpose weights matrix reverseWeights := mat.DenseCopyOf(n.weights) - reverseWeights.Clone(n.weights.T()) + reverseWeights.CloneFrom(n.weights.T()) // We only need a certain number of passes for i := 0; i < maxIterations; i++ { From 850c9147801ab63f51e365f1059838281956b996 Mon Sep 17 00:00:00 2001 From: Richard Townsend Date: Tue, 18 Jun 2019 10:36:02 +0100 Subject: [PATCH 4/4] metrics: fixing pairwise CloneFrom --- metrics/pairwise/chebyshev_test.go | 2 +- metrics/pairwise/cranberra_test.go | 2 +- metrics/pairwise/manhattan_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/metrics/pairwise/chebyshev_test.go b/metrics/pairwise/chebyshev_test.go index 9da758de..d4b4e411 100644 --- a/metrics/pairwise/chebyshev_test.go +++ b/metrics/pairwise/chebyshev_test.go @@ -34,7 +34,7 @@ func TestChebyshev(t *testing.T) { }) Convey("When calculating distance with different dimension matrices", func() { - vectorX.Clone(vectorX.T()) + vectorX.CloneFrom(vectorX.T()) So(func() { chebyshev.Distance(vectorX, vectorY) }, ShouldPanic) }) diff --git a/metrics/pairwise/cranberra_test.go b/metrics/pairwise/cranberra_test.go index 7b3ec438..1443349c 100644 --- a/metrics/pairwise/cranberra_test.go +++ b/metrics/pairwise/cranberra_test.go @@ -43,7 +43,7 @@ func TestCranberrra(t *testing.T) { }) Convey("When calculating distance with different dimension matrices", func() { - vectorX.Clone(vectorX.T()) + vectorX.CloneFrom(vectorX.T()) So(func() { cranberra.Distance(vectorX, vectorY) }, ShouldPanic) }) diff --git a/metrics/pairwise/manhattan_test.go b/metrics/pairwise/manhattan_test.go index 179800ab..acf085b5 100644 --- a/metrics/pairwise/manhattan_test.go +++ b/metrics/pairwise/manhattan_test.go @@ -43,7 +43,7 @@ func TestManhattan(t *testing.T) { }) Convey("When calculating distance with different dimension matrices", func() { - vectorX.Clone(vectorX.T()) + vectorX.CloneFrom(vectorX.T()) So(func() { manhattan.Distance(vectorX, vectorY) }, ShouldPanic) })