Skip to content

Commit 4652a1d

Browse files
committed
fix for Spark 2.1.0
1 parent 2134cd7 commit 4652a1d

File tree

9 files changed

+24
-30
lines changed

9 files changed

+24
-30
lines changed

build.sbt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,7 @@ libraryDependencies ++= Seq(
4343
)
4444

4545

46-
//{
47-
// val defaultHadoopVersion = "[2.6.0,)"
48-
// val hadoopVersion =
49-
// scala.util.Properties.envOrElse("SPARK_HADOOP_VERSION", defaultHadoopVersion)
50-
// libraryDependencies += "org.apache.hadoop" % "hadoop-client" % hadoopVersion
51-
//}
52-
53-
mergeStrategy in assembly <<= (mergeStrategy in assembly) { (old) =>
46+
assemblyMergeStrategy in assembly ~= (old =>
5447
{
5548
case PathList("javax", "servlet", xs @ _*) => MergeStrategy.first
5649
case PathList(ps @ _*) if ps.last endsWith ".html" => MergeStrategy.first
@@ -62,12 +55,12 @@ mergeStrategy in assembly <<= (mergeStrategy in assembly) { (old) =>
6255
case m if m.toLowerCase.startsWith("meta-inf/services/") => MergeStrategy.filterDistinctLines
6356
case _ => MergeStrategy.first
6457
}
65-
}
58+
)
6659

6760

6861
spName := "FurongHuang/spectrallda-tensorspark"
6962

70-
sparkVersion := "2.0.2"
63+
sparkVersion := "2.1.0"
7164

7265
sparkComponents += "mllib"
7366

src/main/scala/edu/uci/eecs/spectralLDA/algorithm/ALS.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class ALS(dimK: Int,
120120
B: DenseMatrix[Double],
121121
C: DenseMatrix[Double]): (DenseMatrix[Double], DenseVector[Double]) = {
122122
val updatedA = unfoldedM3 * TensorOps.krprod(C, B) * TensorOps.to_invert(C, B)
123-
val lambda = norm(updatedA(::, *)).toDenseVector
123+
val lambda = norm(updatedA(::, *)).t.toDenseVector
124124
(AlgebraUtil.matrixNormalization(updatedA), lambda)
125125
}
126126
}

src/main/scala/edu/uci/eecs/spectralLDA/algorithm/TensorLDA.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package edu.uci.eecs.spectralLDA.algorithm
66
* Created by Furong Huang on 11/2/15.
77
*/
88
import edu.uci.eecs.spectralLDA.datamoments.DataCumulant
9-
import breeze.linalg.{DenseMatrix, DenseVector, SparseVector, argtopk, diag, max, min}
9+
import breeze.linalg.{DenseMatrix, DenseVector, SparseVector, argsort, diag, max, min}
1010
import breeze.numerics._
1111
import breeze.stats.distributions.{Rand, RandBasis}
1212
import edu.uci.eecs.spectralLDA.utils.NonNegativeAdjustment
@@ -70,7 +70,7 @@ class TensorLDA(dimK: Int,
7070
val topicWordMatrixUnordered: DenseMatrix[Double] = unwhiteningMatrix * nu * diag(lambda)
7171

7272
// re-arrange alpha and topicWordMatrix in descending order of alpha
73-
val idx = argtopk(alphaUnordered, dimK)
73+
val idx = argsort(alphaUnordered).reverse.take(dimK)
7474
val alpha = alphaUnordered(idx).toDenseVector
7575
val topicWordMatrix = topicWordMatrixUnordered(::, idx).toDenseMatrix
7676

src/main/scala/edu/uci/eecs/spectralLDA/algorithm/TensorLDAModel.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ class TensorLDAModel(val topicWordDistribution: DenseMatrix[Double],
106106
val smoothedBeta: DenseMatrix[Double] = topicWordDistribution * (1 - smoothing)
107107
smoothedBeta += DenseMatrix.ones[Double](vocabSize, k) * (smoothing / vocabSize)
108108

109-
assert(sum(smoothedBeta(::, *)).toDenseVector.forall(a => abs(a - 1) <= 1e-10))
109+
assert(sum(smoothedBeta(::, *)).t.toDenseVector
110+
.forall(a => abs(a - 1) <= 1e-10))
110111
assert(smoothing < 1e-4 || smoothedBeta.forall(_ > 1e-10))
111112

112113
smoothedBeta
@@ -134,4 +135,4 @@ private[algorithm] object TensorLDAModel {
134135
val a = max(x)
135136
a + log(sum(exp(x :- a)))
136137
}
137-
}
138+
}

src/main/scala/edu/uci/eecs/spectralLDA/utils/AlgebraUtil.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object AlgebraUtil {
1313

1414
def matrixNormalization(B: DenseMatrix[Double]): DenseMatrix[Double] = {
1515
val A: DenseMatrix[Double] = B.copy
16-
val colNorms: DenseVector[Double] = norm(A(::, *)).toDenseVector
16+
val colNorms: DenseVector[Double] = norm(A(::, *)).t.toDenseVector
1717

1818
for (i <- 0 until A.cols optimized) {
1919
A(::, i) :/= colNorms(i)

src/main/scala/edu/uci/eecs/spectralLDA/utils/TensorOps.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import scala.reflect.ClassTag
1010
object TensorOps {
1111
/** Complex matrix norm */
1212
def matrixNorm(m: DenseMatrix[Complex]): Double = {
13-
norm(norm(m(::, *)).toDenseVector)
13+
norm(norm(m(::, *)).t.toDenseVector)
1414
}
1515

1616
/** Double matrix norm */
1717
def dmatrixNorm(m: DenseMatrix[Double]): Double = {
18-
norm(norm(m(::, *)).toDenseVector)
18+
norm(norm(m(::, *)).t.toDenseVector)
1919
}
2020

2121
/** Unfold 3rd-order tensor */
@@ -89,4 +89,4 @@ object TensorOps {
8989
}
9090
prod
9191
}
92-
}
92+
}

src/test/scala/edu/uci/eecs/spectralLDA/algorithm/TensorLDATest.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class TensorLDATest extends FlatSpec with Matchers {
6666

6767
// Rearrange the elements/columns of fitted_alpha and fitted_beta
6868
// to the order of initial alpha and beta
69-
val idx = argtopk(fitted_alpha, 3)
69+
val idx = argsort(fitted_alpha).reverse.take(3)
7070
val sorted_beta = fitted_beta(::, idx).toDenseMatrix
7171
// if one vector is all negative, multiply it by -1 to turn it positive
7272
for (j <- 0 until sorted_beta.cols) {
@@ -79,7 +79,7 @@ class TensorLDATest extends FlatSpec with Matchers {
7979
val diff_beta: DenseMatrix[Double] = sorted_beta - allTokenDistributions
8080
val diff_alpha: DenseVector[Double] = sorted_alpha - alpha
8181

82-
val norm_diff_beta = norm(norm(diff_beta(::, *)).toDenseVector)
82+
val norm_diff_beta = norm(norm(diff_beta(::, *)).t.toDenseVector)
8383
val norm_diff_alpha = norm(diff_alpha)
8484

8585
info(s"Expecting alpha: $alpha")
@@ -107,7 +107,7 @@ class TensorLDATest extends FlatSpec with Matchers {
107107

108108
val s = sum(allTokenDistributions(::, *))
109109
val normalisedAllTokenDistributions: DenseMatrix[Double] =
110-
allTokenDistributions * diag(1.0 / s.toDenseVector)
110+
allTokenDistributions * diag(1.0 / s.t.toDenseVector)
111111

112112
val documents = simulateLDAData(
113113
alpha,
@@ -130,7 +130,7 @@ class TensorLDATest extends FlatSpec with Matchers {
130130

131131
// Rearrange the elements/columns of fitted_alpha and fitted_beta
132132
// to the order of initial alpha and beta
133-
val idx = argtopk(fitted_alpha, dimK)
133+
val idx = argsort(fitted_alpha).reverse.take(dimK)
134134
val sorted_beta = fitted_beta(::, idx).toDenseMatrix
135135
val sorted_alpha = fitted_alpha(idx).toDenseVector
136136

@@ -140,7 +140,7 @@ class TensorLDATest extends FlatSpec with Matchers {
140140
val diff_beta: DenseMatrix[Double] = sorted_beta - expected_beta
141141
val diff_alpha: DenseVector[Double] = sorted_alpha - expected_alpha
142142

143-
val norm_diff_beta = norm(norm(diff_beta(::, *)).toDenseVector)
143+
val norm_diff_beta = norm(norm(diff_beta(::, *)).t.toDenseVector)
144144
val norm_diff_alpha = norm(diff_alpha)
145145

146146
info(s"Expecting alpha: $expected_alpha")
@@ -154,4 +154,4 @@ class TensorLDATest extends FlatSpec with Matchers {
154154
norm_diff_beta should be <= 0.025
155155
norm_diff_alpha should be <= 3.5
156156
}
157-
}
157+
}

src/test/scala/edu/uci/eecs/spectralLDA/utils/RandNLATest.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class RandNLATest extends FlatSpec with Matchers {
4545
val expectedResult = m2 * g
4646

4747
val diff: DenseMatrix[Double] = result - expectedResult
48-
val normDiff: Double = norm(norm(diff(::, *)).toDenseVector)
48+
val normDiff: Double = norm(norm(diff(::, *)).t.toDenseVector)
4949
normDiff should be <= 1e-8
5050
}
5151

@@ -59,7 +59,7 @@ class RandNLATest extends FlatSpec with Matchers {
5959
val alpha: DenseVector[Double] = DenseVector[Double](25.0, 20.0, 15.0, 10.0, 5.0)
6060
val beta: DenseMatrix[Double] = DenseMatrix.rand(n, k, Uniform(0.0, 1.0))
6161

62-
val norms = norm(beta(::, *)).toDenseVector
62+
val norms = norm(beta(::, *)).t.toDenseVector
6363
for (j <- 0 until k) {
6464
beta(::, j) /= norms(j)
6565
}
@@ -72,7 +72,7 @@ class RandNLATest extends FlatSpec with Matchers {
7272
val (s: DenseVector[Double], u: DenseMatrix[Double]) = RandNLA.decomp2(a * q, q)
7373

7474
val diff_a = u * diag(s) * u.t - a
75-
val norm_diff_a = norm(norm(diff_a(::, *)).toDenseVector)
75+
val norm_diff_a = norm(norm(diff_a(::, *)).t.toDenseVector)
7676

7777
norm_diff_a should be <= 1e-8
7878
}

src/test/scala/edu/uci/eecs/spectralLDA/utils/TensorOpsTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ class TensorOpsTest extends FlatSpec with Matchers {
4242
val expected = expectedRankOneTensor3d(x, y, z)
4343

4444
val diff = tensor - expected
45-
norm(norm(diff(::, *)).toDenseVector) should be <= 1e-8
45+
norm(norm(diff(::, *)).t.toDenseVector) should be <= 1e-8
4646
}
47-
}
47+
}

0 commit comments

Comments
 (0)