Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
codebasics
GitHub Repository: codebasics/deep-learning-keras-tf-tutorial
Path: blob/master/5_loss/loss_function_exercise_solution.ipynb
1141 views
Kernel: Python 3

Loss function exercise: Implement Mean Squared Error Function In Python

You need to implement mean squared error function first without using numpy and then using numpy

Solution 1: Without using numpy

import numpy as np y_predicted = np.array([1,1,0,0,1]) y_true = np.array([0.30,0.7,1,0,0.5])
def mse(y_true, y_predicted): total_error = 0 for yt, yp in zip(y_true, y_predicted): total_error += (yt-yp)**2 print("Total Squared Error:",total_error) mse = total_error/len(y_true) print("Mean Squared Error:",mse) return mse
mse(y_true, y_predicted)
Total Squared Error: 1.83 Mean Squared Error: 0.366
0.366

Solution 2: By using numpy

np.mean(np.square(y_true-y_predicted))
0.366