concept layer in category algorithms

appears as: layer, layers, layers, A layer, layer
Classic Computer Science Problems in Python

This is an excerpt from Manning's book Classic Computer Science Problems in Python.

It can be very time-consuming to peel back an onion, layer-by-layer, as a breadth-first search does. Like a BFS, an A* search aims to find the shortest path from start state to goal state. Unlike the preceding BFS implementation, an A* search uses a combination of a cost function and a heuristic function to focus its search on pathways most likely to get to the goal quickly.

7.4.2. Implementing layers

A layer in our network will need to maintain three pieces of state: its neurons, the layer that preceded it, and an output cache. The output cache is similar to that of a neuron, but up one level. It caches the outputs (after activation functions are applied) of every neuron in the layer.

At creation time, a layer’s main responsibility is to initialize its neurons. Our Layer class’s __init__() method therefore needs to know how many neurons it should be initializing, what their activation functions should be, and what their learning rates should be. In this simple network, every neuron in a layer has the same activation function and learning rate.

Listing 7.4. layer.py
from __future__ import annotations
from typing import List, Callable, Optional
from random import random
from neuron import Neuron
from util import dot_product

class Layer:
    def __init__(self, previous_layer: Optional[Layer], num_neurons: int,
     learning_rate: float, activation_function: Callable[[float], float],
     derivative_activation_function: Callable[[float], float]) -> None:
         self.previous_layer: Optional[Layer] = previous_layer
         self.neurons: List[Neuron] = []
         # the following could all be one large list comprehension 
         for i in range(num_neurons):
            if previous_layer is None:
                random_weights: List[float] = []
            else:
                random_weights = [random() for _ in range(len(previous_
     layer.neurons))]
            neuron: Neuron = Neuron(random_weights, learning_rate,
     activation_function, derivative_activation_function)
            self.neurons.append(neuron)
        self.output_cache: List[float] = [0.0 for _ in range(num_neurons)]


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":4}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":4}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":22}],[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":18}],[{\"line\":2,\"ch\":12},{\"line\":2,\"ch\":18}],[{\"line\":3,\"ch\":12},{\"line\":3,\"ch\":18}],[{\"line\":4,\"ch\":10},{\"line\":4,\"ch\":16}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":4}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":4}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":22}],[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":18}],[{\"line\":2,\"ch\":12},{\"line\":2,\"ch\":18}],[{\"line\":3,\"ch\":12},{\"line\":3,\"ch\":18}],[{\"line\":4,\"ch\":10},{\"line\":4,\"ch\":16}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":4}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":4}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":22}],[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":18}],[{\"line\":2,\"ch\":12},{\"line\":2,\"ch\":18}],[{\"line\":3,\"ch\":12},{\"line\":3,\"ch\":18}],[{\"line\":4,\"ch\":10},{\"line\":4,\"ch\":16}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":4}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":4}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":22}],[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":18}],[{\"line\":2,\"ch\":12},{\"line\":2,\"ch\":18}],[{\"line\":3,\"ch\":12},{\"line\":3,\"ch\":18}],[{\"line\":4,\"ch\":10},{\"line\":4,\"ch\":16}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":4}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":4}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":4}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":4}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":4}],[{\"line\":0,\"ch\":16},{\"line\":0,\"ch\":22}],[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":18}],[{\"line\":2,\"ch\":12},{\"line\":2,\"ch\":18}],[{\"line\":3,\"ch\":12},{\"line\":3,\"ch\":18}],[{\"line\":4,\"ch\":10},{\"line\":4,\"ch\":16}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":5}],[{\"line\":7,\"ch\":4},{\"line\":7,\"ch\":7}],[{\"line\":9,\"ch\":66},{\"line\":9,\"ch\":70}],[{\"line\":14,\"ch\":33},{\"line\":14,\"ch\":37}],[{\"line\":13,\"ch\":9},{\"line\":13,\"ch\":12}],[{\"line\":17,\"ch\":43},{\"line\":17,\"ch\":46}],[{\"line\":22,\"ch\":46},{\"line\":22,\"ch\":49}],[{\"line\":1,\"ch\":8},{\"line\":1,\"ch\":10}],[{\"line\":7,\"ch\":10},{\"line\":7,\"ch\":12}],[{\"line\":8,\"ch\":10},{\"line\":8,\"ch\":12}],[{\"line\":12,\"ch\":21},{\"line\":12,\"ch\":23}],[{\"line\":13,\"ch\":15},{\"line\":13,\"ch\":17}],[{\"line\":17,\"ch\":49},{\"line\":17,\"ch\":51}],[{\"line\":19,\"ch\":57},{\"line\":19,\"ch\":59}],[{\"line\":22,\"ch\":52},{\"line\":22,\"ch\":54}],[{\"line\":14,\"ch\":12},{\"line\":14,\"ch\":14}],[{\"line\":14,\"ch\":30},{\"line\":14,\"ch\":37}],[{\"line\":16,\"ch\":12},{\"line\":16,\"ch\":16}],[{\"line\":13,\"ch\":9},{\"line\":13,\"ch\":12}],[{\"line\":17,\"ch\":43},{\"line\":17,\"ch\":46}],[{\"line\":22,\"ch\":46},{\"line\":22,\"ch\":49}],[{\"line\":1,\"ch\":8},{\"line\":1,\"ch\":10}],[{\"line\":7,\"ch\":10},{\"line\":7,\"ch\":12}],[{\"line\":8,\"ch\":10},{\"line\":8,\"ch\":12}],[{\"line\":12,\"ch\":21},{\"line\":12,\"ch\":23}],[{\"line\":13,\"ch\":15},{\"line\":13,\"ch\":17}],[{\"line\":17,\"ch\":49},{\"line\":17,\"ch\":51}],[{\"line\":19,\"ch\":57},{\"line\":19,\"ch\":59}],[{\"line\":22,\"ch\":52},{\"line\":22,\"ch\":54}],[{\"line\":13,\"ch\":9},{\"line\":13,\"ch\":12}],[{\"line\":17,\"ch\":43},{\"line\":17,\"ch\":46}],[{\"line\":22,\"ch\":46},{\"line\":22,\"ch\":49}],[{\"line\":1,\"ch\":8},{\"line\":1,\"ch\":10}],[{\"line\":7,\"ch\":10},{\"line\":7,\"ch\":12}],[{\"line\":8,\"ch\":10},{\"line\":8,\"ch\":12}],[{\"line\":12,\"ch\":21},{\"line\":12,\"ch\":23}],[{\"line\":13,\"ch\":15},{\"line\":13,\"ch\":17}],[{\"line\":17,\"ch\":49},{\"line\":17,\"ch\":51}],[{\"line\":19,\"ch\":57},{\"line\":19,\"ch\":59}],[{\"line\":22,\"ch\":52},{\"line\":22,\"ch\":54}]]"}
!@%STYLE%@!

As signals are fed forward through the network, the Layer must process them through every neuron. (Remember that every neuron in a layer receives the signals from every neuron in the previous layer.) outputs() does just that. outputs() also returns the result of processing them (to be passed by the network to the next layer) and caches the output. If there is no previous layer, that indicates the layer is an input layer, and it just passes the signals forward to the next layer.

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

This is an excerpt from Manning's book Classic Computer Science Problems in Swift: Essential techniques for practicing programmers.

It can be very time consuming to peel back an onion, layer-by-layer, as a breadth-first search does. Like a BFS, an A* search aims to find the shortest path from a start state to a goal state. Unlike the preceding BFS implementation, an A* search uses a combination of a cost function and a heuristic function to focus its search on pathways most likely to get to the goal quickly.

7.5.2. Implementing layers

A layer in our network will need to maintain three pieces of state: its neurons, the layer that preceded it, and an output cache. The output cache is similar to that of a neuron, but up one level. It caches the outputs (after activation functions are applied) of every neuron in the layer.

class Layer {
    let previousLayer: Layer?
    var neurons: [Neuron]
    var outputCache: [Double]

At creation time, a layer’s main responsibility is to initialize its neurons. Our Layer class’s init() method therefore needs to know how many neurons it should be initializing, what their activation functions should be, and what their learning rates should be. In this simple network, every neuron in a layer has the same activation function and learning rate.

    init(previousLayer: Layer? = nil, numNeurons: Int, activationFunction:
     @escaping (Double) -> Double, derivativeActivationFunction: @escaping
     (Double) -> Double, learningRate: Double) {
        self.previousLayer = previousLayer
        self.neurons = Array<Neuron>()
        for _ in 0..<numNeurons {
            self.neurons.append(Neuron(weights: randomWeights(number:
             previousLayer?.neurons.count ?? 0), activationFunction:
             activationFunction, derivativeActivationFunction:
             derivativeActivationFunction, learningRate: learningRate))
        }
        self.outputCache = Array<Double>(repeating: 0.0,
         count: neurons.count)
    }

As signals are fed forward through the network, the Layer must process them through every neuron (remember that every neuron in a layer receives the signals from every neuron in the previous layer). outputs() does just that. outputs() also returns the result of processing them (to be passed by the network to the next layer) and caches the output. If there is no previous layer, that indicates the layer is an input layer, and it just passes the signals forward to the next layer.