Skip to content

Instantly share code, notes, and snippets.

@CaptainAshis
Created October 17, 2018 17:23
Show Gist options
  • Save CaptainAshis/0742da22b6ed22535731396015fbd5fd to your computer and use it in GitHub Desktop.
Save CaptainAshis/0742da22b6ed22535731396015fbd5fd to your computer and use it in GitHub Desktop.
pytorch
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 283,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import torch\n",
"import numpy as np\n",
"from torch.autograd import Variable as V\n",
"import torch.nn as nn #Helps in building our linear regression model"
]
},
{
"cell_type": "code",
"execution_count": 303,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x=[j for j in range(12)]"
]
},
{
"cell_type": "code",
"execution_count": 304,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(12,)"
]
},
"execution_count": 304,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=np.array(x,dtype=np.float32)\n",
"x.shape"
]
},
{
"cell_type": "code",
"execution_count": 305,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x=x.reshape(-1,1)"
]
},
{
"cell_type": "code",
"execution_count": 306,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(12, 1)"
]
},
"execution_count": 306,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.shape"
]
},
{
"cell_type": "code",
"execution_count": 308,
"metadata": {},
"outputs": [],
"source": [
"y=[3*j+1 for j in x]\n",
"y=np.array(y,dtype=np.float32)"
]
},
{
"cell_type": "code",
"execution_count": 309,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"y=y.reshape(-1,1)"
]
},
{
"cell_type": "code",
"execution_count": 310,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(12, 1)"
]
},
"execution_count": 310,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y.shape"
]
},
{
"cell_type": "code",
"execution_count": 311,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import torch.nn as nn #Helps in building our linear regression model"
]
},
{
"cell_type": "code",
"execution_count": 312,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class LinearRegression(nn.Module):\n",
" def __init__(self,inp_dim,out_dim):\n",
" super(LinearRegression,self).__init__()\n",
" self.Linear=nn.Linear(inp_dim,out_dim)\n",
" \n",
" def forward(self,x):\n",
" out=self.Linear(x)\n",
" return out\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 313,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"inp_dim=1;out_dim=1\n",
"model=LinearRegression(inp_dim,out_dim)"
]
},
{
"cell_type": "code",
"execution_count": 314,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"LinearRegression(\n",
" (Linear): Linear(in_features=1, out_features=1, bias=True)\n",
")"
]
},
"execution_count": 314,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model"
]
},
{
"cell_type": "code",
"execution_count": 315,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"loss=nn.MSELoss()"
]
},
{
"cell_type": "code",
"execution_count": 316,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"lr_rate=.01\n",
"optim_select=torch.optim.SGD(model.parameters(),lr=lr)"
]
},
{
"cell_type": "code",
"execution_count": 317,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch 1,loss 630.62109375\n",
"epoch 2,loss 12.809333801269531\n",
"epoch 3,loss 0.3119032382965088\n",
"epoch 4,loss 0.05852465704083443\n",
"epoch 5,loss 0.05282098054885864\n",
"epoch 6,loss 0.05213315784931183\n",
"epoch 7,loss 0.051553260535001755\n",
"epoch 8,loss 0.05098196491599083\n",
"epoch 9,loss 0.05041683837771416\n",
"epoch 10,loss 0.049858029931783676\n",
"epoch 11,loss 0.04930543899536133\n",
"epoch 12,loss 0.048758912831544876\n",
"epoch 13,loss 0.04821852967143059\n",
"epoch 14,loss 0.04768409952521324\n",
"epoch 15,loss 0.047155771404504776\n",
"epoch 16,loss 0.04663321375846863\n",
"epoch 17,loss 0.0461161769926548\n",
"epoch 18,loss 0.04560507833957672\n",
"epoch 19,loss 0.04509952291846275\n",
"epoch 20,loss 0.04459987208247185\n",
"epoch 21,loss 0.044105496257543564\n",
"epoch 22,loss 0.04361655190587044\n",
"epoch 23,loss 0.043133217841386795\n",
"epoch 24,loss 0.04265508055686951\n",
"epoch 25,loss 0.042182400822639465\n",
"epoch 26,loss 0.04171481728553772\n",
"epoch 27,loss 0.04125262051820755\n",
"epoch 28,loss 0.04079533368349075\n",
"epoch 29,loss 0.040343400090932846\n",
"epoch 30,loss 0.03989606723189354\n",
"epoch 31,loss 0.03945384919643402\n",
"epoch 32,loss 0.03901654854416847\n",
"epoch 33,loss 0.03858422860503197\n",
"epoch 34,loss 0.038156576454639435\n",
"epoch 35,loss 0.03773362934589386\n",
"epoch 36,loss 0.03731551393866539\n",
"epoch 37,loss 0.03690206632018089\n",
"epoch 38,loss 0.036492884159088135\n",
"epoch 39,loss 0.036088522523641586\n",
"epoch 40,loss 0.03568856790661812\n",
"epoch 41,loss 0.03529297187924385\n",
"epoch 42,loss 0.03490177169442177\n",
"epoch 43,loss 0.03451492637395859\n",
"epoch 44,loss 0.034132298082113266\n",
"epoch 45,loss 0.033754151314496994\n",
"epoch 46,loss 0.033380020409822464\n",
"epoch 47,loss 0.033010125160217285\n",
"epoch 48,loss 0.03264430910348892\n",
"epoch 49,loss 0.03228248655796051\n",
"epoch 50,loss 0.03192466124892235\n",
"epoch 51,loss 0.03157084435224533\n",
"epoch 52,loss 0.031220832839608192\n",
"epoch 53,loss 0.03087496943771839\n",
"epoch 54,loss 0.030532704666256905\n",
"epoch 55,loss 0.030194342136383057\n",
"epoch 56,loss 0.029859721660614014\n",
"epoch 57,loss 0.029528720304369926\n",
"epoch 58,loss 0.029201483353972435\n",
"epoch 59,loss 0.028877777978777885\n",
"epoch 60,loss 0.028557755053043365\n",
"epoch 61,loss 0.028241204097867012\n",
"epoch 62,loss 0.027928218245506287\n",
"epoch 63,loss 0.02761879563331604\n",
"epoch 64,loss 0.027312589809298515\n",
"epoch 65,loss 0.02700994163751602\n",
"epoch 66,loss 0.026710577309131622\n",
"epoch 67,loss 0.02641456387937069\n",
"epoch 68,loss 0.02612174116075039\n",
"epoch 69,loss 0.025832245126366615\n",
"epoch 70,loss 0.025545990094542503\n",
"epoch 71,loss 0.025262735784053802\n",
"epoch 72,loss 0.024982759729027748\n",
"epoch 73,loss 0.024705937132239342\n",
"epoch 74,loss 0.02443218231201172\n",
"epoch 75,loss 0.024161361157894135\n",
"epoch 76,loss 0.023893550038337708\n",
"epoch 77,loss 0.02362876571714878\n",
"epoch 78,loss 0.023366853594779968\n",
"epoch 79,loss 0.023107921704649925\n",
"epoch 80,loss 0.02285178005695343\n",
"epoch 81,loss 0.022598475217819214\n",
"epoch 82,loss 0.0223480686545372\n",
"epoch 83,loss 0.022100435569882393\n",
"epoch 84,loss 0.021855412051081657\n",
"epoch 85,loss 0.02161320298910141\n",
"epoch 86,loss 0.021373601630330086\n",
"epoch 87,loss 0.021136755123734474\n",
"epoch 88,loss 0.02090253122150898\n",
"epoch 89,loss 0.020670810714364052\n",
"epoch 90,loss 0.020441772416234016\n",
"epoch 91,loss 0.020215211436152458\n",
"epoch 92,loss 0.019991246983408928\n",
"epoch 93,loss 0.01976955123245716\n",
"epoch 94,loss 0.01955045945942402\n",
"epoch 95,loss 0.019333699718117714\n",
"epoch 96,loss 0.019119422882795334\n",
"epoch 97,loss 0.018907597288489342\n",
"epoch 98,loss 0.018698012456297874\n",
"epoch 99,loss 0.018490862101316452\n",
"epoch 100,loss 0.01828586682677269\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\ashis.panda\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\ipykernel_launcher.py:20: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number\n"
]
}
],
"source": [
"no_of_epoch=100\n",
"for epo in range(no_of_epoch):\n",
" epo=epo+1\n",
"# Convert into PyTorch suitable datatype i.e Tensors\n",
" inputs=V(torch.from_numpy(x))\n",
" labels=V(torch.from_numpy(y))\n",
" \n",
"# y=a*x+b . The forward function is being called here\n",
" outputs=model(inputs)\n",
" \n",
"# Calculate the Loss.\n",
" loss_calc=loss(outputs,labels)\n",
" \n",
"# Calculate gradients during backpropagation\n",
" loss_calc.backward()\n",
" \n",
"# Update parameters\n",
" optim_select.step()\n",
" \n",
" print('epoch {},loss {}'.format(epo,loss_calc.data[0]))\n",
" \n",
"# Remove the gradients wrt every parameter in each epoch so as not to # accumulate the gradients\n",
" optim_select.zero_grad()"
]
},
{
"cell_type": "code",
"execution_count": 318,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"out_pred=model(V(torch.from_numpy(x))).data.numpy()"
]
},
{
"cell_type": "code",
"execution_count": 319,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1.2529418],\n",
" [ 4.2197304],\n",
" [ 7.1865196],\n",
" [10.153308 ],\n",
" [13.120097 ],\n",
" [16.086885 ],\n",
" [19.053673 ],\n",
" [22.020462 ],\n",
" [24.987251 ],\n",
" [27.95404 ],\n",
" [30.92083 ],\n",
" [33.88762 ]], dtype=float32)"
]
},
"execution_count": 319,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"out_pred"
]
},
{
"cell_type": "code",
"execution_count": 320,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1.],\n",
" [ 4.],\n",
" [ 7.],\n",
" [10.],\n",
" [13.],\n",
" [16.],\n",
" [19.],\n",
" [22.],\n",
" [25.],\n",
" [28.],\n",
" [31.],\n",
" [34.]], dtype=float32)"
]
},
"execution_count": 320,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y"
]
},
{
"cell_type": "code",
"execution_count": 301,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# We can see predicted values are near about close to actual values"
]
},
{
"cell_type": "code",
"execution_count": 302,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"torch.save(model.state_dict(),'MODEL_1.pkl')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment