model.train() PeDurx = 50 batch_size = 50 pe = PoissonEncoder(duration=PeDurx) optimizer = Adam(model.parameters(), lr=0.003) train_loader = DataLoader(dataset=MnistDataset(training=True, flatten=False), collate_fn=collate_fn, shuffle=True, batch_size=batch_size) test_loader = DataLoader(dataset=MnistDataset(training=False, flatten=False), collate_fn=collate_fn, shuffle=False, batch_size=batch_size) for epoch in range(15): for i, (data, target) in enumerate(train_loader): target = Variable(target) for t, q in enumerate(pe.Encoding(data)): data = Variable(q, requires_grad=True) output, time = model(data, t) loss = F.Spikeloss(output, target, time_step=time) loss.backward() # calc gradients optimizer.step() # update gradients print("Epoch:" + str(epoch) + " Time: " + str(t) + " loss: " + str(loss.data))
model = SNN() model.train() PeDurx = 50 batch_size = 64 Pencoder = PoissonEncoder(duration=PeDurx) optimizer = SGD(model.parameters(), lr=0.01) train_loader = DataLoader(dataset=MnistDataset(training=True, flatten=True), collate_fn=collate_fn, shuffle=True, batch_size=batch_size) test_loader = DataLoader(dataset=MnistDataset(training=False, flatten=True), collate_fn=collate_fn, shuffle=False, batch_size=batch_size) for i in range(15): for i, (image, label) in enumerate(train_loader): label = Variable(label) for t, j in enumerate(Pencoder.Encoding(image)): image = Variable(j, requires_grad=True) t = t + 1 output = model(image, t) optimizer.zero_grad() loss = F.Spikeloss(output, label, PeDurx) print(loss.data) loss.backward() optimizer.step()
PeDurx = 70 batch_size = 32 Pencoder = PoissonEncoder(duration=PeDurx) optimizer = SGD(model.parameters(), lr=0.01) train_loader = DataLoader(dataset=MnistDataset(training=True, flatten=True), collate_fn=collate_fn, shuffle=True, batch_size=batch_size) test_loader = DataLoader(dataset=MnistDataset(training=False, flatten=True), collate_fn=collate_fn, shuffle=False, batch_size=batch_size) for epoch in range(15): for i, (data, target) in enumerate(train_loader): target = Variable(target) for t in range(PeDurx): rnum = random.uniform(key=random.PRNGKey(0), shape=data.shape) uin = (jnp.abs(data) / 2 > rnum).astype('float32') q = jnp.multiply(uin, jnp.sign(data)) output, time = model(Variable(q), t) print(str(t)) loss = F.Spikeloss(output, target, time_step=time) loss.backward() # calc gradients optimizer.step() # update gradients print("Epoch:" + str(epoch) + "Time:" + str(i) + "loss:" + str(loss.data))