liveBook · Manning

Neural network layer

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

A layer is an organized group of neural-network neurons that receives signals from a preceding layer and produces outputs for the next layer.

A neural network layer groups neurons so their outputs can become inputs to the next group. In a feed-forward network, signals move one way: from the input layer, through hidden layers, to the output layer. Every neuron in one layer connects to every neuron in the next. The signals are floating-point numbers; the final numbers need interpretation to become a result. section 7.2

Figure 7.3. A simple neural network with one input layer of two neurons, one hidden layer of four neurons, and one output layer of three neurons. The number of neurons in each layer in this figure is arbitrary.

The diagram shows two input neurons, four hidden neurons, and three output neurons. Those counts illustrate a possible layout, not required layer sizes. section 7.2

A small Swift implementation gives each layer three jobs:

  • Hold its neurons, a reference to the previous layer, and a cached array of outputs. section 7.5
  • Pass inputs straight through if it is the input layer; otherwise, call each neuron's output method and cache the resulting values. section 7.5
  • During training, calculate neuron deltas from expected outputs or from the next layer's weights and deltas. Weight updates then use the previous layer's cached outputs. section 7.5

Passing signals forward

A network can pass an array of Double values through its layers with `layers.reduce(input) { $1.outputs(inputs: $0) }`. Each call receives the preceding layer's output array; the last call returns the network's output. section 7.5

A neuron's output combines its inputs and weights, then applies an activation function before reaching the next layer. The sigmoid function is one available choice: it keeps each resulting value between 0 and 1, and its derivative can be used in backpropagation. section 7.5 section 7.4

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Defines input, hidden, and output layers, then implements forwarding and delta calculations without deriving the underlying calculus formulas.
Examples
Uses a two-input, four-hidden, three-output diagram and Swift Layer and Network methods.
What it assumes
Assumes some Swift familiarity; leaves vectors, dot products, and calculus derivations unexplained while keeping the code approachable.
What you can do after
Build it from scratch as Swift layers that pass signals forward and support training updates.
How it connects
Links neurons, activation functions, feed-forward networks, and backpropagation through outputs, cached values, and deltas.

Also explained in

Compare how 7 books explain neural network layer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.