Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package com.intel.analytics.zoo.examples.recommendation

import com.intel.analytics.bigdl.numeric.NumericFloat
import com.intel.analytics.bigdl.optim.{Adam, Top1Accuracy}
import com.intel.analytics.bigdl.optim.{Adam, PrecisionRecallAUC, Top1Accuracy}
import com.intel.analytics.zoo.common.NNContext
import com.intel.analytics.zoo.models.recommendation._
import com.intel.analytics.zoo.pipeline.api.keras.objectives.SparseCategoricalCrossEntropy
import com.intel.analytics.zoo.pipeline.api.keras.metrics.AUC
import com.intel.analytics.zoo.pipeline.api.keras.objectives.{BinaryCrossEntropy, SparseCategoricalCrossEntropy}
import org.apache.log4j.{Level, Logger}
import org.apache.spark.SparkConf
import org.apache.spark.sql.functions._
Expand Down Expand Up @@ -58,7 +59,7 @@ object Ml1mWideAndDeep {

val wideAndDeep: WideAndDeep[Float] = WideAndDeep[Float](
params.modelType,
numClasses = 5,
numClasses = 1,
columnInfo = localColumnInfo)

val isImplicit = false
Expand All @@ -69,14 +70,15 @@ object Ml1mWideAndDeep {
featureRdds.randomSplit(Array(0.8, 0.2))
val trainRdds = trainpairFeatureRdds.map(x => x.sample)
val validationRdds = validationpairFeatureRdds.map(x => x.sample)
println(validationRdds.count())

val optimMethod = new Adam[Float](
learningRate = 1e-2,
learningRateDecay = 1e-5)

wideAndDeep.compile(optimizer = optimMethod,
loss = SparseCategoricalCrossEntropy[Float](zeroBasedLabel = false),
metrics = List(new Top1Accuracy[Float]())
loss = BinaryCrossEntropy[Float](),
metrics = List(new AUC[Float](), new Top1Accuracy[Float]())
)
wideAndDeep.fit(trainRdds, batchSize = params.batchSize,
nbEpoch = params.maxEpoch, validationData = validationRdds)
Expand Down Expand Up @@ -162,7 +164,17 @@ object Ml1mWideAndDeep {
.select(unioned("userId"), unioned("itemId"), col("label"), col("gender"), col("age"),
col("occupation"), col("genres"), col("age-gender"))

val rddOfSample = joined.rdd.map(r => {
// 3|261197|
// 5|226310|
val filtered = joined.filter(joined("label") === 3 || joined("label") === 5)

val relabel = (s: Int) => {
if (s == 3) 0 else 1
}
val relabelUDF = udf(relabel)
val relabeled = filtered.withColumn("label", relabelUDF(col("label")))

val rddOfSample = relabeled.rdd.map(r => {
val uid = r.getAs[Int]("userId")
val iid = r.getAs[Int]("itemId")
UserItemFeature(uid, iid, Utils.row2Sample(r, columnInfo, modelType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ object Utils {
val deepTensor: Array[Tensor[Float]] = getDeepTensors(r, columnInfo)
val l = r.getAs[Int](columnInfo.label)
val label = Tensor[Float](T(l))
label.resize(1, 1)
// label.resize(1, 1)

modelType match {
case "wide_n_deep" =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,21 @@ class WideAndDeep[T: ClassTag](

modelType match {
case "wide" =>
val out = Activation("softmax").inputs(wideLinear)
val out = Activation("sigmoid").inputs(wideLinear)
val model: Model[T] = Model(Array(inputWide), out)
model.asInstanceOf[AbstractModule[Tensor[T], Tensor[T], T]]

case "deep" =>
val (inputDeep, mergeList) = deepMerge(inputInd, inputEmb, inputCon)
val deepLinear = deepHidden(mergeList.toList)
val out = Activation("softmax").inputs(deepLinear)
val out = Activation("sigmoid").inputs(deepLinear)
Model(inputDeep, out).asInstanceOf[AbstractModule[Tensor[T], Tensor[T], T]]

case "wide_n_deep" =>
val (inputDeep, mergeList) = deepMerge(inputInd, inputEmb, inputCon)
val deepLinear = deepHidden(mergeList)
val merged = Merge.merge(List(wideLinear, deepLinear), "sum")
val out = Activation("softmax").inputs(merged)
val out = Activation("sigmoid").inputs(merged)
Model(Array(inputWide) ++ inputDeep, out)
.asInstanceOf[AbstractModule[Tensor[T], Tensor[T], T]]
case _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ class AUC[T](thresholdNum: Int = 200)(implicit ev: TensorNumeric[T])
override def apply(output: Activity, target: Activity):
ValidationResult = {
val _output = if (output.asInstanceOf[Tensor[T]].dim() == 2) {
output.asInstanceOf[Tensor[T]].squeeze(2)
output.asInstanceOf[Tensor[T]].clone().squeeze(2)
} else {
output.asInstanceOf[Tensor[T]].squeeze()
output.asInstanceOf[Tensor[T]].clone().squeeze()
}

val _target = if (target.asInstanceOf[Tensor[T]].dim() == 2) {
target.asInstanceOf[Tensor[T]].squeeze(2)
target.asInstanceOf[Tensor[T]].clone().squeeze(2)
} else {
target.asInstanceOf[Tensor[T]].squeeze()
target.asInstanceOf[Tensor[T]].clone().squeeze()
}
require(_output.dim() <= 2 && _target.dim() <= 2,
s"${_output.dim()} dim format is not supported")
Expand Down