Package: cbpy
Chaotic Back-propagation (CBP)cbpy is a light package for research purpose, which implements the CBP algorithm in the paper "Brain-inspired Chaotic Back-Propagation".InstallpipinstallcbpyExamplesExample1. reveal the principles of Chaotic Back-propagation (CBP) with single-neuron network.1. prepare the datasetimporttorchimporttorch.nnasnnimportcbpyascbpinp=torch.FloatTensor([[1]])# input sampletgt=torch.FloatTensor([[0]])# target of input2. define single-neuron networkclassSingleNet(nn.Module):def__init__(self,init_value=0):super().__init__()self.layer=nn.Linear(1,1)self.act_func=nn.Sigmoid()nn.init.constant_(self.layer.weight,init_value)nn.init.constant_(self.layer.bias,init_value)defforward(self,x):out=self.act_func(self.layer(x))returnout3. training with BP algorithmnet=SingleNet()loss_func=nn.MSELoss()# loss functionoptimizer=torch.optim.SGD(net.parameters(),lr=1)loss_list=[]weight_list=[]bias_list=[]foriinrange(1000):optimizer.zero_grad()out=net(inp)loss_bp=loss_func(out,tgt)loss_bp.backward()optimizer.step()loss_list.append(loss_bp.item())weight_list.append(net.layer.weight.item())bias_list.append(net.layer.bias.item())4. plot the learning curve of the weight for BPimportseabornassnssns.set(context='notebook',style='whitegrid',font_scale=1.2)cbp.plot_series(weight_list,ylabel='w',title='weight of BP')5. training with CBP algorithm# define the chaotic loss functiondefchaos_loss(out,z,I0=0.65):return-z*(I0*torch.log(out)+(1-I0)*torch.log(1-out))# training with CBPnet=SingleNet()optimizer=torch.optim.SGD(net.parameters(),lr=1)z=9# initial chaotic intensitybeta=0.999# anealing constantloss_bp_list=[]loss_cbp_list=[]weight_list=[]bias_list=[]foriinrange(1000):optimizer.zero_grad()out=net(inp)loss_bp=loss_func(out,tgt)loss_chaos=chaos_loss(out,z)# chaotic lossloss_cbp=loss_bp+loss_chaos# loss of CBPloss_cbp.backward()optimizer.step()z*=betaloss_bp_list.append(loss_bp.item())loss_cbp_list.append(loss_cbp.item())weight_list.append(net.layer.weight.item())bias_list.append(net.layer.bias.item())6. plot the learning curve of the weight for CBPcbp.plot_series(weight_list,ylabel='w',title='weight of CBP')Example2. validate the global optimization ability of CBP on the XOR problem1. prepare the dataset and parameters# create the XOR datasettrainloader=cbp.create_xor_dataloader()inp,tgt=next(iter(trainloader))print(inp,'\n',tgt)# define paramsloss_func=torch.nn.BCELoss()# loss functionlr=0.2# learning ratemax_epoch=10000# maximal training epochseed=32# random number seedinit_mode=1# initial weight intervallayer_list=[2,2,1]# layers for MLP2. training by BPcbp.set_random_seed(seed)model=cbp.MLPS(layer_list,init_mode=init_mode,act_layer=torch.nn.Sigmoid(),active_last=True)zs=None# chaotic intensitycbp_epoch=0bp_l_list,bp_a_list,bp_w_list,bp_o_list=cbp.train_with_chaos(model=model,trainloader=trainloader,testloader=trainloader,loss_func=loss_func,zs=zs,record_weight=True,whole_weight=True,cbp_epoch=cbp_epoch,max_epoch=max_epoch,bp_lr=lr)3. plot the trajectories of the weights for BP (first 2000 epochs)cbp.plot_xor_weight(bp_w_list[:2000])Weights of BP4. training by BP from the same initial condition as CBPcbp.set_random_seed(seed)model=cbp.MLPS(layer_list,init_mode=init_mode,act_layer=torch.nn.Sigmoid(),active_last=True)zs=12# chaotic intensitybeta=0.999# annealing constantcbp_epoch=max_epochcbp_l_list,cbp_a_list,cbp_w_list,cbp_o_list=cbp.train_with_chaos(model=model,trainloader=trainloader,testloader=trainloader,loss_func=loss_func,zs=zs,beta=beta,record_weight=True,whole_weight=True,cbp_epoch=cbp_epoch,max_epoch=max_epoch,cbp_lr=lr)5. plot the trajectories of the weights in CBP (first 2000 epochs)cbp.plot_xor_weight(cbp_w_list[:2000], suptitle='CBP')Weights of CBP6. compare the loss and accuracy of BP and CBPimportnumpyasnploss_mat=np.array([bp_l_list,cbp_l_list]).Tacc_mat=np.array([bp_a_list,cbp_a_list]).Tcbp.plot_mul_loss_acc(loss_mat,acc_mat,alpha=1,ylabels=['loss','acc'])Example3. choose the parameter z (initial chaotic intensity)cbp.set_random_seed(seed)model=cbp.MLPS(layer_list,init_mode=init_mode,act_layer=torch.nn.Sigmoid(),active_last=True)ws_lists=cbp.debug_chaos(model,trainloader,loss_func=loss_func)le_list=cbp.cal_lyapunov_exponent(ws_lists)cbp.plot_lyapunov_exponent_with_z(le_list)In this example, the Lyapunov exponent around interval [8, 11] is positive, which indicates chaotic dynamics.Then, z = 12 was chosen as the initial chaotic intensity.Reproduce the results in the paperTo reproduce the results in the paper, check the notebook files inpaper_example.Componentschaos_optim.py: implement the CBP algorithm in the form of optimizer.net.py: contains the neural network class, currently only support MLP.train.py: provide APIs to preform training with the Pytorch style.dataset.py: provide APIs to create the trainloader and testloader.utils.py: several auxiliary functions to analysis the training results.plot.py: several functions to show the training results.LicenseMIT License