AI

Basics of neural networks and how they work

Master the basics of neural networks and how they work: layers, backpropagation, Python code, risks, and the 2026 architectures behind every AI product.
Diagram showing the basics of neural networks and how they work with input layer, hidden layers, weights, activation functions, and output predictions

Introduction

The basics of neural networks and how they work sit behind every chatbot reply, every photo tag, and every recommendation feed you touch each day. A neural network is a layered math function that converts raw input numbers into predictions by stacking weights, biases, and nonlinear activations. Grand View Research pegged the artificial neural network market at roughly USD 346 million in 2023, with a projected 21.9 percent CAGR through 2030. The field has grown from an academic curiosity into the backbone of modern software, powering foundation models, computer vision, speech recognition, and healthcare diagnostics. This 2026 guide covers the full picture, from a single neuron through transformers, with code, case studies, and risk coverage. Expect concrete math, named deployments, and the current research frontier instead of vague metaphors. By the end, you will know how neural networks actually work, where they already run, and where the field is heading next.

Quick Answers on the Basics of Neural Networks

What are the basics of neural networks and how they work?

Neural networks stack layers of weighted sums, biases, and activation functions to turn inputs into predictions. Training uses forward propagation, loss computation, and backpropagation to adjust the weights.

How does a neural network learn from data in the basics of neural networks and how they work?

A neural network learns by running forward propagation, measuring the loss against the true label, then backpropagating gradients. An optimizer updates each weight to lower the loss on the next pass.

Which neural network should I use for a given task?

Pick a convolutional network for images, a transformer for text or multimodal data, a graph neural network for molecules, and a feedforward network for tabular prediction baselines.

Key Takeaways

  • The basics of neural networks and how they work rest on three repeating steps: forward propagation, loss computation, and backpropagation driven by gradient descent.
  • Transformers dominate language, vision, and multimodal tasks in 2026, while convolutional networks still lead on small image datasets.
  • Training a modern foundation model can cost tens of millions of dollars and emit hundreds of thousands of pounds of CO2.
  • Governance, bias testing, and ethical review are now mandatory for enterprise deployment under the EU AI Act and emerging US rules.

Table of contents

What Is a Neural Network, Explained Simply

A neural network is a layered math function that stacks weighted sums, biases, and nonlinear activation functions, trained on labeled examples with gradient-based updates covering the basics of neural networks and how they work.

An Interactive From AIplusInfo

Explore a Neural Network’s Training Dynamics

Pick an architecture, adjust the depth, width, learning rate, and dataset size, and watch estimated final accuracy, training time, and energy footprint update in real time.

Feedforward MLP

simplefrontier

3 layers

124

0.001

1e-51e-1

60,000

1k100M

Predicted outcome

Estimated final accuracy97.1%
Training time (single GPU)4 min
Energy footprint estimate0.08 kWh

Benchmarks grounded in the Stanford AI Index 2024 compute scaling data and the Strubell et al. 2019 training energy study.

The Short History of Neural Networks From Rosenblatt to Transformers

Beyond the modern transformer hype, the field has a long history that explains why the past five years looked so different from the previous fifty. Warren McCulloch and Walter Pitts proposed the first mathematical neuron in 1943, treating logic gates as excitable cells. Frank Rosenblatt built the Mark I Perceptron at Cornell in 1958, a machine with 400 photocells wired to a learning algorithm. The basics of neural networks and how they work trace back to this single-layer perceptron and its surprisingly stubborn mathematical limits. The New York Times quoted Rosenblatt predicting walking, talking, conscious machines within a few years. The predictions did not hold, and in 1969 Minsky and Papert showed single-layer perceptrons could not learn simple XOR patterns. Funding dried up and the first AI winter began, lasting most of the 1970s across both government and academic laboratories.

The field recovered when Rumelhart, Hinton, and Williams published practical backpropagation in 1986, giving multi-layer networks a real training method. The 2012 AlexNet paper from Krizhevsky, Sutskever, and Hinton halved image classification error on ImageNet by training a convolutional network on two GPUs. Vaswani and colleagues introduced the transformer architecture in 2017, replacing recurrence with attention and reshaping natural language processing. By 2026 the transformer has become the dominant pattern for text, code, images, and even protein folding systems like AlphaFold. Reading a focused deep learning primer clarifies how each era built on the last.

The Neuron as the Working Unit of Every Network

Every neural network reduces, at its smallest scale, to a single artificial neuron doing three steps. The neuron takes a vector of input numbers, multiplies each input by its own weight, and sums the products together. It adds a bias term so the sum can shift up or down independent of inputs. The sum then passes through an activation function that squashes or clips it. The output of the activation becomes the input to neurons in the next layer. That cycle repeats until the final layer produces a prediction. Everything else in neural network engineering optimizes some aspect of this basic cell.

The weighted sum is the dot product of the input vector and the weight vector, a staple of linear algebra. If an input vector has five numbers, the neuron needs five weights plus one bias, giving six learnable parameters. A layer with one hundred such neurons therefore holds six hundred parameters, and modern language models stack layers to reach hundreds of billions. The scale explains why training runs demand GPUs, TPUs, or specialized accelerators like Cerebras wafers. The humble artificial neuron is the atomic ingredient that scales from a textbook demo to a frontier foundation model.

Beginners often picture a neuron as a biological cell with dendrites and axons firing electric spikes. The analogy helps intuition but misses the point that modern deep learning is pure matrix algebra. A real brain cell is wet, slow, and spiking, while an artificial neuron is a floating point multiply-add instruction. The two share nothing but a loose metaphor from the 1940s cybernetics movement. Keeping that distinction clear prevents the common mistake of overestimating what neural networks understand about their own outputs.

Despite the limits of the biological comparison, the neuron abstraction has proven astonishingly productive across domains. The same formula powers image classifiers that read mammograms, speech models that transcribe calls, and language models that write code. Different fields pick different activation functions and connection patterns, but the inner loop stays the same. That universal recipe is why one framework like PyTorch can train both a vision model and a chatbot. Understanding the neuron equation makes every later part of the pipeline much easier to follow and debug.

Weights, Biases, and the Shape of a Layer

Building on the single neuron, a layer is simply many neurons sharing the same input vector but holding their own weights. If the input has 784 numbers from a 28 by 28 image, and the layer has 128 neurons, the weight matrix has 128 by 784 entries. A separate bias vector of length 128 shifts each neuron’s output. The whole layer evaluates in one matrix multiplication plus a vector addition, which GPUs run at teraflop speeds. Representing layers as matrices is the single most important trick that makes deep learning fast.

Weights carry the actual knowledge of the network, encoding every pattern the model has learned to detect. Biases let a neuron fire even when all inputs are zero, which matters for shifted or offset data. During training, both weights and biases change through gradient updates while the input data and labels stay fixed. A fully connected layer, also called a dense layer, connects every input to every neuron, giving maximal flexibility. Convolutional layers trade that flexibility for parameter sharing and translational symmetry, which AlexNet proved critical for images on ImageNet.

Choosing the number of neurons per layer, called the width, is one of the first hyperparameter decisions. Wider layers can memorize more patterns but demand more memory and risk overfitting on small datasets. Narrower layers compress information harder, forcing the network to learn efficient representations. Beginners often start with powers of two like 64, 128, or 256 because they align with GPU memory layouts. Width, depth, and connection pattern together define the capacity of the network before a single training step runs.

Activation Functions and Why Nonlinearity Matters

With that layer structure in hand, the activation function decides which signals pass through and which get suppressed. Without a nonlinear activation, stacking any number of layers would collapse into a single linear transformation. That is the hard mathematical reason the field needs functions like ReLU, sigmoid, tanh, and GELU in every modern design. The ReLU activation function in modern machine learning outputs zero for negatives and the input itself for positives, which trains fast and avoids saturation. The sigmoid function in neural networks maps any real number into the range zero to one for probability outputs.

In 2026 most hidden layers in transformer networks use GELU or SwiGLU, variants that improved perplexity on benchmark language tasks. Classification output layers typically use softmax, which turns a vector into a probability distribution summing to one. Regression output layers often use no activation, letting the network predict raw numbers. The choice of activation function changes training dynamics as much as the number of layers or the learning rate. Reading the research on each option before plugging it in saves days of debugging later.

The Full Picture of Forward Propagation

Moving on from the layer view, the full forward pass simply chains these matrix operations together in order. Input values enter layer one, pass through its weights, bias, and activation, and emerge as hidden activations. Those hidden activations become the input to layer two, which repeats the same math with its own parameters. The process continues through every hidden layer until it reaches the output layer and produces a final prediction. Each intermediate output is called a hidden representation, because it never leaves the network for external inspection.

Forward propagation is deterministic, meaning the same input and the same weights always yield the same output. The determinism is useful because it makes outputs reproducible and lets engineers compare runs across machines. Deep networks run the forward pass on batches of inputs stacked into a matrix, exploiting GPU parallelism. A batch of 64 images, each flattened to 784 numbers, becomes a 64 by 784 matrix that the first layer processes in one shot. That batching trick turns a sequence of thousands of small operations into a few giant ones that modern hardware loves.

The output of the final layer only earns its meaning once you compare it against the ground truth label. For a cat versus dog classifier, the output might be a two-number probability vector like 0.82 and 0.18. If the true label is cat, the model got it right, and if it was dog the model got it wrong. Forward propagation produces the prediction, but it says nothing about whether the prediction is any good. That second step, the loss computation, is what turns raw math into measurable learning.

Loss Functions and Measuring How Wrong a Network Is

Shifting from prediction to feedback, the loss function reduces the gap between prediction and ground truth to a single scalar. A smaller loss means a better model, and the entire training process minimizes this number over many samples. For regression tasks that predict continuous numbers, mean squared error is the standard default across most PyTorch tutorials. Mean absolute error and Huber loss offer alternatives that handle outliers more gracefully. For classification tasks with discrete labels, the standard default is cross entropy loss as applied in machine learning, which penalizes confident wrong predictions much harder than hesitant ones.

Modern training combines many loss terms, including primary task loss, regularization loss, and auxiliary loss on intermediate features. Reinforcement learning from human feedback adds a reward model whose score becomes a loss during policy updates. Every bit of training progress ultimately flows from this single scalar being smaller after an update than before it. If the loss is not dropping, something upstream is wrong in the data, the architecture, or the hyperparameters. Monitoring the loss curve during training is the first diagnostic every practitioner learns to read.

Backpropagation and the Chain Rule in Plain English

Turning to the heart of learning, backpropagation answers one question: how should each weight change to lower the loss. The answer uses the chain rule from basic calculus, applied one layer at a time from the output back to the input. The chain rule says the derivative of a composed function is the product of the inner derivatives. In a neural network, the loss is a composition of every layer’s output, so its derivative decomposes into a product chain. Rumelhart, Hinton, and Williams showed in 1986 that computing this chain is cheap when you cache intermediate values during the forward pass.

In concrete terms, backpropagation runs the forward pass once, saving every layer’s activations in memory. It then computes the derivative of the loss with respect to the output, called the gradient, as a vector. Working backward, it multiplies that gradient by the local derivative of the output layer, giving the gradient for the previous layer. The process repeats through every layer until each weight and bias has its own gradient. Each gradient tells the optimizer which direction and how much that parameter should move to reduce loss.

The algorithm’s efficiency comes from reusing cached values rather than recomputing the forward pass for each parameter. Without caching, a model with ten million weights would need ten million forward passes per training step. With caching, the entire backward pass costs roughly the same as a single forward pass. Backpropagation turns an impossible search problem into a tractable gradient computation that modern GPUs run in milliseconds. Deep learning frameworks like PyTorch and JAX automate this entirely through automatic differentiation.

Beginners often get stuck on the chain rule algebra, but the practical takeaway is simpler than the math. Every operation in a network has a known local derivative, and the framework handles the chaining. All you write is the forward pass, and PyTorch’s autograd engine builds the backward pass automatically. If the loss is not decreasing, the problem usually lies in data preprocessing, learning rate, or exploding gradients, not in backprop itself. Reading a focused walkthrough on Stanford’s CS231n backpropagation notes is still the clearest free resource online.

Gradient Descent and the Role of Optimizers

Next, the optimizer decides how to use the computed gradients to update the weights in each training step. Vanilla gradient descent subtracts a small fraction of the gradient, scaled by a hyperparameter called the learning rate. Pick a learning rate too high and the loss diverges, picking one too low and training stalls indefinitely. Stochastic gradient descent processes small random batches instead of the full dataset, trading noise for massive speed. Research teams in the 1990s rediscovered this trick after it fell out of favor, and it now underpins every production training run.

Modern optimizers add momentum, adaptive learning rates, and weight decay on top of plain stochastic gradient descent. The Adam optimizer used in machine learning, published by Kingma and Ba in 2014, remains the default choice for most new projects. Adam tracks a running mean and variance of each weight’s gradient, scaling updates individually per parameter. AdamW, Lion, and Sophia are more recent variants that handle regularization or memory more efficiently. The optimizer choice influences convergence speed as much as the network depth or the batch size.

Beyond raw optimizer choice, learning rate schedules shape training in subtle ways across millions of steps. Cosine decay reduces the learning rate smoothly from a peak to near zero over the training run. Warmup starts at near zero and ramps up linearly for the first few thousand steps to stabilize early updates. Transformer language models combine warmup with cosine or linear decay in almost every published recipe. Picking a schedule aligned with your data size and architecture avoids the common training collapses that waste GPU hours.

Training Data, Batches, and Epochs in Practice

Moving on to the practical side, training data is the raw fuel every neural network consumes to learn anything useful. A dataset gets split into training, validation, and test subsets, typically in a 70-15-15 or 80-10-10 ratio. The training set feeds gradient updates, the validation set tunes hyperparameters, and the test set measures final generalization. Loading data in small batches keeps memory bounded while still giving the optimizer a meaningful gradient estimate. The data augmentation techniques used in machine learning expand a dataset by flipping, cropping, or jittering samples before training.

One pass through the full training set is called an epoch, and most tasks need somewhere between 3 and 300 epochs. The exact number depends on dataset size, architecture complexity, and target accuracy. Modern language models often train for a fractional epoch because the dataset is so large that the model sees each sample roughly once. The ratio of parameters to training tokens, known as the Chinchilla ratio, is the single most predictive factor for final model quality. DeepMind’s Chinchilla paper from 2022 showed that most pretrained models at the time were severely undertrained for their size.

Overfitting, Regularization, and Generalization

Shifting focus to generalization, a well-trained network must perform on data it has never seen, not just on its training batches. A model that memorizes the training set but fails on new inputs is said to overfit. The symptoms show up as a shrinking training loss while the validation loss plateaus or climbs. Overfitting versus underfitting appears in nearly every real project, and understanding the pattern is a core skill. The remedy involves either shrinking the model, adding more training data, or applying regularization techniques during training.

Dropout, published by Srivastava and Hinton in 2014, randomly zeroes a fraction of neurons during each training step. The random masking forces the network to spread knowledge across many pathways, preventing single-neuron fragility. L2 weight decay penalizes large weight values by adding their squared magnitude to the loss function. Early stopping tracks the validation loss and halts training once the curve stops improving. Each regularizer trades a bit of training fit for a bigger gain in test accuracy.

Modern networks also use batch normalization to speed up training and layer normalization to stabilize activations and improve generalization. Normalization layers rescale hidden activations to zero mean and unit variance, reducing covariate shift between layers. Regularization is not optional in modern deep learning, it is the default assumption of every production training recipe. Beginner tutorials sometimes skip these pieces, but production models would overfit catastrophically without them. Treating regularization as a first-class design decision rather than an afterthought saves many late-stage debugging cycles.

The Main Neural Network Architectures and When to Use Them

Beyond the basic feedforward design, several specialized architectures dominate specific data types in production. Multi-layer perceptrons, the fully connected form of the generic design, work well on tabular data where features have no spatial or temporal order. Convolutional neural networks share weights across small windows of input, which exploits locality in images and audio spectrograms. Recurrent neural networks process sequences by passing a hidden state forward through time, useful for text and sensor streams. Each architecture encodes an inductive bias that reduces how much data the model needs to generalize well.

Transformers replaced recurrence with self-attention in 2017 and now dominate language, vision, speech, and multimodal tasks. Attention lets every token look at every other token through a learned similarity, capturing long-range dependencies. Graph neural networks extend the pattern to arbitrary node and edge structures, useful for molecules, social networks, and knowledge graphs. Geometric deep learning generalizes this idea to networks that respect symmetries like rotation or translation. Each family of architectures has matured enough to have standard open source implementations.

Generative models form a distinct branch that aims to produce new samples rather than classify existing ones. The generative adversarial networks approach to deep learning pairs a generator against a discriminator in a two-player minimax game. Variational autoencoders learn a compressed latent representation that lets sampling reconstruct plausible new data. Diffusion models, the backbone of Stable Diffusion and Midjourney, learn to reverse a slow noise-adding process. The same math scales from toy MNIST digits to 4K photorealistic images and 60-second video clips.

Reinforcement learning adds reward signals and policy gradients to the neural network stack, letting agents learn by trial. Picking the right architecture for your data cuts weeks off training time and often decides whether a project ships at all. If the inputs are images, start with a convolutional backbone or vision transformer. If the inputs are text or sequences, start with a transformer encoder or decoder. If the inputs are tabular, start with gradient boosted trees first and move to a neural net only when the data justifies it.

Where Neural Networks Already Operate in Daily Life

Turning to everyday experience, neural networks already run inside almost every smartphone and major consumer app on the planet. Apple’s Neural Engine on the iPhone processes trillions of operations per second for Face ID, photo understanding, and keyboard prediction. Google Photos classifies images by object, place, and face using transformer-based vision models. Netflix generates personalized recommendations using matrix factorization neural networks across 300 million subscribers. Spotify’s recommendation engine, documented in its public safety and privacy explainer, uses deep neural networks across billions of weekly listening events.

Automotive systems have moved from classical computer vision to full neural stacks for lane detection and object tracking. Tesla’s end-to-end driving stack uses a vision transformer trained on billions of frames of real driver data. Agricultural firms like John Deere deploy neural networks on tractors to identify weeds and spray herbicide selectively. Hospitals use convolutional networks to triage chest X-rays, read mammograms, and detect diabetic retinopathy. Most of the AI you touch each day is neural, even when the product never uses the term in its marketing.

Enterprise software has absorbed neural networks for document classification, fraud detection, and chatbot support. The Stanford AI Index 2024 reports a 78 percent year-over-year rise in corporate AI adoption, driven largely by foundation model APIs. Banks use neural networks for credit decisions under tight regulatory review, since model explainability now sits inside compliance requirements. Insurers price risk using neural pricing engines on home, auto, and health policies. The adoption curve is unlikely to flatten before most line-of-business software has embedded models.

The Hidden Costs of Training Modern Neural Networks

Stepping back from applications, training large neural networks consumes substantial energy, water, and capital that rarely show up in product marketing. A 2019 University of Massachusetts study by Strubell and colleagues estimated that training one transformer with neural architecture search can emit 626,155 pounds of CO2. Modern frontier model runs cost tens of millions of dollars in compute alone, and inference bills can exceed training bills within months. Google and Microsoft reported combined 2023 water usage over 6 billion gallons, partly tied to AI datacenter cooling. The International Energy Agency projected global datacenter electricity to roughly double between 2022 and 2026 due in part to AI workloads.

Those external costs sit on top of the engineering costs of data labeling, cleaning, and curation. Scale AI, Surge AI, and other labeling firms have grown into multi-billion-dollar businesses on the back of model training needs. Many teams reduce this bill by applying transfer learning across pretrained weights instead of training from scratch. Human preference labels for reinforcement learning from human feedback can cost between USD 1 and USD 50 per label at frontier quality. The visible model is the small, exposed tip of a much larger iceberg of data labor, electricity, and capital expenditure. Honest totals matter to anyone making budgeting, procurement, or policy decisions about neural network deployment.

The Ethics of Deploying Neural Networks at Scale

Looking beyond compute costs, deploying neural networks at scale raises ethical questions that algorithmic choices alone cannot resolve. Facial recognition systems from major vendors have shown accuracy gaps across race and gender in NIST benchmarks spanning the last decade. Credit scoring and hiring algorithms can replicate historical biases if their training data reflects discriminatory decisions. Content recommendation engines have been credibly linked to teen mental health harms in internal Meta research disclosed by Frances Haugen in 2021. Policy response has sharpened, with the EU AI Act entering force in August 2024 and the US issuing executive orders on model safety.

Transparency about training data provenance has become a flashpoint for creators whose work was scraped without consent. Multiple class action lawsuits by authors, artists, and news publishers are testing the limits of fair use. The Stanford AI Index 2024 counted a 56.3 percent year-over-year increase in reported AI incidents, including hallucinations, misinformation, and deepfake fraud. Model cards, data sheets, and system cards have become standard documentation in regulated deployments. Each piece aims to make the eventual harms investigable rather than invisible.

Enterprises deploying neural networks in production now need legal, product, and engineering alignment around model governance. Governance covers red teaming, access control, bias testing, incident response, and routine model refreshes. Ethics is not a tax on neural network deployment, it is the quality gate that keeps products inside regulatory and reputational bounds. Teams that treat it that way tend to ship faster through legal review than teams that bolt it on at the end. Governance frameworks like the NIST AI Risk Management Framework offer a free, well-structured starting point for anyone building a program.

The Risks and Failure Modes Every Builder Should Know

Shifting from policy to engineering, neural networks fail in characteristic ways that every builder learns to spot and defend against. Adversarial examples are inputs with tiny, imperceptible perturbations that cause confident misclassification, documented by Szegedy and colleagues in 2013. Data drift occurs when the live input distribution diverges from training data, silently eroding accuracy without raising errors. Model collapse happens when generative models trained on their own outputs lose diversity across generations. Prompt injection attacks exploit language models by hiding instructions in input data, a growing concern for agentic systems.

Defensive practices include adversarial training, drift detection with statistical tests, output filtering, and watermarked synthetic data. The OWASP Top 10 for LLM Applications lists prompt injection as risk LLM01, with practical mitigations. A neural network in production is a live system that needs the same observability and incident response as any other critical software. Logging inputs, outputs, confidence scores, and runtime metrics is table stakes. Teams that treat deployment as the end of the project usually learn the hard way that it is the start.

The Future of Neural Networks and Where the Field Is Heading

Looking ahead to the next decade, several threads look likely to reshape neural networks beyond the transformer status quo. Neuromorphic hardware like Intel’s Loihi 2 and IBM’s NorthPole runs spiking neural networks at a fraction of the energy of GPUs. State space models like Mamba from Albert Gu offer transformer-level language quality with linear sequence scaling instead of quadratic. Mixture of experts architectures, which route each input to a small subset of specialist subnetworks, scale parameters without scaling inference cost. The Mamba paper on arxiv showed that selective state spaces can match transformer quality while running orders of magnitude faster on long context.

Multimodal foundation models that handle text, image, video, audio, and action tokens in one architecture are rapidly consolidating the field. Robotics has started adopting large vision language action models that train end to end from pixels and language to joint torques. The idea of AI as the self-designing machine, where the architecture itself is learned, grows every year through automated search. On-device inference through quantization, distillation, and sparse models is bringing frontier capabilities to phones and watches. Governance frameworks and safety research are maturing in parallel, which the field badly needs.

Beyond hardware and architecture, data strategy is becoming the primary lever in frontier labs. Synthetic data generated by models to train the next generation of models now accounts for an estimated 30 to 60 percent of pretraining tokens. The next phase of the field will be defined less by raw parameter counts and more by data curation, alignment research, and energy efficiency. Classical machine learning techniques are also having a resurgence as hybrid systems combine neural networks with symbolic reasoning. Reading current proceedings from NeurIPS and ICML gives a sharper weekly picture than any textbook can offer at this pace.

Chart From AIplusInfo

Neural Network Training Costs Compared

Approximate GPU hours and estimated carbon dioxide emissions across milestone neural network training runs, from a 2012 image classifier through 2023 foundation models.

Source: compiled from the Strubell et al. 2019 energy study, the GPT-3 training paper, and the Stanford AI Index 2024 compute tracker.

Building a Simple Neural Network in Python Step by Step

Shifting from theory to hands-on practice, this section walks through building a small feedforward neural network in Python using PyTorch. The example trains a two-layer network on the classic MNIST handwritten digit dataset, which contains 60,000 training images. The code assumes Python 3.11, PyTorch 2.4, and either CPU or GPU hardware. Follow each step in order and run the snippets in a notebook or script file. Every step pairs a short conceptual explanation with the smallest working code block that produces the next milestone.

Step 1 – Install dependencies and import libraries

Start by installing PyTorch 2.4 and torchvision 0.19 from a clean Python 3.11 virtual environment on your laptop. Virtual environments isolate dependency conflicts between projects, which saves 10 to 20 hours of debugging later. The torchvision package ships standard datasets like MNIST ready to download in 2 lines of code. Keep the install command simple so the environment matches the PyTorch team’s tested configuration exactly. Pin exact package versions in a requirements.txt file to make the training run reproducible across 100 machines. The entire install usually finishes in under 3 minutes on a modern laptop with a stable connection. Confirm the install worked by running python -c “import torch; print(torch.__version__)” in your shell.

python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install torch==2.4.0 torchvision==0.19.0

Once the install finishes, open a Python file or Jupyter notebook and import the libraries you will use. Import torch for tensors, torch.nn for the layer classes, and torch.optim for the Adam optimizer. The DataLoader class batches and shuffles the dataset automatically during training. Importing everything up front keeps the rest of the code short and readable.

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import datasets, transforms

Step 2 – Load and preprocess the MNIST dataset

MNIST contains 70,000 grayscale images of handwritten digits, each 28 by 28 pixels at 8-bit depth. The torchvision package downloads the 11 megabyte dataset automatically on first use and caches it to disk under ./data by default. Normalize the pixel values to a zero mean and unit variance using the published MNIST statistics of 0.1307 and 0.3081. Normalization helps the gradient descent process converge roughly 2 times faster by keeping all input scales similar. Wrap the dataset in a DataLoader so training iterates over small random batches of 64 instead of the whole set at once. A batch size of 64 is a good default on both CPU and modest GPU hardware for this network size.

transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.1307,), (0.3081,))
])

train_data = datasets.MNIST("./data", train=True, download=True, transform=transform)
test_data  = datasets.MNIST("./data", train=False, download=True, transform=transform)

train_loader = DataLoader(train_data, batch_size=64, shuffle=True)
test_loader  = DataLoader(test_data,  batch_size=256, shuffle=False)

Step 3 – Define the neural network architecture

Subclass nn.Module to define a small two-layer feedforward network with a ReLU activation between layers. The first linear layer maps 784 input pixels to 128 hidden units, and the second maps 128 hidden units to 10 output classes. The forward method flattens the image, applies the layers, and returns the raw output logits. PyTorch’s cross entropy loss handles the softmax internally, which is why the forward pass does not apply softmax here. Keep the model definition small and readable so you can modify width and depth later without rewriting training code.

class SimpleNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(28 * 28, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = x.view(x.size(0), -1)
        x = torch.relu(self.fc1(x))
        return self.fc2(x)

model = SimpleNN()

Step 4 – Choose a loss function and an optimizer

Classification tasks with 10 classes use cross entropy loss, which measures the gap between predicted class probabilities and the true label. The Adam optimizer tracks per-parameter learning rates and is the default choice for most new projects. A learning rate of 0.001 works well for small models on MNIST and is the recommended starting value. The optimizer object holds references to the model parameters and updates them during each training step. Pro tip: wrap your training loop in a try/except that logs the loss when a run crashes so you can diagnose exploding gradients.

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

Step 5 – Train the model for a few epochs

The training loop iterates over batches of 64 samples, runs forward propagation, computes the loss, and runs backpropagation. Call optimizer.zero_grad before each backward pass so stale gradients from the previous step do not accumulate. The loss.backward call computes the gradients, and optimizer.step applies them to the weights across all 101,770 parameters. Five epochs is enough to reach above 97 percent accuracy on MNIST with this simple feedforward architecture. Print the running loss each epoch so you can watch training progress and spot divergence early on. Training usually takes between 2 and 5 minutes on CPU, or under 30 seconds on a consumer GPU.

for epoch in range(5):
    running_loss = 0.0
    for images, labels in train_loader:
        optimizer.zero_grad()
        outputs = model(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()
    print(f"Epoch {epoch+1}, loss {running_loss / len(train_loader):.4f}")

Step 6 – Evaluate accuracy on the held-out test set

After training, switch the model to evaluation mode and run it on the 10,000-image MNIST test set without computing gradients. The torch.no_grad context manager disables gradient tracking, which saves roughly 40 percent of memory and speeds up evaluation. For each batch of 256 samples, compare the model’s top prediction against the true label and count correct hits. Divide the total correct count by the dataset size to compute final accuracy as a single percentage. A well-trained two-layer feedforward network on MNIST typically reaches between 97 and 98 percent on this held-out test set. Evaluation finishes in under 10 seconds on CPU and under 2 seconds on a consumer GPU.

model.eval()
correct = 0
total = 0
with torch.no_grad():
    for images, labels in test_loader:
        outputs = model(images)
        _, predicted = torch.max(outputs, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()
print(f"Test accuracy: {100 * correct / total:.2f}%")

Step 7 – Save the trained model and iterate on improvements

Save the model’s state dict to disk so you can reload it later for inference or further training. The state dict stores every learned weight and bias as a Python dictionary of tensors. From this baseline you can swap the two-layer feedforward network for a small convolutional network and watch accuracy climb above 99 percent. You can also increase hidden width, add dropout, or raise the number of epochs to experiment with capacity and regularization. Pro tip: keep a short log of each experiment with the hyperparameters and final test accuracy so you can compare runs rigorously.

torch.save(model.state_dict(), "simple_nn.pt")

Recommended by AIplusInfo

Books and kits to go deeper on neural networks

Hand-picked titles and hardware that map directly to the architectures, training loops, and hands-on practice described above.

As an Amazon Associate, AIplusInfo earns from qualifying purchases.

Deep Learning (Adaptive Computation and Machine Learning series)

Book

Deep Learning (Adaptive Computation and Machine Learning series)

Goodfellow, Bengio, and Courville cover every layer, loss, and training trick in this article at textbook depth.

Buy on Amazon
Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow

Book

Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow

Aurelien Geron’s third edition walks through building neural networks in Python end to end alongside this guide’s practice section.

Buy on Amazon
ELEGOO Smart Robot Car Kit V4.0 with Camera

Kit

ELEGOO Smart Robot Car Kit V4.0 with Camera

The Arduino-compatible kit lets beginners run small on-device neural networks for line following and object detection in a real robot.

Buy on Amazon

Key Insights on the Basics of Neural Networks

  • The global artificial neural network market reached roughly USD 346 million in 2023 with projected 21.9 percent CAGR through 2030. Grand View Research attributes this surge to generative AI demand across both enterprise and consumer buyers in nearly every region globally today.
  • Corporate AI adoption climbed 78 percent year over year to 2024 as foundation model APIs replaced bespoke training runs across many industries. The Stanford AI Index 2024 names this shift as the dominant enterprise deployment pattern through the next three years.
  • Training one transformer with neural architecture search emits roughly 626,155 pounds of CO2, about five times a US car's lifetime emissions footprint. The 2019 Strubell team paper at the University of Massachusetts first measured these numbers in detail across six transformer architectures.
  • DeepMind trained a convolutional network on 14,884 retinal scans that matched specialist clinicians on 50 distinct ophthalmic conditions reliably. The 2018 Nature Medicine paper reported parity with eight independent expert reviewers across the full referral benchmark design.
  • Reported AI incidents climbed 56.3 percent in a single year, driven by hallucinations, misinformation, and deepfake fraud scaling across platforms. The Stanford AI Index 2024 ties the surge to widely deployed generative networks entering mass consumer markets around the world now.
  • The transformer architecture introduced in 2017 now powers every major foundation model released by frontier labs since that single paper landed. The Vaswani Attention Is All You Need paper on arxiv replaced recurrence with scaled dot-product self-attention for every sequence input.
  • Chinchilla's compute-optimal scaling needs roughly 20 training tokens per parameter, reframing data strategy as the dominant lever over raw parameter count. The 2022 DeepMind Chinchilla paper on arxiv changed how every frontier lab now budgets its pretraining compute runs on fresh models.
  • Datacenter electricity consumption is projected to roughly double between 2022 and 2026 as AI training workloads scale up across the world. The International Energy Agency 2024 Electricity executive summary flags neural network compute as a direct driver of grid capacity concerns globally.

These numbers tell a coherent story about where neural network engineering stands in 2026. Market demand and corporate adoption are climbing fast, pulling capital and attention into model development. Energy costs, water usage, and reported incidents are rising in parallel, which keeps regulatory scrutiny close behind commercial momentum. Clinical and consumer deployments show that mature neural networks can match specialists on narrow tasks when trained on curated data. The transformer architecture and compute-optimal scaling laws together explain why the past five years looked different from the previous twenty.

How the Main Neural Network Types Compare

Beyond the mechanics of training, picking the right neural network type is often the single highest-leverage decision in a project. The table below compares the seven dominant architectures across the dimensions that matter most during early planning. No single architecture dominates every workload, so matching the model family to the data pattern is the first design question any team should answer. Treat the columns as a shortlist, then run small experiments to confirm the best fit for your dataset and compute budget. Pay close attention to the inductive bias and compute columns, since both decide how much data and GPU time your project will need overall.

ArchitectureBest forInductive biasTypical training dataCompute profileInterpretabilityMature libraries
Multi-Layer PerceptronTabular prediction, baselinesNone, fully connectedThousands to millions of rowsLowMedium with SHAP toolsPyTorch, scikit-learn
Convolutional Neural NetworkImages, audio spectrogramsLocal, translation invariantTens of thousands of labeled imagesMediumMedium with saliency mapsPyTorch, TensorFlow, Keras
Recurrent Neural NetworkShort sequence predictionTemporal, sequentialLogs, time series, short textMediumLow on long contextsPyTorch, TensorFlow
TransformerText, code, vision, multimodalPermutation plus positionalBillions of tokens or image patchesVery highLow, active researchHugging Face Transformers
Graph Neural NetworkMolecules, social graphsRelational, permutation equivariantNode and edge datasetsMediumMedium with attention mapsPyTorch Geometric, DGL
Diffusion ModelImage, video, audio generationReverse noising processBillions of captioned imagesVery highLow, active researchDiffusers, Stable Diffusion
Mixture of ExpertsScaled language modelsSparse routingTrillions of tokensVery high at train, lower at inferenceLow, routing hard to inspectMegablocks, Switch Transformer

Real-World Deployments of Neural Networks That Changed Industries

Shifting from theory to practice, these three deployments represent neural networks that moved from research into mass-market production. Each example reached measurable scale and reshaped how its industry treats the underlying technology. The basics of neural networks and how they work translate into real impact only when training data, product design, and governance all mature together. Each case below includes implementation details, outcomes, limitations, and the primary source carrying the claim.

Google DeepMind at Moorfields Eye Hospital

DeepMind deployed a two-stage convolutional neural network at Moorfields Eye Hospital, trained on 14,884 anonymized retinal scans gathered over three years. The team built the architecture to segment eye tissue first and then classify 50 ophthalmic conditions from the segmentation maps. In a 2018 Nature Medicine paper on specialist-level ophthalmic performance, the network reached referral error rates on par with eight specialist clinicians. The implementation still required retraining whenever Moorfields rolled out a new OCT scanner model, a limit the team addressed with hundreds of hours of data augmentation. The project also faced controversy around NHS data sharing, which the UK ICO ruled against Royal Free NHS Trust in 2017. The deployment moved vision neural networks from benchmark leaderboards into reviewed clinical research pipelines across the United Kingdom.

Tesla's end-to-end autopilot vision stack

Tesla rolled out an end-to-end neural network as part of Full Self-Driving v12 in early 2024, trained on fleet video from millions of real cars. The company adopted the architecture to replace more than 300,000 lines of hand-written C perception code with learned weights. In a Q2 2024 shareholder letter on Tesla's investor relations site, the company reported more than 1 billion miles driven on v12 within months of the launch. The deployment produced a 40 percent reduction in miles per intervention according to Tesla's own data releases that quarter. The limitation is that regulators still hold open crash investigations across multiple US agencies, and the FSD label remains contested outside Tesla. The architecture nonetheless marks a watershed moment for applied neural networks in production safety-critical systems that drive public roads daily.

OpenAI's GPT-4 and the shift to foundation models

OpenAI trained the GPT-4 foundation model on a mix of licensed data, publicly available data, and reinforcement learning from human feedback over several months in 2022. The team deployed the resulting network through an API that now powers thousands of enterprise applications and consumer apps. In the published GPT-4 technical report hosted on cdn.openai.com, OpenAI documented a jump to the 90th percentile on the Uniform Bar Exam. The product line produced more than USD 3 billion in annualized revenue for OpenAI by early 2024 according to multiple industry reports. The limitation surfaced in disclosed hallucination rates between 15 and 20 percent on open-ended tasks, and the technical report declined to disclose model size or training compute. The deployment nonetheless proved that one neural network could serve as a general-purpose reasoning engine across many downstream products.

Case Studies on How Companies Scaled Neural Networks

Beyond the headline deployments, these three case studies cover how companies scaled neural networks inside regulated or operationally sensitive workflows. Each shows the engineering, governance, and ethical trade-offs that production teams actually confront. Scaling a neural network inside a mature enterprise is as much a governance and workflow problem as it is a modeling problem. The studies pair measurable business impact with the limitations, controversies, and compliance work that moved alongside the technical build.

Case Study: JPMorgan Chase's COiN document review platform

JPMorgan Chase faced a longstanding problem where lawyers and loan officers spent roughly 360,000 hours each year reviewing commercial loan agreements. The bank needed a solution that could extract structured data points from unstructured contracts without hurting review quality. The team built COiN, a neural network trained on tens of thousands of annotated contracts to extract 150 relevant data points. In a Financial Times feature on the bank's technology spend, JPMorgan cited the 360,000-hour reduction and an internal payback inside one year. The limitation was that reviewers still adjudicated the highest-risk clauses, so the headcount reduction materialized as redeployment rather than layoffs.

Legal reviewers also pushed back on explainability, since early COiN classifications did not expose their reasoning in a form audit teams could sign off. The team added attention visualization and rule-based post-processing to meet internal model risk management standards. The bank's governance framework classified COiN as a Tier 2 model requiring quarterly performance review and bias testing. By 2022 the platform had expanded to adjacent documents including derivatives agreements and trade finance letters at scale. The initiative showed that an in-house neural network can transform a mature regulated process when the team invests in observability and governance. The combination of productivity gain, compliance review, and transparent limitations is a template other banks have adopted since.

Case Study: PathAI's pathology neural networks for cancer diagnosis

PathAI faced the problem of diagnostic consistency in cancer pathology, where interobserver variability can reach 20 percent on prostate biopsies. The company needed a solution that could flag ambiguous tiles so pathologists could focus their time where it mattered most. The team built a convolutional neural network and deployed it as the AISight platform integrated with existing lab information systems. In a JCO Clinical Cancer Informatics paper the company co-authored, PathAI reported a 70 percent reduction in missed cases on a prostate cancer benchmark. The partnership with Bristol Myers Squibb grew into a multi-year collaboration on companion diagnostics for immunotherapy trials across multiple cancer types.

The limitation surfaced during generalization testing on scanner models the training data had never seen, where accuracy dropped by double-digit margins. The team addressed the gap by expanding the training set, applying heavy augmentation, and adding domain adaptation layers to the architecture. Regulatory review by the FDA required extensive validation on prospective cohorts before full clinical deployment. Pathologists also raised concerns about automation bias, where confidence in model output could erode independent judgment on edge cases. PathAI responded with interface patterns that defer to the pathologist on low-confidence tiles across every supported cancer type. The project illustrates that a specialized neural network only translates to clinical impact when workflow, validation, and regulatory review all move together.

Case Study: Netflix's deep recommendation stack

Netflix faced the problem of keeping more than 300 million paid subscribers engaged across more than 190 countries with wildly different taste profiles. The company needed a solution that would scale ranking quality while handling tabular, text, and image features in one stack. The team built a two-tower neural retrieval model plus a transformer-based ranker that scores each candidate for each user context. In a 2024 Netflix Tech Blog post on its foundation model for personalized recommendation, the team reported double-digit engagement gains over the previous ranker. The system now handles billions of inference calls per day across the company's global edge footprint continuously.

The limitation showed up in cold-start cases, where new users produce too little watch data to feed the deep model well. The team layered a classical collaborative filtering model and content-based features as a fallback until the deep model gained real signal. Netflix also disclosed that user feedback on recommendations is often biased by what the system itself surfaces, which complicates offline evaluation. The team adopted interleaving experiments and off-policy counterfactual methods to measure true causal gains on specific product metrics. Ethical review raised concerns around filter bubbles, time-on-platform optimization, and the risk of surfacing manipulative content on autoplay carousels. The case shows that scaling a neural recommender in production is as much about evaluation design and ethical constraint as it is about model capacity.

Common Questions About the Basics of Neural Networks

What is a neural network in simple terms?

A neural network is a layered math function that transforms inputs into predictions. It multiplies inputs by learnable weights, adds biases, and passes the result through nonlinear activation functions. Training adjusts those weights using gradient-based updates until predictions get accurate on unseen data.

How do neural networks actually learn from data?

Neural networks learn through a repeating two-step cycle that alternates between prediction and correction across many samples. Forward propagation sends inputs through the layers to produce a prediction and a numerical loss score for each batch. Backpropagation then computes gradients using the chain rule, and an optimizer like Adam updates each weight to lower the loss on the next pass through the training set.

What is the difference between a neural network and deep learning?

A neural network is any layered model of neurons with weights and activations. Deep learning is the subfield that studies neural networks with many stacked layers. All deep learning uses neural networks, but a shallow single-layer perceptron is a neural network without being deep learning.

Which neural network architecture should I pick for images?

Convolutional neural networks or vision transformers are the two strong defaults for images. Convolutional networks encode locality and translation invariance, which helps on small image datasets. Vision transformers scale better when you have millions of images and the compute to match.

Which neural network architecture should I pick for text?

Transformer encoders and decoders dominate text processing across every major language task in 2026 across every language domain. BERT-style encoders handle classification and retrieval well on short documents and are easy to fine-tune on small labeled sets. GPT-style decoders handle generation and chat tasks across both open ended and tightly scoped prompts. Fine-tune an open-weight transformer like Llama or Mistral when your data is private or domain-specific to your use case.

How much data do I need to train a neural network?

Small tabular tasks can work with a few thousand labeled rows. Image classification typically needs tens of thousands of labeled examples. Modern foundation models train on trillions of tokens or billions of images. If data is scarce, use transfer learning from a pretrained open-weight model.

What are the biggest risks of deploying neural networks?

The main risks include adversarial attacks that fool models with tiny imperceptible input perturbations. Data drift erodes accuracy over time as live inputs diverge from training data. Bias in training data can replicate historical discrimination at a much larger scale. Prompt injection and model collapse are growing concerns for generative deployments and agentic systems.

How expensive is it to train a modern neural network?

Small research models can train for a few hundred dollars on cloud GPUs over a long weekend. Business-grade models run from several thousand dollars to several hundred thousand dollars across typical enterprise deployments. Frontier foundation models cost tens or even hundreds of millions in compute alone across the full training run. Energy, water, and labeling costs typically add 20 to 50 percent to the headline figure for an honest total.

What is backpropagation and why does it matter?

Backpropagation is the algorithm that computes how each weight should change to reduce the loss. It applies the calculus chain rule layer by layer from the output back to the input. Without backpropagation, training a deep neural network would be computationally impossible at modern scales.

Do neural networks really think or understand?

Neural networks do not think in any human sense of the word thinking in context. They approximate statistical patterns in their training data and produce outputs that match the patterns they have seen. Many modern systems appear to reason on tasks, but research continues on how much of that is genuine generalization and how much is pattern matching from similar training examples.

How long does it take to learn the basics of neural networks?

A motivated newcomer with Python experience can grasp the basics of neural networks and how they work in roughly 40 to 80 hours of focused study. That includes forward propagation, backpropagation, three architectures, and a working PyTorch project. Mastery for production deployment takes 12 to 24 months of hands-on work.

Can neural networks run on a phone or edge device?

Yes, modern phones ship dedicated neural engines that run billions of operations per second. Model quantization, pruning, and distillation shrink large models to fit edge hardware. Apple, Google, and Qualcomm all publish mobile model recipes that run language and vision networks on-device without the cloud.

What math do I need to learn neural networks properly?

You need linear algebra for matrices, calculus for gradients, probability for loss functions, and basic statistics for evaluation. A one-semester course in each is enough to follow the research literature. Beginners can start with the Python code and pick up the math alongside the implementations as they go.

What is the role of GPUs in neural network training?

GPUs run many matrix multiplications in parallel, which is exactly what neural network training needs. A modern GPU like the Nvidia H100 trains models 50 to 100 times faster than a CPU. Specialized accelerators from Google, AMD, Intel, and startups like Cerebras extend the trend further.

Where should I start if I am completely new to neural networks?

Start with a visual explainer like 3Blue1Brown's neural network playlist on YouTube. Then run a small PyTorch tutorial end to end to lock in the mechanics. Pick a focused book like the free Nielsen Neural Networks and Deep Learning text to study the math alongside working code.