Key terms for this tutorial
Here are the words that matter most in this tutorial. Come back here if a term feels fuzzy.
- activation
- A non-linear function (like ReLU or Sigmoid) that lets a neural network learn curves and boundaries instead of just straight lines.
- batch
- A small group of examples processed together. Batches let us estimate gradients quickly and use hardware efficiently.
- model
- A function with learnable parameters that maps inputs to outputs.
- neural network
- A stack of layers that transform data through learned weights and non-linearities.
- tensor
- A multi-dimensional array of numbers, the basic data type in deep learning.
What is a tensor?
A tensor is a generalization of a number. A scalar is a 0-dimensional tensor, a vector is 1D, a matrix is 2D, and a batch of images is 4D. The only new thing is the shape.
In Python, tensors live inside numpy.ndarray or torch.Tensor. They give you fast math on the CPU and GPU without writing loops.
Core idea: shape is everything. If you keep track of the shape of each tensor, you can debug almost any neural-network bug.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Tensors and NumPy'. Explain the section 'What is a tensor?' in plain English. Include simple analogies, vocabulary definitions, and a short example.
Creating and reshaping tensors
import numpy as np
scalar = np.array(3.14)
vector = np.array([1, 2, 3])
matrix = np.array([[1, 2], [3, 4], [5, 6]])
print("scalar shape:", scalar.shape)
print("vector shape:", vector.shape)
print("matrix shape:", matrix.shape)
# Reshape without changing the data
reshaped = matrix.reshape(2, 3)
print("reshaped:\n", reshaped)
scalar shape: () vector shape: (3,) matrix shape: (3, 2) reshaped: [[1 2 3] [4 5 6]]
Line-by-line: tensors.py
Read this slowly. Each line does one small, concrete thing.
import numpy as np— Import thenumpylibrary so we can use its built-in tools.print("scalar shape:", scalar.shape)— Print a result so we can see what the model learned.print("vector shape:", vector.shape)— Print a result so we can see what the model learned.print("matrix shape:", matrix.shape)— Print a result so we can see what the model learned.print("reshaped:\n", reshaped)— Print a result so we can see what the model learned.
Pop-in: RAG gives the model a textbook at test time
A language model only knows what it was trained on. RAG lets it look up relevant documents before answering, so it can use facts that were not in the training data.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Tensors and NumPy'. Explain the section 'Creating and reshaping tensors' in plain English. Include simple analogies, vocabulary definitions, and a short example. Also walk through the code example in this section step by step.
Broadcasting: one of ML's secret weapons
Broadcasting lets arrays with different shapes be used together. NumPy (and PyTorch) stretches the smaller array along leading dimensions so the operation makes sense, without actually copying data.
import numpy as np
a = np.array([[1, 2, 3],
[4, 5, 6]]) # shape (2, 3)
b = np.array([10, 20, 30]) # shape (3,)
print(a + b)
[[11 22 33] [14 25 36]]
Line-by-line: broadcasting.py
Read this slowly. Each line does one small, concrete thing.
import numpy as np— Import thenumpylibrary so we can use its built-in tools.print(a + b)— Print a result so we can see what the model learned.
Pop-in: Read the code one block at a time
Industry code imports many libraries. Focus on the sequence: prepare data, define the model, run the training loop, and evaluate. The rest is setup.
The row vector b is conceptually copied once for every row of a. This is how a bias is added to every neuron in a batch.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Tensors and NumPy'. Explain the section 'Broadcasting: one of ML's secret weapons' in plain English. Include simple analogies, vocabulary definitions, and a short example. Also walk through the code example in this section step by step.
Matrix multiplication is the workhorse
A neural network layer is almost always input @ weights.T + bias. The inner dimensions must match: a matrix of shape (batch, in_features) times weights of shape (out_features, in_features) gives an output of shape (batch, out_features).
import numpy as np
X = np.array([[0.5, 1.0]]) # (1, 2)
W = np.array([[0.1, 0.2],
[0.3, 0.4]]) # (2, 2)
Z = X @ W # (1, 2)
print("X @ W =", Z)
X @ W = [[0.35 0.5 ]]
Line-by-line: matmul.py
Read this slowly. Each line does one small, concrete thing.
import numpy as np— Import thenumpylibrary so we can use its built-in tools.print("X @ W =", Z)— Print a result so we can see what the model learned.
Pop-in: Read the code one block at a time
Industry code imports many libraries. Focus on the sequence: prepare data, define the model, run the training loop, and evaluate. The rest is setup.
This is the forward pass of a linear layer. Add an activation function afterwards and you have a real neuron.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Tensors and NumPy'. Explain the section 'Matrix multiplication is the workhorse' in plain English. Include simple analogies, vocabulary definitions, and a short example. Also walk through the code example in this section step by step.
Check yourself
Shape of a batched matrix multiply
If A has shape (32, 10) and B has shape (10, 5), what is the shape of A @ B?
(32, 5). The inner dimensions (10) cancel, and the batch dimension (32) is preserved.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Tensors and NumPy'. The practice question 'Shape of a batched matrix multiply' asks: If <code>A</code> has shape <code>(32, 10)</code> and <code>B</code> has shape <code>(10, 5)</code>, what is the shape of <code>A @ B</code>? The answer is: <code>(32, 5)</code>. The inner dimensions (10) cancel, and the batch dimension (32) is preserved.. Explain the concept behind this in simple terms.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Tensors and NumPy'. Explain the section 'Check yourself' in plain English. Include simple analogies, vocabulary definitions, and a short example.
Three questions
1. Which of these is a 2D tensor?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Tensors and NumPy'. The quiz asks: Which of these is a 2D tensor? The correct answer is: A matrix. Explain why that answer is right and why the other choices are wrong, using a simple example.
2. What does broadcasting do?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Tensors and NumPy'. The quiz asks: What does broadcasting do? The correct answer is: Allows arrays with compatible shapes to be used together without explicit copying. Explain why that answer is right and why the other choices are wrong, using a simple example.
3. Why do AI engineers think about tensor shapes so much?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Tensors and NumPy'. The quiz asks: Why do AI engineers think about tensor shapes so much? The correct answer is: Because shapes determine whether an operation is valid and what the output shape will be. Explain why that answer is right and why the other choices are wrong, using a simple example.
Keep learning
Go deeper into strides, einsum, GPU tensors, and autograd.
Part of the CodeHS tutorial library.