Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Common Network Types

Common Network Types

Imagine we are teaching a child to recognize cats and dogs.

At first, we show him many pictures of cats and dogs, telling him "this is a cat, this is a dog." Gradually, the child's brain derives patterns from these images: cats' ears are usually pointed and their faces rounder; dogs' ears may droop and their faces are longer. This process, in essence, islearning featuresandbuilding patterns。

Deep learning, as a powerful branch of machine learning, is fundamentally about getting computers to simulate this process. By building multi-layer neural networks, it enables machines to automatically learn and extract complex features from massive amounts of data, ultimately accomplishing advanced tasks such as recognizing images, understanding language, and predicting trends. Different tasks, however, require networks of different structures. This article introduces several of the most core and common types of deep learning networks, and explains their design ideas and typical applications.

Deep Learning Networks

Chinese Full Name Full English name Shorthand
Artificial Neural Network Artificial Neural Network ANN
Convolutional neural networks Convolutional Neural Network CNN
Recurrent neural networks Recurrent Neural Network RNN
Long Short-Term Memory network Long Short-Term Memory LSTM
Gated Recurrent Unit Gated Recurrent Unit GRU
Generative adversarial network Generative Adversarial Network GAN
Transformer Transformer Transformer
Autoencoders Autoencoder AE
Variational autoencoder Variational Autoencoder VAE
Deep Belief Networks Deep Belief Network DBN
Graph Neural Network Graph Neural Network GNN

Neural Network Fundamentals and Fully Connected Networks

Before diving into the various networks, we need to understand the most fundamental model—Fully Connected Networks, also known asMultilayer perceptron。

Core Idea: Everything Can Be Connected

The fully connected network is the most straightforward architecture in deep learning. As its name suggests, eachneuron in every layeris connected to every neuron in theneuron in every layeradjacent layer.

We can imagine it as an extremely dense information-processing network: data enters through the input layer, passes through transformations in multiple hidden layers, and finally yields a result at the output layer.

Typical Applications and Limitations

Apply: Due to its strong fitting ability, FCN is very suitable for structured data (such as tabular data, e.g., house area, location, and number of rooms in house-price prediction).

Example

A simple fully connected network example (using PyTorch)

import

torch.

nn

as

nn

class

SimpleFCN

(

nn.

Module

)

:

   

def

init

(

self

,

input_size

,

num_classes

)

:

       

super

(

SimpleFCN

,

self

)

.

init

(

)

       

Define network layers

       

self

.

fc1

=

nn.

Linear

(

input_size

,

128

)

 

First hidden layer

       

self

.

relu

=

nn.

ReLU

(

)

                 

Activation function

       

self

.

fc2

=

nn.

Linear

(

128

,

64

)

       

Second hidden layer

       

self

.

fc3

=

nn.

Linear

(

64

,

num_classes

)

Output layer

   

def

forward

(

self

,

x

)

:

        x

=

self

.

fc1

(

x

)

        x

=

self

.

relu

(

x

)

        x

=

self

.

fc2

(

x

)

        x

=

self

.

relu

(

x

)

        x

=

self

.

fc3

(

x

)

       

return

x

Suppose the input is a 100-dimensional feature and we perform 10-class classification

model

=

SimpleFCN

(

input_size

=

100

,

num_classes

=

10

)

Limitations: When handling grid-like data such as images and speech, FCN faces huge challenges. Pixels in an image are highly correlated in space, but FCN ignores this spatial structure, flattening the image into a one-dimensional vector for processing. This causes an explosion in the number of parameters and makes it hard to learn effective spatial features.


Convolutional Neural Networks — The Cornerstone of Computer Vision

To address the problem of image processing,Convolutional neural networks it emerged and completely transformed the field of computer vision.

Core Ideas: Local Perception and Parameter Sharing

CNN's design was inspired by the biological visual cortex, and its two core ideas are:

  1. Local perception: Unlike FCN, which lets neurons connect to the entire image, each CNN neuron only senses asmall local region(such as a 3x3 or 5x5 pixel block). This better matches the property that neighboring pixels in an image are more strongly correlated.
  2. Parameter Sharing: Using the sameconvolution kernel(or filter) sliding across different positions of the image to extract the same type of features (such as edges and textures). This greatly reduces the network parameters.

Core Components and Typical Applications

A typical CNN is built by stacking the following components:

Apply: Image classification, object detection, face recognition, and almost all computer vision tasks.

Example

A simple CNN example (for image classification)

class

SimpleCNN

(

nn.

Module

)

:

   

def

init

(

self

,

num_classes

=

10

)

:

       

super

(

SimpleCNN

,

self

)

.

init

(

)

       

self

.

conv1

=

nn.

Conv2d

(

in_channels

=

3

,

out_channels

=

16

,

kernel_size

=

3

,

padding

=

1

)

       

self

.

pool

=

nn.

MaxPool2d

(

kernel_size

=

2

,

stride

=

2

)

       

self

.

conv2

=

nn.

Conv2d

(

16

,

32

,

3

,

padding

=

1

)

       

self

.

fc1

=

nn.

Linear

(

32

8

8

,

256

)

Suppose the feature map size after two pooling operations is 8x8

       

self

.

fc2

=

nn.

Linear

(

256

,

num_classes

)

   

def

forward

(

self

,

x

)

:

        x

=

self

.

pool

(

nn.

functional

.

relu

(

self

.

conv1

(

x

)

)

)

Convolution -> Activation -> Pooling

        x

=

self

.

pool

(

nn.

functional

.

relu

(

self

.

conv2

(

x

)

)

)

        x

=

x.

view

(

1

,

32

8

8

)

Flatten the feature map into a one-dimensional vector

        x

=

nn.

functional

.

relu

(

self

.

fc1

(

x

)

)

        x

=

self

.

fc2

(

x

)

       

return

x


Recurrent Neural Networks — Experts at Handling Sequential Data

For language, speech, time series, and other data withsequential dependencies from front to back,we need a network that can remember historical information, and this is Recurrent neural networks。

Core Idea: Introducing a Memory Mechanism

The core of an RNN lies in its recurrent structure. When processing the current input, the network combines the current input andthe hidden state from the previous time stepto jointly determine the current output and the hidden state passed to the next time step. This is like when you read a sentence: understanding the meaning of the current word depends on the words you have read before.

Variants and Typical Applications

Basic RNNs suffer from the long-term dependency problem and struggle to learn information in long sequences. Hence two important variants emerged:

Apply: Machine translation, text generation, speech recognition, and stock price prediction.

Example

A simple RNN example (for text sentiment classification)

class

SimpleRNN

(

nn.

Module

)

:

   

def

init

(

self

,

vocab_size

,

embed_size

,

hidden_size

,

num_classes

)

:

       

super

(

SimpleRNN

,

self

)

.

init

(

)

       

self

.

embedding

=

nn.

Embedding

(

vocab_size

,

embed_size

)

Word embedding layer

       

self

.

rnn

=

nn.

RNN

(

input_size

=

embed_size

,

hidden_size

=

hidden_size

,

batch_first

=

True

)

       

self

.

fc

=

nn.

Linear

(

hidden_size

,

num_classes

)

   

def

forward

(

self

,

x

)

:

       

Shape of x: (batch_size, sequence_length)

        x

=

self

.

embedding

(

x

)

After embedding: (batch_size, seq_len, embed_size)

        _

,

h_n

=

self

.

rnn

(

x

)

 

h_n is the hidden state of the last time step

        out

=

self

.

fc

(

h_n.

squeeze

(

0

)

)

Classify using the final state

       

return

out


Generative Adversarial Networks — From Learning to Creating

If the previous networks are discriminative models (learning to distinguish data), then Generative adversarial network GAN is an outstanding representative of generative models (learning to create data).

Core Idea: Evolving Through the Game

GAN was inspired by game theory. It consists of two networks that compete with each other:

The two improve together through continuous adversarial training: the generator strives to produce more realistic data to fool the discriminator, while the discriminator works to sharpen its ability to tell them apart. Eventually, the generator can produce high-quality new data.

Typical application

Apply: Image generation, image super-resolution, style transfer, and data augmentation.

Example

Pseudocode illustration of GAN's core training loop

for

epoch

in

range

(

num_epochs

)

:

   

1. Train the discriminator: maximize its ability to judge real data as real and generated data as fake

    real_data

=

get_real_data

(

)

    noise

=

generate_random_noise

(

)

    fake_data

=

generator

(

noise

)

.

detach

(

)

Note detach, to prevent the generator from being updated

    d_loss_real

=

criterion

(

discriminator

(

real_data

)

,

real_labels

)

    d_loss_fake

=

criterion

(

discriminator

(

fake_data

)

,

fake_labels

)

    d_loss

=

d_loss_real + d_loss_fake

    d_loss.

backward

(

)

    optimizer_D.

step

(

)

   

2. Train the generator: minimize the discriminator's ability to judge generated data as fake (i.e., fool the discriminator)

    noise

=

generate_random_noise

(

)

    fake_data

=

generator

(

noise

)

    g_loss

=

criterion

(

discriminator

(

fake_data

)

,

real_labels

)

Make the discriminator believe the generated data is real

    g_loss.

backward

(

)

    optimizer_G.

step

(

)


Summary and comparison

Network Type Core idea Type of Data It Excels At Handling Typical application
Fully Connected Networks Global connections, dense fitting Structured data (tables) House price prediction, credit scoring
Convolutional neural networks Local perception, parameter sharing Grid-like data (images) Image classification, object detection
Recurrent neural networks Sequential dependencies, memory states Sequential data (text, time series) Machine translation, speech recognition
Generative adversarial network Adversarial game, data generation Used to generate data similar to real data Image generation, style transfer