(net, env, spikes, episodes, gran=100, rfname="", pfname="")
| 100 | |
| 101 | |
| 102 | def runSimulator(net, env, spikes, episodes, gran=100, rfname="", pfname=""): |
| 103 | steps = env.timesteps |
| 104 | dt = net.dt |
| 105 | spike_ims, spike_axes = None, None |
| 106 | |
| 107 | # For each episode... |
| 108 | for ep in range(episodes): |
| 109 | # Reset variables for new episode. |
| 110 | total_reward = 0 |
| 111 | rewards = np.zeros(steps) |
| 112 | intercepts = 0 |
| 113 | step = 0 |
| 114 | net.reset_state_variables() |
| 115 | env.reset() |
| 116 | done = False |
| 117 | env.render() |
| 118 | clock = time.time() |
| 119 | |
| 120 | # Initialize action tensor, network output monitor, and spike train record. |
| 121 | action = torch.randint(low=0, high=env.action_space.n, size=(1,))[0] |
| 122 | spike_record = torch.zeros( |
| 123 | (steps, int(gran / dt), env.action_space.n), device=DEVICE |
| 124 | ) |
| 125 | # perf_ax = None |
| 126 | |
| 127 | # Run through episode. |
| 128 | while not done: |
| 129 | step += 1 |
| 130 | obs, reward, done, intercept = env.step(action) |
| 131 | obs = torch.Tensor(obs).to(DEVICE) |
| 132 | reward = reward.to(DEVICE) |
| 133 | |
| 134 | # Determine the action probabilities |
| 135 | probabilities = torch.softmax( |
| 136 | torch.sum(spike_record[step - 1 % steps], dim=0), dim=0 |
| 137 | ) |
| 138 | action = torch.multinomial(probabilities, num_samples=1).item() |
| 139 | |
| 140 | # Place the observations into the inputs. |
| 141 | obs = obs.unsqueeze(0) |
| 142 | inputs = {LAYER1: poisson(obs * 5e2, gran, dt, device=DEVICE)} |
| 143 | if DEVICE == "cuda": |
| 144 | inputs = {k: v.cuda() for k, v in inputs.items()} |
| 145 | |
| 146 | # Run the network on the spike train-encoded inputs. |
| 147 | net.run(inputs=inputs, time=gran, reward=reward) |
| 148 | spike_record[step % steps] = spikes[LAYER3].get("s").squeeze() |
| 149 | rewards[step - 1] = reward.item() |
| 150 | |
| 151 | # record successful intercept |
| 152 | if intercept: |
| 153 | intercepts += 1 |
| 154 | |
| 155 | if done: |
| 156 | # Update network with cumulative reward |
| 157 | if net.reward_fn is not None: |
| 158 | net.reward_fn.update(accumulated_reward=total_reward, steps=step) |
| 159 |
no test coverage detected