Skip to content

Instantly share code, notes, and snippets.

@TerrorJack
Last active July 26, 2017 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TerrorJack/a7af811a0ee592d41ab57f2c5d49f08b to your computer and use it in GitHub Desktop.
Save TerrorJack/a7af811a0ee592d41ab57f2c5d49f08b to your computer and use it in GitHub Desktop.
INDArrayDumping.sc
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## INDArrayDumping\n",
"\n",
"This plugin provides automatic dumping of intermediate `INDArrayWeight`s during training. Given a path prefix, it dumps weights using the Java's `Serializable` interface."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dependencies for use in Ammonite REPL"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import $ivy.`org.nd4j::nd4s:0.8.0`\n",
"import $ivy.`com.thoughtworks.deeplearning::plugins-builtins:2.0.0`\n",
"\n",
"import scala.concurrent.ExecutionContext.Implicits.global\n",
"import java.io.{FileOutputStream, ObjectOutputStream}\n",
"import scalaz.syntax.all._\n",
"import org.nd4j.linalg.api.ndarray.INDArray\n",
"import com.thoughtworks.feature.{Factory, ImplicitApply, PartialApply}\n",
"import com.thoughtworks.raii.asynchronous._\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.githubusercontent.com/TerrorJack/a7af811a0ee592d41ab57f2c5d49f08b/raw/dd94943eff42926b12f6d3cec64f237823b2e633/INDArrayDumping.sc\")).mkString)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"interp.load(\"\"\"\n",
" val hyperparameters = Factory[Builtins with INDArrayDumping].newInstance(dumpingPathPrefix = \"/Users/TerrorJack\")\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.githubusercontent.com/TerrorJack/a7af811a0ee592d41ab57f2c5d49f08b/raw/dd94943eff42926b12f6d3cec64f237823b2e633/INDArrayDumping.sc`\n",
"\n",
"import scala.concurrent.ExecutionContext.Implicits.global\n",
"import java.io.{FileOutputStream, ObjectOutputStream}\n",
"import scalaz.syntax.all._\n",
"import org.nd4j.linalg.api.ndarray.INDArray\n",
"import com.thoughtworks.feature.{Factory, ImplicitApply, PartialApply}\n",
"import com.thoughtworks.raii.asynchronous._\n",
"import com.thoughtworks.deeplearning.plugins.INDArrayWeights\n",
"import com.thoughtworks.deeplearning.plugins.Builtins\n",
" \n",
"val hyperparameters = Factory[Builtins with INDArrayDumping].newInstance(dumpingPathPrefix = \"/Users/TerrorJack\")"
]
}
],
"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 INDArrayDumping extends INDArrayWeights {
val dumpingPathPrefix: String
private var currentDumped: Int = 0
override type INDArrayWeight <: INDArrayWeightApi with Weight
trait INDArrayWeightApi extends super.INDArrayWeightApi {
this: INDArrayWeight =>
override protected def backward[SubtypeOfOptimizer](
originalDelta: INDArray)(
implicit implicitApplyRest: ImplicitApply.Aux[PartiallyAppliedOptimizer, SubtypeOfOptimizer],
asOptimizer: SubtypeOfOptimizer <:< OptimizerApi {type Delta <: INDArray}
): Do[Unit] = {
super.backward(originalDelta).map { _ =>
val os = new ObjectOutputStream(new FileOutputStream(dumpingPathPrefix + "/" + currentDumped.toString))
try {
os.writeObject(data)
} finally {
os.close()
}
currentDumped += 1
}
}
}
}
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