Kokil Thapa - Professional Web Developer in Nepal
Freelancer Web Developer in Nepal with 15+ Years of Experience

Kokil Thapa is an experienced full-stack web developer focused on building fast, secure, and scalable web applications. He helps businesses and individuals create SEO-friendly, user-focused digital platforms designed for long-term growth.

TensorFlow Fundamentals

By Kokil Thapa | Last reviewed: September 2026

TensorFlow Fundamentals matter the moment your product needs prediction, classification, or ranking beyond hard-coded rules. You might be wiring a recommendation layer into a Laravel API or evaluating whether a client portal should run inference on-server or call a hosted model. Either way, you need a clear mental model of tensors, graphs, Keras, and deployment—not a PhD thesis. This guide walks through the core concepts a working engineer needs, aligned with how teams actually ship ML in 2026. For broader context on supervised learning and model lifecycle, start with our machine learning fundamentals overview.

What Are TensorFlow Fundamentals Every Developer Should Know?

TensorFlow is Google's open-source numerical computing library for machine learning and deep learning. At its core, it manipulates tensors—generalised arrays that can hold scalars, vectors, matrices, or higher-rank data. A 28×28 grayscale image is a rank-2 tensor. A batch of 32 RGB images shaped 32×224×224×3 is rank-4.

Three layers define modern TensorFlow:

  • TensorFlow Core — low-level ops, tf.function, GradientTape, tf.data pipelines.
  • Keras — the official high-level API bundled since TensorFlow 2.x for defining, training, and exporting models.
  • Ecosystem tools — TensorBoard for metrics, TensorFlow Lite for mobile/edge, TF Serving for scalable inference, and TensorFlow.js for browser deployment.

If you build web systems, treat TensorFlow as an inference and training runtime—not something that replaces your REST API layer. The application still owns auth, rate limits, logging, and business rules. ML adds a specialised compute step inside or beside that stack.

TensorFlow Fundamentals EcosystemTensorFlow Coretensors, ops, autogradKeras APImodels, layers, fit()tf.datainput pipelinesTensorBoardmetrics, graphsTF Litemobile and edgeTF ServingREST and gRPCTF.jsbrowser inference
TensorFlow Fundamentals span Core computation, Keras modelling, data pipelines, and multiple deployment runtimes.

On client projects where I integrate LLM or ML APIs into Laravel backends, TensorFlow rarely sits inside PHP itself. Instead, a Python microservice or managed endpoint runs the model. Understanding these fundamentals helps you scope infrastructure, estimate latency, and debug shape mismatches when JSON payloads reach the inference layer.

How Do You Install and Set Up TensorFlow in 2026?

TensorFlow ships as a Python package. Use a virtual environment on Ubuntu 22/24 or macOS. GPU builds require CUDA-compatible drivers; CPU-only installs are fine for learning and small models.

Create an isolated environment

python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install tensorflow

Verify the install and list visible devices:

python -c "import tensorflow as tf; print(tf.__version__); print(tf.config.list_physical_devices())"

For reproducible teams, pin versions in requirements.txt and commit it alongside training scripts. Treat model artefacts like application releases: versioned, tagged, and deployable through the same Git workflow you use for custom software projects.

Project layout that scales

A maintainable TensorFlow project separates concerns early:

  1. data/ — raw and processed datasets (often gitignored; store large files in object storage).
  2. src/data_pipeline.py — tf.data loading, shuffling, batching, prefetch.
  3. src/model.py — Keras model definition.
  4. src/train.py — training loop, callbacks, checkpoint paths.
  5. exports/ — SavedModel directories for serving.

Keep secrets out of notebooks. Use environment variables for cloud credentials, mirroring how you manage .env files in production Laravel apps. When debugging JSON payloads sent to inference services, a JSON formatter saves time validating feature vectors before they hit the model.

How Does the TensorFlow Computational Graph and Eager Execution Work?

TensorFlow 2.x runs in eager execution by default. Operations execute immediately, like NumPy. You can still trace functions into optimised graphs using @tf.function for performance on GPU and TPU hardware.

Understanding both modes matters when you read stack traces versus when you profile training throughput.

Tensors and basic operations

import tensorflow as tf

a = tf.constant([[1.0, 2.0], [3.0, 4.0]])
b = tf.constant([[5.0, 6.0], [7.0, 8.0]])
c = tf.matmul(a, b)
print(c.shape)   # (2, 2)
print(c.dtype)   # float32

Dtypes matter. Most vision models use float32. Quantised edge models may use int8. A dtype mismatch between training and serving is a common production bug.

Automatic differentiation with GradientTape

Training neural networks requires gradients. tf.GradientTape records operations and computes derivatives:

x = tf.Variable(3.0)
with tf.GradientTape() as tape:
    y = x ** 2
dy_dx = tape.gradient(y, x)
print(dy_dx.numpy())  # 6.0

Keras hides most of this inside model.fit(), but Tape-based custom training loops appear in research code and GANs. If you inherit such a codebase during an AI integration engagement, knowing Tape semantics prevents silent gradient bugs.

Eager vs Graph ExecutionEager Mode (default)Ops run immediatelyEasy debuggingPython-like flow@tf.function GraphTrace and compileFaster on GPUStatic shape optswrapTraining Step Inside Graphforward pass → loss → backward pass → optimizer applyGradientTape records forward ops onlyXLA may fuse kernels on supported hardware
TensorFlow Fundamentals include knowing when eager debugging beats compiled graph performance.

The official TensorFlow guide to graphs and functions documents tracing rules and retracing triggers. Read it before wrapping large Python loops in @tf.function.

How Do You Build and Train a Model with Keras in TensorFlow?

Keras is the fastest path from idea to trained model. Two API styles exist: Sequential for stackable layers, and Functional for multi-input or multi-output architectures.

Minimal classification example

This MNIST-style workflow shows the standard pattern: load data, normalise, define model, compile, train, evaluate.

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0

model = keras.Sequential([
    layers.Input(shape=(28, 28)),
    layers.Flatten(),
    layers.Dense(128, activation="relu"),
    layers.Dropout(0.2),
    layers.Dense(10, activation="softmax"),
])

model.compile(
    optimizer="adam",
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"],
)

history = model.fit(
    x_train, y_train,
    epochs=5,
    batch_size=128,
    validation_split=0.1,
)

test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"Test accuracy: {test_acc:.4f}")

tf.data for production-scale input

Loading entire datasets into RAM fails quickly. Use tf.data.Dataset for streaming, shuffling, and prefetching:

def make_dataset(images, labels, batch_size, shuffle=True):
    ds = tf.data.Dataset.from_tensor_slices((images, labels))
    if shuffle:
        ds = ds.shuffle(buffer_size=10000)
    ds = ds.batch(batch_size).prefetch(tf.data.AUTOTUNE)
    return ds

train_ds = make_dataset(x_train, y_train, batch_size=128)
model.fit(train_ds, epochs=5, validation_data=(x_test, y_test))

Prefetch overlaps data loading with GPU compute. On CPU-only servers—common on budget Nepal hosting—batch size tuning matters more than exotic optimisers. Profile before buying bigger hardware.

Callbacks and checkpointing

Never train without checkpoints. A single power cut should not destroy days of compute.

callbacks = [
    keras.callbacks.ModelCheckpoint(
        filepath="exports/checkpoint.keras",
        save_best_only=True,
        monitor="val_accuracy",
    ),
    keras.callbacks.EarlyStopping(
        monitor="val_loss",
        patience=3,
        restore_best_weights=True,
    ),
    keras.callbacks.TensorBoard(log_dir="logs/fit"),
]

model.fit(train_ds, epochs=50, callbacks=callbacks)

TensorBoard logs land locally by default. For team visibility, sync log directories to shared storage or wire metrics into your existing observability stack—similar to how you would track API latency in a Prometheus metrics pipeline.

Keras Training PipelineRaw Datatf.dataKeras ModelCallbacksmodel.fit() — forward, loss, backward, optimizerepochs × batches per epochSavedModel Exportmodel.export() or tf.saved_model.save()
Standard TensorFlow Fundamentals training flow: pipeline data, fit with callbacks, export a versioned artefact.

The Keras Functional API guide covers shared layers, residual connections, and multi-head outputs. Use it when Sequential stacks feel too rigid.

TensorFlow vs PyTorch: Which Should You Choose?

Both frameworks solve the same problem. Your choice affects hiring, library availability, and deployment tooling—not raw model quality on most benchmarks.

CriterionTensorFlowPyTorch
Primary API styleKeras high-level + Core low-levelPythonic, dynamic by default
Mobile / edge deploymentTensorFlow Lite (mature, wide device support)PyTorch Mobile, ExecuTorch (growing)
Production servingTF Serving (battle-tested gRPC/REST)TorchServe, Triton, custom FastAPI
Research adoptionStrong in industry ML platformsDominant in academic papers
Learning curve for web devsKeras feels similar to scikit-learn workflowsExplicit tensors everywhere from day one
Google Cloud integrationFirst-class on Vertex AISupported, slightly more manual wiring

Pick TensorFlow when mobile export, TF Serving, or managed Google pipelines are non-negotiable. Pick PyTorch when your ML team already standardised on it or publishes custom autograd research code. For many Nepal SMB projects, the framework matters less than data quality and a clean API boundary—patterns we also apply in Apache Spark batch pipelines feeding feature stores.

Do not maintain two training stacks unless revenue justifies the ops cost. Wrap inference behind a versioned REST contract so you can swap runtimes later without rewriting the Laravel or WordPress front end.

How Do You Deploy TensorFlow Models in Production Web Applications?

Training completes half the job. Production means versioned exports, health checks, latency budgets, and rollback paths—the same discipline as any enterprise application deployment.

Export SavedModel format

model.export("exports/mnist_classifier/1")

# Or legacy API:
# tf.saved_model.save(model, "exports/mnist_classifier/1")

The trailing /1 is a version directory TF Serving expects. Increment it on each release.

Serve with TensorFlow Serving

TF Serving loads SavedModel directories and exposes gRPC and REST endpoints. A minimal Docker workflow:

docker run -p 8501:8501 \
  --mount type=bind,source=/path/to/exports,target=/models/mnist \
  -e MODEL_NAME=mnist \
  tensorflow/serving

Your Laravel or Node API forwards JSON feature vectors to port 8501. Validate input schema at the API gateway. Return structured errors when shapes drift—do not pass raw stack traces to clients.

Edge inference with TensorFlow Lite

Convert for on-device use when cloud round-trips are too slow or privacy rules forbid sending pixels off-device:

converter = tf.lite.TFLiteConverter.from_saved_model("exports/mnist_classifier/1")
tflite_model = converter.convert()
open("exports/mnist.tflite", "wb").write(tflite_model)

Quantization reduces model size and speeds inference on ARM chips. Test accuracy after quantisation; aggressive int8 conversion can collapse precision on small datasets.

Production Deployment PathsWeb / Mobile AppLaravel, React NativeAPI Gatewayauth, rate limitsTF ServingSavedModel vNCloud GPU Serverbatch or online inferenceTF Lite on Deviceoffline edge scoringMonitor latency, drift, and model versionrollback via previous SavedModel folder
TensorFlow Fundamentals extend into deployment: API boundaries, TF Serving, and optional TF Lite for edge cases.

I've seen production failures where opcache or container restarts loaded the wrong model version. Tag releases in Git, map them to SavedModel folders, and automate health probes that run a canonical inference request after deploy—similar to smoke tests in testing and optimisation workflows.

For directory or marketplace platforms like Gulfbizlist, ML features (search ranking, fraud scoring) belong behind feature flags. Ship the API contract first; enable the model when offline metrics clear your threshold.

Security and cost notes

GPU instances on AWS or GCP cost roughly Rs 15,000–50,000/month (~USD 110–370) for always-on inference. Batch scoring on cron plus CPU serving fits many Nepal SMB budgets better. Never expose TF Serving directly to the public internet; place it on a private network segment, the same way you would hide MySQL behind your app tier on Linux-managed servers.

When serialising preprocessed features between services, validate payloads with strict schemas. A Base64 encoder-decoder helps debug binary tensor payloads during integration, though JSON float arrays remain the common interchange format for simple models.

Key Takeaways

  • Tensors, eager execution, and Keras APIs form the core of TensorFlow Fundamentals—master these before touching distributed training.
  • Use tf.data pipelines and callbacks from day one; checkpoint best weights and log metrics to TensorBoard.
  • Export SavedModel artefacts with versioned directories for TF Serving rollback capability.
  • Choose TensorFlow when TF Lite or TF Serving integration is central; defer framework wars until team skills and cloud contracts are clear.
  • Wrap inference behind your existing API auth and rate limiting—ML is one service, not a separate silo.
  • Profile CPU batch sizes and GPU costs before committing to always-on inference infrastructure.

People Also Ask

Is TensorFlow still relevant in 2026?

Yes. TensorFlow remains widely used in production, especially where Keras simplicity, TensorFlow Lite mobile export, and TF Serving matter. PyTorch leads many research labs, but industry deployments on Google Cloud and Android still lean heavily on the TensorFlow stack.

Do I need a GPU to learn TensorFlow Fundamentals?

No. CPU training works for small datasets and educational models like MNIST. A GPU speeds deep networks with large image or text batches. Start on CPU, then rent GPU time only when epoch duration blocks iteration.

Can TensorFlow run inside a PHP Laravel application?

Not natively. Laravel calls Python inference services or cloud APIs over HTTP. Common patterns include a FastAPI microservice hosting SavedModel, or managed endpoints on Vertex AI and similar platforms. Keep PHP responsible for HTTP, auth, and persistence.

What is the difference between Keras and TensorFlow?

Keras is the high-level model-building API integrated into TensorFlow 2.x. TensorFlow Core provides tensors, graphs, and hardware acceleration beneath Keras. You import Keras via tensorflow.keras in current releases.

Ship ML Features With Clear Engineering Boundaries

TensorFlow Fundamentals give you the vocabulary to design training pipelines, review model exports, and integrate inference without guessing at tensor shapes or deployment formats. You do not need to become a research scientist to deliver value. You need reproducible data pipelines, versioned artefacts, and an API contract your web stack already understands.

If you are planning document classification, recommendation scoring, or another ML feature inside a business application, map the integration path before picking hardware. Read our AI-assisted debugging workflow for triaging model-serving issues alongside application logs. When you want hands-on help wiring Python inference into a production backend, review our AI integration and automation services or contact us to discuss architecture, hosting, and realistic budgets for Nepal and international deployments.

Frequently Asked Questions

Tensors as multi-dimensional arrays, automatic differentiation via tf.GradientTape, Keras model building, and export paths (SavedModel, TensorFlow Lite, TF Serving) for production inference.

No. CPU-only installs work for learning and small models like MNIST. Rent GPU time only when epoch duration blocks your iteration cycle.

Yes. Production use remains strong where Keras, TensorFlow Lite mobile export, and TF Serving matter, especially on Google Cloud and Android deployments.

TensorFlow ships as a Python package. Create a virtual environment with python3 -m venv .venv, activate it, run pip install tensorflow, then verify with python -c "import tensorflow as tf; print(tf.version); print(tf.config.list_physical_devices())". GPU builds need CUDA-compatible drivers; CPU-only installs suit learning and small models. Pin versions in requirements.txt and treat model artefacts like versioned application releases, mirroring the Git workflow you use for Laravel or custom software projects.

Keras is the high-level model-building API bundled into TensorFlow 2.x for defining, training, and exporting models. TensorFlow Core handles low-level tensor operations, tf.function graph tracing, GradientTape differentiation, and tf.data pipelines. In practice you build with Keras Sequential or Functional APIs, compile with optimizer and loss settings, train via model.fit(), and export SavedModel artefacts. Core APIs appear when you need custom training loops, research-style code, or fine-grained control over graphs and hardware throughput.

Not natively. Laravel handles HTTP, authentication, rate limiting, and persistence; TensorFlow runs in a separate Python runtime. Common patterns include a FastAPI microservice hosting a SavedModel, TF Serving behind your private network, or managed endpoints on Vertex AI. Your Laravel API forwards JSON feature vectors to the inference layer and validates input schemas before they reach the model. I've integrated ML into Laravel backends this way—the application owns business rules while Python owns prediction compute.

Both solve the same problem; the choice affects hiring, library availability, and deployment tooling more than raw benchmark quality. Pick TensorFlow when TensorFlow Lite mobile export, TF Serving, or managed Google pipelines are non-negotiable. Pick PyTorch when your ML team already standardised on it or publishes custom autograd research code. TensorFlow offers mature Keras workflows and battle-tested serving; PyTorch dominates many research labs. For Nepal SMB projects, framework choice matters less than data quality and a clean API boundary—wrap inference behind a versioned REST contract so you can swap runtimes later.

TensorFlow 2.x runs eager execution by default, so operations execute immediately like NumPy and stack traces read naturally during debugging. Wrap performance-critical code in @tf.function to trace optimised graphs for GPU and TPU throughput. Training uses GradientTape to record operations and compute gradients—for example, tape.gradient on x squared yields 2x. Keras model.fit() hides most Tape usage, but custom loops in GANs or research code expose it directly. Dtype mismatches between training and serving, often float32 versus int8 quantisation, are a common production bug worth catching early.

Load data, normalise inputs, define a Sequential or Functional model with layers like Flatten, Dense, Dropout, then compile with an optimizer, loss, and metrics. Call model.fit() with epochs, batch size, and validation split—or pass a tf.data.Dataset for streaming. A minimal MNIST workflow uses sparse_categorical_crossentropy loss and softmax output for ten classes. Use callbacks from day one: ModelCheckpoint saves best weights, EarlyStopping halts unproductive epochs, and TensorBoard logs metrics locally. Never train without checkpoints; a power cut should not destroy days of compute.

Loading entire datasets into RAM fails quickly as data grows. tf.data.Dataset streams samples, shuffles with a buffer, batches records, and prefetches with AUTOTUNE so data loading overlaps GPU compute. A typical pipeline calls from_tensor_slices, optional shuffle, batch, and prefetch. On CPU-only servers—common on budget Nepal hosting—batch size tuning matters more than exotic optimisers. Profile throughput before buying bigger hardware. Separating pipeline logic into src/data_pipeline.py keeps training scripts maintainable alongside src/model.py and src/train.py in a scalable project layout.

Export a versioned SavedModel with model.export("exports/mnist_classifier/1")—the trailing /1 is the version directory TF Serving expects; increment it on each release. Serve via TensorFlow Serving in Docker, exposing gRPC and REST on port 8501, with your Laravel or Node API forwarding JSON feature vectors. Validate input schema at the API gateway and return structured errors on shape drift, never raw stack traces. Tag Git releases, map them to SavedModel folders, and run health probes with a canonical inference request after deploy—similar to smoke tests in standard application deployment workflows.

TF Serving loads SavedModel directories and exposes scalable gRPC and REST inference endpoints. A minimal Docker workflow mounts your exports folder, sets MODEL_NAME, and publishes port 8501. Your application tier forwards validated JSON payloads; TF Serving handles model loading and batch prediction. Use it when you need battle-tested production serving with versioned model directories and rollback capability. Never expose TF Serving directly to the public internet—place it on a private network segment, the same way you hide MySQL behind your app tier on Linux-managed servers.

TensorFlow Lite converts SavedModel exports for on-device inference when cloud round-trips are too slow or privacy rules forbid sending pixels off-device. Use TFLiteConverter.from_saved_model, write the .tflite binary, and deploy on mobile or ARM edge hardware. Quantization reduces model size and speeds inference, but test accuracy after conversion—aggressive int8 quantisation can collapse precision on small datasets. TensorFlow Lite offers mature, wide device support compared to PyTorch Mobile alternatives, making it the practical choice when mobile or edge deployment is central to your product requirements.

Always-on GPU instances on AWS or GCP typically run roughly Rs 15,000–50,000 per month (~USD 110–370) for inference workloads. Many Nepal SMB budgets fit batch scoring on cron plus CPU serving better than dedicated GPU boxes. Start training on CPU for small datasets, profile batch sizes, and rent GPU time only when epoch duration blocks iteration. Before committing to always-on inference infrastructure, measure latency budgets and compare against managed endpoint pricing. ML adds a specialised compute step—scope it like any other production service with clear cost ceilings.

Shape and dtype mismatches between training and serving cause silent prediction failures—validate JSON feature vectors against expected tensor shapes before inference. Container restarts loading the wrong model version happen when SavedModel folders lack version discipline; increment /1, /2 directories and tie them to Git tags. Skipping checkpoints risks losing days of training after a power cut. Exposing TF Serving publicly bypasses your auth layer entirely. Serialise preprocessed features with strict schemas between services. On client projects, I treat ML like any enterprise deployment: health checks, rollback paths, feature flags, and inference behind existing API rate limiting.

Share this article

0 Comments

Leave a comment

Your email is not published. Comments appear once they have been read. Sign in to have your details filled in.

Quick Contact Options
Choose how you want to connect me: