Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions notebooks/01_basics.ipynb

Large diffs are not rendered by default.

111 changes: 111 additions & 0 deletions notebooks/02_manual_gradient.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"x_data = [1.0, 2.0, 3.0]\n",
"y_data = [2.0, 4.0, 6.0]\n",
"\n",
"w = 1.0 # a random guess: random value"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# our model forward pass\n",
"\n",
"def forward(x):\n",
" return x * w\n",
"\n",
"# Loss function\n",
"def loss(x, y):\n",
" y_pred = forward(x)\n",
" return (y_pred - y)**2\n",
"\n",
"# compute gradient\n",
"def gradient(x, y): # d_loss/d_w\n",
" return 2 * x * (x * w - y)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"predict (before training) 4 , 4.0\n"
]
}
],
"source": [
"# Before training\n",
"print(\"predict (before training)\", 4, ',',forward(4))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"predict (after training) 4 , 7.804863933862125\n"
]
}
],
"source": [
"# Training loop\n",
"for epoch in range(10):\n",
" for x_val, y_val in zip(x_data, y_data):\n",
" grad = gradient(x_val, y_val)\n",
" w = w - 0.01 * grad\n",
" #print(\"\\tgrad: \", x_val, y_val, round(grad, 2))\n",
" l = loss(x_val, y_val)\n",
"\n",
" #print(\"progress:\", epoch, \"w=\", round(w, 2), \"loss=\", round(l, 2))\n",
"\n",
"# After training\n",
"print(\"predict (after training)\", \"4 ,\", forward(4))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
149 changes: 149 additions & 0 deletions notebooks/03_auto_gradient.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.4.1\n"
]
}
],
"source": [
"import torch\n",
"import torchvision\n",
"from torch.autograd import Variable\n",
"\n",
"print(torch.__version__)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"x_data = [1.0, 2.0, 3.0]\n",
"y_data = [2.0, 4.0, 6.0]\n",
"\n",
"w = Variable(torch.Tensor([1.0]), requires_grad=True) # Any random value"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"predict (before training) : 4 , tensor(4.)\n"
]
}
],
"source": [
"# our model forward pass\n",
"\n",
"def forward(x):\n",
" return x * w\n",
"\n",
"# Loss function\n",
"\n",
"def loss(x, y):\n",
" y_pred = forward(x)\n",
" return (y_pred - y) ** 2\n",
"\n",
"# Before training\n",
"print(\"predict (before training) : \", 4,\",\", forward(4).data[0])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"progress: 0 tensor(7.3159)\n",
"progress: 1 tensor(3.9988)\n",
"progress: 2 tensor(2.1857)\n",
"progress: 3 tensor(1.1946)\n",
"progress: 4 tensor(0.6530)\n",
"progress: 5 tensor(0.3569)\n",
"progress: 6 tensor(0.1951)\n",
"progress: 7 tensor(0.1066)\n",
"progress: 8 tensor(0.0583)\n",
"progress: 9 tensor(0.0319)\n"
]
}
],
"source": [
"# Training loop\n",
"for epoch in range(10):\n",
" for x_val, y_val in zip(x_data, y_data):\n",
" l = loss(x_val, y_val)\n",
" l.backward()\n",
" #print(\"\\tgrad: \", x_val, y_val, w.grad.data[0])\n",
" w.data = w.data - 0.01 * w.grad.data\n",
"\n",
" # Manually zero the gradients after updating weights\n",
" w.grad.data.zero_()\n",
"\n",
" print(\"progress:\", epoch, l.data[0])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"predict (after training) 4 , tensor(7.8049)\n"
]
}
],
"source": [
"# After training\n",
"print(\"predict (after training)\", 4, \",\",forward(4).data[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading