Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export class Network {
- private activations: {[type: string]: (x: number) => number}
- /**
- * creates a new network.
- * @param {Model} the network model.
- * @returns {Network}
- */
- constructor(private model: neuron.Model) {
- this.activations = {
- "linear" : (x: number) => (x),
- "sigmoid" : (x: number) => (1.0 / (1.0 + Math.exp(-x))),
- "tanh" : (x: number) => (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x))
- }
- // randomly initialize weights.
- this.model.layers().each(layer => {
- layer.neurons().skip(1).each(neuron => {
- neuron.outputs().each(synapse => {
- //synapse.weight(Math.random())
- synapse.weight(0.5)
- })
- })
- })
- }
- /**
- * forward propagates the given input through the network.
- * @param {Array<number>} the input to this network.
- * @returns {Array<number>} the networks output layer.
- */
- public forward(input: Array<number>): Array<number> {
- this.model.layers()
- .first()
- .neurons()
- .skip(1)
- .each((neuron, index) =>
- neuron.value(input[index]))
- this.model.layers().skip(1).each(hidden => {
- let activation = this.activations[hidden.activation()]
- hidden.neurons().each(neuron => {
- neuron.value(
- activation(
- neuron.inputs().aggregate((acc, synapse) => {
- return (acc + (synapse.weight() * synapse.target().value()))
- }, 0)
- )
- )
- })
- })
- return this.model.layers()
- .last()
- .neurons()
- .skip(1)
- .select(neuron => neuron.value())
- .collect()
- }
- /**
- * trains this network via back propagation where the actual value
- * obtained from a call to forward() on this network is compared against
- * the ideal value given.
- * @param {Array<number>} the actual value for this network.
- * @param {Array<number>} the ideal / expected value for this actual.
- * @returns {void}
- */
- public backward(actual: Array<number>, ideal: Array<number>): void {
- // todo: mission
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement