Package: bacon-net
Bacon-NetBacon-Netis a neural network architecture for building fully explainable neural network for arithmetic and gradient logic expression approximation. A Bacon-Net network can be used to discover an arithmetical or a logical expression that approximates the given dataset. And the result network is precisely explainable.This repository contains a family of 2-variable Bacon-Net implementations. Multiple Bacon-Net can be used together to expand the search space; And Bacon-Net can be stacked into aBacon-Stackthat handles arbitrary number of variables.The following table presents a list of famous formulas in different fields that are re-discovered using Bacon-Net using synthetic training data. All networks in this repository are implemented usingKerasand Python.Bacon-Poly2Bacon-Poly2can be used to discover 1-variable or 2-variable quadratic polynomials or linear polynomials. The following table lists some samples of Bacon-Poly2 re-discovering some of the well-known geometric formulas and physics formulas.NOTE: Coefficients and constant terms will vary a little in different runs.FormulaExpressionBacon-Poly2 ExplanationArea of a circlez = 3.1416x^2Area of a ellipsez = 3.1416xyNewton's equation of motion (displacement)z = 4.905x^2 + 5.0x(initial speed = 5, acceleration=9.81)An arbitary 2-degree 2-variable polynomial expressionz = 1.0x^2 + 4.0xy + 4.0y^2 + 5.0Einstein's mass-energy equivalencez = 0.0899x(with c normalized to 0.299792458 m/s)Simple intergalz = 0.5x^2 + <C>(C is determined by training data)Bacon-LSP3Bacon-LSP3evaluates four possible gradient logic relationships between two variables in spaceI = [0, 1]: full conjunction, full disjunction, product t-norm (medium hyperconjunction) and neutrality.Bacon-LSP3 can be used to reason the logic behind some simple decisions, like “a face image needs to show 2 eye features AND a mouth feature”RelationshipPlotBacon-LSP3 ExplanationFull conjunctionmin(A, B)Product t-normA * BNeutrality(A + B) / 2Full disjunctionmax(A, B)Bacon-StackBacon-Stack expands on Bacon-Net and allows arbitrary number of variables. SeeBack-Stack Architecturefor more details on Bacon-Stack design.InstallationRun the following command to install:pipinstallbacon-netSample Usagefrombacon.netimportdataCreatorfrombacon.nets.poly2importpoly2# create a network from the bacon-net network familynet=poly2()# optionally, use dataCreator to generate training data# set params to 1 if only a single variable (a) is usedx,y=dataCreator.create(1000,1,lambdax:math.pi*x[0]*x[0],params=1)# train the networknet.fit(x[0],x[1],y)# explain the networkm=net.explain(singleVariable=True)print("f(x) = "+m.string(4))# make prediction (pass two parameters if two variables are used)p=net.predict(2.4)# make predictions on array (pass two arrays if two variables are used)p=net.predict([1.0,2.3,4.3])Developing Bacon-NetTo install Bacon-Net, along with the tools you need to develop and run tests, run the following command:pipinstall-e.[dev]To run all test cases, run the following command from the project's root folder:pytestPlease seeherefor instructions on creating a new Bacon-Net network.Bacon-Net ArchitectureThe idea behind Bacon-Net is simple: to construct a network that can do linear interpolation among a group of selected terms likemin(x,y)andsin(x^2), as shown in the following diagram:Input layercontains two variables. For gradient logic expressions.Expansion layerdefines the search space. Each node in this layer represents a candidate expression for the final approximation. Obviously, it’s desirable to have minimum overlaps among the function curves.Interpolation layercreates a linear interpolation of candidate terms from the expansion layer by adjusting weights associated with candidates.Aggregation layercalculate the interpolation result, which is compared to the training data.It's also to feed the inputs to a family of Bacon-Net networks to search multiple expression spaces in parallel. A 1-activeSelection layeris added on top to select the appropriate Bacon-Net in this case, as shown in the following diagram:Bacon-Stack ArchitectureA Bacon-Stack is recursively defined: a Bacon-Stack that handlesnvariables (denoted asB(n)) is constructed by feeding variablex(i)and the result of aB(n-1)into aB(2)network, which is a Bacon-Net, as shown in the following diagram:The following diagram illustrates how a 5-variable Bacon-Stack is implemented using Keras custom layers and multi-input features.Bacon-Stack doesn't assume variables to be commutative. To explore permutation of variable orders, aPermutation layeris added at the bottom of the Bacon-Stack, as shown in the following diagram:NOTE: Before the permutation layer is implemented, Bacon-Stack is constrained to handle expressions that can be rewrite as binary trees with ordered parameters. For example, the network will have difficulties to understand(a+b)*c+d*e, but will be fine with(a+b)*c+d+e.Why the name "BACON"?When I was in high school in encountered with a BASIC program that used a brute-force method to discover a arithmetical expression to approximate a given dataset. I remembered the program was called “BACON”. However, it’s been unfruitful to find such references in Internet, so my memory may have failed me. Regardless, I’ve been wanting to recreate “BACON” all these years, and I finally got around to do it just now during my week off.As I research into explainable AI, I see an opportunity to combine “BACON” with AI so that we can build some precisely explainable AI networks, plus the benefit of implementing a parallelable, GPU-accelerated BACON using modern technologies.Upcoming Bacon-Net networksBacon-Poly3For degree 3 polynomial expressionsBacon-Trig2For degree 2 trigonometic functionsBacon-LSP6Explainable gradient logic network for decision makingBacon-CNNExplainability layer on top of a CNN networkBacon-H1A combination of selected Bacon-Net networksBacon-Cal1A simple calculus solverContact authorTwitter:@HaishiBai2010LinkedIn:Haishi Bai