Published
Edited
Jan 9, 2020
3 forks
2 stars
Insert cell
Insert cell
Insert cell
testTrain = {
const point = { x: 200, y: 400 } // -1
return train(randomWeights, point, team(point))
}
Insert cell
trainedWeights = {
const examples = generatePoints(1000000).map(point => ({
point,
team: team(point)
}))

let currentWeights = randomWeights
for (const example of examples) {
currentWeights = train(currentWeights, example.point, example.team)
//await sleep(25)
//yield currentWeights
}
return currentWeights
}
Insert cell
function train(weights, point, team) {
const guessResult = guess(weights, point) // 1
const error = team - guessResult
const learningRate = 0.1
return {
x: weights.x + point.x * error * learningRate,
y: weights.y + point.y * error * learningRate,
}
}
Insert cell
guess = (weights, point) => {
const sum =
point.x * weights.x +
point.y * weights.y
const team = sum >= 0 ? 1 : -1
return team
}
Insert cell
randomWeights = ({
x: rand(-1, 1),
y: rand(-1, 1)
})
Insert cell
team = point => point.x > point.y ? 1 : -1
Insert cell
Insert cell
Insert cell
randomPoints = generatePoints(200)
Insert cell
generatePoints = (num) => R.range(0, num).map(_ => ({
x: rand(0, X_MAX),
y: rand(0, Y_MAX)
}))
Insert cell
sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
Insert cell
rand = (high, low) => Math.random() * (high - low) + low
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