Appendix
A.1: Full Python Code for the Neural Network
import numpy as np
np.random.seed(0)
X = np.round(np.random.rand(100, 1), 3)
Y = np.round(10 * X + 0.2 * np.random.randn(100, 1), 3)
weights = 1.000
def model(X, weights):
return np.dot(X, weights)
def loss(Y_true, Y_pred):
return np.mean((Y_true - Y_pred) ** 2)
def train(X, Y, weights, lr, epochs):
for epoch in range(epochs):
Y_pred = model(X, weights)
current_loss = loss(Y, Y_pred)
gradients = -2 * np.dot(X.T, (Y - Y_pred)) / len(X)
weights -= lr * gradients
Y_pred = model(X, weights)
new_loss = loss(Y, Y_pred)
return weights
weights = train(X, Y, weights, lr=0.1, epochs=50)
import matplotlib.pyplot as plt
X_test = np.round(np.random.rand(100, 1), 3)
Y_test = np.round(10 * X_test + 0.2 * np.random.randn(100, 1), 3)
def test(X, Y, weights):
Y_pred = model(X, weights)
test_loss = loss(Y, Y_pred)
return Y_pred
Y_pred = test(X_test, Y_test, weights)
plt.figure(figsize=(8, 6))
plt.scatter(X_test, Y_test, color='blue', label='True values')
plt.scatter(X_test, Y_pred, color='red', label='Predicted values')
plt.legend()
plt.xlabel('Input')
plt.ylabel('Output')
plt.title('True vs Predicted Values')
plt.show()
def predict(X, weights):
return np.dot(X, weights)
new_input = np.array([[-100]])
prediction = predict(new_input, weights)A.2: Glossary of Key Terms
A.3: Additional Resources for Learning
Contributors
Last updated