Last active
July 11, 2017 10:40
-
-
Save shouya/87fd62cd9bc8ef9a3176a1b634b2acf6 to your computer and use it in GitHub Desktop.
plain numpy, torch.autograd and torch.nn implementation of a simple nn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import torch\n", | |
"from torch.autograd import Variable" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# N is batch size; D_in is input dimension;\n", | |
"# H is hidden dimension; D_out is output dimension.\n", | |
"N, D_in, H, D_out = 6, 3, 10, 1\n", | |
"\n", | |
"w_1 = Variable(torch.randn(D_in, H), requires_grad=True)\n", | |
"h_1 = Variable(torch.randn(1, H), requires_grad=True)\n", | |
"w_2 = Variable(torch.randn(H, D_out), requires_grad=True)\n", | |
"h_2 = Variable(torch.randn(1, D_out), requires_grad=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"learning_rate = 1e-6\n", | |
"# X: N x D_in\n", | |
"# y: N x D_out\n", | |
"def train(X, y):\n", | |
" global w_1, h_1, w_2, h_2, learning_rate\n", | |
" \n", | |
" X, y = Variable(torch.Tensor(X)), Variable(torch.Tensor(y))\n", | |
" \n", | |
" # forward\n", | |
" # we have to use `repeat` since\n", | |
" # torch doesn't support broadcasting\n", | |
" m = X.mm(w_1) + h_1.repeat(N, 1) # m: N x H\n", | |
" m_relu = m.clamp(min=0) # m_relu: N x H\n", | |
" p = m_relu.mm(w_2) + h_2.repeat(N, 1) # p: N x D_out\n", | |
" \n", | |
" # loss = Sum((y - p)^2)\n", | |
" loss = (p - y).pow(2).sum()\n", | |
" \n", | |
" loss.backward()\n", | |
" \n", | |
" w_1.data -= w_1.grad.data * learning_rate\n", | |
" h_1.data -= h_1.grad.data * learning_rate\n", | |
" w_2.data -= w_2.grad.data * learning_rate\n", | |
" h_2.data -= h_2.grad.data * learning_rate\n", | |
" \n", | |
" w_1.grad.data.zero_()\n", | |
" h_1.grad.data.zero_()\n", | |
" w_2.grad.data.zero_()\n", | |
" h_2.grad.data.zero_()\n", | |
" \n", | |
" return (p, loss)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0 106.6131820678711\n", | |
"1000 64.248291015625\n", | |
"2000 41.778751373291016\n", | |
"3000 28.251008987426758\n", | |
"4000 19.758804321289062\n", | |
"5000 14.260133743286133\n", | |
"6000 10.607431411743164\n", | |
"7000 8.12516975402832\n", | |
"8000 6.402351379394531\n", | |
"9000 5.181938171386719\n", | |
"10000 4.300069808959961\n", | |
"11000 3.650324583053589\n", | |
"12000 3.162468671798706\n", | |
"13000 2.789226531982422\n", | |
"14000 2.498776435852051\n", | |
"15000 2.2687385082244873\n", | |
"16000 2.0838499069213867\n", | |
"17000 1.9329262971878052\n", | |
"18000 1.8078796863555908\n", | |
"19000 1.7029905319213867\n", | |
"20000 1.6138317584991455\n", | |
"21000 1.5370678901672363\n", | |
"22000 1.470285415649414\n", | |
"23000 1.411571741104126\n", | |
"24000 1.3593971729278564\n", | |
"25000 1.3125015497207642\n", | |
"26000 1.2700220346450806\n", | |
"27000 1.2311971187591553\n", | |
"28000 1.1954683065414429\n", | |
"29000 1.1623502969741821\n", | |
"30000 1.131489872932434\n", | |
"31000 1.1025365591049194\n", | |
"32000 1.0751968622207642\n", | |
"33000 1.0493370294570923\n", | |
"34000 1.0245527029037476\n", | |
"35000 1.0009534358978271\n", | |
"36000 0.9783092141151428\n", | |
"37000 0.9566856026649475\n", | |
"38000 0.9378811120986938\n", | |
"39000 0.9202557802200317\n", | |
"40000 0.9032085537910461\n", | |
"41000 0.886813759803772\n", | |
"42000 0.8709046244621277\n", | |
"43000 0.8553593158721924\n", | |
"44000 0.8402742147445679\n", | |
"45000 0.8256545066833496\n", | |
"46000 0.8113911747932434\n", | |
"47000 0.7974827885627747\n", | |
"48000 0.7839452028274536\n", | |
"49000 0.7708430886268616\n" | |
] | |
} | |
], | |
"source": [ | |
"for i in range(50000):\n", | |
" data = [\n", | |
" (1, [1,2,3]),\n", | |
" (1, [1,2,4]),\n", | |
" (1, [2,3,4]),\n", | |
" (0, [1,4,2]),\n", | |
" (0, [2,4,3]),\n", | |
" (0, [5,3,1])\n", | |
" ]\n", | |
" res, loss = train([x[1] for x in data], [[x[0]] for x in data])\n", | |
" if i % 1000 == 0:\n", | |
" print(i, loss.data[0])" | |
] | |
} | |
], | |
"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.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://pytorch.org/tutorials/beginner/pytorch_with_examples.html.