Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

bug fix: DLModel prediction #4

Merged
Merged
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 @@ -391,7 +391,8 @@ class DLModel[@specialized(Float, Double) T: ClassTag](
val localBatchSize = $(batchSize)

val resultRDD = dataFrame.rdd.mapPartitions { rowIter =>
val localModel = modelBroadCast.value()
// call the evaluate method to enable DLModel.train=False during the predict process
val localModel = modelBroadCast.value().evaluate()
rowIter.grouped(localBatchSize).flatMap { rowBatch =>
val samples = rowBatch.map { row =>
val features = featureFunc(row, featureColIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ class DLEstimatorSpec extends FlatSpec with Matchers with BeforeAndAfter {
assert(correct > nRecords * 0.8)
}

"An DLEstimator" should "throws exception when DLModel is predicting with DLModel.train=True" in {
val model = new Sequential().add(Linear[Float](6, 2))
.add(Dropout[Float](initP = 0.5))
.add(LogSoftMax[Float])
val criterion = ClassNLLCriterion[Float]()
val estimator = new DLEstimator[Float](model, criterion, Array(6), Array(1))
.setBatchSize(nRecords)
.setOptimMethod(new LBFGS[Float]())
.setLearningRate(0.1)
.setMaxEpoch(maxEpoch)
val data = sc.parallelize(smallData)
val df = sqlContext.createDataFrame(data).toDF("features", "label")
val dlModel = estimator.fit(df)
dlModel.isInstanceOf[DLModel[_]] should be(true)
val correct = dlModel.transform(df).select("label", "prediction").rdd.filter {
case Row(label: Double, prediction: Seq[Double]) =>
label == prediction.indexOf(prediction.max) + 1
}.count()
assert(correct > nRecords * 0.8)
}

"An DLEstimator" should "support different FEATURE types" in {
val model = new Sequential().add(Linear[Float](6, 2)).add(LogSoftMax[Float])
val criterion = ClassNLLCriterion[Float]()
Expand Down