PYTHON KEY POINTS

📘 Python Technical Notes (Cheat Sheet)


1. Python Basics

1.1 Data Types & Conversions

  • Check datatype:

    type(variable)
    
  • Check length:

    len(variable)
    
  • Case conversion:

    variable.isupper()  
    variable.islower()  
    variable.upper()  
    variable.lower()
    
  • Type casting:

    int1 = 500
    str(int1)   # "500"
    
  • String formatting:

    print("----{ }--------{ }".format(val1, val2))

2. Python Data Structures 


🔹 1️⃣ LISTS

Declaration:

list1 = []               # empty list
list2 = [1, 2, 3, 'a']   # heterogeneous
list3 = list((10, 20, 30))  # using list() constructor

Properties:

  • ✅ Ordered

  • ✅ Mutable (modifiable)

  • ✅ Allows duplicates

  • ✅ Supports indexing & slicing


🔸 Common Operations:

Operation Description Example
append(x) Add single element at end list1.append(5)
extend(iterable) Add multiple elements list1.extend([6,7])
insert(i, x) Insert at index i list1.insert(1, 99)
remove(x) Remove first occurrence of x list1.remove(3)
pop(i=-1) Remove and return element at index i list1.pop(2)
clear() Remove all elements list1.clear()
index(x) Return index of first x list1.index(99)
count(x) Count occurrences list1.count(2)
sort(reverse=False, key=None) Sort list list1.sort()
reverse() Reverse list list1.reverse()
copy() Shallow copy new_list = list1.copy()

🔸 Indexing & Slicing:

a = [10, 20, 30, 40, 50]
a[0]       # 10
a[-1]      # 50
a[1:4]     # [20, 30, 40]
a[::-1]    # [50, 40, 30, 20, 10]

🔸 Other Useful Operations:

len(a)
sum(a)
max(a)
min(a)
sorted(a)

🔸 List Comprehension:

squares = [x**2 for x in range(5)]

🔸 Unpacking:

x, y, z = [1, 2, 3]

🔸 Nested Lists:

matrix = [[1,2,3], [4,5,6]]
matrix[1][2]  # 6

🔹 2️⃣ TUPLES

Declaration:

tup1 = ()                     # empty tuple
tup2 = (1, 2, 3)
tup3 = tuple([10, 20])
tup4 = (1,)                   # single-element tuple (note comma)

Properties:

  • ✅ Ordered

  • ❌ Immutable

  • ✅ Allows duplicates

  • ✅ Supports indexing & slicing


🔸 Common Operations:

Method / Operation Description Example
count(x) Count occurrences tup.count(2)
index(x) Return index of x tup.index(10)
len(tup) Get length len(tup)
max(tup) / min(tup) Get largest/smallest max(tup)
sum(tup) Sum (for numeric) sum(tup)
+ Concatenation (1,2)+(3,4)
* Repetition (1,2)*3
in Membership 2 in tup

🔸 Tuple Unpacking:

a, b, c = (10, 20, 30)

🔸 Nested Tuples:

nested = ((1,2), (3,4))
nested[0][1]  # 2

🔸 Conversion:

tuple([1, 2, 3])
list((4, 5, 6))

🔹 3️⃣ SETS

Declaration:

s1 = {1, 2, 3}
s2 = set([4, 5, 6])
empty_set = set()   # {} creates dictionary, not set!

Properties:

  • ❌ Unordered

  • ❌ Unindexed

  • ✅ Mutable (elements can be added/removed)

  • ❌ No duplicates


🔸 Common Methods:

Method Description Example
add(x) Add single element s1.add(4)
update(iterable) Add multiple s1.update([5,6])
remove(x) Remove element, raises error if not found s1.remove(2)
discard(x) Remove if present (no error) s1.discard(10)
pop() Remove random element s1.pop()
clear() Remove all s1.clear()
copy() Shallow copy s2 = s1.copy()

🔸 Set Operations:

Operation Example Result
Union `s1 s2ors1.union(s2)`
Intersection s1 & s2 or s1.intersection(s2) Common elements
Difference s1 - s2 Elements in s1 not in s2
Symmetric Difference s1 ^ s2 Elements not in both
Subset s1.issubset(s2) True/False
Superset s1.issuperset(s2) True/False
Disjoint s1.isdisjoint(s2) True/False

🔸 Frozenset:

Immutable version of set.

fs = frozenset([1,2,3])

🔹 4️⃣ DICTIONARIES

Declaration:

dict1 = {}                                  # empty
dict2 = {'a':1, 'b':2}
dict3 = dict(name="John", age=25)
dict4 = dict([('x',10), ('y',20)])

Properties:

  • ✅ Key–Value pairs

  • ✅ Mutable

  • ❌ No duplicate keys (last value retained)

  • ✅ Keys must be immutable (e.g., str, int, tuple)


🔸 Access & Update:

d = {'a':1, 'b':2}
d['a']          # 1
d.get('b')      # 2
d['c'] = 3      # add new
d['a'] = 10     # update

🔸 Common Methods:

Method Description Example
keys() Returns all keys d.keys()
values() Returns all values d.values()
items() Returns key-value pairs d.items()
get(key, default) Safe access d.get('x', 0)
pop(key, default) Remove key and return value d.pop('a')
popitem() Removes last inserted pair d.popitem()
update(dict2) Merge another dictionary d.update({'x': 9})
clear() Remove all items d.clear()
copy() Shallow copy d2 = d.copy()
setdefault(key, default) If key missing, add with default d.setdefault('z', 5)

🔸 Iteration:

for k in d.keys(): print(k)
for v in d.values(): print(v)
for k,v in d.items(): print(k, v)

🔸 Dictionary Comprehension:

squares = {x: x**2 for x in range(5)}

🔸 Nested Dictionaries:

students = {
    'John': {'age': 25, 'grade': 'A'},
    'Sara': {'age': 22, 'grade': 'B'}
}
print(students['John']['grade'])  # A

🔹 SUMMARY TABLE

Data Structure Ordered Mutable Allows Duplicates Syntax Example
List [1,2,3]
Tuple (1,2,3)
Set {1,2,3}
Dictionary ✅ (from Py3.7+) ❌ (keys) {'a':1, 'b':2}



3. Comprehensions

  • List:

    [x for x in variable if condition]
    
  • Dictionary:

    {x: x+2 for x in variable if condition}
    
  • Set:

    {x for x in variable if condition}
    

4. Functions & Errors

4.1 Function Structure

def func_name(args):
    try:
        ...
    except Exception:
        ...
    else:
        ...
    finally:
        ...

4.2 Error Types

  • Syntax Errors

  • Logical / Semantic Errors

  • Runtime Errors (Exceptions)

4.3 Common Exceptions

  • IndexError

  • ModuleNotFoundError

  • KeyError

  • ImportError

  • TypeError

  • ValueError

  • NameError

  • ZeroDivisionError


5. Lambda & Functional Programming

  • Conditional lambda:

    f = lambda x, y: x if x > y else y
    
  • Map:

    list(map(lambda x: x**2, [1,2,3]))
    
  • Filter:

    list(filter(lambda x: x%2==0, [1,2,3,4]))
    
  • Reduce:

    from functools import reduce
    reduce(lambda x, y: x+y, [1,2,3])
    

6. NumPy Basics

6.1 Array Creation

import numpy as np
x = np.array([...])
x.ndim
np.arange(1,10,2)
np.zeros((3,3))
np.random.randint(1,10, size=(2,3))

6.2 Indexing & Slicing

arr[2]
arr[1:4]

6.3 Operations

np.sum(arr, axis=0)
np.mod(arr, 2)
np.remainder(arr, 3)
np.divide(arr, 2)
np.multiply(arr1, arr2)
np.matmul(arr1, arr2)
np.add(arr1, arr2)
np.subtract(arr1, arr2)
np.max(arr)
np.min(arr)

6.4 Shape & Transform

np.arange(9).reshape(3,3)
np.array_split(arr, 3)
np.hsplit(arr, 2)
np.concatenate([arr1, arr2])
np.repeat(arr, 2)

7. Pandas Basics

7.1 Series

import pandas as pd
s = pd.Series(data=[1,2,3], index=['a','b','c'])
s.describe()
s.count()
s.sum()
s.mean()
s.median()
s.mode()
s.apply(lambda x: x*2)

7.2 DataFrame

df = pd.DataFrame({...})
pd.read_csv("file.csv")
pd.read_excel("file.xlsx")
  • Access:

    df.loc[row_label]
    df.iloc[row_num]
    df.columns
    df.dtypes
    
  • Iteration:

    df.iterrows()
    df.itertuples()
    df.items()
    
  • Descriptive:

    df.describe()
    df.shape
    df.info()
    df.head()
    df.tail()
    

7.3 Cleaning & Transformation

df.isnull().sum()
df.drop_duplicates(inplace=True)
df.replace(val1, val2, inplace=True)
df.groupby('col')['value'].mean()
df.sort_values(by='col')
df.reset_index()

7.4 Pivot & Aggregation

pd.pivot_table(df, index=['col1'], columns=['col2'], values=['val'])
df.agg({'col': ['min', 'max', 'mean']})

7.5 Export

df.to_csv("out.csv")
df.to_json("out.json")
df.to_html("out.html")
df.to_pickle("out.pkl")

8. Statistics in Python

  • Five-point summary: min, Q1, Q2, Q3, max

  • IQR = Q3 – Q1

  • Range = max – min

  • Variance: x.var()

  • Std Dev: x.std()

  • Covariance: x.cov()

  • Correlation: x.corr()

  • Skewness: x.skew()


9. Visualization

Univariate

  • Histogram: plt.hist(x, bins=10)

  • Distplot: sns.distplot(x)

  • Violinplot: sns.violinplot(x)

Multivariate

  • Pairplot: sns.pairplot(df)

  • Scatterplot: sns.scatterplot(x, y)

  • Heatmap: sns.heatmap(df.corr(), annot=True)

  • Pie chart: plt.pie(values)

  • Boxplot: plt.boxplot(x)

  • Countplot: sns.countplot(x)

  • Stripplot/Swarmplot


10. Data Preprocessing

10.1 Handling Missing Values

  • Remove unwanted chars

  • Impute with mean/median/KNN

  • Log transformation for skewness

10.2 Outlier Handling

  • IQR method

  • Log transformation

  • Capping

10.3 Encoding

  • Label Encoding:

    from sklearn.preprocessing import LabelEncoder
    le = LabelEncoder()
    df['col'] = le.fit_transform(df['col'])
    
  • OneHot Encoding:

    from sklearn.preprocessing import OneHotEncoder
    enc = OneHotEncoder()
    enc.fit_transform(df[['col']])
    

10.4 Scaling

  • StandardScaler

  • MinMaxScaler

  • Log / Exponential transformation



🧠 1️⃣ Statsmodels Cheat Sheet (for Statistical Modeling & Inference)

🔹 Importing

import statsmodels.api as sm import statsmodels.formula.api as smf

🔹 Core Purpose

Used for statistical modeling, hypothesis testing, and inference (unlike sklearn which focuses on prediction).


🔹 Regression Models

👉 Linear Regression (OLS)

X = sm.add_constant(X) # Adds intercept term model = sm.OLS(y, X).fit() print(model.summary()) # Shows coefficients, R², p-values, etc.

👉 Formula API (like R-style)

model = smf.ols('y ~ x1 + x2 + C(category)', data=df).fit() print(model.summary())

🔹 Logistic Regression

model = sm.Logit(y, X).fit() print(model.summary())

🔹 Time Series

from statsmodels.tsa.arima.model import ARIMA model = ARIMA(y, order=(p,d,q)).fit() print(model.summary()) forecast = model.forecast(steps=10)

🔹 Hypothesis Testing

from statsmodels.stats.weightstats import ttest_ind t_stat, p_value, df = ttest_ind(sample1, sample2)

Other tests:

  • ANOVAsm.stats.anova_lm(model, typ=2)

  • Durbin-Watson (autocorrelation)sm.stats.durbin_watson(model.resid)

  • Jarque-Bera (normality)sm.stats.jarque_bera(model.resid)

  • Breusch-Pagan (heteroscedasticity)sm.stats.diagnostic.het_breuschpagan(...)


🔹 Diagnostic Plots

sm.graphics.plot_regress_exog(model, 'x1') sm.qqplot(model.resid, line='s')

🔹 Key Strengths

✅ Detailed statistical summary
✅ Hypothesis testing & p-values
✅ Time series & econometrics support
✅ Formula-based modeling (like R)


⚙️ 2️⃣ Scikit-Learn (sklearn) Cheat Sheet

🔹 Import Core

from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler, LabelEncoder from sklearn.linear_model import LinearRegression, LogisticRegression from sklearn.metrics import accuracy_score, confusion_matrix, r2_score

🔹 Workflow

1️⃣ Split Data

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

2️⃣ Scale Data

scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test)

3️⃣ Train Model

model = LinearRegression() model.fit(X_train, y_train)

4️⃣ Predict

y_pred = model.predict(X_test)

5️⃣ Evaluate

print(r2_score(y_test, y_pred)) # regression print(accuracy_score(y_test, y_pred)) # classification

🔹 Key Modules

TaskCommon Classes
PreprocessingStandardScaler, LabelEncoder, OneHotEncoder, MinMaxScaler
RegressionLinearRegression, Ridge, Lasso, ElasticNet, SVR, RandomForestRegressor
ClassificationLogisticRegression, KNeighborsClassifier, SVC, RandomForestClassifier, XGBClassifier
ClusteringKMeans, DBSCAN, AgglomerativeClustering
Dimensionality ReductionPCA, TruncatedSVD, TSNE
Model SelectionGridSearchCV, RandomizedSearchCV, cross_val_score
Metricsr2_score, mean_squared_error, accuracy_score, precision_score, recall_score, f1_score, roc_auc_score

🔹 Pipelines

from sklearn.pipeline import Pipeline pipe = Pipeline([ ('scaler', StandardScaler()), ('model', LogisticRegression()) ]) pipe.fit(X_train, y_train) pipe.score(X_test, y_test)

🔹 Save/Load Model

import joblib joblib.dump(model, 'model.pkl') model = joblib.load('model.pkl')

🔹 Key Strengths

✅ Predictive modeling
✅ Feature engineering tools
✅ Pipelines and GridSearchCV
✅ Works seamlessly with numpy/pandas


🤖 3️⃣ Keras Cheat Sheet (for Deep Learning)

🔹 Import

from tensorflow import keras from keras.models import Sequential from keras.layers import Dense, Dropout, LSTM, Conv2D, Flatten

🔹 Sequential Model

model = Sequential([ Dense(64, activation='relu', input_shape=(X_train.shape[1],)), Dense(32, activation='relu'), Dense(1, activation='sigmoid') ])

🔹 Compile

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

🔹 Fit

history = model.fit(X_train, y_train, epochs=20, batch_size=32, validation_split=0.2)

🔹 Evaluate & Predict

model.evaluate(X_test, y_test) y_pred = model.predict(X_test)

🔹 Save/Load Model

model.save('model.h5') model = keras.models.load_model('model.h5')

🔹 Common Layers

LayerDescription
DenseFully connected layer
DropoutPrevents overfitting
FlattenConverts 2D → 1D
LSTM, GRUSequence modeling
Conv2D, MaxPooling2DCNN layers for image tasks
BatchNormalizationNormalizes activations

🔹 Activation Functions

NameUsage
reluHidden layers
sigmoidBinary classification
softmaxMulti-class output
tanhLSTM layers

🔹 Optimizers

OptimizerNotes
adamMost commonly used, adaptive
sgdVanilla stochastic gradient
rmspropRecurrent models (LSTM/GRU)

🔹 Loss Functions

ProblemLoss
Regressionmse, mae
Binary Classificationbinary_crossentropy
Multi-class Classificationcategorical_crossentropy

🔹 Visualize Training

import matplotlib.pyplot as plt plt.plot(history.history['loss'], label='train') plt.plot(history.history['val_loss'], label='val') plt.legend() plt.show()

🔹 Key Strengths

✅ Easy model building
✅ Works seamlessly with TensorFlow
✅ Fast prototyping
✅ Supports CNNs, RNNs, Transformers


🚀 Summary Table

LibraryFocusStrength
StatsmodelsStatistical analysis, inferencep-values, confidence intervals
SklearnPredictive MLPreprocessing, pipelines, metrics
KerasDeep LearningNeural networks, easy to build


TensorFlow Cheat Sheet


🧩 1️⃣ Core Concepts

🔹 Import TensorFlow

import tensorflow as tf print(tf.__version__)

TensorFlow is an end-to-end framework for building, training, and deploying machine learning models — from linear regression → deep neural networks → production deployment.


🧠 2️⃣ Tensors (Core Data Structure)

🔹 Creating Tensors

x = tf.constant([[1, 2], [3, 4]]) # Constant tensor y = tf.Variable([[5, 6], [7, 8]]) # Trainable tensor (used in models) z = tf.zeros([2, 3]) # Zero tensor w = tf.random.normal([3, 3], mean=0, stddev=1)

🔹 Tensor Operations

a = tf.add(x, y) b = tf.multiply(x, y) c = tf.matmul(x, y, transpose_b=True) d = tf.reduce_mean(x)

🔹 Converting Between NumPy and Tensor

import numpy as np n = np.array([[1, 2], [3, 4]]) t = tf.convert_to_tensor(n) n_back = t.numpy()

⚙️ 3️⃣ Basic Workflow (Model Pipeline)

1️⃣ Prepare Data →
2️⃣ Build Model →
3️⃣ Compile →
4️⃣ Train →
5️⃣ Evaluate →
6️⃣ Predict / Save


🧰 4️⃣ Dataset Handling

🔹 TensorFlow Dataset API

dataset = tf.data.Dataset.from_tensor_slices((X, y)) dataset = dataset.shuffle(buffer_size=100).batch(32).prefetch(1)

Use Case: Efficient data pipelines for large datasets.


🧱 5️⃣ Building Neural Networks

🔹 Sequential Model

from tensorflow.keras import Sequential from tensorflow.keras.layers import Dense, Dropout model = Sequential([ Dense(128, activation='relu', input_shape=(X_train.shape[1],)), Dropout(0.3), Dense(64, activation='relu'), Dense(1, activation='sigmoid') ])

⚗️ 6️⃣ Compiling the Model

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

🚀 7️⃣ Training the Model

history = model.fit( X_train, y_train, epochs=20, batch_size=32, validation_split=0.2 )

📈 8️⃣ Evaluate & Predict

model.evaluate(X_test, y_test) preds = model.predict(X_test)

💾 9️⃣ Save / Load Model

# Save full model model.save('model.h5') # Load model loaded_model = tf.keras.models.load_model('model.h5')

📊 🔟 Visualization

import matplotlib.pyplot as plt plt.plot(history.history['loss'], label='train_loss') plt.plot(history.history['val_loss'], label='val_loss') plt.legend() plt.show()

🧮 11️⃣ Common Layers in TensorFlow / Keras

LayerPurpose
Dense()Fully connected layer
Dropout()Prevents overfitting
Flatten()Convert 2D → 1D
Conv2D()Convolutional layer for images
MaxPooling2D()Downsampling
LSTM(), GRU()Sequence / time-series
BatchNormalization()Stabilizes training

🧠 12️⃣ Common Activation Functions

FunctionUse Case
reluHidden layers
sigmoidBinary output
softmaxMulti-class
tanhRecurrent networks

🔧 13️⃣ Optimizers

OptimizerDescription
adamAdaptive, default choice
sgdSimple gradient descent
rmspropGood for RNNs
adagradSparse data optimization

🧩 14️⃣ Loss Functions

TaskLoss Function
Regressionmse, mae
Binary Classificationbinary_crossentropy
Multi-class Classificationcategorical_crossentropy
CustomDefine using tf.keras.losses.Loss

🧰 15️⃣ Callbacks (During Training)

from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint es = EarlyStopping(monitor='val_loss', patience=3) mc = ModelCheckpoint('best_model.h5', save_best_only=True) model.fit(X_train, y_train, epochs=30, validation_split=0.2, callbacks=[es, mc])

🧮 16️⃣ TensorFlow Math & Gradient Operations

🔹 Auto-Differentiation

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

🧰 17️⃣ Transfer Learning (Example)

base_model = tf.keras.applications.MobileNetV2(weights='imagenet', include_top=False) base_model.trainable = False model = Sequential([ base_model, tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(1, activation='sigmoid') ]) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

☁️ 18️⃣ TensorFlow Extended / Serving / Lite

ModulePurpose
TFXProduction ML pipelines
TF LiteMobile / Edge deployment
TF ServingModel serving via API
TF HubPretrained model repository

🧩 19️⃣ TensorFlow Useful Shortcuts

tf.random.set_seed(42) # For reproducibility tf.keras.utils.plot_model(model, show_shapes=True)

🚀 20️⃣ TensorFlow vs Keras Quick Difference

AspectTensorFlowKeras
LevelLow-level, flexibleHigh-level, easy
Use CaseCustom training loops, fine controlQuick prototyping
IntegrationKeras now part of TensorFlow (tf.keras)Unified since TF 2.x

21️⃣ Common Interview-Ready Points

  • TensorFlow uses computational graphs to execute efficiently on CPUs, GPUs, TPUs.

  • Supports eager execution (run immediately like Python).

  • Keras API is now the official high-level interface of TensorFlow.

  • TensorFlow datasets (tf.data) allow parallelized + streamed data feeding.

  • You can write custom loss functions, custom layers, and custom training loops using tf.GradientTape.


🔥 PyTorch Cheat Sheet (For Data Science & Deep Learning)


🧩 1️⃣ Import & Setup

import torch import torch.nn as nn import torch.optim as optim import torch.nn.functional as F print(torch.__version__)

🔹 Check Device (CPU / GPU)

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

🧮 2️⃣ Tensors (Core Data Structure)

🔹 Creating Tensors

x = torch.tensor([[1, 2], [3, 4]]) y = torch.zeros((2, 3)) z = torch.ones((3, 3)) r = torch.rand((2, 2)) # Uniform random n = torch.randn((2, 2)) # Normal distribution

🔹 Tensor Operations

a = x + 2 b = x * y c = torch.matmul(x, x.T) d = x.mean()

🔹 Converting Between NumPy and Torch

import numpy as np arr = np.array([[1, 2, 3]]) tensor = torch.from_numpy(arr) arr_back = tensor.numpy()

🔹 GPU Usage

x = x.to(device)

⚙️ 3️⃣ Gradient & Autograd (Automatic Differentiation)

🔹 Enable Gradient Tracking

x = torch.tensor(2.0, requires_grad=True) y = x**2 + 3*x + 1 y.backward() print(x.grad) # dy/dx = 2x + 3 = 7

🧠 4️⃣ Building Neural Networks

🔹 Using nn.Module

class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.fc1 = nn.Linear(10, 50) self.fc2 = nn.Linear(50, 1) def forward(self, x): x = F.relu(self.fc1(x)) x = torch.sigmoid(self.fc2(x)) return x

⚗️ 5️⃣ Instantiate Model & Move to GPU

model = Net().to(device)

🧮 6️⃣ Loss & Optimizer

criterion = nn.BCELoss() # For binary classification optimizer = optim.Adam(model.parameters(), lr=0.001)

🚀 7️⃣ Training Loop

for epoch in range(10): model.train() optimizer.zero_grad() outputs = model(X_train) loss = criterion(outputs, y_train) loss.backward() optimizer.step() print(f'Epoch [{epoch+1}/10], Loss: {loss.item():.4f}')

📈 8️⃣ Evaluation

model.eval() with torch.no_grad(): y_pred = model(X_test) y_pred_class = (y_pred > 0.5).float()

💾 9️⃣ Save / Load Model

# Save torch.save(model.state_dict(), 'model.pth') # Load model = Net() model.load_state_dict(torch.load('model.pth')) model.eval()

🧰 🔟 Datasets & Dataloaders

🔹 TensorDataset + DataLoader

from torch.utils.data import TensorDataset, DataLoader train_ds = TensorDataset(X_train, y_train) train_dl = DataLoader(train_ds, batch_size=32, shuffle=True)

🔹 Iterate

for xb, yb in train_dl: pred = model(xb)

🧱 11️⃣ Common Layers

LayerDescription
nn.Linear()Fully connected layer
nn.Conv2d()2D convolution (images)
nn.MaxPool2d()Downsampling
nn.ReLU()Activation
nn.Dropout()Regularization
nn.BatchNorm2d()Normalize activations
nn.LSTM()Sequence model (RNN)
nn.Embedding()Word embeddings

12️⃣ Common Activation Functions

FunctionUse
F.relu(x)Hidden layers
torch.sigmoid(x)Binary output
F.softmax(x, dim=1)Multi-class
torch.tanh(x)RNNs

🧮 13️⃣ Loss Functions

TaskLoss Function
Regressionnn.MSELoss()
Binary Classificationnn.BCELoss() or nn.BCEWithLogitsLoss()
Multi-classnn.CrossEntropyLoss()

⚙️ 14️⃣ Optimizers

OptimizerImport
SGDoptim.SGD(model.parameters(), lr=0.01)
Adamoptim.Adam(model.parameters(), lr=0.001)
RMSpropoptim.RMSprop(model.parameters(), lr=0.001)

📊 15️⃣ Learning Rate Scheduler

scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.1) for epoch in range(10): train(...) scheduler.step()

🧩 16️⃣ TorchVision (for Images)

🔹 Import

import torchvision from torchvision import transforms, datasets, models

🔹 Transformations

transform = transforms.Compose([ transforms.Resize((128,128)), transforms.ToTensor(), transforms.Normalize(mean=[0.5], std=[0.5]) ])

🔹 Pretrained Model (Transfer Learning)

model = models.resnet18(pretrained=True) for param in model.parameters(): param.requires_grad = False model.fc = nn.Linear(model.fc.in_features, 2)

🧠 17️⃣ Custom Dataset Class

from torch.utils.data import Dataset class CustomDataset(Dataset): def __init__(self, X, y): self.X = X self.y = y def __len__(self): return len(self.X) def __getitem__(self, idx): return self.X[idx], self.y[idx]

🧮 18️⃣ GPU Acceleration Example

model = Net().to(device) X_train, y_train = X_train.to(device), y_train.to(device)

📈 19️⃣ Plot Training History

You can record losses manually in a list:

losses = [] for epoch in range(epochs): ... losses.append(loss.item()) import matplotlib.pyplot as plt plt.plot(losses) plt.title('Training Loss') plt.show()

🧠 20️⃣ Interview-Level Key Points

✅ PyTorch uses dynamic computation graphs (Define-by-Run) — built during execution (vs TensorFlow static graph).
torch.autograd handles automatic differentiation.
torch.nn is the neural network module.
torch.utils.data simplifies data loading.
torchvision & torchaudio handle vision/audio datasets.
✅ PyTorch is preferred for research, experimentation, and flexibility.
✅ Models can easily be exported to ONNX for deployment.


🧩 21️⃣ Comparing PyTorch vs TensorFlow

FeaturePyTorchTensorFlow
Graph TypeDynamic (define-by-run)Static (define-then-run)
Ease of DebuggingEasierSlightly complex
CommunityResearchProduction
High-Level APItorch.nn, Lightningtf.keras
DeploymentTorchServe / ONNXTF Serving / Lite

🧠 22️⃣ Useful Utilities

torch.manual_seed(42) # Reproducibility torchsummary.summary(model, (1, 28, 28)) # Model summary torch.cuda.empty_cache() # Clear GPU memory




Comments

Popular posts from this blog

Resume Work and Project Details

Time Series and MMM basics

LINEAR REGRESSION