Clifford Hackett <3659745>
|
|
Fri, Sep 25, 12:29 AM (1 day ago) |
|
|
to vewu327qaxi
 |
|
import math
import random
# ==========================================
# 1. Engine Utilities & Network Implementation
# ==========================================
def exp(z):
return math.exp(z)
class NeuralNetwork:
def __init__(self, input_dim, hidden_dim, output_dim, act):
self.act = act
# Initialize random weights and biases
self.w1 = [[random.uniform(-1, 1) for _ in range(hidden_dim)] for _ in range(input_dim)]
self.b1 = [random.uniform(-1, 1) for _ in range(hidden_dim)]
self.w2 = [[random.uniform(-1, 1) for _ in range(output_dim)] for _ in range(hidden_dim)]
self.b2 = [random.uniform(-1, 1) for _ in range(output_dim)]
def forward(self, x):
# Hidden layer pass
self.h_raw = [
sum(x[i] * self.w1[i][j] for i in range(len(x))) + self.b1[j]
for j in range(len(self.b1))
]
self.h_out = [self.act(z) for z in self.h_raw]
# Output layer pass
self.o_raw = [
sum(self.h_out[i] * self.w2[i][j] for i in range(len(self.h_out))) + self.b2[j]
for j in range(len(self.b2))
]
# Output uses sigmoid for classification mapping (0 to 1)
self.o_out = [1 / (1 + math.exp(-z)) for z in self.o_raw]
return self.o_out
def learn(xs, ys, hidden=2, act=None, lr=0.1, epochs=10000):
input_dim = len(xs[0])
output_dim = len(ys[0])
net = NeuralNetwork(input_dim, hidden, output_dim, act)
for _ in range(epochs):
for x, y in zip(xs, ys):
pred = net.forward(x)
# Compute loss error and adjust weights numerically
for i in range(len(x)):
for j in range(hidden):
error = pred[0] – y[0]
net.w1[i][j] -= lr * error * x[i]
for j in range(hidden):
for k in range(output_dim):
error = pred[0] – y[0]
net.w2[j][k] -= lr * error * net.h_out[j]
return net
def grade(net, xs, ys):
total_loss = 0.0
correct = 0
for x, y in zip(xs, ys):
pred = net.forward(x)
# Mean Squared Error
total_loss += sum((p – t) ** 2 for p, t in zip(pred, y))
# Binary rounding check
binary_pred = 1 if pred[0] >= 0.5 else 0
if binary_pred == y[0]:
correct += 1
accuracy = correct / len(xs)
mse = total_loss / len(xs)
return f"Accuracy: {accuracy * 100:.0f}%, MSE Loss: {mse:.4f}"
# ==========================================
# 2. Activations, Dataset & Benchmark Execution
# ==========================================
def none(z):
return z
def sigmoid(z):
return 1 / (1 + math.exp(-z))
def relu(z):
return max(0, z)
def neuron(x, w, b, act):
z = sum(xi * wi for xi, wi in zip(x, w)) + b
return act(z)
xs = [[0, 0], [0, 1], [1, 0], [1, 1]]
ys = [[0], [1], [1], [0]]
for act in (none, sigmoid, relu):
net = learn(xs, ys, hidden=2, act=act)
print(act.__name__, grade(net, xs, ys))
Mahalo
SIGNATURE:
Clifford "RAY" Hackett I founded www.adapt.org in 1980 it now has over 50 million members.
$500 of material=World’s fastest hydrofoil sailboat. http://sunrun.biz