AI/ML · TensorFlow Guide
TensorFlow Tutorial for Beginners — Complete Learning Guide
Quick summary — TensorFlow for beginners
TensorFlow is an open-source ML framework by Google. You use it to build and train neural networks. For beginners, the easiest entry point is Keras, which is built into TensorFlow.
In this guide you will learn:
- Tensors — the building blocks of TensorFlow.
- Models — building networks with Keras.
- Training — compiling and fitting a model.
- Evaluation — checking accuracy.
- Projects — your first complete ML pipeline.
SECTION 01Installing TensorFlow
Set up TensorFlow in a clean virtual environment.
| Step | Command | Purpose | Notes |
|---|---|---|---|
| 1 | python -m venv tf-env | Create environment | Recommended |
| 2 | source tf-env/bin/activate | Activate | Windows: tf-env\Scripts\activate |
| 3 | pip install tensorflow | Install TF | Latest stable |
| 4 | python -c "import tensorflow as tf" | Verify | No error = ready |
# Create a virtual environment
python -m venv tf-env
source tf-env/bin/activate # Windows: tf-env\Scripts\activate
# Install TensorFlow
pip install tensorflow
# Optional: install extras
pip install numpy pandas matplotlib
import tensorflow as tf
print(tf.__version__)
print("GPU available:", len(tf.config.list_physical_devices('GPU')) > 0)
SECTION 02Tensors & operations
A tensor is a multi-dimensional array. Everything in TensorFlow is a tensor.
| Concept | Meaning | Example | Tip |
|---|---|---|---|
| Scalar | 0-D tensor | tf.constant(5) | A single number |
| Vector | 1-D tensor | [1, 2, 3] | A list |
| Matrix | 2-D tensor | [[1,2],[3,4]] | A table |
| Operations | Math on tensors | tf.add(a, b) | Similar to NumPy |
import tensorflow as tf
# Scalars, vectors, matrices
a = tf.constant(5)
b = tf.constant([1, 2, 3])
c = tf.constant([[1, 2], [3, 4]])
print(a.shape) # ()
print(b.shape) # (3,)
print(c.shape) # (2, 2)
import tensorflow as tf
x = tf.constant([[1., 2.], [3., 4.]])
y = tf.constant([[5., 6.], [7., 8.]])
print(tf.add(x, y))
print(tf.matmul(x, y)) # matrix multiply
print(tf.reduce_sum(x))
SECTION 03Building your first model
Use the Keras Sequential API to build a simple neural network.
| Layer | Purpose | Typical Use | Tip |
|---|---|---|---|
| Input | Shape of the data | Features | Match dataset |
| Dense | Fully connected layer | Hidden layers | Use ReLU |
| Dropout | Regularization | Prevent overfitting | 0.2 – 0.5 |
| Output | Final prediction | Softmax / sigmoid | Task-dependent |
import tensorflow as tf
from tensorflow.keras import layers
model = tf.keras.Sequential([
layers.Input(shape=(8,)),
layers.Dense(16, activation='relu'),
layers.Dense(8, activation='relu'),
layers.Dense(1, activation='sigmoid'),
])
model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'],
)
model.summary()
# Output:
# Model: "sequential"
# Layer (type) Output Shape Param #
# dense (Dense) (None, 16) 144
# dense_1 (Dense) (None, 8) 136
# dense_2 (Dense) (None, 1) 9
# Total params: 289
SECTION 04Training & evaluating
Training means adjusting weights to minimize the loss. Keras makes this simple with fit().
| Step | Action | Why | Tip |
|---|---|---|---|
| 1 | Split data | Train / test | 80/20 |
| 2 | Fit the model | Learn weights | Use validation_split |
| 3 | Evaluate | Measure accuracy | Use test set |
| 4 | Predict | Use the model | Check on new data |
history = model.fit(
X_train, y_train,
epochs=20,
batch_size=32,
validation_split=0.2,
verbose=2,
)
loss, accuracy = model.evaluate(X_test, y_test)
print(f"Loss: {loss:.4f}")
print(f"Accuracy: {accuracy:.4f}")
# Predict on new data
preds = model.predict(X_test[:5])
print(preds)
SECTION 05A complete beginner project
Put everything together on a small dataset — for example, classifying Iris flowers.
| Phase | Focus | Tools | Goal |
|---|---|---|---|
| Data | Load & inspect | pandas, sklearn | Understand features |
| Preprocess | Normalize, split | sklearn | Train/test split |
| Model | Build network | Keras | Working model |
| Evaluate | Metrics | Keras | Accuracy > 90% |
import tensorflow as tf
from tensorflow.keras import layers
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Load data
X, y = load_iris(return_X_y=True)
X = StandardScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Build model
model = tf.keras.Sequential([
layers.Input(shape=(4,)),
layers.Dense(16, activation='relu'),
layers.Dense(3, activation='softmax'),
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train
model.fit(X_train, y_train, epochs=50, verbose=0)
# Evaluate
loss, acc = model.evaluate(X_test, y_test)
print(f"Accuracy: {acc:.3f}")
After your first project:
□ Try a bigger dataset (MNIST, CIFAR-10)
□ Learn data augmentation
□ Try transfer learning with a pre-trained model
□ Learn model saving (model.save)
□ Learn deployment (TF Serving / FastAPI)
SECTION 06Interview Q&A — TensorFlow
Q1What is TensorFlow?
An open-source ML framework by Google for building and training neural networks.
Q2What is a tensor?
A multi-dimensional array. It's the fundamental data structure in TensorFlow.
Q3What is Keras?
A high-level API built into TensorFlow that makes building models simple and readable.
Q4What is overfitting?
When a model memorizes training data and performs poorly on new data.
Q5Do I need a GPU?
Not for small beginner projects. A GPU helps for large datasets and deep models.
SECTION 07Test yourself — TensorFlow quiz
Five questions. No sign-up.
0 / 5Pick an answer to see why it is right or wrong.
SECTION 08Frequently asked questions
Is TensorFlow good for beginners?
Yes. With Keras, TensorFlow is beginner-friendly. Start with small projects.
Should I learn NumPy before TensorFlow?
Yes. NumPy basics make tensors much easier to understand.
TensorFlow vs PyTorch — which is better?
Both are excellent. TensorFlow is widely used in production; PyTorch is popular in research.
How much Python do I need?
Basic Python — variables, loops, functions, and a little OOP — is enough to start.
What is the easiest first project?
Iris classification or MNIST digit recognition — small, fast, and visual.
SECTION 09Related reads
Classroom & online · Noida
Start your AI/ML journey with TensorFlow
Our Data Science & AI Course covers Python, TensorFlow, deep learning, and real ML projects.
₹15,500 · full programme- Complete TensorFlow roadmap
- Hands-on ML projects
- Mock interviews
- Weekday & weekend batches




