Loss Function with Simple Python
Python Simulation Without Libraries
# Simple loss calculator: y = mx + b
def predict(x, m, b):
return m * x + b
def mse_loss(y_true, y_pred):
error = 0
for yt, yp in zip(y_true, y_pred):
error += (yt - yp) ** 2
return error / len(y_true)
# Data
x_data = [1, 2, 3]
y_data = [2, 4, 6] # Ideal: y = 2x
# Let's predict with wrong values
m, b = 1.5, 0.5
predictions = [predict(x, m, b) for x in x_data]
loss = mse_loss(y_data, predictions)
print("Loss:", loss)
Summary — Relevancy of Loss Function
- Core metric guiding learning.
- Without it, no feedback — no learning.
- Shapes how we optimize the network.
- Helps detect underfitting/overfitting.
- Used to compare multiple models objectively.
