Chapter 26

Neural Networks

Learning Objectives

  • Understand neural network basics
  • Learn about layers and neurons
  • Understand forward propagation
  • See how neural networks process audio

What are Neural Networks?

Neural networks are computing systems inspired by biological brains. They consist of interconnected nodes (neurons) organized in layers.

  • Input layer: Receives data
  • Hidden layers: Process data (can be many)
  • Output layer: Produces results

Each connection has a weight that the network learns. These weights determine how signals flow through the network.

Neurons and Activations

// A single neuron
function neuron(inputs, weights, bias) {
    // 1. Weighted sum
    let sum = bias;
    for (let i = 0; i < inputs.length; i++) {
        sum += inputs[i] * weights[i];
    }
    
    // 2. Activation function (adds non-linearity)
    return relu(sum);
}

// Common activation functions
function relu(x) {
    return Math.max(0, x); // Rectified Linear Unit
}

function sigmoid(x) {
    return 1 / (1 + Math.exp(-x)); // S-curve, output 0-1
}

function tanh(x) {
    return Math.tanh(x); // Output -1 to 1
}

// Activation functions decide which neurons "fire"
// They add non-linearity, allowing networks to learn complex patterns

Forward Propagation

// Neural network forward pass
function forwardPropagate(input, network) {
    let current = input;
    
    for (const layer of network.layers) {
        const outputs = [];
        
        for (const neuron of layer.neurons) {
            const output = neuron(current, neuron.weights, neuron.bias);
            outputs.push(output);
        }
        
        current = outputs; // Output becomes next layer's input
    }
    
    return current; // Final output
}

// Example: simple network for audio classification
// Input: 128 features (spectral features)
// Hidden: 64 neurons
// Output: 4 classes (vocals, drums, bass, other)

const network = {
    layers: [
        { neurons: 64 }, // Hidden layer
        { neurons: 4 } // Output layer (4 stem classes)
    ]
};

const prediction = forwardPropagate(audioFeatures, network);
// prediction = [0.8, 0.1, 0.05, 0.05]
// Means: 80% vocals, 10% drums, 5% bass, 5% other

Convolutional Neural Networks (CNNs)

CNNs are specialized for grid-like data (images, spectrograms). They use convolutionβ€”sliding filters across the input.

// Convolution operation
function convolve(input, kernel) {
    const output = [];
    
    for (let y = 0; y < input.height - kernel.height; y++) {
        for (let x = 0; x < input.width - kernel.width; x++) {
            let sum = 0;
            
            // Slide kernel over input
            for (let ky = 0; ky < kernel.height; ky++) {
                for (let kx = 0; kx < kernel.width; kx++) {
                    sum += input[y + ky][x + kx] * kernel[ky][kx];
                }
            }
            
            output.push(sum);
        }
    }
    
    return output;
}

// CNNs are great for audio because:
// 1. Spectrograms are 2D (time Γ— frequency)
// 2. CNNs can detect local patterns (drum hits, harmonics)
// 3. They're translation-invariant (recognize patterns anywhere)

🎡 MixMind Connection

How MixMind Uses Neural Networks

// MixMind uses a U-Net architecture for stem separation
// U-Net: encoder-decoder with skip connections

class UNetModel {
    // Architecture:
    // Encoder: downsamples spectrogram, extracts features
    // Bottleneck: processes at lowest resolution
    // Decoder: upsamples, reconstructs spectrograms
    // Skip connections: preserve fine details
    
    async run(spectrogram) {
        // Encoder path
        let x = spectrogram;
        const skipConnections = [];
        
        x = this.convBlock(x, 16); // [batch, 16, time, freq]
        skipConnections.push(x);
        x = this.downsample(x); // [batch, 16, time/2, freq/2]
        
        x = this.convBlock(x, 32);
        skipConnections.push(x);
        x = this.downsample(x); // [batch, 32, time/4, freq/4]
        
        // ... more layers ...
        
        // Bottleneck
        x = this.convBlock(x, 256);
        
        // Decoder path (with skip connections)
        x = this.upsample(x);
        x = this.concat(x, skipConnections.pop());
        x = this.convBlock(x, 32);
        
        // ... more layers ...
        
        // Output: 4 spectrograms (vocals, drums, bass, other)
        return x;
    }
}

// This architecture learns to:
// - Identify frequency patterns for each source
// - Separate overlapping sounds
// - Preserve temporal structure
// - Handle different instruments and voices

Exercise 26.1: Practice

Goal: Apply what you've learned in this chapter

Try the concepts from this chapter in your browser's console or a simple HTML file. Experiment with the code examples and modify them to see what happens.

πŸ“ Quick Reference:

Review the key concepts from this chapter and make sure you understand each one before moving to the next chapter.