Skip to main content

What’s happening inside a Convolutional Neural Network (CNN)?


In general, a Convolutional Neural Network (CNN) consists of an input layer, hidden layers, and an output layer. Real-world CNNs are nonlinear because they include activation functions that introduce nonlinearity.

In our case, the model takes a single input, which is passed to a 13-dimensional linear layer, followed by a nonlinear 13-dimensional tanh activation layer. The output from this layer is then passed through another 13-dimensional linear layer that produces a single output value.

Linear layers contain weights and biases, following the equation y = mx + b, while the tanh activation function outputs values in the range [-1, 1].


1. What’s happening overall

The text explains how PyTorch stores and updates the weights and biases (called parameters) of a small neural network built using nn.Sequential.

A “parameter” is just a number that the model can learn, like weights and biases.


2. model.parameters()

When you call model.parameters(), PyTorch collects all the weights and biases from every layer in your model.


[param.shape for param in seq_model.parameters()]
# Output:
[torch.Size([13, 1]), torch.Size([13]), torch.Size([1, 13]), torch.Size([1])]
  

This means:

  • First layer weights: [13, 1]
  • First layer bias: [13]
  • Second layer weights: [1, 13]
  • Second layer bias: [1]

These are the exact numbers the optimizer (like SGD or Adam) will update during training.


3. After backward()

When you run loss.backward(), PyTorch calculates how each parameter should change (the gradient).

  1. Compute loss
  2. Call loss.backward() → get gradients
  3. Call optimizer.step() → update weights

4. named_parameters()

This function gives you the names of the parameters along with their values.


for name, param in seq_model.named_parameters():
    print(name, param.shape)

# Output:
0.weight torch.Size([13, 1])
0.bias torch.Size([13])
2.weight torch.Size([1, 13])
2.bias torch.Size([1])
  

Here 0 and 2 are the layer order numbers inside nn.Sequential.


5. Using OrderedDict for readable names


from collections import OrderedDict
seq_model = nn.Sequential(OrderedDict([
    ('hidden_linear', nn.Linear(1, 8)),
    ('hidden_activation', nn.Tanh()),
    ('output_linear', nn.Linear(8, 1))
]))
  

Now the parameters look more descriptive:


hidden_linear.weight torch.Size([8, 1])
hidden_linear.bias torch.Size([8])
output_linear.weight torch.Size([1, 8])
output_linear.bias torch.Size([1])

  

6. Accessing specific parameters


seq_model.output_linear.bias

# Output:
Parameter containing:
tensor([-0.0173], requires_grad=True)
  

This means it’s a bias value that will be updated during training.


7. Checking gradients

After training, you can see how much each parameter changed by checking its .grad value:


seq_model.hidden_linear.weight.grad
  

This shows how each weight in the hidden layer changed after training.


Summary Table

Concept Meaning
parameters() Collects all weights and biases of the model
named_parameters() Same, but includes names for easier identification
loss.backward() Calculates gradients (how much each parameter should change)
optimizer.step() Updates all parameters using those gradients
OrderedDict Lets you name your layers instead of using numbers
.grad Shows the gradient of a parameter after backpropagation

In short: PyTorch tracks every learnable weight and bias in your model, computes their gradients when you train, and updates them using the optimizer to make the model perform better.


Further Reading


People are good at skipping over material they already know!

View Related Topics to







Contact Us

Name

Email *

Message *

Popular Posts

BER vs SNR for M-ary QAM, M-ary PSK, QPSK, BPSK, ...

๐Ÿ“˜ Overview of BER and SNR ๐Ÿงฎ Online Simulator for BER calculation of m-ary QAM and m-ary PSK ๐Ÿงฎ MATLAB Code for BER calculation of M-ary QAM, M-ary PSK, QPSK, BPSK, ... ๐Ÿ“š Further Reading ๐Ÿ“‚ View Other Topics on M-ary QAM, M-ary PSK, QPSK ... ๐Ÿงฎ Online Simulator for Constellation Diagram of m-ary QAM ๐Ÿงฎ Online Simulator for Constellation Diagram of m-ary PSK ๐Ÿงฎ MATLAB Code for BER calculation of ASK, FSK, and PSK ๐Ÿงฎ MATLAB Code for BER calculation of Alamouti Scheme ๐Ÿงฎ Different approaches to calculate BER vs SNR What is Bit Error Rate (BER)? The abbreviation BER stands for Bit Error Rate, which indicates how many corrupted bits are received (after the demodulation process) compared to the total number of bits sent in a communication process. BER = (number of bits received in error) / (total number of tran...

Online Simulator for ASK, FSK, and PSK

Try our new Digital Signal Processing Simulator!   Start Simulator for binary ASK Modulation Message Bits (e.g. 1,0,1,0) Carrier Frequency (Hz) Sampling Frequency (Hz) Run Simulation Simulator for binary FSK Modulation Input Bits (e.g. 1,0,1,0) Freq for '1' (Hz) Freq for '0' (Hz) Sampling Rate (Hz) Visualize FSK Signal Simulator for BPSK Modulation ...

MATLAB Code for Rms Delay Spread

RMS delay spread is crucial when you need to know how much the signal is dispersed in time due to multipath propagation, the spread (variance) around the average. In high-data-rate systems like LTE, 5G, or Wi-Fi, even small time dispersions can cause ISI. RMS delay spread is directly related to the amount of ISI in such systems. RMS Delay Spread [↗] Delay Spread Calculator Enter delays (ns) separated by commas: Enter powers (dB) separated by commas: Calculate   The above calculator Converts Power to Linear Scale: It correctly converts the power values from decibels (dB) to a linear scale. Calculates Mean Delay: It accurately computes the mean excess delay, which is the first moment of the power delay profile. Calculates RMS Delay Spread: It correctly calculates the RMS delay spread, defined as the square root of the second central moment of the power delay profile.   MATLAB Code  clc...

Constellation Diagrams of ASK, PSK, and FSK

๐Ÿ“˜ Overview of Energy per Bit (Eb / N0) ๐Ÿงฎ Online Simulator for constellation diagrams of ASK, FSK, and PSK ๐Ÿงฎ Theory behind Constellation Diagrams of ASK, FSK, and PSK ๐Ÿงฎ MATLAB Codes for Constellation Diagrams of ASK, FSK, and PSK ๐Ÿ“š Further Reading ๐Ÿ“‚ Other Topics on Constellation Diagrams of ASK, PSK, and FSK ... ๐Ÿงฎ Simulator for constellation diagrams of m-ary PSK ๐Ÿงฎ Simulator for constellation diagrams of m-ary QAM BASK (Binary ASK) Modulation: Transmits one of two signals: 0 or -√Eb, where Eb​ is the energy per bit. These signals represent binary 0 and 1.    BFSK (Binary FSK) Modulation: Transmits one of two signals: +√Eb​ ( On the y-axis, the phase shift of 90 degrees with respect to the x-axis, which is also termed phase offset ) or √Eb (on x-axis), where Eb​ is the energy per bit. These signals represent binary 0 and 1.  BPSK (Binary PSK) Modulation: Transmits one of two signals...

Alamouti Scheme for 2x2 MIMO in MATLAB

๐Ÿ“˜ Overview & Theory ๐Ÿงฎ MATLAB Code for Alamouti Scheme ๐Ÿงฎ MATLAB Code for BER vs. SNR for Alamouti Scheme ๐Ÿงฎ Alamouti Scheme Simulator ๐Ÿงฎ Alamouti Scheme Transmission Table ๐Ÿ“š Further Reading    Read about the Alamouti Scheme first MATLAB Code for Alamouti's Precoding Matrix for 2 X 2 MIMO % Clear any existing data and figures clc; clear; close all; % Define system parameters transmitAntennas = 2; % Number of antennas at the transmitter receiveAntennas = 2; % Number of antennas at the receiver symbolCount = 1000000; % Number of symbols to transmit SNR_dB = 15; % Signal-to-Noise Ratio in decibels % Generate random binary data for transmission rng(10); % Set seed for reproducibility transmitData = randi([0, 1], transmitAntennas, symbolCount); % Perform Binary Phase Shift Keying (BPSK) modulation modulatedSymbols = 1 - 2 * transmitData; % Define Alamouti's Precoding Matrix precodingMatrix = [1 1; -1i 1i]; % Encode and transmit dat...

ASK, FSK, and PSK

๐Ÿ“˜ Overview ๐Ÿ“˜ Amplitude Shift Keying (ASK) ๐Ÿ“˜ Frequency Shift Keying (FSK) ๐Ÿ“˜ Phase Shift Keying (PSK) ๐Ÿ“˜ Which of the modulation techniques—ASK, FSK, or PSK—can achieve higher bit rates? ๐Ÿงฎ MATLAB Codes ๐Ÿ“˜ Simulator for binary ASK, FSK, and PSK Modulation ๐Ÿ“š Further Reading ASK or OFF ON Keying ASK is a simple (less complex) Digital Modulation Scheme where we vary the modulation signal's amplitude or voltage by the message signal's amplitude or voltage. We select two levels (two different voltage levels) for transmitting modulated message signals. For example, "+5 Volt" (upper level) and "0 Volt" (lower level). To transmit binary bit "1", the transmitter sends "+5 Volts", and for bit "0", it sends no power. The receiver uses filters to detect whether a binary "1" or "0" was transmitted. ...

LDPC Encoding and Decoding Techniques

๐Ÿ“˜ Overview & Theory ๐Ÿงฎ LDPC Encoding Techniques ๐Ÿงฎ LDPC Decoding Techniques ๐Ÿ“š Further Reading 'LDPC' is the abbreviation for 'low density parity check'. LDPC code H matrix contains very few amount of 1's and mostly zeroes. LDPC codes are error correcting code. Using LDPC codes, channel capacities that are close to the theoretical Shannon limit can be achieved.  Low density parity check (LDPC) codes are linear error-correcting block code suitable for error correction in a large block sizes transmitted via very noisy channel. Applications requiring highly reliable information transport over bandwidth restrictions in the presence of noise are increasingly using LDPC codes. 1. LDPC Encoding Technique The proper form of H matrix is derived from the given matrix by doing multiple row operations as shown above. In the above, H is parity check matrix and G is generator matrix. If you consider matrix H as [-P' | I] then matrix G will be...

Comparisons among ASK, PSK, and FSK | And the definitions of each

๐Ÿ“˜ Comparisons among ASK, FSK, and PSK ๐Ÿงฎ Online Simulator for calculating Bandwidth of ASK, FSK, and PSK ๐Ÿงฎ MATLAB Code for BER vs. SNR Analysis of ASK, FSK, and PSK ๐Ÿ“š Further Reading ๐Ÿ“‚ View Other Topics on Comparisons among ASK, PSK, and FSK ... ๐Ÿงฎ Comparisons of Noise Sensitivity, Bandwidth, Complexity, etc. ๐Ÿงฎ MATLAB Code for Constellation Diagrams of ASK, FSK, and PSK ๐Ÿงฎ Online Simulator for ASK, FSK, and PSK Generation ๐Ÿงฎ Online Simulator for ASK, FSK, and PSK Constellation ๐Ÿงฎ Some Questions and Answers Modulation ASK, FSK & PSK Constellation MATLAB Simulink MATLAB Code Comparisons among ASK, PSK, and FSK    Comparisons among ASK, PSK, and FSK Comparison among ASK, FSK, and PSK Parameters ASK FSK PSK Variable Characteristics Amplitude Frequency ...