> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cirron.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TensorFlow

> TensorFlow project template.

# TensorFlow Template

The TensorFlow template scaffolds a production-ready ML project with TensorFlow, Keras, and common ML dependencies. Two variants are available:

* **`tensorflow`**: basic inference template for deployment
* **`tensorflow-train`**: full training pipeline with `ModelCheckpoint` and `EarlyStopping` callbacks

## Quick Start

```bash theme={null}
cirron init my-tensorflow-model --template tensorflow
```

The model type (classification, regression, etc.) is selected interactively during `cirron init`.

## Project Structure

```
my-tensorflow-model/
├── src/
│   ├── model.py           # create_model() factory
│   ├── data_loader.py     # tf.data.Dataset utilities
│   ├── train.py           # Training script (tensorflow-train only)
│   └── inference.py       # Inference / prediction
├── data/
├── checkpoints/           # best_model.h5
├── models/                # final_model.h5
├── requirements.txt
├── Dockerfile
└── cirron.yaml
```

For shared files (`Dockerfile`, `.gitignore`, `README.md`, `.env.example`, tests), see [Common files](/cli/templates/common).

## Cirron-specific bits

### cirron.yaml

```yaml theme={null}
framework: tensorflow
model_type: classification
train_entrypoint: src/train.py
inference_entrypoint: src/inference.py
```

### Profiling hooks

The generated `train.py` and `inference.py` integrate with the Cirron SDK. Call `ci.profile()` once before training begins; the Keras callback auto-registers and tracks `model.fit`:

```python theme={null}
import cirron as ci

ci.profile()

def train(self):
    return self.model.fit(self.train_dataset, ...)
```

### requirements.txt

```txt theme={null}
tensorflow>=2.12.0
numpy>=1.21.0
scikit-learn>=1.3.0
matplotlib>=3.5.0
Pillow>=9.0.0
```

## Model Types

* **Classification**: `sparse_categorical_crossentropy` loss, softmax output, accuracy metric. Default architecture is a small CNN.
* **Regression**: `mse` loss, linear output, MAE metric. Default architecture is a Keras `Sequential` MLP.

The interactive `cirron init` prompt selects which one is generated into `src/model.py` via `create_model()`.

## Built-in Callbacks

The generated `train.py` wires standard Keras callbacks:

* `ModelCheckpoint`: saves best model by validation loss to `checkpoints/best_model.h5`
* `EarlyStopping`: stops training when val loss stops improving (patience=5)

Add more callbacks (e.g. `ReduceLROnPlateau`) in `get_callbacks()`.

## Usage

```bash theme={null}
python src/train.py     # Train
cirron build            # Container
cirron deploy           # Deploy
```

## GPU Support

TensorFlow automatically detects available GPUs. The `tensorflow-train` variant supports mixed-precision training:

```python theme={null}
policy = tf.keras.mixed_precision.Policy('mixed_float16')
tf.keras.mixed_precision.set_global_policy(policy)
```

## Next Steps

<CardGroup cols={2}>
  <Card title="PyTorch Template" href="/cli/templates/pytorch">
    Alternative deep learning framework
  </Card>

  <Card title="scikit-learn Template" href="/cli/templates/sklearn">
    Traditional ML algorithms
  </Card>

  <Card title="Common files" href="/cli/templates/common">
    Dockerfile, tests, and shared scaffolding
  </Card>

  <Card title="Deployment" href="/cli/commands/deploy">
    Deploy your trained model
  </Card>
</CardGroup>
