Skip to content

Instantly share code, notes, and snippets.

@redwrasse
Created July 19, 2017 03:09
Show Gist options
  • Save redwrasse/33c53604cf45b746c7df1940a1521077 to your computer and use it in GitHub Desktop.
Save redwrasse/33c53604cf45b746c7df1940a1521077 to your computer and use it in GitHub Desktop.
Generic neural network in Scala
abstract class NeuralNetwork {
val numImmediateInputs: Int
var children: Map[Int, NeuralNetwork]
/**
* The integration function contains implicitly the weights
* for this compute unit
*/
def integration(immediateInput: Array[Double]): Double
/**
* The activation function
*/
def activation(e: Double): Double
/**
* Compute the output given the specified input
*/
def compute(input: Array[Double]): Double = {
// require (input.size == getNumInputs())
val numInputs = getNumInputs()
var inputsToPeelOff: Array[Double] = input
var immediateInputs: Array[Double] = Array.empty
var childNetwork: Option[NeuralNetwork] = None
var childInput: Array[Double] = null
var childOutput: Double = 0.0
var j: Int = 0
for (i <- (0 to numImmediateInputs-1)) {
childNetwork = child(i)
if (childNetwork.isDefined) {
j = childNetwork.get.getNumInputs()
childInput = inputsToPeelOff.slice(0, j)
inputsToPeelOff = inputsToPeelOff.slice(j, numInputs)
childOutput = childNetwork.get.compute(childInput)
immediateInputs = immediateInputs :+ childOutput
} else {
immediateInputs = immediateInputs :+ inputsToPeelOff.head
inputsToPeelOff = inputsToPeelOff.tail
}
}
// require (inputsToPeelOff.size == 0)
activation(integration(immediateInputs))
}
/**
* Add child nodes at the specified input indices.
* Children are added at immediate input indices, *not*
* leaf indices
*/
def addChildren(m: Map[Int, NeuralNetwork]) = {
children = children ++ m
}
/**
* Returns a potential child at the given index;
* potential children can exist at any of 0 to numImmediateInputs-1
* indices.
*/
def child(i: Int): Option[NeuralNetwork] = {
children.get(i)
}
/**
* Returns the number of children for this compute unit
*/
def getNumChildren(): Int = {
(0 to numImmediateInputs-1).filter(i => child(i).isDefined).size
}
/**
* Returns the number of incoming vertices for this compute unit
*/
def getNumImmediateInputs(): Int = {
numImmediateInputs
}
/**
* Returns the number of leaf incoming vertices for
* the composite neural network
*/
def getNumInputs(): Int = {
(0 to numImmediateInputs-1)
.map(i => child(i))
.map { c => c match {
case None => 1
case _ => c.get.getNumInputs()
}
}.sum
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment