1+ import torch
2+
3+ from bindsnet .network import Network
4+ from bindsnet .pipeline import Pipeline
5+ from bindsnet .learning import MSTDP
6+ from bindsnet .encoding import bernoulli
7+ from bindsnet .network .topology import Connection
8+ from bindsnet .environment import GymEnvironment
9+ from bindsnet .network .nodes import Input , LIFNodes
10+ from bindsnet .pipeline .action import select_softmax
11+
12+ # Build network.
13+ network = Network (dt = 1.0 )
14+
15+ # Layers of neurons.
16+ inpt = Input (n = 80 * 80 , shape = [80 , 80 ], traces = True )
17+ middle = LIFNodes (n = 100 , traces = True )
18+ out = LIFNodes (n = 4 , refrac = 0 , traces = True )
19+
20+ # Connections between layers.
21+ inpt_middle = Connection (source = inpt , target = middle , wmin = 0 , wmax = 1e-1 )
22+ middle_out = Connection (source = middle , target = out , wmin = 0 , wmax = 1 , update_rule = MSTDP , nu = 1e-1 , norm = 0.5 * middle .n )
23+
24+ # Add all layers and connections to the network.
25+ network .add_layer (inpt , name = 'Input Layer' )
26+ network .add_layer (middle , name = 'Hidden Layer' )
27+ network .add_layer (out , name = 'Output Layer' )
28+ network .add_connection (inpt_middle , source = 'Input Layer' , target = 'Hidden Layer' )
29+ network .add_connection (middle_out , source = 'Hidden Layer' , target = 'Output Layer' )
30+
31+ # Load SpaceInvaders environment.
32+ environment = GymEnvironment ('BreakoutDeterministic-v4' )
33+ environment .reset ()
34+
35+ # Build pipeline from specified components.
36+ pipeline = Pipeline (network , environment , encoding = bernoulli ,
37+ action_function = select_softmax , output = 'Output Layer' ,
38+ time = 100 , history_length = 1 , delta = 1 ,
39+ plot_interval = 1 , render_interval = 1 )
40+
41+
42+ # Train agent for 100 episodes.
43+ print ("Training: " )
44+ for i in range (100 ):
45+ pipeline .reset_ ()
46+ # initialize episode reward
47+ reward = 0
48+ while True :
49+ pipeline .step ()
50+ reward += pipeline .reward
51+ if pipeline .done :
52+ break
53+ print ("Episode " + str (i ) + " reward:" , reward )
54+
55+ # stop MSTDP
56+ pipeline .network .learning = False
57+
58+ print ("Testing: " )
59+ for i in range (100 ):
60+ pipeline .reset_ ()
61+ # initialize episode reward
62+ reward = 0
63+ while True :
64+ pipeline .step ()
65+ reward += pipeline .reward
66+ if pipeline .done :
67+ break
68+ print ("Episode " + str (i ) + " reward:" , reward )
0 commit comments