0% found this document useful (0 votes)
12 views40 pages

Deep Learning Lab Manual for AI Diploma

This document is a lab manual for the Deep Learning Laboratory course at PSG Polytechnic College, focusing on various implementations of machine learning algorithms such as Linear Regression and Softmax Regression. It includes a structured index of experiments, aims, theories, algorithms, and sample programs for each experiment. The manual is prepared by Ms. V. Karpaga Varshini, the Head of the Department of Computer Networking.

Uploaded by

srikumaranrishi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views40 pages

Deep Learning Lab Manual for AI Diploma

This document is a lab manual for the Deep Learning Laboratory course at PSG Polytechnic College, focusing on various implementations of machine learning algorithms such as Linear Regression and Softmax Regression. It includes a structured index of experiments, aims, theories, algorithms, and sample programs for each experiment. The manual is prepared by Ms. V. Karpaga Varshini, the Head of the Department of Computer Networking.

Uploaded by

srikumaranrishi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PSG POLYTECHNIC COLLEGE

COIMBATORE -641004
DEPARTMENT OF COMPUTER NETWORKING
DIPLOMA IN ARTIFICIAL INTELLIGENCE & MACHINE LEARNING

REGULATIONS: 2021
EVEN SEMESTER (6th)
LAB MANUAL FOR DEEP LEARNING LABORATORY
COUSE CODE: R21651

Prepared by

Faculty Name:[Link] VARSHINI HoD


PSG POLYTECHNIC COLLEGE COIMBATORE -641004
DEPARTMENT OF COMPUTER NETWORKING
DIPLOMA IN ARTIFICIAL INTELLIGENCE & MACHINE LEARNING

R21651-DEEP LEARNING
LABORATORY OBSERVATION

Name:……………………………………………………………..

Roll No:…………………………………………………………...
Class:………………………………………………………………

CERTIFIED BONAFIDE LAB WORK DONE BY

Mr./Miss:…………………………………………………………………………

……………………………….. for the period from …………. to ……………

Date:…………… STAFF IN-CHARGE

INTERNAL EXAMINER EXTERNAL EXAMINER


INDEX

[Link] Date Name of the experiment Page no Marks Signature

1 Write a program for Linear Regression


Implementation from Scratch

2 Write a program to Concise Implementation of Linear


Regression

3 Write a program for Implementation of Soft max


Regression from Scratch

4 Write a program for Concise Implementation of


Softmax Regression

5 Write a program to Implementation of Multilayer


Perceptrons from Scratch

6 Write a program for Concise Implementation of


Multilayer Perceptrons

7 Write a program to Implement Multilayer


Perceptrons for house prices prediction on Kaggle

8 Write a program for Executing Code in the Forward


Propagation Function

9 Write a program to Loading and Saving Tensors

10 Write a program to Loading and Saving model


parameters

1. Case study of VGG

2. Case Study of NiN

3. Case Study of GoogleNet


[Link]-1 WRITE A PROGRAM FOR LINEAR REGRESSION IMPLEMENTATION FROM SCRATCH

Date-

AIM:

To implement Linear Regression using Gradient Descent from scratch in Python

THEORY:

Linear Regression is a supervised learning algorithm used to predict continuous values. It finds the best-fit line
by minimizing the difference between actual and predicted values using Gradient Descent, an optimization
technique that updates weights iteratively to reduce error.

ALGORITHM:

Step 1: Initialize ParametersSet all weights and bias to zero (or small random values). These will be updated
during training.

Step 2: Predict OutputCalculate predicted values using the current weights and bias:[ \hat{y} = X \cdot w + b ]

Step 3: Compute LossUse Mean Squared Error (MSE) to measure the difference between predicted and actual
values.

Step 4: Calculate GradientsCompute the gradients of the loss with respect to weights and bias to understand
the direction of steepest ascent.

Step 5: Update ParametersAdjust weights and bias in the opposite direction of the gradientsusing the learning
rate.

Step 6: RepeatRepeat steps 2–5 for a fixed number of iterations or until the loss converges.

Step 7: Make PredictionsOnce trained, use the final weights and bias to predict outcomes on new data.

PROGRAM:

1. importnumpyas np
2. classLinearRegression:
3. """Linear Regression using Gradient Descent"""
4. def __init__(self,learning_rate=0.01,n_iterations=1000):
5. self.learning_rate=learning_rate
6. self.n_iterations=n_iterations
7. [Link]=None
8. [Link]=None
9. deffit(self, X, y):
10. """Train the model"""
11. n_samples,n_features=[Link]
12. [Link]=[Link](n_features)
13. [Link]=0
14. foriin range(self.n_iterations):
15. y_pred=[Link](X,[Link])+[Link]
16. dw=(1/n_samples)*[Link](X.T,(y_pred- y))
17. db=(1/n_samples)*[Link](y_pred- y)
18. [Link]-=self.learning_rate*dw
19. [Link]-=self.learning_rate*db
20. defpredict(self, X):
21. """Make predictions"""
22. [Link](X,[Link])+[Link]
23.
24.
25.
26. # ============================================================================
27. # SAMPLE INPUT AND OUTPUT
28. # ============================================================================
29.
30. # Sample Input
31. X_train=[Link]([[1],[2],[3],[4],[5]]) # Features
32. y_train=[Link]([2,4,6,8,10]) # Target values
33. print(X_train)
34. print(y_train)
35.
36. # Train model
37. model =LinearRegression(learning_rate=0.01,n_iterations=1000)
38. [Link](X_train,y_train)
39.
40. # Sample Output
41. X_test=[Link]([[6],[7],[8]])
42. predictions =[Link](X_test)
43. print("SAMPLE OUTPUT:")
44. print(f"Learned Weight: {[Link][0]:.4f}")
45. print(f"Learned Bias: {[Link]:.4f}")
46. print(f"\nTest Input: {X_test.flatten()}")
47. print(f"Predictions: {[Link](2)}")
48.
49. # Another example with multiple features
50. print("EXAMPLE WITH MULTIPLE FEATURES:")
51.
52. X_multi =[Link]([[1,2],[2,3],[3,4],[4,5],[5,6]])
53. y_multi=[Link]([5,7,9,11,13])
54. print("Input X (2 features):")
55. print(X_multi)
56. print("\nTarget y:")
57. print(y_multi)
58. model_multi=LinearRegression(learning_rate=0.01,n_iterations=1000)
59. model_multi.fit(X_multi,y_multi)
60. X_test_multi=[Link]([[6,7],[7,8]])
61. predictions_multi=model_multi.predict(X_test_multi)
62. print("\nLearned Weights:",model_multi.[Link](4))
63. print(f"Learned Bias: {model_multi.bias:.4f}")
64. print(f"\nTest Input:\n{X_test_multi}")
65. print(f"Predictions: {predictions_multi.round(2)}")
SAMPLE OUTPUT:

Viva Questions
1. What is the objective function used in linear regression?

2. Why do we use gradient descent in linear regression?

3. What is the role of the learning rate?

4. Explain how weight updates are calculated in your implementation.

5. Why do we normalize data before training?

RESULT:

The Linear Regression model was successfully implemented using Gradient Descent. After training, the model
accurately learned the relationship between input features and target values, and was able to make
predictions on new data.
[Link]-2 WRITE A PROGRAM TO CONCISE IMPLEMENTATION OF LINEAR REGRESSION

Date-

AIM:

To implement a simplified version of Linear Regression using NumPy for predicting continuous values.

THEORY:

Linear Regression models the relationship between input features and a continuous target by fitting a straight
line. It minimizes the prediction error using a closed-form solution (Normal Equation) or iterative optimization
like Gradient Descent.

ALGORITHM:

Step 1: Import Libraries Use NumPy for numerical operations.

Step 2: Define the LinearRegression Class Create a class with and methods.

Step 3: Initialize Parameters Set learning rate, number of iterations, and initialize weights and bias.

Step 4: Compute Predictions Use the linear equation: [ \hat{y} = X \cdot w + b ]

Step 5: Calculate Loss and Gradients Use Mean Squared Error and compute gradients for weights and bias.

Step 6: Update Parameters Apply Gradient Descent to update weights and bias.

Step 7: Predict New Values Use the trained model to predict outputs for new inputs.

PROGRAM:

1 1. .importnumpyas np
2. [Link]
3. [Link]:
4. 4. def __init__(self):
5. 5. [Link]=None
6. 6. [Link]=None
7. 7. deffit(self, X, y):
8. 8. """Train the model using least squares method"""
9. 9. n =len(X)
10. 10. # Calculate slope and intercept
11. 11. x_mean=[Link](X)
12. 12. y_mean=[Link](y)
13. 13. numerator =[Link]((X -x_mean)*(y -y_mean))
14. 14. denominator =[Link]((X -x_mean)**2)
15. 15. [Link]= numerator / denominator
16. 16. [Link]=y_mean-[Link]*x_mean
17. 17. returnself
18. 18. defpredict(self, X):
19. 19. """Make predictions using the trained model"""
20. 20. [Link]* X +[Link]
21. 21. defscore(self, X, y):
22. 22. """Calculate R-squared score"""
23. 23. y_pred=[Link](X)
24. 24. ss_total=[Link]((y -[Link](y))**2)
25. 25. ss_residual=[Link]((y -y_pred)**2)
26. 26. return1-(ss_residual/ss_total)
27. # Sample Input: Hours studied vs Exam score
28. X =[Link]([1,2,3,4,5,6,7,8,9,10]) # Hours studied
29. y =[Link]([45,51,53,60,65,68,72,78,82,90]) # Exam scores
30.
31. # Train the model
32. model =LinearRegression()
33. [Link](X, y)
34.
35. # Make predictions
36. predictions =[Link](X)
37.
38. # Print results
39.
40. print("LINEAR REGRESSION RESULTS")
41. print(f"Slope (m): {[Link]:.4f}")
42. print(f"Intercept (b): {[Link]:.4f}")
43. print(f"Equation: y = {[Link]:.4f}x + {[Link]:.4f}")
44. print(f"R-squared Score: {[Link](X, y):.4f}")
45.
46. print("PREDICTIONS")
47.
48. print(f"{'Hours':<10} {'Actual':<10} {'Predicted':<10}")
49.
50. forx_val,y_actual,y_predinzip(X, y, predictions):
51. print(f"{x_val:<10} {y_actual:<10} {y_pred:<10.2f}")
52.
53. # Test with new data
54. print("NEW PREDICTIONS")
55. new_hours=[Link]([3.5,7.5,11])
56. new_predictions=[Link](new_hours)
57. for hours, score inzip(new_hours,new_predictions):
58. print(f"Studying {hours} hours → Predicted score: {score:.2f}")
59.
60. # Visualize
61. [Link](figsize=(10,6))
62. [Link](X, y, color='blue', label='Actual data', s=100, alpha=0.7)
63. [Link](X, predictions, color='red', linewidth=2, label='Regression line')
64. [Link]('Hours Studied',fontsize=12)
65. [Link]('Exam Score',fontsize=12)
66. [Link]('Linear Regression: Hours Studied vs Exam Score',fontsize=14)
67. [Link]()
68. [Link](True, alpha=0.3)
69. plt.tight_layout()
70. [Link]()
71.
SAMPLE OUTPUT:
Viva Questions
1. What libraries are used for concise linear regression?

2. How does the API automate gradient calculation?

3. What is the role of DataLoader in training?

4. Why is the concise model faster to implement?

5. What is an autograd engine?

RESULT:

The concise Linear Regression model was successfully implemented. It learned the optimal weights and bias
using Gradient Descent and accurately predicted outcomes for given input data.
[Link]-3 WRITE A PROGRAM FOR IMPLEMENTATION OF SOFT MAX REGRESSION FROM SCRATCH

Date-

AIM:

To implement Softmax Regression from scratch using NumPy for multi-class classification problems

THEORY:

Softmax Regression is a generalization of Logistic Regression for multi-class classification. It uses the softmax
function to convert raw scores (logits) into probabilities that sum to 1. The model is trained by minimizing the
cross-entropy loss using gradient descent.

ALGORITHM:

Step 1: Import Libraries Use NumPy for matrix operations.

Step 2: Initialize Parameters Set weights and bias to zeros. Dimensions depend on number of features and
classes.

Step 3: Compute Linear Scores Calculate raw scores: [ Z = X \cdot W + b ]

Step 4: Apply Softmax Function Convert scores to probabilities: [ \text{softmax}(z_i) = \frac{e^{z_i}}{\sum_j


e^{z_j}} ]

Step 5: Compute Cross-Entropy Loss Measure the difference between predicted and actual class probabilities.

Step 6: Compute Gradients Calculate gradients of loss w.r.t. weights and bias.

Step 7: Update Parameters Use gradient descent to update weights and bias.

PROGRAM:

1. importnumpyas np
2.
3. classSoftmaxRegression:
4. def __init__(self,learning_rate=0.1, epochs=500):
5. [Link] =learning_rate
6. [Link]= epochs
7. self.W=None
8. self.b=None
9.
10. defsoftmax(self, z):
11. exp_z=[Link](z -[Link](z))
12. returnexp_z/exp_z.sum(axis=1,keepdims=True)
13.
14. deffit(self, X, y):
15. n_samples,n_features=[Link]
16. n_classes=len([Link](y))
17.
18. # Initialize parameters
19. self.W=[Link](n_features,n_classes)*0.01
20. self.b=[Link]((1,n_classes))
21.
22. # One-hot encode labels
23. y_one_hot=[Link](n_classes)[y]
24.
25. # Training
26. for epoch in range([Link]):
27. # Forward pass
28. z = X @self.W+self.b
29. y_pred=[Link](z)
30.
31. # Gradients
32. dW= X.T @(y_pred-y_one_hot)/n_samples
33. db=[Link](y_pred-y_one_hot, axis=0)/n_samples
34.
35. # Update
36. self.W-=[Link] *dW
37. self.b-=[Link] *db
38.
39. defpredict(self, X):
40. z = X @self.W+self.b
41. probs =[Link](z)
42. [Link](probs, axis=1)
43.
44. # Sample Data: Classify fruits based on weight and color
45. # Features: [weight_grams, color_score]
46. # Classes: 0=Apple, 1=Orange, 2=Banana
47.
48. X =[Link]([
49. [150,1], [160,1], [155,2], # Apples (red=1-2)
50. [140,3], [145,4], [142,3], # Oranges (orange=3-4)
51. [120,5], [118,6], [125,5] # Bananas (yellow=5-6)
52. ])
53.
54. y =[Link]([0,0,0,1,1,1,2,2,2])
55.
56. # Train
57. model =SoftmaxRegression(learning_rate=0.5, epochs=500)
58. [Link](X, y)
59. # Predict
60. predictions =[Link](X)
61.
62. # Results
63. print("Training Results:")
64. print("-"*40)
65. classes =['Apple','Orange','Banana']
66. foriin range(len(X)):
67. print(f"Weight: {X[i,0]}g, Color: {X[i,1]} -> {classes[predictions[i]]}")
68.
69. # Test new data
70. print("\nNew Predictions:")
71. print("-"*40)
72. X_new=[Link]([[152,1],[143,4],[122,5]])
73. predictions_new=[Link](X_new)
74. foriin range(len(X_new)):
75. print(f"Weight: {X_new[i,0]}g, Color: {X_new[i,1]} -> {classes[predictions_new[i]]}")
76.
77.

SAMPLE OUTPUT:

Viva Questions
1. What is softmax function used for?

2. Why do we use cross-entropy loss in classification?

3. Explain how probabilities are computed in softmax.

4. What is the purpose of one-hot encoding?

5. Why is softmax sensitive to large input values?

RESULT:

The Softmax Regression model was successfully implemented. It learned to classify multi-class data by
optimizing cross-entropy loss and produced accurate predictions using the softmax probability distribution.
[Link]-4 WRITE A PROGRAM FOR CONCISE IMPLEMENTATION OF SOFTMAX REGRESSION

Date-

AIM:

To implement a simplified Softmax Regression model using NumPy for multi-class classification.

THEORY:

Softmax Regression extends Logistic Regression to handle multiple classes. It uses the softmax function to
convert raw scores into class probabilities and optimizes the model using cross-entropy loss and gradient
descent.

ALGORITHM:

Step 1: Import Libraries Use NumPy for numerical operations.

Step 2: Initialize Parameters Set weights and bias to zero based on input and output dimensions.

Step 3: Compute Scores Calculate raw class scores using matrix multiplication.

Step 4: Apply Softmax Function Convert scores to probabilities using the softmax formula.

Step 5: Compute Cross-Entropy Loss Measure prediction error using one-hot encoded labels.

Step 6: Update Parameters Use gradients to adjust weights and bias via gradient descent.

Step 7: Predict Class Labels Select the class with the highest probability for each input.

PROGRAM:

1. importnumpyas np
2.
3. classSoftmaxRegression:
4. def __init__(self,lr=0.01, iterations=1000):
5. [Link] =lr
6. [Link]= iterations
7. [Link]=None
8.
9. defsoftmax(self, scores):
10. exp_scores=[Link](scores -[Link](scores, axis=1,keepdims=True))
11. returnexp_scores/[Link](exp_scores, axis=1,keepdims=True)
12.
13. defcross_entropy_loss(self,y_true,y_pred):
14. m =y_true.shape[0]
15. [Link](y_true*[Link](y_pred+1e-9))/ m
16.
17. deffit(self, X, y):
18. m, n =[Link]
19. k =len([Link](y))
20.
21. # Add bias term
22. X_b=np.c_[[Link]((m,1)), X]
23.
24. # Initialize theta
25. [Link]=[Link](n +1, k)*0.01
26.
27. # One-hot encoding
28. Y =[Link]((m, k))
29. Y[[Link](m), y]=1
30.
31. # Gradient descent
32. foriin range([Link]):
33. scores = X_b.dot([Link])
34. probs =[Link](scores)
35. gradient =X_b.[Link](probs - Y)/ m
36. [Link]-=[Link] * gradient
37. ifi%250==0:
38. loss =self.cross_entropy_loss(Y, probs)
39. print(f"Iteration {i}: Loss = {loss:.4f}")
40.
41. defpredict(self, X):
42. m =[Link][0]
43. X_b=np.c_[[Link]((m,1)), X]
44. scores = X_b.dot([Link])
45. probs =[Link](scores)
46. [Link](probs, axis=1)
47. defaccuracy(self, X, y):
48. [Link]([Link](X)== y)
49.
50. # Dataset: Weather classification
51. # Features: [temperature_celsius, humidity_percent]
52. # Classes: 0=Sunny, 1=Rainy, 2=Cloudy
53. X_train=[Link]([
54. [30,40],[32,45],[28,42], # Sunny
55. [18,85],[16,90],[20,88], # Rainy
56. [22,65],[24,68],[23,70] # Cloudy
57. ])
58. y_train=[Link]([0,0,0,1,1,1,2,2,2])
59. weather_types=['Sunny','Rainy','Cloudy']
60.
61. # Train model
62. print("SOFTMAX REGRESSION - WEATHER CLASSIFICATION")
63. model =SoftmaxRegression(lr=0.5, iterations=1000)
64. [Link](X_train,y_train)
65.
66. acc =[Link](X_train,y_train)
67. print(f"\nTraining Accuracy: {acc * 100:.1f}%")
68.
69. # Predictions on training data
70. print("TRAINING DATA PREDICTIONS")
71. print(f"{'Temp(°C)':<10} {'Humidity%':<12} {'Actual':<10} {'Predicted'}")
72. print("-"*50)
73. pred_train=[Link](X_train)
74. foriin range(len(X_train)):
75. print(f"{X_train[i,0]:<10} {X_train[i,1]:<12} {weather_types[y_train[i]]:<10}
{weather_types[pred_train[i]]}")
76.
77. # Test with new data
78. print("NEW WEATHER PREDICTIONS")
79. X_test=[Link]([
80. [31,43], # Should be Sunny
81. [17,87], # Should be Rainy
82. [25,67], # Should be Cloudy
83. [29,50] # Should be Sunny
84. ])
85. pred_test=[Link](X_test)
86. print(f"{'Temp(°C)':<10} {'Humidity%':<12} {'Prediction'}")
87. print("-"*40)
88. foriin range(len(X_test)):
89. print(f"{X_test[i,0]:<10} {X_test[i,1]:<12} {weather_types[pred_test[i]]}")
90.

SAMPLE OUTPUT:
Viva Questions
1. What built-in functions are used to compute softmax?

2. How does the framework handle gradient computation?

3. What is the purpose of using CrossEntropyLoss directly?

4. Why is it unnecessary to one-hot encode labels here?

5. How does DataLoader help with batching?

RESULT:

The concise Softmax Regression model was implemented successfully. It learned to classify multi-class data by
optimizing cross-entropy loss and produced accurate predictions using softmax probabilities.
[Link]-5 WRITE A PROGRAM TO IMPLEMENTATION OF MULTILAYER PERCEPTRONS FROM
SCRATCH
Date-

AIM:

To implement a basic Multilayer Perceptron (MLP) from scratch using NumPy for binary or multi-class
classification tasks.

THEORY:

A Multilayer Perceptron is a feedforward neural network with one or more hidden layers. Each neuron applies
a weighted sum followed by a non-linear activation function. The network is trained using backpropagation
and gradient descent to minimize the loss.

ALGORITHM:

Step 1: Initialize Parameters Randomly initialize weights and biases for each layer.

Step 2: Forward Propagation Compute activations layer by layer using:

Step 3: Compute Loss Use binary cross-entropy or categorical cross-entropy depending on the task.

Step 4: Backward Propagation Calculate gradients of loss w.r.t. weights and biases using chain rule.

Step 5: Update Parameters Adjust weights and biases using gradient descent.

Step 6: Repeat Iterate steps 2–5 for multiple epochs.

Step 7: Predict Use final weights to compute output and classify new inputs.

PROGRAM:

1. importnumpyas np
2.
3. class MLP:
4. def __init__(self,input_size,hidden_size,output_size,lr=0.1):
5. [Link] =lr
6. # Initialize weights
7. self.W1 =[Link](input_size,hidden_size)*0.01
8. self.b1 =[Link]((1,hidden_size))
9. self.W2 =[Link](hidden_size,output_size)*0.01
10. self.b2 =[Link]((1,output_size))
11.
12. defsigmoid(self, z):
13. return1/(1+[Link](-z))
14.
15. defsigmoid_derivative(self, z):
16. return z *(1- z)
17.
18. defsoftmax(self, z):
19. exp_z=[Link](z -[Link](z, axis=1,keepdims=True))
20. returnexp_z/[Link](exp_z, axis=1,keepdims=True)
21.
22. defforward(self, X):
23. # Hidden layer
24. self.z1 = [Link](self.W1)+self.b1
25. self.a1 =[Link](self.z1)
26.
27. # Output layer
28. self.z2 =[Link](self.W2)+self.b2
29. self.a2 =[Link](self.z2)
30. returnself.a2
31.
32. defbackward(self, X, y, output):
33. m =[Link][0]
34.
35. # One-hot encode
36. y_one_hot=np.zeros_like(output)
37. y_one_hot[[Link](m), y]=1
38.
39. # Output layer gradients
40. dz2 = output -y_one_hot
41. dW2 =[Link](dz2)/ m
42. db2 =[Link](dz2, axis=0,keepdims=True)/ m
43.
44. # Hidden layer gradients
45. dz1 = [Link](self.W2.T)*self.sigmoid_derivative(self.a1)
46. dW1 = [Link](dz1)/ m
47. db1 =[Link](dz1, axis=0,keepdims=True)/ m
48.
49. # Update weights
50. self.W2 -=[Link] * dW2
51. self.b2 -=[Link] * db2
52. self.W1 -=[Link] * dW1
53. self.b1 -=[Link] * db1
54.
55. deftrain(self, X, y, epochs=1000):
56. for epoch in range(epochs):
57. output =[Link](X)
58. [Link](X, y, output)
59.
60. if epoch %250==0:
61. loss =-[Link]([Link](output[[Link](len(y)), y]+1e-8))
62. print(f"Epoch {epoch}: Loss = {loss:.4f}")
63.
64. defpredict(self, X):
65. output =[Link](X)
66. [Link](output, axis=1)
67.
68. defaccuracy(self, X, y):
69. predictions =[Link](X)
70. [Link](predictions == y)
71.
72. # Dataset: Animal classification
73. # Features: [weight_kg, height_cm, speed_kmh]
74. # Classes: 0=Cat, 1=Dog, 2=Horse
75.
76. X_train=[Link]([
77. [4,25,48], [5,28,50], [4.5,26,49], # Cats
78. [15,60,40], [18,65,42], [16,62,41], # Dogs
79. [450,160,70],[500,165,72],[480,162,71] # Horses
80. ])
81. y_train=[Link]([0,0,0,1,1,1,2,2,2])
82. animal_types=['Cat','Dog','Horse']
83.
84. # Create and train model
85. print("MULTILAYER PERCEPTRON - ANIMAL CLASSIFICATION")
86. mlp=MLP(input_size=3,hidden_size=5,output_size=3,lr=0.5)
87. [Link](X_train,y_train, epochs=1000)
88. acc =[Link](X_train,y_train)
89. print(f"\nTraining Accuracy: {acc * 100:.1f}%")
90.
91. # Predictions on training data
92. print("TRAINING DATA PREDICTIONS")
93. print(f"{'Weight(kg)':<12} {'Height(cm)':<12} {'Speed':<10} {'Actual':<8} {'Predicted'}")
94. print("-"*60)
95.
96. pred_train=[Link](X_train)
97. foriin range(len(X_train)):
98. print(f"{X_train[i,0]:<12} {X_train[i,1]:<12} {X_train[i,2]:<10}
{animal_types[y_train[i]]:<8} {animal_types[pred_train[i]]}")
99.
100. # Test with new data
101. print("NEW ANIMAL PREDICTIONS")
102. X_test=[Link]([
103. [4.2,27,49], # Cat
104. [17,63,41], # Dog
105. [470,161,71], # Horse
106. [5.5,30,51] # Cat
107. ])
108. pred_test=[Link](X_test)
109. print(f"{'Weight(kg)':<12} {'Height(cm)':<12} {'Speed':<10} {'Prediction'}")
110. print("-"*50)
111. foriin range(len(X_test)):
112. print(f"{X_test[i,0]:<12} {X_test[i,1]:<12} {X_test[i,2]:<10}
{animal_types[pred_test[i]]}")
113.
SAMPLE OUTPUT:

Viva Questions
1. What is the purpose of activation functions in MLP?

2. Why is ReLU preferred over sigmoid?

3. How do you calculate gradients in backpropagation?

4. What happens if we remove the activation function?

5. Explain forward propagation step-by-step.

RESULT:

The Multilayer Perceptron was successfully implemented. It learned to classify input data by adjusting weights
through backpropagation and produced accurate predictions after training.
[Link]- 6 WRITE A PROGRAM FOR CONCISE IMPLEMENTATION OF MULTILAYER PERCEPTRONS

Date-

AIM:

To implement a basic Multilayer Perceptron using NumPy for binary classification tasks with one hidden layer.

THEORY:

An MLP is a feedforward neural network with at least one hidden layer. Each neuron applies a weighted sum
followed by a non-linear activation (e.g., ReLU, Sigmoid). The model is trained using backpropagation and
gradient descent to minimize prediction error.

ALGORITHM:

Step 1: Initialize Parameters Randomly initialize weights and biases for input-to-hidden and hidden-to-output
layers.

Step 2: Forward Pass Compute activations using ReLU for hidden layer and Sigmoid for output layer.

Step 3: Compute Loss Use binary cross-entropy to measure prediction error.

Step 4: Backward Pass Calculate gradients of loss w.r.t. weights and biases using chain rule.

Step 5: Update Parameters Adjust weights and biases using learning rate and gradients.

Step 6: Repeat Iterate forward and backward passes for multiple epochs.

Step 7: Predict Use final weights to classify new inputs based on output probabilities.

PROGRAM:

1. importnumpyas np
2.
3. classMultilayerPerceptron:
4. def __init__(self, layers,lr=0.1, epochs=1000):
5. [Link]= layers
6. [Link] =lr
7. [Link]= epochs
8. [Link]=[]
9. [Link]=[]
10.
11. # Initialize weights and biases
12. foriin range(len(layers)-1):
13. w =[Link](layers[i], layers[i+1])*0.1
14. b =[Link]((1, layers[i+1]))
15. [Link](w)
16. [Link](b)
17.
18. defrelu(self, x):
19. [Link](0, x)
20.
21. defrelu_derivative(self, x):
22. return(x >0).astype(float)
23.
24. defsoftmax(self, x):
25. exp_x=[Link](x -[Link](x, axis=1,keepdims=True))
26. returnexp_x/[Link](exp_x, axis=1,keepdims=True)
27.
28. defforward_pass(self, X):
29. [Link]=[X]
30. self.z_values=[]
31.
32. foriin range(len([Link])):
33. z =[Link][-1].dot([Link][i])+[Link][i]
34. self.z_values.append(z)
35.
36. ifi<len([Link])-1:
37. a =[Link](z)
38. else:
39. a =[Link](z)
40.
41. [Link](a)
42.
43. [Link][-1]
44.
45. defbackward_pass(self, X, y):
46. m =[Link][0]
47.
48. # One-hot encoding
49. y_encoded=[Link]((m,[Link][-1]))
50. y_encoded[[Link](m), y]=1
51.
52. # Output layer error
53. delta =[Link][-1]-y_encoded
54.
55. # Backpropagate
56. foriin range(len([Link])-1,-1,-1):
57. grad_w=[Link][i].[Link](delta)/ m
58. grad_b=[Link](delta, axis=0,keepdims=True)/ m
59.
60. [Link][i]-=[Link] *grad_w
61. [Link][i]-=[Link] *grad_b
62.
63. ifi>0:
64. delta = [Link]([Link][i].T)*self.relu_derivative(self.z_values[i-1])
65.
66. deffit(self, X, y):
67. for epoch in range([Link]):
68. output =self.forward_pass(X)
69. self.backward_pass(X, y)
70.
71. if epoch %250==0:
72. loss =-[Link]([Link](output[[Link](len(y)), y]+1e-8))
73. print(f"Epoch {epoch}: Loss = {loss:.4f}")
74.
75. defpredict(self, X):
76. output =self.forward_pass(X)
77. [Link](output, axis=1)
78.
79. # Dataset: Grade prediction based on study patterns
80. # Features: [study_hours, assignments_completed]
81. # Classes: 0=Fail, 1=Pass, 2=Excellence
82.
83. X =[Link]([
84. [1,2], [2,3], [1.5,2], # Fail
85. [5,7], [6,8], [5.5,7], # Pass
86. [9,10], [10,10],[9.5,10] # Excellence
87. ])
88. y =[Link]([0,0,0,1,1,1,2,2,2])
89. grades =['Fail','Pass','Excellence']
90.
91. # Create MLP with architecture: 2 input -> 4 hidden -> 3 output
92. print("="*50)
93. print("MULTILAYER PERCEPTRON - GRADE PREDICTION")
94. print("="*50)
95. print("Architecture: 2 -> 4 -> 3")
96. print("Activation: ReLU (hidden), Softmax (output)")
97. print("="*50)
98. model =MultilayerPerceptron(layers=[2,4,3],lr=0.5, epochs=1000)
99. [Link](X, y)
100.
101. # Training predictions
102. predictions =[Link](X)
103. accuracy =[Link](predictions == y)*100
104. print(f"\nTraining Accuracy: {accuracy:.1f}%")
105. print("\n"+"="*50)
106. print("TRAINING RESULTS")
107. print("="*50)
108. print(f"{'Study Hrs':<12} {'Assignments':<14} {'Actual':<12} {'Predicted'}")
109. print("-"*50)
110.
111. foriin range(len(X)):
112. print(f"{X[i,0]:<12} {X[i,1]:<14} {grades[y[i]]:<12} {grades[predictions[i]]}")
113.
114. # New predictions
115. print("\n"+"="*50)
116. print("NEW PREDICTIONS")
117. print("="*50)
118.
119. X_new=[Link]([
120. [1.8,2.5],
121. [5.2,7.5],
122. [9.8,10],
123. [3,5]
124. ])
125. pred_new=[Link](X_new)
126. print(f"{'Study Hrs':<12} {'Assignments':<14} {'Predicted Grade'}")
127. print("-"*40)
128. foriin range(len(X_new)):
129. print(f"{X_new[i,0]:<12} {X_new[i,1]:<14} {grades[pred_new[i]]}")

SAMPLE OUTPUT:
Viva Questions Exercise – 6
1. Why is GPU support important?

2. How does the framework define layers automatically?

3. What is the difference between [Link]() and [Link]()?

4. What is dropout and why is it used?

5. How does the optimizer update weights internally?

RESULT:

The concise MLP model was implemented successfully. It learned to classify binary input data by optimizing
weights through backpropagation and produced accurate predictions after training.
[Link]- 7 WRITE A PROGRAM TO IMPLEMENT MULTILAYER PERCEPTRONS FOR HOUSE PRICES
PREDICTION ON KAGGLE
Date-

AIM:

To implement a Multilayer Perceptron from scratch for predicting house prices using Kaggle dataset features.

THEORY:

A Multilayer Perceptron (MLP) is a feedforward neural network with one or more hidden layers. It learns
complex patterns in data by applying non-linear transformations and adjusting weights using backpropagation
and gradient descent. For regression tasks like house price prediction, the output layer uses a linear activation.

ALGORITHM:

Step 1: Load and Preprocess Data Use pandas to load the dataset, normalize features, and split into training
and test sets.

Step 2: Initialize Parameters Randomly initialize weights and biases for each layer.

Step 3: Forward Propagation Apply ReLU in hidden layers and linear activation in the output layer.

Step 4: Compute Loss Use Mean Squared Error (MSE) as the loss function.

Step 5: Backward Propagation Compute gradients of loss w.r.t. weights and biases using chain rule.

Step 6: Update Parameters Apply gradient descent to update weights and biases.

Step 7: Predict and Evaluate Use the trained model to predict house prices and evaluate using RMSE.

PROGRAM:

1. importnumpyas np
2.
3. class MLP:
4. def __init__(self,lr=0.0001, epochs=1000):
5. [Link] =lr
6. [Link]= epochs
7.
8. defrelu(self, x):
9. [Link](0, x)
10.
11. defrelu_derivative(self, x):
12. return(x >0).astype(float)
13.
14. deffit(self, X, y):
15. n_features=[Link][1]
16.
17. # Initialize weights (smaller values)
18. self.W1 =[Link](n_features,8)*0.01
19. self.b1 =[Link]((1,8))
20. self.W2 =[Link](8,1)*0.01
21. self.b2 =[Link]((1,1))
22.
23. for epoch in range([Link]):
24. # Forward pass
25. z1 = [Link](self.W1)+self.b1
26. a1 =[Link](z1)
27. z2 = [Link](self.W2)+self.b2
28. y_pred= z2 # Linear output for regression
29.
30. # Backward pass
31. m =[Link][0]
32. dz2 =(y_pred-[Link](-1,1))/ m
33. dW2 = [Link](dz2)
34. db2 =[Link](dz2, axis=0,keepdims=True)
35.
36. dz1 = [Link](self.W2.T)*self.relu_derivative(a1)
37. dW1 = [Link](dz1)
38. db1 =[Link](dz1, axis=0,keepdims=True)
39.
40. # Update weights
41. self.W1 -=[Link] * dW1
42. self.b1 -=[Link] * db1
43. self.W2 -=[Link] * dW2
44. self.b2 -=[Link] * db2
45.
46. if epoch %250==0:
47. mse=[Link]((y_pred.flatten()- y)**2)
48. print(f"Epoch {epoch}: MSE = {mse:.2f}")
49.
50. defpredict(self, X):
51. z1 = [Link](self.W1)+self.b1
52. a1 =[Link](z1)
53. z2 = [Link](self.W2)+self.b2
54. return [Link]()
55.
56. # Simple House Price Dataset
57. # Features: [size_sqft, bedrooms, age_years]
58. # Price in thousands
59.
60. X =[Link]([
61. [1500,3,10],
62. [2000,4,5],
63. [1200,2,15],
64. [2500,4,2],
65. [1800,3,8],
66. [3000,5,1],
67. [1000,2,20],
68. [2200,4,6]
69. ])
70. y =[Link]([250,350,180,420,290,550,150,380])
71.
72. # Normalize features (better scaling)
73. X_mean=[Link](axis=0)
74. X_std=[Link](axis=0)
75. X_norm=(X -X_mean)/X_std
76.
77. # Normalize target
78. y_mean=[Link]()
79. y_std=[Link]()
80. y_norm=(y -y_mean)/y_std
81.
82. # Train model
83. print("MLP FOR HOUSE PRICE PREDICTION")
84. model =MLP(lr=0.01, epochs=1000)
85. [Link](X_norm,y_norm)
86.
87. # Predictions (denormalize)
88. predictions_norm=[Link](X_norm)
89. predictions =predictions_norm*y_std+y_mean
90. print("RESULTS")
91. print(f"{'Size':<8} {'Beds':<6} {'Age':<6} {'Actual':<10} {'Predicted'}")
92. print("-"*50)
93. foriin range(len(X)):
94. print(f"{X[i,0]:<8} {X[i,1]:<6} {X[i,2]:<6} ${y[i]:<9}k ${predictions[i]:<9.0f}k")
95.
96. # Test new houses
97. print("NEW PREDICTIONS")
98. X_new=[Link]([
99. [1700,3,9],
100. [2800,5,3]
101. ])
102. X_new_norm=(X_new-X_mean)/X_std
103. pred_new_norm=[Link](X_new_norm)
104. pred_new=pred_new_norm*y_std+y_mean
105. print(f"{'Size':<8} {'Beds':<6} {'Age':<6} {'Predicted Price'}")
106. print("-"*40)
107. foriin range(len(X_new)):
108. print(f"{X_new[i,0]:<8} {X_new[i,1]:<6} {X_new[i,2]:<6} ${pred_new[i]:<9.0f}k")
109.
SAMPLE OUTPUT:

Viva Questions
1. Why is regression used for predicting house prices?

2. What preprocessing steps are needed for Kaggle datasets?

3. Why do we normalize continuous features?

4. What is k-fold cross validation?

5. What evaluation metric does Kaggle use for this problem?

RESULT:

The MLP model was successfully implemented and trained on the Kaggle house prices dataset. It learned to
predict house prices with reasonable accuracy, as evaluated by the RMSE metric.
[Link]- 8 WRITE A PROGRAM FOR EXECUTING CODE IN THE FORWARD PROPAGATION
FUNCTION
Date-

AIM:

To implement and execute the forward propagation function in a neural network to compute output
activations from input features.

THEORY:

Forward propagation is the process of passing input data through the layers of a neural network to compute
the output. Each layer applies a linear transformation followed by a non-linear activation function. It is the first
step in training and prediction.

ALGORITHM:

Step 1: Initialize Parameters Set weights and biases for each layer randomly or with zeros.

Step 2: Input Layer Feed input data ( X ) into the network.

Step 3: Hidden Layer Computation Compute: [ Z_1 = X \cdot W_1 + b_1,\quad A_1 = \text{ReLU}(Z_1) ]

Step 4: Output Layer Computation Compute: [ Z_2 = A_1 \cdot W_2 + b_2,\quad A_2 = \text{Sigmoid}(Z_2) ]

Step 5: Return Output Return final activation ( A_2 ) as the prediction.

PROGRAM:

1. importnumpyas np
2.
3. def sigmoid(z):
4. """Converts any number to range 0-1"""
5. return1/(1+[Link](-z))
6.
7. defforward_propagation(X, W1, b1, W2, b2):
8. """
9. Forward Propagation: Pass data through neural network
10. Input → Hidden Layer → Output
11. """
12. print("\n🔹 STEP 1: Input to Hidden Layer")
13. print("-"*40)
14. Z1 = [Link](W1)+ b1 # Multiply and add bias
15. print(f"Z1 = {Z1}")
16. A1 =[Link](0, Z1) # ReLU: negative values become 0
17. print(f"A1 (after ReLU) = {A1}")
18. print("\n🔹 STEP 2: Hidden Layer to Output")
19. print("-"*40)
20. Z2 = [Link](W2)+ b2 # Multiply and add bias
21. print(f"Z2 = {Z2}")
22. A2 = sigmoid(Z2) # Sigmoid: convert to 0-1
23. print(f"A2 (final output) = {A2}")
24. return A2
25.
26. # Example: Predict if student will pass or fail
27. print("FORWARD PROPAGATION - SIMPLE EXAMPLE")
28.
29. # Input: [study_hours, attendance_percentage]
30. X =[Link]([[5,80]]) # 5 hours study, 80% attendance
31. print(f"\nInput (1 student): {X}")
32. print("Features: [study_hours, attendance%]")
33.
34. # Simple weights (randomly initialized)
35. W1 =[Link]([[0.5,0.3], # weights from input to hidden
36. [0.2,0.4]])
37.
38. b1 =[Link]([[0.1,0.2]]) # bias for hidden layer
39. W2 =[Link]([[0.6], # weights from hidden to output
40. [0.7]])
41. b2 =[Link]([[0.3]]) # bias for output
42. print("\nWeights W1 (Input→Hidden):")
43. print(W1)
44. print("\nWeights W2 (Hidden→Output):")
45. print(W2)
46.
47. # Run Forward Propagation
48. result =forward_propagation(X, W1, b1, W2, b2)
49.
50. print("FINAL RESULT")
51. print(f"Output: {result[0][0]:.4f}")
52. print(f"\nInterpretation:")
53. ifresult[0][0]>0.5:
54. print(f"PASS (confidence: {result[0][0]:.1%})")
55. else:
56. print(f"FAIL (confidence: {1-result[0][0]:.1%})")
57.
58. # Another example
59. print("TESTING WITH DIFFERENT STUDENT")
60.
61. X2 =[Link]([[2,50]]) # 2 hours study, 50% attendance
62. print(f"Input: {X2}")
63. print("Features: [study_hours=2, attendance=50%]")
64.
65. result2 =forward_propagation(X2, W1, b1, W2, b2)
66. print("FINAL RESULT")
67. print(f"Output: {result2[0][0]:.4f}")
68. print(f"\nInterpretation:")
69. if result2[0][0]>0.5:
70. print(f"PASS (confidence: {result2[0][0]:.1%})")
71. else:
72. print(f"FAIL (confidence: {1-result2[0][0]:.1%})")
73.
74. # Summary
75. print("HOW IT WORKS (SIMPLE)")
76. print("1. Take input (study hours, attendance)")
77. print("2. Multiply by weights + add bias → Hidden layer")
78. print("3. Apply ReLU (remove negatives)")
79. print("4. Multiply by weights + add bias → Output")
80. print("5. Apply Sigmoid (convert to 0-1 probability)")
81. print("\nIf output > 0.5 → PASS, else → FAIL")
82.
83.

SAMPLE OUTPUT:
Viva Questions
1. What is forward propagation?

2. Why do we define a forward() function?

3. How are intermediate values stored during propagation?

4. What is the difference between forward and backward passes?

5. How does the autograd system track operations?

RESULT:

The forward propagation function was successfully executed. It computed the output activation from the input
features using ReLU and Sigmoid functions, producing a prediction value.
[Link]-9 WRITE A PROGRAM TO LOADING AND SAVING TENSORS

Date-

AIM:

To demonstrate how to save and load tensors using PyTorch for efficient data reuse and model persistence.

THEORY:

Tensors are the core data structures in PyTorch. Saving tensors allows you to store intermediate results or
model parameters, while loading them enables reuse without recomputation. PyTorch [Link]()
[Link]() for serialization and deserialization.

ALGORITHM:

Step 1: Import PyTorch Use import torch to access tensor operations.

Step 2: Create a Tensor Define a tensor using [Link]() or [Link]() .

Step 3: Save the Tensor Use [Link](tensor, “[Link]”) to store the tensor.

Step 4: Load the Tensor Use [Link](“[Link]”) to retrieve the tensor.

Step 5: Verify Print the loaded tensor to confirm successful loading.

PROGRAM:

1. importnumpyas np
2.
3. print("="*50)
4. print("LOADING AND SAVING TENSORS")
5. print("="*50)
6.
7. # Create sample tensors
8. tensor_1d =[Link]([1,2,3,4,5])
9. tensor_2d =[Link]([[1,2,3],
10. [4,5,6],
11. [7,8,9]])
12.
13. weights ={
14. 'W1':[Link]([[0.5,0.3],[0.2,0.4]]),
15. 'b1':[Link]([0.1,0.2]),
16. 'W2':[Link]([[0.6],[0.7]]),
17. 'b2':[Link]([0.3])
18. }
19. print("\nOriginal Tensors:")
20. print(f"1D: {tensor_1d}")
21. print(f"2D:\n{tensor_2d}")
22.
23. # SAVING
24. print("SAVING TENSORS")
25.
26. # Save single tensor
27. [Link]('tensor_1d.npy', tensor_1d)
28. print("Saved single tensor: tensor_1d.npy")
29.
30. # Save multiple tensors
31. [Link]('[Link]', t1d=tensor_1d, t2d=tensor_2d)
32. print("Saved multiple tensors: [Link]")
33.
34. # Save model weights
35. [Link]('[Link]',**weights)
36. print("Saved model weights: [Link]")
37.
38. # LOADING
39. print("LOADING TENSORS")
40.
41. # Load single tensor
42. loaded_1d =[Link]('tensor_1d.npy')
43. print(f"Loaded 1D: {loaded_1d}")
44.
45. # Load multiple tensors
46. data =[Link]('[Link]')
47. print(f"Loaded files: {[Link]}")
48. print(f"Loaded 2D:\n{data['t2d']}")
49.
50. # Load model weights
51. model =[Link]('[Link]')
52. print(f"\nModel weights: {[Link]}")
53. print(f"W1:\n{model['W1']}")
54. print(f"W2:\n{model['W2']}")
55.
56. # VERIFICATION
57. print("VERIFICATION")
58. ifnp.array_equal(tensor_1d, loaded_1d):
59. print("✓ Data matches after save/load")
60. else:
61. print("✗ Data mismatch")
62.
63. # SUMMARY
64. print("SUMMARY")
65. print("Commands:")
66. print(" [Link]('[Link]', tensor) - Save single")
67. print(" [Link]('[Link]', a=t1, b=t2) - Save multiple")
68. print(" [Link]('[Link]') - Load tensor")
69. print(" [Link]('[Link]')['name'] - Access from npz")
SAMPLE OUTPUT:

Viva Questions
1. Why do we need to save tensors?

2. What file formats are used for saving tensors?

3. How do you load tensors back into memory?

4. What is serialization?

5. What are the advantages of saving tensors as binary files?

RESULT:

The tensor was successfully saved to disk and reloaded using PyTorch. The loaded tensor matched the original,
confirming correct serialization and deserialization.
[Link]- 10 WRITE A PROGRAM TO LOADING AND SAVING MODEL PARAMETERS

Date- 1. CASE STUDY OF VGG

2. CASE STUDY OF NIN

3. CASE STUDY OF GOOGLENET

AIM:

To demonstrate how to save and load model parameters in PyTorch using pretrained or custom models, with
case studies on VGG, NiN, and GoogLeNet architectures.

THEORY:

In PyTorch, model parameters (weights and biases) can be saved using [Link](model.state_dict()) and
loaded using model.load_state_dict([Link]()). This allows for checkpointing and reusing trained models
without retraining.

ALGORITHM:

Step 1: Import required libraries and model architecture

Step 2: Initialize the model (pretrained or custom)

Step 3: Save model parameters using

Step 4: Load parameters into a new model using

Step 5: Verify by comparing outputs or printing model summary

CASE STUDY 1: VGG

1. import torch
2. [Link] models
3.
4. # Step 1: Load pretrained VGG16
5. vgg=models.vgg16(pretrained=True)
6. # Step 2: Save model parameters
7. [Link](vgg.state_dict(),"vgg16_weights.pth")
8. # Step 3: Load into a new model
9. vgg_loaded=models.vgg16()
10. vgg_loaded.load_state_dict([Link]("vgg16_weights.pth"))
11. # Step 4: Verify
12. print("VGG model loaded successfully.")
13.
CASE STUDY 2: NiN

1. [Link]
2. import torch
3.
4. # Step 1: Define NiN block and model
5. defnin_block(in_channels,out_channels,kernel_size, stride, padding):
6. [Link](
7. nn.Conv2d(in_channels,out_channels,kernel_size, stride, padding),
8. [Link](),
9. nn.Conv2d(out_channels,out_channels,kernel_size=1),
10. [Link](),
11. nn.Conv2d(out_channels,out_channels,kernel_size=1),
12. [Link]()
13. )
14.
15. classNiN([Link]):
16. def __init__(self):
17. super(NiN,self).__init__()
18. [Link] =[Link](
19. nin_block(1,32,kernel_size=5, stride=1, padding=2),
20. nn.AdaptiveAvgPool2d((1,1)),
21. [Link](),
22. [Link](32,10)
23. )
24. defforward(self, x):
25. [Link](x)
26. # Step 2: Initialize and save
27. nin_model=NiN()
28. [Link](nin_model.state_dict(),"nin_model.pth")
29. # Step 3: Load
30. nin_loaded=NiN()
31. nin_loaded.load_state_dict([Link]("nin_model.pth"))
32. # Step 4: Verify
33. print("NiN model loaded successfully.")
34.
CASE STUDY 3: GoogLeNet

1. [Link] models
2. import torch
3.
4. # Step 1: Load pretrained GoogLeNet
5. googlenet=[Link](pretrained=True)
6. # Step 2: Save parameters
7. [Link](googlenet.state_dict(),"googlenet_weights.pth")
8. # Step 3: Load into a new model
9. googlenet_loaded=[Link]()
10. googlenet_loaded.load_state_dict([Link]("googlenet_weights.pth"))
11. # Step 4: Verify
12. print("GoogLeNet model loaded successfully.")
13.

Viva Questions
1. What is a state dictionary?

2. Why do we save only model parameters and not the full model?

3. How do you restore a saved model for inference?

4. What is the difference between save and load_state_dict()?

5. Why is model versioning important?

RESULT:

Model parameters for VGG, NiN, and GoogLeNet were successfully saved and reloaded using PyTorch. This
enables model reuse, transfer learning, and deployment without retraining.

You might also like