Neural architecture selection
Choose an architecture for the input structure
A neural architecture describes how a network connects its computations and parameters. A dense (fully connected) layer combines every input feature with learned weights, so it is a natural candidate for a fixed-size vector such as package weight and dimensions. An image has spatial neighborhoods; a sequence has order. Architectures can exploit these structures rather than flatten everything and learn each relationship separately. An inductive bias is an assumption built into the model that may help it generalize, but it can also be wrong for a particular task. A simple rule or linear baseline is still worth measuring on the same split.
Convolution: reuse a filter across locations
A convolutional neural network (CNN) uses learned local filters, often followed by nonlinearities and sometimes pooling or striding. In the common deep learning convention, the filter is multiplied with a patch without reversing its entries (mathematically, cross-correlation). This example uses one grayscale image, one fixed 2-by-2 filter, stride 1, no padding, no bias and no dilation. At the top-left patch, -1+1-1+1=0; at the next patch, -1+3-1+3=4. Reusing the filter at every valid position produces a 2-by-2 feature map: [[0, 4], [0, 4]]. In general the output size in this restricted setting is (height-kernel_height+1, width-kernel_width+1). It detects a particular local change, not a learned defect classifier. CNNs can exploit locality, but are not automatically invariant to all shifts, rotations or lighting changes. Real layers can have many input and output channels.
def correlate(image, kernel): if not image or not image[0] or not kernel or not kernel[0]: raise ValueError("Empty image or kernel") h, w = len(image), len(image[0]) kh, kw = len(kernel), len(kernel[0]) if any(len(row) != w for row in image) or any(len(row) != kw for row in kernel): raise ValueError("Ragged rows") if kh > h or kw > w: raise ValueError("Kernel too large") return [[sum(image[r+i][c+j] * kernel[i][j] for i in range(kh) for j in range(kw)) for c in range(w-kw+1)] for r in range(h-kh+1)]
image = [[1, 1, 3], [1, 1, 3], [2, 2, 4]]kernel = [[-1, 1], [-1, 1]]print(correlate(image, kernel))Recurrent state and attention
A recurrent neural network (RNN) updates a hidden state using the current input and the previous state. The same update is reused at each position. This toy linear recurrence uses h=2*h+x, starting at zero. It yields [1,4,11] for [1,2,3] and [3,8,17] for the reverse order. This is an illustration, not a stable general RNN: its state can grow rapidly. Gated RNNs such as LSTM and GRU control information flow and can improve learning over longer sequences. Attention instead combines values according to input-dependent relevance scores. Here we supply two scalar scores, normalize with softmax and weight two values; equal scores give 4.0 from values 2 and 6. Subtracting the maximum score before exponentiation improves numerical stability for finite moderate inputs. Real attention derives scores from learned query and key representations and may use masks. This example is not a complete attention layer.
from math import exp
def recurrent_states(values): state = 0 history = [] for value in values: state = 2 * state + value history.append(state) return history
def attention_value(scores, values): if not scores or len(scores) != len(values): raise ValueError("Need matching nonempty inputs") weights = [exp(s-max(scores)) for s in scores] return sum(w*v for w, v in zip(weights, values)) / sum(weights)
print(recurrent_states([1, 2, 3]))print(recurrent_states([3, 2, 1]))print(attention_value([0, 0], [2, 6]))Transformers and architecture trade-offs
A transformer typically combines attention, position information, feedforward sublayers, residual connections and normalization. Self-attention relates positions within a sequence; position information lets a model distinguish order. Causal masking can prevent a token from attending to future tokens during next-token prediction. Standard full attention can require work quadratic in sequence length, although implementations and alternative attention patterns can change memory and runtime behavior. CNNs, RNNs and transformers can all be used beyond their familiar domains. Choose based on input structure, label availability, memory, latency and validation performance rather than popularity. For a toy package image with a fixed contrast defect, compare a thresholded local filter against a small CNN candidate; for sensor sequences, compare the last observed value against a recurrent or attention model. Keep the same target, split and error costs.
Common mistakes and practice
Do not claim that a hand-selected filter has learned from data, confuse an RNN state with a prediction, or treat attention weights as a guaranteed explanation. Do not interpret attention as a complete transformer. Practice: use a 3-by-4 image and a 2-by-2 filter to predict the output dimensions (2-by-3). Then explain why a fixed threshold may be sufficient when the defect rule is stable and exact. Next: learn how frameworks manage training and inference.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Neural architecture selection quiz
Check architecture and workflow decisions.
Which property of a CNN can help model repeated local patterns in package images?
For the lesson’s valid cross-correlation with stride 1 and no dilation or padding, what output shape follows from a 4-by-5 image and 2-by-3 filter?
What changes when [1,2,3] is reversed in the toy recurrence?
For scores [0,0], values [4,10], what is the toy attention output?
Which experiment provides a fairer comparison of candidate architectures for one prediction task?