Python / Python Deep Learning and Neural Networks Interview Questions
How do you choose the right layer type (Linear, Conv, Attention) for a given input modality?
Each layer type encodes different structural assumptions (inductive biases) about the data. Using a layer whose assumptions match the data's structure allows the model to learn faster and with less data than a generic alternative.
| Data type | Structure | Recommended layer | Reason |
|---|---|---|---|
| Tabular | No spatial/sequential structure | Linear (MLP) | Features are independent; no shared structure to exploit |
| Images | 2D spatial locality + translation equivariance | Conv2d | Same pattern anywhere in image; fewer params than FC |
| Text/sequences | Long-range dependencies, variable length | Transformer (self-attention) | O(1) path length between any two positions |
| Short sequences / time series | Local temporal patterns | Conv1d or LSTM | Local: Conv1d; long-range: LSTM |
| Graphs | Irregular node connectivity | Graph Conv (GCN/GAT) | Aggregates neighbor information per node |
| Point clouds | Permutation invariant 3D | PointNet / sparse conv | Must handle unordered sets |
import torch
import torch.nn as nn
# Tabular data: simple MLP
mlp = nn.Sequential(
nn.Linear(30, 128), nn.ReLU(), nn.Dropout(0.2),
nn.Linear(128, 64), nn.ReLU(),
nn.Linear(64, 1)
)
# Image: CNN
cnn = nn.Sequential(
nn.Conv2d(3, 32, 3, padding=1), nn.BatchNorm2d(32), nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(),
nn.AdaptiveAvgPool2d(1), # global average pooling -> (B, 64, 1, 1)
nn.Flatten(),
nn.Linear(64, 10)
)
# Text: embedding + transformer encoder
encoder_layer = nn.TransformerEncoderLayer(
d_model=256, nhead=4, dim_feedforward=512,
dropout=0.1, batch_first=True
)
text_model = nn.Sequential(
nn.Embedding(10000, 256),
nn.TransformerEncoder(encoder_layer, num_layers=4)
)
# Time series: Conv1d (local patterns) or LSTM (sequential patterns)
ts_cnn = nn.Conv1d(in_channels=1, out_channels=32, kernel_size=5, padding=2)
ts_rnn = nn.LSTM(input_size=1, hidden_size=64, batch_first=True)
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
