|
| 1 | +class ListGenerator: |
| 2 | + def __init__(self, hparams, L_in, L_out): |
| 3 | + self.hparams = hparams |
| 4 | + self._L_in = L_in |
| 5 | + self._L_out = L_out |
| 6 | + |
| 7 | + def _get_hidden_sizes(self) -> list: |
| 8 | + """ |
| 9 | + Generate the hidden layer sizes for the network based on nn_shape. |
| 10 | +
|
| 11 | + Returns: |
| 12 | + list: A list of hidden layer sizes. |
| 13 | + """ |
| 14 | + n_low = self._L_in // 4 # Minimum number of neurons |
| 15 | + # n_high = max(self.hparams.l1, 2 * n_low) # Maximum number of neurons |
| 16 | + |
| 17 | + # TODO: Überlegen, wie rum es besser ist |
| 18 | + if self.hparams.l_n > self.hparams.l1: |
| 19 | + self.hparams.l1 = self.hparams.l_n |
| 20 | + # raise ValueError("l_n must be bigger than l1") |
| 21 | + |
| 22 | + if self.hparams.nn_shape == "Funnel": |
| 23 | + step_size = (self.hparams.l1 - self._L_out) // self.hparams.l_n |
| 24 | + hidden_sizes = list(range(self.hparams.l1, self._L_out, -step_size)) |
| 25 | + |
| 26 | + elif self.hparams.nn_shape == "Diamond": |
| 27 | + mid_point = (self.hparams.l_n + 1) // 2 |
| 28 | + increasing_part = [self.hparams.l1] |
| 29 | + for _ in range(1, mid_point): |
| 30 | + next_size = int(increasing_part[-1] * 1.2) |
| 31 | + increasing_part.append(next_size) |
| 32 | + |
| 33 | + remaining_layers = self.hparams.l_n - mid_point |
| 34 | + step_size = (increasing_part[-1] - self._L_out) // (remaining_layers + 1) |
| 35 | + |
| 36 | + decreasing_part = [] |
| 37 | + current_size = increasing_part[-1] |
| 38 | + for _ in range(remaining_layers): |
| 39 | + current_size = max(self._L_out, current_size - step_size) |
| 40 | + decreasing_part.append(current_size) |
| 41 | + |
| 42 | + hidden_sizes = increasing_part + decreasing_part |
| 43 | + |
| 44 | + elif self.hparams.nn_shape == "Hourglass": |
| 45 | + mid_point = (self.hparams.l_n) // 2 |
| 46 | + step_size = (self.hparams.l1 - n_low) // (mid_point - 1) |
| 47 | + |
| 48 | + decreasing_part = [self.hparams.l1] |
| 49 | + for _ in range(1, mid_point): |
| 50 | + next_size = decreasing_part[-1] - step_size |
| 51 | + decreasing_part.append(max(n_low, next_size)) |
| 52 | + |
| 53 | + increasing_part = [decreasing_part[-1] + step_size] |
| 54 | + for _ in range(mid_point, self.hparams.l_n - 2): |
| 55 | + next_size = increasing_part[-1] + step_size |
| 56 | + increasing_part.append(min(self.hparams.l1, next_size)) |
| 57 | + |
| 58 | + last_step_size = (increasing_part[-1] - self._L_out) // 2 |
| 59 | + decreasing_to_output = max(self._L_out, increasing_part[-1] - last_step_size) |
| 60 | + |
| 61 | + hidden_sizes = decreasing_part + increasing_part + [decreasing_to_output] |
| 62 | + |
| 63 | + elif self.hparams.nn_shape == "Wave": |
| 64 | + half_wave = (self.hparams.l_n) // 4 |
| 65 | + step_size = (self.hparams.l1 - n_low) // (half_wave - 1) |
| 66 | + |
| 67 | + decreasing_part_1 = [self.hparams.l1] |
| 68 | + for _ in range(1, half_wave): |
| 69 | + next_size = decreasing_part_1[-1] - step_size |
| 70 | + decreasing_part_1.append(max(n_low, next_size)) |
| 71 | + |
| 72 | + increasing_part_1 = [decreasing_part_1[-1] + step_size] |
| 73 | + for _ in range(half_wave, 2 * half_wave - 1): |
| 74 | + next_size = increasing_part_1[-1] + step_size |
| 75 | + increasing_part_1.append(next_size) |
| 76 | + |
| 77 | + decreasing_part_2 = [increasing_part_1[-1] - step_size] |
| 78 | + for _ in range(2 * half_wave, 3 * half_wave - 1): |
| 79 | + next_size = decreasing_part_2[-1] - step_size |
| 80 | + decreasing_part_2.append(max(n_low, next_size)) |
| 81 | + |
| 82 | + increasing_part_2 = [decreasing_part_2[-1] + step_size] |
| 83 | + for _ in range(3 * half_wave, self.hparams.l_n - 2): |
| 84 | + next_size = increasing_part_2[-1] + step_size |
| 85 | + increasing_part_2.append(next_size) |
| 86 | + |
| 87 | + last_step_size = (increasing_part_2[-1] - self._L_out) // 2 |
| 88 | + decreasing_to_output = max(self._L_out, increasing_part_2[-1] - last_step_size) |
| 89 | + |
| 90 | + hidden_sizes = decreasing_part_1 + increasing_part_1 + decreasing_part_2 + increasing_part_2 + [decreasing_to_output] |
| 91 | + |
| 92 | + elif self.hparams.nn_shape == "Block": |
| 93 | + hidden_sizes = [self.hparams.l1] * self.hparams.l_n |
| 94 | + |
| 95 | + else: |
| 96 | + raise ValueError(f"Unknown nn_shape: {self.hparams.nn_shape}") |
| 97 | + |
| 98 | + return hidden_sizes |
0 commit comments