-
-
Save RevySR/0c2fc6ba4cfa536e4788112a94200b50 to your computer and use it in GitHub Desktop.
Adam.sc
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Adam\n", | |
"\n", | |
"This plugin provides the Adam optimizer for SGD. " | |
] | |
}, | |
{ | |
"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/Rabenda/0c2fc6ba4cfa536e4788112a94200b50/raw/233cbc83932dad659519c80717d145a3983f57e1/Adam.sc\")).mkString)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"interp.load(\"\"\"\n", | |
" val hyperparameters = Factory[Builtins with Adam].newInstance()\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/Rabenda/0c2fc6ba4cfa536e4788112a94200b50/raw/233cbc83932dad659519c80717d145a3983f57e1/Adam.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 org.nd4j.linalg.ops.transforms.Transforms\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 Adam].newInstance()" | |
] | |
} | |
], | |
"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 | |
} |
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
trait Adam extends INDArrayWeights { | |
import org.nd4j.linalg.ops.transforms.Transforms | |
trait INDArrayWeightApi extends super.INDArrayWeightApi { | |
this: INDArrayWeight => | |
private var m0: Option[INDArray] = None | |
def m = m0.getOrElse(Nd4j.zeros(data.shape: _*)) | |
def m_=(value: INDArray) = m0 = Some(value) | |
private var v0: Option[INDArray] = None | |
def v = v0.getOrElse(Nd4j.zeros(data.shape: _*)) | |
def v_=(value: INDArray) = v0 = Some(value) | |
} | |
override type INDArrayWeight <: INDArrayWeightApi with Weight | |
trait INDArrayOptimizerApi extends super.INDArrayOptimizerApi { | |
this: INDArrayOptimizer => | |
def beta1: scala.Double = 0.9 | |
def beta2: scala.Double = 0.999 | |
def eps: scala.Double = 1e-8 | |
abstract override def delta = delta0 | |
private lazy val delta0: INDArray = { | |
import org.nd4s.Implicits._ | |
import weight._ | |
m = m * beta1 + super.delta * (1.0 - beta1) | |
v = v * beta2 + (super.delta * super.delta) * (1.0 - beta2) | |
m / (Transforms.sqrt(v) + eps) | |
} | |
} | |
override type INDArrayOptimizer <: INDArrayOptimizerApi with Optimizer | |
} |
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
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