TensorFlow and Keras Basics

अब जब आपने Deep Learning के theory और models (जैसे CNN, RNN, BERT) अच्छे से समझ लिए हैं —
तो अगला practical step है:
⚙️ TensorFlow और Keras के साथ Deep Learning models बनाना सीखना।


🔷 1. TensorFlow क्या है?

TensorFlow एक open-source deep learning framework है जिसे Google ने बनाया है।
यह numerical computation और large-scale machine learning models के लिए design किया गया है।

🧠 TensorFlow का नाम “Tensor” (data structure) + “Flow” (computation graph) से आया है।


✅ Key Features:

FeatureDetail
📊 Automatic DifferentiationGradient calculation
🧮 GPU/TPU Supportतेज़ computation
🧠 High-level + Low-level APIsFlexibility
🔧 DeploymentAndroid, Web, Edge devices
🤝 EcosystemTF Hub, TF Lite, TF.js, TF-Serving

🔷 2. Keras क्या है?

Keras एक high-level deep learning API है, जो TensorFlow के ऊपर चलता है।
यह models को लिखना, train करना और debug करना बहुत आसान बना देता है।

🎯 “Keras = Simplicity + Productivity + Modularity”


✅ Keras क्यों चुनें?

BenefitReason
🚀 Easy to LearnPythonic syntax
🧩 ModularLayers, Optimizers, Loss अलग-अलग
🧠 PowerfulAdvanced models possible
🔧 Fast prototypingजल्दी result देखने के लिए
🔌 TF BackendTensorFlow की ताकत use करता है

🔷 3. Tensor, Model, and Layer Basics

🔹 Tensor:

Multidimensional array (जैसे NumPy array, लेकिन GPU-compatible)

import tensorflow as tf
x = tf.constant([[1, 2], [3, 4]])
print(x.shape) # (2, 2)

🔹 Layer:

Neural network का एक building block (Dense, Conv2D, LSTM)

from tensorflow.keras.layers import Dense
dense = Dense(units=64, activation='relu')

🔹 Model:

Input से Output तक का पूरा network architecture

from tensorflow.keras.models import Sequential

model = Sequential([
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])

🔷 4. Keras में Model बनाना (Step-by-Step)

✅ Step 1: Import

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

✅ Step 2: Model Define

model = Sequential([
Dense(64, activation='relu', input_shape=(100,)),
Dense(10, activation='softmax')
])

✅ Step 3: Compile

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

✅ Step 4: Train

model.fit(x_train, y_train, epochs=10, batch_size=32)

✅ Step 5: Evaluate

model.evaluate(x_test, y_test)

🧪 Example: Simple Binary Classifier

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

model = Sequential([
Dense(16, activation='relu', input_shape=(2,)),
Dense(1, activation='sigmoid')
])

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

model.fit(X_train, y_train, epochs=20)

🔧 Useful Layers in Keras

LayerUse
DenseFully connected layer
Conv2DImage convolution layer
LSTM / GRUSequence modeling
DropoutRegularization
FlattenInput flattening
EmbeddingWord embedding for NLP

🧠 Visualization: Model Summary

model.summary()

📝 Practice Questions:

  1. TensorFlow और Keras में क्या अंतर है?
  2. Sequential model क्या होता है?
  3. Model को compile करने में किन चीज़ों की ज़रूरत होती है?
  4. Dense layer क्या है?
  5. एक simple 3-layer model का कोड लिखिए।

🧠 Summary Table

ConceptDescription
TensorFlowGoogle का ML framework
KerasEasy high-level API
TensorMultidimensional data
LayerModel का हिस्सा (Dense, Conv)
ModelComplete NN architecture