Add model
Browse files- main.py +138 -0
- trained_model.npz +3 -0
main.py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import json
|
3 |
+
|
4 |
+
class LayerConfig:
|
5 |
+
def __init__(self, name, size, activation):
|
6 |
+
self.name = name
|
7 |
+
self.size = size
|
8 |
+
self.activation = activation
|
9 |
+
|
10 |
+
class SimpleMLModel:
|
11 |
+
def __init__(self, layer_configs, learning_rate=0.01, loss='mse'):
|
12 |
+
self.learning_rate = learning_rate
|
13 |
+
self.loss = loss
|
14 |
+
self.layer_configs = layer_configs
|
15 |
+
self.model = self._init_model()
|
16 |
+
|
17 |
+
def _init_model(self):
|
18 |
+
model = {}
|
19 |
+
sizes = [self.layer_configs[0].size] # Input layer size
|
20 |
+
|
21 |
+
for config in self.layer_configs[1:]: # Exclude input layer
|
22 |
+
sizes.append(config.size)
|
23 |
+
|
24 |
+
for i in range(len(sizes) - 1):
|
25 |
+
model[f'W{i}'] = np.random.randn(sizes[i], sizes[i+1]) * 0.01
|
26 |
+
model[f'b{i}'] = np.zeros((1, sizes[i+1]))
|
27 |
+
|
28 |
+
return model
|
29 |
+
|
30 |
+
def forward(self, X):
|
31 |
+
activations = [X]
|
32 |
+
for i, config in enumerate(self.layer_configs[1:]): # Exclude input layer
|
33 |
+
W = self.model[f'W{i}']
|
34 |
+
b = self.model[f'b{i}']
|
35 |
+
X = np.dot(X, W) + b
|
36 |
+
|
37 |
+
if config.activation == 'relu':
|
38 |
+
X = np.maximum(0, X)
|
39 |
+
elif config.activation == 'sigmoid':
|
40 |
+
X = 1 / (1 + np.exp(-X))
|
41 |
+
elif config.activation == 'tanh':
|
42 |
+
X = np.tanh(X)
|
43 |
+
|
44 |
+
activations.append(X)
|
45 |
+
return activations
|
46 |
+
|
47 |
+
def backward(self, activations, y_true):
|
48 |
+
grads = {}
|
49 |
+
dA = activations[-1] - y_true
|
50 |
+
|
51 |
+
for i in reversed(range(len(self.model) // 2)):
|
52 |
+
dZ = dA * (activations[i+1] > 0) # ReLU backward
|
53 |
+
grads[f'dW{i}'] = np.dot(activations[i].T, dZ) / y_true.shape[0]
|
54 |
+
grads[f'db{i}'] = np.sum(dZ, axis=0, keepdims=True) / y_true.shape[0]
|
55 |
+
if i > 0:
|
56 |
+
dA = np.dot(dZ, self.model[f'W{i}'].T)
|
57 |
+
|
58 |
+
return grads
|
59 |
+
|
60 |
+
def update_params(self, grads):
|
61 |
+
for i in range(len(self.model) // 2):
|
62 |
+
self.model[f'W{i}'] -= self.learning_rate * grads[f'dW{i}']
|
63 |
+
self.model[f'b{i}'] -= self.learning_rate * grads[f'db{i}']
|
64 |
+
|
65 |
+
def train(self, X, y, epochs=100):
|
66 |
+
for epoch in range(epochs):
|
67 |
+
activations = self.forward(X)
|
68 |
+
grads = self.backward(activations, y)
|
69 |
+
self.update_params(grads)
|
70 |
+
|
71 |
+
def predict(self, X):
|
72 |
+
activations = self.forward(X)
|
73 |
+
return activations[-1]
|
74 |
+
|
75 |
+
def save_model(self, filepath):
|
76 |
+
np.savez(filepath, **self.model)
|
77 |
+
|
78 |
+
def load_model(self, filepath):
|
79 |
+
data = np.load(filepath)
|
80 |
+
self.model = {k: data[k] for k in data}
|
81 |
+
|
82 |
+
def save_config(self, filepath):
|
83 |
+
config_list = []
|
84 |
+
for config in self.layer_configs:
|
85 |
+
config_list.append({
|
86 |
+
"name": config.name,
|
87 |
+
"size": config.size,
|
88 |
+
"activation": config.activation
|
89 |
+
})
|
90 |
+
|
91 |
+
with open(filepath, 'w') as f:
|
92 |
+
json.dump(config_list, f, indent=4)
|
93 |
+
|
94 |
+
def load_config(self, filepath):
|
95 |
+
with open(filepath, 'r') as f:
|
96 |
+
config_list = json.load(f)
|
97 |
+
|
98 |
+
self.layer_configs = []
|
99 |
+
for config_data in config_list:
|
100 |
+
self.layer_configs.append(LayerConfig(**config_data))
|
101 |
+
|
102 |
+
self.model = self._init_model() # Re-initialize model based on loaded config
|
103 |
+
|
104 |
+
input_size = 2
|
105 |
+
output_size = 1
|
106 |
+
|
107 |
+
# Example: Specific floating-point values for X and y
|
108 |
+
X = np.array([
|
109 |
+
[10, 10],
|
110 |
+
[5, 5],
|
111 |
+
[15, 15],
|
112 |
+
], dtype=np.float32) # Specify dtype if needed
|
113 |
+
|
114 |
+
y = np.array([
|
115 |
+
[20],
|
116 |
+
[10],
|
117 |
+
[30],
|
118 |
+
], dtype=np.float32)
|
119 |
+
|
120 |
+
# Define your model architecture using LayerConfig
|
121 |
+
layer_configs = [
|
122 |
+
LayerConfig("input", input_size, None),
|
123 |
+
LayerConfig("hidden1", 16, "sigmoid"),
|
124 |
+
#LayerConfig("hidden2", 32, "relu"),
|
125 |
+
LayerConfig("output", output_size, None)
|
126 |
+
]
|
127 |
+
|
128 |
+
# Create and train the model
|
129 |
+
model = SimpleMLModel(layer_configs, learning_rate=0.01, loss='mse')
|
130 |
+
model.train(X, y, epochs=1000)
|
131 |
+
|
132 |
+
# Save the trained model (optional)
|
133 |
+
model.save_model("trained_model.npz")
|
134 |
+
|
135 |
+
# Make predictions
|
136 |
+
predictions = model.predict(X)
|
137 |
+
|
138 |
+
print(predictions)
|
trained_model.npz
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8bc16c2817e7e999df630212410a1e199eda4f70911c2b1029ab359265c9c273
|
3 |
+
size 1486
|