Repository navigation
Expand file tree
/
Copy pathtrain.py
More file actions
62 lines (47 loc) · 1.43 KB
/
Copy pathtrain.py
File metadata and controls
62 lines (47 loc) · 1.43 KB
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
import csv
import json
with open("params.json", "r") as f:
params = json.load(f)
b = float(params["bias"])
w = params["weights"]
with open("Housing_ls.csv", "r") as f:
dataset = [[float(x) for x in row] for row in csv.reader(f)]
n = len(dataset)
epochs = 10000
learn_rate = 1e-5
for i, row in enumerate(dataset):
if len(row) != len(w) + 1:
raise ValueError(f"Row {i} has {len(row)} values, expected {len(w)+1}: {row}")
old_mse = None
for epoch in range(epochs):
errors = []
mse = 0.0
for row in dataset:
y = row[0]
x = row[1:]
y_p = sum(x_i * w_i for x_i, w_i in zip(x, w)) + b
error = y_p - y
errors.append(error)
mse += error ** 2
mse /= n
if epoch == 0:
old_mse = mse
grad_w = [0.0 for _ in range(len(w))]
grad_b = 0.0
for i, row in enumerate(dataset):
x = row[1:]
for j in range(len(w)):
grad_w[j] -= 2 * errors[i] * x[j] / n
grad_b -= 2 * errors[i] / n
w = [w_j + learn_rate * grad_j for w_j, grad_j in zip(w, grad_w)]
b += learn_rate * grad_b
if epoch % 100 == 0 or epoch == epochs - 1:
print("--------------------")
print(f"Epoch: #{epoch}")
print(f"MSE: {mse}")
print("\n--Training--")
print(f"Old Loss: {old_mse}")
print(f"New Loss: {mse}")
print(f"diff: {old_mse - mse}")
with open("params.json", "w") as f:
json.dump({"bias": b, "weights": w}, f)