TensorFlow
Installation
SKILL.md
tf.function Retracing
- New input shape/dtype causes retrace — expensive, prints warning
- Use
input_signaturefor fixed shapes —@tf.function(input_signature=[tf.TensorSpec(...)]) - Python values retrace — pass as tensors, not Python ints/floats
- Avoid Python side effects in tf.function — only runs once during tracing
GPU Memory
- TensorFlow grabs all GPU memory by default — set
memory_growth=Truebefore any ops tf.config.experimental.set_memory_growth(gpu, True)— must be called before GPU init- OOM with large models — reduce batch size or use gradient checkpointing
CUDA_VISIBLE_DEVICES=""to force CPU — for testing without GPU
Data Pipeline
tf.data.Datasetwithout.prefetch()— CPU/GPU idle time between batches.cache()after expensive ops — but before random augmentation.batch()before.map()for vectorized ops — faster than per-elementnum_parallel_calls=tf.data.AUTOTUNE— parallel preprocessing- Dataset iteration in eager mode is slow — use in tf.function or model.fit