MCPcopy
hub / github.com/geekcomputers/Python / SearchSpace

Class SearchSpace

ML/src/python/neuralforge/nas/search_space.py:18–181  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16 return f"Architecture(fitness={self.fitness:.4f}, acc={self.accuracy:.2f}%, params={self.params})"
17
18class SearchSpace:
19 def __init__(self, config: Dict[str, Any]):
20 self.config = config
21
22 self.layer_types = ['conv3x3', 'conv5x5', 'conv7x7', 'depthwise', 'bottleneck', 'identity']
23 self.activation_types = ['relu', 'gelu', 'silu', 'mish']
24 self.pooling_types = ['max', 'avg', 'none']
25 self.channels = [32, 64, 128, 256, 512]
26
27 self.num_layers = config.get('num_layers', 20)
28 self.num_blocks = config.get('num_blocks', 5)
29
30 def random_architecture(self) -> Architecture:
31 genome = []
32
33 for block_idx in range(self.num_blocks):
34 num_layers_in_block = random.randint(2, 5)
35
36 for layer_idx in range(num_layers_in_block):
37 layer_gene = {
38 'type': random.choice(self.layer_types),
39 'channels': random.choice(self.channels),
40 'activation': random.choice(self.activation_types),
41 'use_bn': random.choice([True, False]),
42 'dropout': random.uniform(0.0, 0.3),
43 }
44 genome.append(layer_gene)
45
46 pooling_gene = {
47 'type': 'pooling',
48 'pooling_type': random.choice(self.pooling_types),
49 }
50 genome.append(pooling_gene)
51
52 return Architecture(genome)
53
54 def build_model(self, architecture: Architecture, input_channels: int = 3, num_classes: int = 1000) -> nn.Module:
55 layers = []
56 current_channels = input_channels
57
58 for gene in architecture.genome:
59 if gene.get('type') == 'pooling':
60 if gene['pooling_type'] == 'max':
61 layers.append(nn.MaxPool2d(2))
62 elif gene['pooling_type'] == 'avg':
63 layers.append(nn.AvgPool2d(2))
64 else:
65 layer_type = gene['type']
66 out_channels = gene['channels']
67 activation = gene['activation']
68 use_bn = gene['use_bn']
69 dropout = gene['dropout']
70
71 if layer_type == 'conv3x3':
72 layers.append(nn.Conv2d(current_channels, out_channels, 3, padding=1))
73 elif layer_type == 'conv5x5':
74 layers.append(nn.Conv2d(current_channels, out_channels, 5, padding=2))
75 elif layer_type == 'conv7x7':

Callers 2

mainFunction · 0.90
mainFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected