From 54943c06b301aee1cce65ee2b79371b097afa5ef Mon Sep 17 00:00:00 2001 From: Guoqiong Song Date: Thu, 8 Aug 2019 16:04:46 -0700 Subject: [PATCH] fix session recommender (#1558) --- .../SessionRecommenderSpec.scala | 40 ++++++++----------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/scala/com/intel/analytics/zoo/models/recommendation/SessionRecommenderSpec.scala b/scala/com/intel/analytics/zoo/models/recommendation/SessionRecommenderSpec.scala index 42cb96d0ac4..e50c0ea710c 100644 --- a/scala/com/intel/analytics/zoo/models/recommendation/SessionRecommenderSpec.scala +++ b/scala/com/intel/analytics/zoo/models/recommendation/SessionRecommenderSpec.scala @@ -52,7 +52,7 @@ class SessionRecommenderSpec extends ZooSpecHelper { val itemCount = 100 val sessionLength = 10 - val model = SessionRecommender[Float](itemCount, sessionLength, includeHistory = false) + val model = SessionRecommender[Float](itemCount, sessionLength = sessionLength) val ran = new Random(42L) val data = (1 to 100).map { x => val items: Seq[Float] = for (i <- 1 to sessionLength) yield @@ -69,7 +69,7 @@ class SessionRecommenderSpec extends ZooSpecHelper { val itemCount = 100 val sessionLength = 10 val historyLength = 5 - val model = SessionRecommender[Float](itemCount, sessionLength, + val model = SessionRecommender[Float](itemCount, sessionLength = sessionLength, includeHistory = true, historyLength = historyLength) val ran = new Random(42L) val data = (1 to 100).map { x => @@ -91,26 +91,10 @@ class SessionRecommenderSpec extends ZooSpecHelper { val itemCount = 100 val sessionLength = 10 val historyLength = 5 - val model = SessionRecommender[Float](itemCount, sessionLength, + val model = SessionRecommender[Float](itemCount, sessionLength = sessionLength, includeHistory = true, historyLength = historyLength) val ran = new Random(42L) - val data1: RDD[Sample[Float]] = sc.parallelize(1 to 100) - .map { x => - val items1: Seq[Float] = for (i <- 1 to sessionLength) yield ran.nextInt(itemCount).toFloat - val items2: Seq[Float] = for (i <- 1 to historyLength) yield ran.nextInt(itemCount).toFloat - val input1 = Tensor(items1.toArray, Array(sessionLength)) - val input2 = Tensor(items2.toArray, Array(historyLength)) - Sample[Float](Array(input1, input2)) - } - - val recommedations1 = model.recommendForSession(data1, 3, zeroBasedLabel = false) - recommedations1.take(10) - .map { x => - assert(x.size == 3) - assert(x(0)._2 >= x(1)._2) - } - - val data2: Array[Sample[Float]] = (1 to 10) + val data1: Array[Sample[Float]] = (1 to 10) .map { x => val items1: Seq[Float] = for (i <- 1 to sessionLength) yield ran.nextInt(itemCount).toFloat val items2: Seq[Float] = for (i <- 1 to historyLength) yield ran.nextInt(itemCount).toFloat @@ -119,11 +103,18 @@ class SessionRecommenderSpec extends ZooSpecHelper { Sample[Float](Array(input1, input2)) }.toArray - val recommedations2 = model.recommendForSession(data2, 4, zeroBasedLabel = false) - recommedations2.map { x => + val recommedations1 = model.recommendForSession(data1, 4, zeroBasedLabel = false) + recommedations1.map { x => assert(x.size == 4) assert(x(0)._2 >= x(1)._2) } + + val data2: RDD[Sample[Float]] = sc.parallelize(data1) + val recommedations2 = model.recommendForSession(data2, 3, zeroBasedLabel = false) + recommedations2.take(10).map { x => + assert(x.size == 3) + assert(x(0)._2 >= x(1)._2) + } } "SessionRecommender compile and fit" should "work properly" in { @@ -131,7 +122,7 @@ class SessionRecommenderSpec extends ZooSpecHelper { val itemCount = 100 val sessionLength = 10 val historyLength = 5 - val model = SessionRecommender[Float](itemCount, sessionLength, + val model = SessionRecommender[Float](itemCount, 10, sessionLength = sessionLength, includeHistory = true, historyLength = historyLength) val ran = new Random(42L) val data1 = sc.parallelize(1 to 100) @@ -144,6 +135,7 @@ class SessionRecommenderSpec extends ZooSpecHelper { Sample(Array(input1, input2), Array(label)) } model.compile(optimizer = "rmsprop", loss = "sparse_categorical_crossentropy") + model.summary() model.fit(data1, nbEpoch = 1) } } @@ -153,7 +145,7 @@ class SessionRecommenderSerialTest extends ModuleSerializationTest { val ran = new Random(42L) val itemCount = 100 val sessionLength = 10 - val model = SessionRecommender[Float](100, 10, includeHistory = false) + val model = SessionRecommender[Float](100, sessionLength = 10) val items: Seq[Float] = for (i <- 1 to sessionLength) yield ran.nextInt(itemCount - 1).toFloat + 1 val data = Tensor(items.toArray, Array(sessionLength)).resize(1, sessionLength)