Github user MLnick commented on a diff in the pull request:
https://github.com/apache/spark/pull/11119#discussion_r83694961
--- Diff: mllib/src/main/scala/org/apache/spark/ml/clustering/KMeans.scala
---
@@ -333,13 +389,45 @@ class KMeans @Since("1.5.0") (
override def transformSchema(schema: StructType): StructType = {
validateAndTransformSchema(schema)
}
+
+ @Since("2.1.0")
+ override def write: MLWriter = new KMeans.KMeansWriter(this)
}
@Since("1.6.0")
object KMeans extends DefaultParamsReadable[KMeans] {
+ // TODO: [SPARK-17784]: Add a fromCenters method
+
@Since("1.6.0")
override def load(path: String): KMeans = super.load(path)
+
+ @Since("1.6.0")
+ override def read: MLReader[KMeans] = new KMeansReader
+
+ /** [[MLWriter]] instance for [[KMeans]] */
+ private[KMeans] class KMeansWriter(instance: KMeans) extends MLWriter {
+ override protected def saveImpl(path: String): Unit = {
+ DefaultParamsWriter.saveInitialModel(instance, path)
+ DefaultParamsWriter.saveMetadata(instance, path, sc)
+ }
+ }
+
+ private class KMeansReader extends MLReader[KMeans] {
+
+ /** Checked against metadata when loading estimator */
+ private val className = classOf[KMeans].getName
+
+ override def load(path: String): KMeans = {
+ val metadata = DefaultParamsReader.loadMetadata(path, sc, className)
+ val instance = new KMeans(metadata.uid)
+
+ DefaultParamsReader.getAndSetParams(instance, metadata)
+ DefaultParamsReader.loadInitialModel[KMeansModel](path, sc)
+ .foreach(v => instance.setInitialModel(v))
--- End diff --
Here I was thinking more along the lines that we could explicitly ignore
the error we expect. So if the model load fails for the reason that the
"initialModel" directory is empty, then we can ignore (that's basically the
only case). Otherwise, it may be a legitimate failure to load the model (say
some future load incompatibility or bug, or some other issue with loading an
initial model that is there) and we will just swallow it without the user
knowing there is a problem.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]