Skip to content

Instantly share code, notes, and snippets.

@Atry
Forked from TerrorJack/L2Regularization.sc
Last active July 27, 2017 07:40
Show Gist options
  • Save Atry/8a52557ea68b269d74d0a09c4282a267 to your computer and use it in GitHub Desktop.
Save Atry/8a52557ea68b269d74d0a09c4282a267 to your computer and use it in GitHub Desktop.
L2Regularization.sc
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## L2Regularization\n",
"\n",
"L2Regularization applies L2 regularization during back propagation. Note that it only modifies `delta`, so the loss value user obtained is the \"raw\" loss value without L2 regularization applied."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dependencies for use in Ammonite REPL"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import $ivy.`com.thoughtworks.deeplearning::plugins-builtins:2.0.0`\n",
"\n",
"import scala.concurrent.ExecutionContext.Implicits.global\n",
"import org.nd4j.linalg.factory.Nd4j\n",
"import org.nd4j.linalg.api.ndarray.INDArray\n",
"import com.thoughtworks.feature.Factory\n",
"import com.thoughtworks.deeplearning.plugins.INDArrayWeights\n",
"import com.thoughtworks.deeplearning.plugins.Builtins"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"interp.load(scala.io.Source.fromURL(new java.net.URL(\"https://gist.github.com/Atry/8a52557ea68b269d74d0a09c4282a267/raw/4624d8612e189d5281a2fb82312162d485cefd8c/L2Regularization.sc\")).mkString)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"interp.load(\"\"\"\n",
" val hyperparameters = Factory[Builtins with L2Regularization].newInstance(l2Regularization = 0.001)\n",
"\"\"\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### For sbt projects"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"// build.sbt\n",
"libraryDependencies += \"com.thoughtworks.deeplearning\" %% \"plugins-builtins\" % \"latest.release\"\n",
"\n",
"addCompilerPlugin(\"com.thoughtworks.import\" %% \"import\" % \"latest.release\")\n",
"\n",
"// XXX.scala\n",
"import $exec.`https://gist.github.com/Atry/8a52557ea68b269d74d0a09c4282a267/raw/4624d8612e189d5281a2fb82312162d485cefd8c/L2Regularization.sc`\n",
" \n",
"import scala.concurrent.ExecutionContext.Implicits.global\n",
"import org.nd4j.linalg.factory.Nd4j\n",
"import org.nd4j.linalg.api.ndarray.INDArray\n",
"import com.thoughtworks.feature.Factory\n",
"import com.thoughtworks.deeplearning.plugins.INDArrayWeights\n",
"import com.thoughtworks.deeplearning.plugins.Builtins\n",
" \n",
"val hyperparameters = Factory[Builtins with L2Regularization].newInstance(l2Regularization = 0.001)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Scala",
"language": "scala",
"name": "scala"
},
"language_info": {
"codemirror_mode": "text/x-scala",
"file_extension": ".scala",
"mimetype": "text/x-scala",
"name": "scala211",
"nbconvert_exporter": "script",
"pygments_lexer": "scala",
"version": "2.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
trait L2Regularization extends com.thoughtworks.deeplearning.plugins.INDArrayWeights {
import org.nd4j.linalg.api.ndarray.INDArray
val l2Regularization: Double
trait INDArrayOptimizerApi extends super.INDArrayOptimizerApi {
this: INDArrayOptimizer =>
abstract override def delta: INDArray = {
import org.nd4s.Implicits._
super.delta + weight.data * l2Regularization
}
}
override type INDArrayOptimizer <: INDArrayOptimizerApi with Optimizer
}
MIT License
Copyright (c) 2017 ThoughtWorks Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment