Zubnet AIAprenderWiki › Tensor
Fundamentos

Tensor

Multidimensional Array
Um array multidimensional de números — a estrutura de dados fundamental em deep learning. Um escalar é um tensor 0D (um único número). Um vetor é um tensor 1D. Uma matriz é um tensor 2D. Uma imagem é um tensor 3D (altura × largura × canais). Um batch de imagens é um tensor 4D. Pesos do modelo, ativações, gradientes — tudo em uma rede neural é um tensor.

Por que importa

Tensors são a linguagem do deep learning. PyTorch, TensorFlow e JAX são fundamentalmente bibliotecas de computação tensorial. Entender shapes e operações de tensors é essencial para ler código de modelo, debugar mismatches de shape (o erro mais comum em código ML), e entender o que acontece dentro de redes neurais. Se você consegue seguir os shapes de tensors, consegue seguir a arquitetura.

Deep Dive

Common tensor shapes in NLP: input tokens are (batch_size, sequence_length) integers. Embeddings are (batch_size, seq_len, model_dim) floats. Attention weights are (batch_size, num_heads, seq_len, seq_len). The output logits are (batch_size, seq_len, vocab_size). Understanding these shapes tells you exactly what's happening: the attention tensor is N×N because each token attends to every other token.

Operations

Key tensor operations: matmul (matrix multiplication — the core computation in neural networks), reshape (changing dimensions without changing data), transpose (swapping dimensions), concat (joining tensors along a dimension), slice (extracting subtensors), and broadcast (making differently-shaped tensors compatible for element-wise operations). Deep learning is really just a sequence of these operations applied to tensors.

GPU Acceleration

Tensors are computed on GPUs because tensor operations are massively parallel: multiplying two matrices involves millions of independent multiply-add operations that can run simultaneously. This is why GPU VRAM matters — all tensors involved in computation must reside in GPU memory. When you run out of VRAM, it's because the sum of all tensor sizes (model weights + activations + gradients + optimizer states) exceeds capacity. Techniques like gradient checkpointing, mixed precision, and model sharding are all about managing tensor memory.

Conceitos relacionados

← Todos os termos
ESC