Skip to content

Instantly share code, notes, and snippets.

@lbollar
Created March 28, 2017 04:52
Show Gist options
  • Save lbollar/f7bf60762817a6991038f4323c28f5aa to your computer and use it in GitHub Desktop.
Save lbollar/f7bf60762817a6991038f4323c28f5aa to your computer and use it in GitHub Desktop.
Linear regression example using Pytorch.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import torch\n",
"from torch.autograd import Variable\n",
"\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 202.09515380859375\n",
"10 171.15576171875\n",
"20 144.06959533691406\n",
"30 120.9667739868164\n",
"40 101.63298034667969\n",
"50 85.61366271972656\n",
"60 72.35327911376953\n",
"70 61.30906295776367\n",
"80 52.017799377441406\n",
"90 44.118812561035156\n",
"100 37.34712219238281\n",
"110 31.512853622436523\n",
"120 26.4785099029541\n",
"130 22.140188217163086\n",
"140 18.414417266845703\n",
"150 15.23002815246582\n",
"160 12.523662567138672\n",
"170 10.237588882446289\n",
"180 8.318814277648926\n",
"190 6.718794822692871\n",
"200 5.393377304077148\n",
"210 4.302750110626221\n",
"220 3.411351442337036\n",
"230 2.6877212524414062\n",
"240 2.1042821407318115\n",
"250 1.6370915174484253\n",
"260 1.2655516862869263\n",
"270 0.9721100926399231\n",
"280 0.7419456839561462\n",
"290 0.5626537799835205\n",
"300 0.42395275831222534\n",
"310 0.31739136576652527\n",
"320 0.23608431220054626\n",
"330 0.17447291314601898\n",
"340 0.12810657918453217\n",
"350 0.0934523195028305\n",
"360 0.06772942841053009\n",
"370 0.04876668006181717\n",
"380 0.03488374873995781\n",
"390 0.024789126589894295\n",
"400 0.017499692738056183\n",
"410 0.01227207574993372\n",
"420 0.008548851124942303\n",
"430 0.0059155626222491264\n",
"440 0.0040659294463694096\n",
"450 0.0027757221832871437\n",
"460 0.001882058335468173\n",
"470 0.0012674160534515977\n",
"480 0.00084764912026003\n",
"490 0.0005629826919175684\n"
]
}
],
"source": [
"torch.manual_seed(42)\n",
"np.random.seed(42)\n",
"\n",
"N = 64\n",
"\n",
"alpha = 1.3\n",
"beta = np.array([[0.5], [1.9]])\n",
"\n",
"x_data = np.random.randn(N, 2)\n",
"y_data = x_data.dot(beta) + alpha\n",
"\n",
"x = Variable(torch.Tensor(x_data), requires_grad=False)\n",
"y = Variable(torch.Tensor(y_data), requires_grad=False)\n",
"\n",
"w_beta = Variable(torch.randn(2, 1), requires_grad=True)\n",
"w_alpha = Variable(torch.randn(1), requires_grad=True)\n",
"\n",
"learning_rate = 1e-2\n",
"optimizer = torch.optim.Adam([w_beta, w_alpha], lr=learning_rate)\n",
"\n",
"for t in range(500):\n",
" y_pred = x.mm(w_beta).add(w_alpha.expand(N))\n",
" \n",
" loss = (y_pred - y).pow(2).sum()\n",
" \n",
" if t % 10 == 0:\n",
" print(t, loss.data[0])\n",
" \n",
" optimizer.zero_grad() \n",
" \n",
" loss.backward()\n",
" \n",
" optimizer.step()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Optimized Beta: \n",
" 0.4973\n",
" 1.9002\n",
"[torch.FloatTensor of size 2x1]\n",
"\n",
"Optimized Alpha: \n",
" 1.2997\n",
"[torch.FloatTensor of size 1]\n",
"\n"
]
}
],
"source": [
"print(\"Optimized Beta: {0}\".format(w_beta.data))\n",
"print(\"Optimized Alpha: {0}\".format(w_alpha.data))"
]
}
],
"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.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment