Published
Edited
Aug 7, 2020
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mlp = new MLP(3, [4, 1])
Insert cell
Insert cell
epochs = 5000
Insert cell
lr = 5e-1
Insert cell
regularization = true
Insert cell
alpha = 1e-2
Insert cell
Insert cell
Insert cell
Insert cell
res = {
let losses = []
for(let e=0; e<epochs; e++) {
let loss = 0;
let diff = 0;
let regularization = 0;
mlp.zeroGrad();
for(let i=0; i<housePricesNorm.length; i++) {
let sample = housePricesNorm[i];
let input = [new Value(parseFloat(sample.LotArea)),
new Value(parseFloat(sample.OverallQual)),
new Value(parseFloat(sample.SalePrice))];
let target = new Value(parseFloat(sample.SalePrice));
let score = mlp.calc(input)[0];
let mse = pow(sub(score, target), 2);
let reg = mul(new Value(alpha), mlp.parameters().reduce((acc, current) => add(acc, pow(current, 2)), new Value(0)));
let result = div(add(mse, reg), new Value(housePricesNorm.length));
result.backward()
diff += mse.data;
regularization += reg.data;
loss += result.data
}
losses.push({epoch: e, loss: loss})
mlp.parameters().forEach(p => {p.data -= lr * p.grad});
yield {
log: losses,
network: mlp
};

}
}
Insert cell
Insert cell
width = 600
Insert cell
height = 400
Insert cell
thickness = 20
Insert cell
import {MLP, mlp2graph} from '@grjzwaan/building-neural-networks-from-scratch'
Insert cell
import {Value, add, mul, pow, relu, sub, div, graph, sum} from '@grjzwaan/building-autodiff-from-scratch'
Insert cell
Insert cell
Insert cell
Insert cell
dot = require("@observablehq/graphviz@0.2")
Insert cell
d3 = require("d3@5")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more