Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
HSLU deep learning
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jeanette Lee
HSLU deep learning
Commits
d4c05a9a
Commit
d4c05a9a
authored
3 years ago
by
Jeanette Lee
Browse files
Options
Downloads
Patches
Plain Diff
new code
parent
5504216b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#334066
passed
3 years ago
Stage: build
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
notebooks/Block_1/NN from scratch.ipynb
+58
-0
58 additions, 0 deletions
notebooks/Block_1/NN from scratch.ipynb
with
58 additions
and
0 deletions
notebooks/Block_1/NN from scratch.ipynb
0 → 100644
+
58
−
0
View file @
d4c05a9a
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "bd546fce-edaa-4c2b-9de2-2d4b290d7efe",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from numpy.random import randn\n",
"\n",
"# define the network\n",
"N, D_in, H, D_out = 64, 1000, 100, 10\n",
"x, y = randn(N, D_in), randn(N, D_out)\n",
"w1, w2 = randn(D_in, H), randn(H, D_out)\n",
"\n",
"for t in range(2000):\n",
" # forward pass\n",
" h = 1 / (1 + np.exp(-x.dot(w1))) # sigmoid activation function\n",
" y_pred = h.dot(w2)\n",
" loss = np.square(y_pred - y).sum()\n",
" print(t, loss)\n",
" \n",
" # calculate analytical gradients\n",
" grad_y_pred = 2.0 * (y_pred - y)\n",
" grad_w2 = h.T.dot(grad_y_pred)\n",
" grad_h = grad_y_pred.dot(w2.T)\n",
" grad_w1 = x.T.dot(grad_h * h * (1 - h))\n",
" \n",
" # update parameters of gradient descent\n",
" w1 -= 1e-4 * grad_w1\n",
" w2 -= 1e-4 * grad_w2 "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
%% Cell type:code id:bd546fce-edaa-4c2b-9de2-2d4b290d7efe tags:
```
python
import
numpy
as
np
from
numpy.random
import
randn
# define the network
N
,
D_in
,
H
,
D_out
=
64
,
1000
,
100
,
10
x
,
y
=
randn
(
N
,
D_in
),
randn
(
N
,
D_out
)
w1
,
w2
=
randn
(
D_in
,
H
),
randn
(
H
,
D_out
)
for
t
in
range
(
2000
):
# forward pass
h
=
1
/
(
1
+
np
.
exp
(
-
x
.
dot
(
w1
)))
# sigmoid activation function
y_pred
=
h
.
dot
(
w2
)
loss
=
np
.
square
(
y_pred
-
y
).
sum
()
print
(
t
,
loss
)
# calculate analytical gradients
grad_y_pred
=
2.0
*
(
y_pred
-
y
)
grad_w2
=
h
.
T
.
dot
(
grad_y_pred
)
grad_h
=
grad_y_pred
.
dot
(
w2
.
T
)
grad_w1
=
x
.
T
.
dot
(
grad_h
*
h
*
(
1
-
h
))
# update parameters of gradient descent
w1
-=
1e-4
*
grad_w1
w2
-=
1e-4
*
grad_w2
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment