Published
Edited
Jan 9, 2021
Importers
Insert cell
Insert cell
Insert cell
class DenseNeuronNetwork {
constructor(opt) {
this.layers = opt?.layers
this.func = opt?.func
let initFn = opt?.initParamsFn || (() => Math.random() - 0.5)
this.params = opt?.params
|| _.drop(this.layers, 1).map((n, i) => new Array((this.layers[i] + 1) * n).fill(0).map(initFn))
}
setParams(p, autoChunk) {
if (!autoChunk) {
this.params = p
return
}
let acc = 0
this.params = _.drop(this.layers, 1)
.map((l,i) => {
let readLen = l * (this.layers[i] + 1)
let from = acc
let to = acc + readLen
acc += readLen
return p.slice(from, to)
})
}
getParams(flat) {
return flat
? _.flatten(this.params)
: this.params
}
metricMul(mData, mulTo) {
let colCount = mulTo.length
let rowCount = mData.length / colCount
if (!_.isInteger(_.round(rowCount, 4))) {
throw new Error(`can not mul: ${mData} * ${mulTo}`)
}
return _.chunk(mData, colCount).map(row => row.reduce((acc, m, i) => acc + m * mulTo[i], 0))
}
sigmoid(arr) {
let {pow, E} = Math
return arr.map(v => 1 / (1 + pow(E, -v)))
}
tanh(arr) {
let {tanh} = Math
return arr.map(v => tanh(v))
}
relu(arr) {
let {max} = Math
return arr.map(v => max(0, v))
}
calc(inputs) {
return this.params.reduce((acc, curr, i) => {
let a = this.metricMul(curr, [1, ...acc])
let fnName = this.func[i]
return this[fnName](a)
}, inputs)
}
cost(inputs, targets) {
let prediction = this.calc(inputs)
let square = v => v * v
return prediction.reduce((acc, v, i) => acc + square(targets[i] - v), 0)
}
calcR2() {
return this.getParams(1).reduce((acc,v)=>acc+v*v,0)
}
}
Insert cell
network = new DenseNeuronNetwork({
layers: [3, 2, 4],
func: ['relu', 'tanh']
})
Insert cell
network.calc([-1, 0.5, 10])
Insert cell
network.calcR2()
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