Published
Edited
Aug 2, 2018
15 stars
Insert cell
Insert cell
Insert cell
Insert cell
Algebra(0,0,1,()=>{
var f = x=>3*x*x + 6;
return f(2+1e0)
})
Insert cell
Insert cell
Algebra({
basis : ['1','e1','e2'],
Cayley: [["1","e1","e2"],
["e1","0","0"],
["e2","0","0"]]},
()=>{
var f = (x,y)=>3*x*x - 2*y*y*y + 6;
return f(5+1e1,2+1e2)
}
)
Insert cell
Insert cell
Insert cell
A = {
var nrDuals = 12;
var basis = [...Array(nrDuals+1)].map((x,i)=>i?"e"+i:"1");
var Cayley = basis.map(x=>basis.map(y=>x=='1'?y:y=='1'?x:"0"));
var A = Algebra({basis,Cayley});
A.inline(function(A){
A.describe();
A.inv = x=>x.map((c,i)=>i?-c/(x.s**2):1/c); // inverse function.
})(A);
return A;
}
Insert cell
Insert cell
setup = A.inline`{
sample : [0.05, 0.1],
expected : [0.01, 0.99],
WH : [[.15+1e1,.20+1e2,.35+1e3],[.25+1e4,.30+1e5,.35+1e6]],
WO : [[.40+1e7,.45+1e8,.60+1e9],[.50+1e10,.55+1e11,.60+1e12]]
}`
Insert cell
Insert cell
sigmoid = A.sigmoid = A.inline(x=>x.map(x=>this.inv(1+this.exp(-x))))
Insert cell
Insert cell
forward = A.forward = A.inline((sample,WO,WH)=>this.sigmoid(WO*[...this.sigmoid(WH*[...sample,1]),1]))
Insert cell
Insert cell
error = A.error = A.inline((sample,output)=>.5*(output-sample)**2)
Insert cell
Insert cell
forward(setup.sample,setup.WO,setup.WH).map(x=>x.s)
Insert cell
Insert cell
error(forward(setup.sample,setup.WO,setup.WH),setup.expected).s
Insert cell
Insert cell
optimize = A.inline((sample,WO,WH,expected)=>{
for (var k=0;k<1000;k++) {
var update = -0.5*this.error(this.forward(sample,WO,WH), expected); // forward evaluation and error calc
WH = WH+[[...update.slice(1,4)],[...update.slice(4,7)]]; // backpropagate hidden layer
WO = WO+[[...update.slice(7,10)],[...update.slice(10,13)]]; // backpropagate output layer
}
return {WO,WH}
})
Insert cell
Insert cell
optimal = optimize(setup.sample,setup.WO,setup.WH,setup.expected)
Insert cell
Insert cell
forward(setup.sample,optimal.WO,optimal.WH).map(x=>x.s)
Insert cell
Insert cell
error(forward(setup.sample,optimal.WO,optimal.WH),setup.expected).s
Insert cell
Insert cell
NN_AND = A.inline(()=>{
// The initial weights, input and desired output of our network.
var input = [[.35,.20],[0.7,0.55],[0.17,0.8],[0.9,0.3]], // input samples.
WH = [[.15+1e1,.20+1e2,.35+1e3],[.25+1e4,.30+1e5,.35+1e6]], // hidden layer weights and bias
WO = [[.40+1e7,.45+1e8,.60+1e9],[.50+1e10,.55+1e11,.60+1e12]], // output layer weights and bias
expected = [[0.01,0.99],[0.99,0.01],[0.01,0.99],[0.01,0.99]]; // desired outputs.

// The activation function, forward evaluator and error function.
var logistic = x=>this.inv(1+this.exp(-x)),
forward=(sample)=>(WO*[...(WH*[...sample,1]).map(logistic),1]).map(logistic),
error = (sample,expected)=>.5*(expected-sample)**2;

// Now train the network.
for (var k=0;k<500;k++) {
var update=input.reduce((l,c,i)=>l-5*error(forward(c), expected[i]),0); // forward eval
WH = WH+[[...update.slice(1,4)],[...update.slice(4,7)]]; // backpropagate hidden layer
WO = WO+[[...update.slice(7,10)],[...update.slice(10,13)]]; // backpropagate output layer
}
// Return some info (only WO and WH really needed)
return { forward:forward,input,WO,WH,out:input.map(i=>forward(i).map(x=>x.s.toFixed(2)*1)),
error:input.reduce((a,b,i)=>a+error(forward(b),expected[i]).s,0),expected}
})()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more