---
id: wiki-2026-0508-tensorflow-foundations
title: TensorFlow Foundations
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [TensorFlow, TF2, Keras]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [tensorflow, deep-learning, keras]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
  language: Python
  framework: TensorFlow 2.x / Keras 3
---

# TensorFlow Foundations

## 매 한 줄
> **"매 production-first ML framework"**. Google이 2015년에 release한 graph-based deep learning framework. TF 2.x는 eager-by-default + Keras integration으로 PyTorch-like UX를 제공하지만, 매 핵심 강점은 mobile (TFLite) / web (TF.js) / production serving (TF Serving) 의 cross-platform deployment.

## 매 핵심

### 매 TF 2.x 의 essence
- **Eager execution** default — 매 `tf.function` 으로 graph compile (XLA / autograph).
- **Keras** 가 high-level API — 매 `tf.keras.Model` / `Sequential` / Functional API.
- **`tf.data`** — 매 efficient input pipeline (prefetch, parallel map, sharding).
- **`tf.distribute`** — 매 multi-GPU / TPU strategy (`MirroredStrategy`, `TPUStrategy`).

### 매 PyTorch 와 의 비교 (2026)
- **연구 share**: PyTorch ~85%, TF ~10%. JAX growing.
- **Production share**: TF still dominant (TFLite mobile, TF.js web, TF Serving) thanks to mature deployment.
- **Keras 3 (2024+)**: 매 backend-agnostic — TF / JAX / PyTorch 모두 backend로 사용 가능.

### 매 응용
1. Mobile inference (Android / iOS via TFLite).
2. Browser ML (TF.js — pose detection, on-device LLM).
3. Recommender systems (TFRS, large embedding tables).

## 💻 패턴

### Sequential model
```python
import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Dense(128, activation="relu"),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10),
])
model.compile(optimizer="adam",
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5, validation_split=0.1)
```

### Functional API (multi-input)
```python
img_in = tf.keras.Input(shape=(224, 224, 3))
txt_in = tf.keras.Input(shape=(128,), dtype="int32")

img_feat = tf.keras.applications.EfficientNetV2B0(include_top=False, pooling="avg")(img_in)
txt_feat = tf.keras.layers.Embedding(10000, 64)(txt_in)
txt_feat = tf.keras.layers.GlobalAveragePooling1D()(txt_feat)

x = tf.keras.layers.Concatenate()([img_feat, txt_feat])
out = tf.keras.layers.Dense(1, activation="sigmoid")(x)

model = tf.keras.Model([img_in, txt_in], out)
```

### tf.function (graph compile)
```python
@tf.function(jit_compile=True)  # XLA
def train_step(x, y):
    with tf.GradientTape() as tape:
        logits = model(x, training=True)
        loss = loss_fn(y, logits)
    grads = tape.gradient(loss, model.trainable_variables)
    optimizer.apply_gradients(zip(grads, model.trainable_variables))
    return loss
```

### tf.data input pipeline
```python
ds = (tf.data.Dataset.from_tensor_slices((x, y))
      .shuffle(10000)
      .map(preprocess, num_parallel_calls=tf.data.AUTOTUNE)
      .batch(64)
      .prefetch(tf.data.AUTOTUNE))
```

### Multi-GPU
```python
strategy = tf.distribute.MirroredStrategy()
with strategy.scope():
    model = build_model()
    model.compile(...)
model.fit(ds, epochs=10)
```

### TFLite export
```python
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]  # int8 quantization
tflite_model = converter.convert()
open("model.tflite", "wb").write(tflite_model)
```

### Keras 3 backend swap
```python
import os
os.environ["KERAS_BACKEND"] = "jax"  # or "torch", "tensorflow"
import keras
model = keras.Sequential([keras.layers.Dense(10)])
```

## 매 결정 기준
| 상황 | Approach |
|---|---|
| Mobile/edge deployment | TF + TFLite (mature) |
| Browser inference | TF.js |
| Production serving at scale | TF Serving / TFX |
| Research / new architectures | PyTorch (ecosystem) |
| Backend-agnostic code | Keras 3 |
| TPU training | TF or JAX |

**기본값**: 매 research 는 PyTorch, deployment 는 TF or ONNX export.

## 🔗 Graph
- 부모: [[Deep-Learning]]
- 변형: [[PyTorch-Foundations]] · [[Keras]]
- 응용: [[Recommender-Systems]]
- Adjacent: [[Model-Serving]]

## 🤖 LLM 활용
**언제**: mobile/web deployment target, TPU access, legacy TF1 codebase migration, TFX pipeline.
**언제 X**: 매 cutting-edge research model 의 reproduce — 매 PyTorch reference impl 의 dominant.

## ❌ 안티패턴
- **TF1 graph mode 의 cling**: 매 `tf.compat.v1.Session` 의 use 를 새 code 에서 X — 매 TF2 eager + tf.function.
- **Custom training loop 의 unnecessary use**: 매 `model.fit` 으로 충분한 case 에 manual loop 작성.
- **`tf.py_function` overuse**: 매 graph 밖 fallback — 매 performance kill.
- **No `prefetch`**: 매 input pipeline 이 GPU 의 starve.

## 🧪 검증 / 중복
- Verified (TensorFlow official docs, Keras 3 release notes, 2024-2026 tracking).
- 신뢰도 A.

## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — TF2/Keras3 modern usage + deployment focus |
