def batch(network: lb.Net, input_patterns: Iterable[Iterable[float]], output_patterns: Iterable[Iterable[float]]) -> None: """Runs a training batch.""" num_epochs = 2 for _ in range(num_epochs): epoch(network, input_patterns, output_patterns) network.end_batch()
def epoch(network: lb.Net, input_patterns: Iterable[Iterable[float]], output_patterns: Iterable[Iterable[float]]) -> None: """Runs an epoch.""" for in_pattern, out_pattern in zip(input_patterns, output_patterns): for _ in range(10): trial(network, in_pattern, out_pattern) network.end_epoch()
def output(network: lb.Net, pattern: Iterable[float]) -> List[float]: """Runs the network with an input pattern and cleans up the output.""" network.clamp_layer("input", pattern) for _ in range(50): network.cycle() # We skip logging for speed out = network.layers["output"].units.act.numpy() out[out > 0.7] = 1 out[out < 0.1] = 0 return list(out)
def test_trial( network: lb.Net, input_pattern=torch.Tensor, output_pattern=torch.Tensor ) -> Tuple[Dict[str, torch.Tensor], Dict[str, torch.Tensor], bool, bool]: # Initial Response network.clamp_layer("Input", input_pattern) network.phase_cycle(TestPhase, num_cycles=20) initial_response = net_snapshot(network) print(type(initial_response["EC_out"])) print(type(output_pattern.byte())) sys.stdout.flush() initial_guess = ((initial_response["EC_out"] > 0.5) == output_pattern.byte()).all() # Final Response network.phase_cycle(TestPhase, num_cycles=60) final_response = net_snapshot(network) final_guess = ((final_response["EC_out"] > 0.5) == output_pattern.byte()).all() # Reset network.end_trial() return initial_response, final_response, initial_guess, final_guess
def trial(network: lb.Net, input_pattern: Iterable[float], output_pattern: Iterable[float]) -> None: """Runs a trial.""" network.clamp_layer("input", input_pattern) network.phase_cycle(phase=lb.MinusPhase, num_cycles=50) network.clamp_layer("output", output_pattern) network.phase_cycle(phase=lb.PlusPhase, num_cycles=25) network.unclamp_layer("input") network.unclamp_layer("output") network.end_trial()
def learn_trial(network: lb.Net, input_pattern=torch.Tensor, output_pattern=torch.Tensor) -> None: # Theta Trough network.clamp_layer("Input", input_pattern) network.uninhibit_projns("MSP: EC_in -> CA1") network.inhibit_projns("TSP: CA3 -> CA1", "Loop: EC_out -> EC_in") network.phase_cycle(ThetaTrough, num_cycles=20) # Theta Peak network.uninhibit_projns("TSP: CA3 -> CA1") network.inhibit_projns("MSP: EC_in -> CA1", "Loop: EC_out -> EC_in") network.phase_cycle(ThetaPeak, num_cycles=20) # Theta Plus network.uninhibit_projns("MSP: EC_in -> CA1", "Loop: EC_out -> EC_in") network.inhibit_projns("TSP: CA3 -> CA1") network.clamp_layer("EC_out", output_pattern) network.phase_cycle(ThetaPlus, num_cycles=60) # Reset network.uninhibit_projns("TSP: CA3 -> CA1") network.unclamp_layer("EC_in", "EC_out") network.end_trial() network.learn()
def trial(network: lb.Net, input_pattern: Iterable[float], output_pattern: Iterable[float]) -> None: """Runs a trial.""" network.clamp_layer("input", input_pattern) network.minus_phase_cycle(num_cycles=50) network.clamp_layer("output", output_pattern) network.plus_phase_cycle(num_cycles=25) network.unclamp_layer("input") network.unclamp_layer("output") network.learn()