-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshaping_network.py
More file actions
65 lines (49 loc) · 1.81 KB
/
shaping_network.py
File metadata and controls
65 lines (49 loc) · 1.81 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
63
64
65
# Author: Bendik Brissach
# Created: 25.01.2026
# Description:
from typing import List
from ctx_vector import ContextVector
from layers import LayerConfig, PartitionLayer, LayerBuilder
class ShapingNetwork:
def __init__(self, configs: List[LayerConfig]):
self.configs = configs
self.layers: List[PartitionLayer] = []
self.epochs = 0
self.loss = 0.0
self.initialized = False
def initialize(self, input_dim: int):
if self.initialized: return
for config in self.configs:
self.layers.append(PartitionLayer(config.attribute, config.functions, input_dim))
self.initialized = True
def randomize(self):
if not self.initialized: raise RuntimeError("Network not initialized")
for layer in self.layers:
layer.randomize()
def feed(self, vector: ContextVector, target: int):
self.initialize(vector.dimension())
target_layers = [l for l in self.layers if l.attribute() == target]
if not target_layers:
raise ValueError(f"No layers found for target {target}")
for layer in target_layers:
layer.shape(vector)
probs = self.predict(vector)
self.loss = 1.0 - max(probs)
self.epochs += 1
def predict(self, vector: ContextVector) -> List[float]:
return [layer.predict(vector) for layer in self.layers]
def accuracy(self) -> float:
return 1.0 - self.loss
class ShapingNetworkBuilder:
def __init__(self):
self.configs = []
def connect(self, layer_builder: LayerBuilder):
self.configs.append(layer_builder.build())
return self
def connect_in_range(self, start: int, end: int, layer_builder: LayerBuilder):
for i in range(start, end + 1):
builder = layer_builder.copy().attribute(i)
self.configs.append(builder.build())
return self
def build(self) -> ShapingNetwork:
return ShapingNetwork(self.configs)