Platform
Resources
Pricing
Sign in
Get started
Philippe Rigovanov
math & informatics teacher
Workspace
Fork
Published
By
Philippe Rigovanov
Edited
Jan 9, 2020
Fork of
Neural network from scratch
•
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.
Try it for free
Learn more
Compare fork
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
testTrain
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
trainedWeights
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
train
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
guess
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
randomWeights
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
team
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Y_MAX
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
X_MAX
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
randomPoints
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
generatePoints
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
sleep
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
rand
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
R
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML