-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SPARKNLP-835: ProtectedParam and ProtectedFeature (#13797)
* SPARKNLP-835: Finalize protected Features * SPARKNLP-835: Remove redundant checks for protected Features * SPARKNLP-835: Introduce ProtectedParam * SPARKNLP-835: Resolve encoding/decoding issue for HasProtectedParams * SPARKNLP-835: Make caseSensitive settable * SPARKNLP-835: Make maxSentenceLength, batchSize settable * SPARKNLP-835: Enable protected Params for Annotators
- Loading branch information
Showing
61 changed files
with
263 additions
and
295 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/main/scala/com/johnsnowlabs/nlp/HasProtectedParams.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.johnsnowlabs.nlp | ||
|
||
import org.apache.spark.ml.param.{Param, Params} | ||
|
||
/** Enables a class to protect a parameter, which means that it can only be set once. | ||
* | ||
* This trait will enable a implicit conversion from Param to ProtectedParam. In addition, the | ||
* new set for ProtectedParam will then check, whether or not the value was already set. If so, | ||
* then a warning will be output and the value will not be set again. | ||
*/ | ||
trait HasProtectedParams { | ||
this: Params => | ||
implicit class ProtectedParam[T](baseParam: Param[T]) | ||
extends Param[T](baseParam.parent, baseParam.name, baseParam.doc, baseParam.isValid) { | ||
|
||
var isProtected = false | ||
|
||
/** Sets this parameter to be protected, which means that it can only be set once. | ||
* | ||
* Default values do not count as a set value and can be overridden. | ||
* | ||
* @return | ||
* This object | ||
*/ | ||
def setProtected(): this.type = { | ||
isProtected = true | ||
this | ||
} | ||
|
||
def toParam: Param[T] = this.asInstanceOf[Param[T]] | ||
|
||
// Overrides needed for individual Param implementation | ||
override def jsonEncode(value: T): String = baseParam.jsonEncode(value) | ||
override def jsonDecode(json: String): T = baseParam.jsonDecode(json) | ||
} | ||
|
||
/** Sets the value for a protected Param. | ||
* | ||
* If the parameter was already set, it will not be set again. Default values do not count as a | ||
* set value and can be overridden. | ||
* | ||
* @param param | ||
* Protected parameter to set | ||
* @param value | ||
* Value for the parameter | ||
* @tparam T | ||
* Type of the parameter | ||
* @return | ||
* This object | ||
*/ | ||
def set[T](param: ProtectedParam[T], value: T): this.type = { | ||
if (param.isProtected && get(param).isDefined) | ||
println( | ||
s"Warning: The parameter ${param.name} is protected and can only be set once." + | ||
" For a pretrained model, this was done during the initialization process." + | ||
" If you are trying to train your own model, please check the documentation." + | ||
" If this is intentional, set the parameter directly with set(annotator.param, value).") | ||
else | ||
set(param.toParam, value) | ||
this | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.