1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| import numpy as np import matplotlib.pyplot as plt
def Softmax(z): exp_scores = np.exp(z - np.max(z, axis=1, keepdims=True)) return exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
def predict(X, W): return Softmax(np.dot(X, W))
def cost_gradient(W, X, Y, n): G = (1 / n) * np.dot(X.T, predict(X, W) - Y) j = -np.sum(Y * np.log(predict(X, W))) / n
return (j, G)
def train(W, X, Y, n, lr, iterations): J = np.zeros([iterations, 1])
for i in range(iterations): (J[i], G) = cost_gradient(W, X, Y, n) W = W - lr * G
return (W, J)
def error(W, X, Y): Y_hat = predict(X, W) pred = np.argmax(Y_hat, axis=1) label = np.argmax(Y, axis=1) return (1 - np.mean(np.equal(pred, label)))
iterations = 200000 learning_rates = [0.0001, 0.00025, 0.0005, 0.001, 0.002]
data = np.loadtxt('SR.txt', delimiter=',') n = data.shape[0] X = np.concatenate([np.ones([n, 1]), np.expand_dims(data[:, 0], axis=1), np.expand_dims(data[:, 1], axis=1), np.expand_dims(data[:, 2], axis=1)], axis=1) Y = data[:, 3].astype(np.int32) c = np.max(Y) + 1 Y = np.eye(c)[Y]
W = np.random.random([X.shape[1], c])
best_lr = None best_error = float('inf') for lr in learning_rates: (W, J) = train(W.copy(), X, Y, n, lr, iterations) err = error(W, X, Y) print(f"Learning rate {lr}: Error = {err}") if err < best_error: best_lr = lr best_error = err
print(f"Best learning rate: {best_lr}, Best error: {best_error}")
(W, J) = train(W, X, Y, n, best_lr, iterations)
plt.figure() plt.plot(range(iterations), J) plt.xlabel('Iterations') plt.ylabel('Loss') plt.title('Loss over Iterations with Best Learning Rate') plt.show()
print(f"Final error with best learning rate: {error(W, X, Y)}")
|