惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
博客园_首页
爱范儿
爱范儿
博客园 - 叶小钗
aimingoo的专栏
aimingoo的专栏
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
博客园 - 【当耐特】
Y
Y Combinator Blog
量子位
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Blog of Author Tim Ferriss
月光博客
月光博客
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
A
About on SuperTechFans

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Part-03 Tensorflow
Dolly Sharma · 2026-04-25 · via DEV Community

📊 TensorFlow Computational Graph

🔹 What is a Computational Graph?

A computational graph is a directed graph used to represent mathematical computations.

  • Nodes (vertices) → Operations (like addition, multiplication, activation)
  • Edges → Tensors (data flowing between operations)

👉 Simple idea:
Graph = Operations + Data flow


🔹 Components of the Graph

1. Nodes (Operations / Ops)

  • Represent computations
  • Examples:

    • Matrix multiplication (matmul)
    • Addition
    • Activation functions (ReLU, Sigmoid)

👉 They take tensors as input and produce tensors as output


2. Edges (Tensors)

  • Represent data flowing between nodes
  • Carry:

    • Inputs
    • Intermediate results
    • Outputs

👉 Think: Edges = Data pipeline


🔹 How to Build a Computational Graph

Step 1: Define Operations

  • Decide what computations you need (e.g., multiplication, loss calculation)

Step 2: Create Tensors

  • Inputs
  • Model parameters
  • Intermediate values

Step 3: Connect Operations

  • Link outputs of one operation to inputs of another

👉 This creates a graph structure


🔹 Example (Conceptual)

import tensorflow as tf

a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[5, 6], [7, 8]])

c = tf.matmul(a, b)

Enter fullscreen mode Exit fullscreen mode

👉 Here:

  • a, b → tensors (edges/data)
  • matmul → node (operation)

🔹 Execution of Graph (Sessions)

In TensorFlow 1.x style:

  • Graph is built first
  • Then executed using a session
with tf.compat.v1.Session() as sess:
    result = sess.run(c)
    print(result)

Enter fullscreen mode Exit fullscreen mode

👉 Important:

  • Graph = Blueprint 🧠
  • Session = Execution 🚀

🔹 Visualization (TensorBoard)

TensorFlow provides a tool called
TensorBoard

Use:

  • Visualize graph structure
  • Understand data flow
  • Debug models

Example:

writer = tf.summary.FileWriter("logs/", graph=tf.compat.v1.get_default_graph())
writer.close()

Enter fullscreen mode Exit fullscreen mode

Then run TensorBoard to view it.


🔹 Benefits of Computational Graph

✅ 1. Optimization

  • TensorFlow optimizes execution
  • Improves speed and memory usage

✅ 2. Portability

  • Graph can be saved and reused

✅ 3. Debugging

  • Visualization helps track errors

✅ 4. Parallelism

  • Independent operations can run simultaneously

🔹 Important Note (Modern TensorFlow)

In TensorFlow 2.x:

  • No need for explicit sessions
  • Uses eager execution (default)

👉 Code runs immediately, like normal Python

But:

  • Computational graph still exists internally (for optimization)

🎯 One-Line Summary (Exam Ready)

A TensorFlow computational graph is a directed graph where nodes represent operations and edges represent tensors, enabling efficient execution, optimization, and visualization of machine learning models.


⚡ What is Eager Execution (TensorFlow 2.x)?

👉 Eager Execution = “Run code immediately”

  • As soon as you write a line, it executes instantly
  • Works like normal Python / NumPy

✅ Example:

import tensorflow as tf

x = tf.constant([1, 2, 3])
print(x)

Enter fullscreen mode Exit fullscreen mode

👉 Output comes immediately


🧠 Simple Understanding

Eager = “No waiting, no graph building, just direct result”


🧩 What is Graph Execution (TensorFlow 1.x)?

👉 Graph Execution = “Build first, run later”

  • First: create a computational graph (blueprint)
  • Then: execute using a session

❗ Key Idea:

Code does NOT run immediately


🧠 Simple Understanding

Graph = “Plan everything first, then execute”


⚔️ Eager vs Graph Execution (Easy Comparison)

Feature Eager Execution ⚡ Graph Execution 📊
Execution Immediate Delayed
Debugging Easy Hard
Style Like Python Like building a model graph
Flexibility High Less
Performance Slower (sometimes) Faster (optimized)
Default TF 2.x TF 1.x

🔥 Important Concept: @tf.function

TensorFlow 2.x gives both worlds 👇

👉 By default: Eager mode
👉 With @tf.function: Graph mode


Example:

import tensorflow as tf

@tf.function
def matmul(a, b):
    return tf.matmul(a, b)

Enter fullscreen mode Exit fullscreen mode

👉 What happens:

  • Function is converted into a computational graph
  • Runs faster (optimized)

🧠 Simple Understanding

@tf.function = “Convert Python code → Graph for speed”


🚀 Why Use Graph Mode?

Even though eager is easy, graph mode is powerful:

✅ Benefits:

  • Faster execution (optimization)
  • Better for large models
  • Can use XLA (Accelerated Linear Algebra) optimization
  • Easy to save & deploy models

🎯 Final Exam Answer (Short)

Eager execution in TensorFlow 2.x executes operations immediately like normal Python, making debugging easy. In contrast, graph execution builds a computational graph first and executes it later in a session. TensorFlow 2.x uses eager execution by default but allows graph execution using @tf.function for better performance and optimization.


Good question—this is exactly where many people get confused 👍
In TensorFlow 2.x, graphs are mostly hidden, so you need a tool to see them.


👀 How to Visualize a TensorFlow Graph

The main tool is 👉 TensorBoard


🔹 Method (TensorFlow 2.x)

You need to:

  1. Convert your function into a graph using @tf.function
  2. Log it
  3. Open TensorBoard

✅ Step-by-Step

1. Create a Graph Function

import tensorflow as tf

@tf.function
def my_func(a, b):
    return tf.matmul(a, b)

Enter fullscreen mode Exit fullscreen mode


2. Enable Logging

log_dir = "logs/graph"

writer = tf.summary.create_file_writer(log_dir)

tf.summary.trace_on(graph=True, profiler=False)

a = tf.constant([[1, 2]])
b = tf.constant([[3], [4]])

my_func(a, b)

with writer.as_default():
    tf.summary.trace_export(
        name="my_graph",
        step=0,
        profiler_outdir=log_dir
    )

Enter fullscreen mode Exit fullscreen mode


3. Run TensorBoard

Open terminal and run:

tensorboard --logdir=logs/graph

Enter fullscreen mode Exit fullscreen mode

Then open browser:
👉 http://localhost:6006


🧠 What You’ll See

  • Nodes = operations (MatMul, etc.)
  • Edges = tensors (data flow)
  • Full computational graph visualization

🔥 Simple Understanding

TensorBoard = “Graph ka map” 🗺️
It shows how data flows inside your model


⚠️ Important Notes

  • Without @tf.function, graph won’t appear properly
  • Eager execution does not create a visible static graph
  • Graph is created only when tracing happens

🎯 One-Line Answer (Exam)

TensorFlow graphs can be visualized using TensorBoard by tracing a @tf.function and exporting it to log files, which are then displayed as a computational graph.