{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"raw","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-12T07:21:26.70343Z","iopub.execute_input":"2022-09-12T07:21:26.704237Z","iopub.status.idle":"2022-09-12T07:21:26.710265Z","shell.execute_reply.started":"2022-09-12T07:21:26.704195Z","shell.execute_reply":"2022-09-12T07:21:26.7086Z"}}},{"cell_type":"code","source":"import torch\nimport torchvision\nfrom torch.utils.data import Dataset","metadata":{"execution":{"iopub.status.busy":"2022-10-18T08:40:16.955332Z","iopub.execute_input":"2022-10-18T08:40:16.956276Z","iopub.status.idle":"2022-10-18T08:40:19.245725Z","shell.execute_reply.started":"2022-10-18T08:40:16.956135Z","shell.execute_reply":"2022-10-18T08:40:19.243861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Gradient Descent","metadata":{}},{"cell_type":"code","source":"w = torch.tensor(-10.0,requires_grad = True)\nx = torch.arange(-3,3,0.1).view(-1,1)\nf = -3*x\n","metadata":{"execution":{"iopub.status.busy":"2022-10-12T11:40:10.372557Z","iopub.execute_input":"2022-10-12T11:40:10.373098Z","iopub.status.idle":"2022-10-12T11:40:10.382427Z","shell.execute_reply.started":"2022-10-12T11:40:10.373064Z","shell.execute_reply":"2022-10-12T11:40:10.381571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.plot(x.numpy(),f.numpy())\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-12T09:08:05.058066Z","iopub.execute_input":"2022-10-12T09:08:05.058439Z","iopub.status.idle":"2022-10-12T09:08:05.262614Z","shell.execute_reply.started":"2022-10-12T09:08:05.058408Z","shell.execute_reply":"2022-10-12T09:08:05.261797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = f+0.1*torch.randn(x.size())\nplt.plot(x.numpy(),y.numpy(),'ro')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-12T09:08:08.032954Z","iopub.execute_input":"2022-10-12T09:08:08.033747Z","iopub.status.idle":"2022-10-12T09:08:08.21181Z","shell.execute_reply.started":"2022-10-12T09:08:08.033708Z","shell.execute_reply":"2022-10-12T09:08:08.210666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def forward(x):\n    y =  w*x + b\n    return y\n#cost Funtion\ndef criterian(yhat,y):\n    return torch.mean((yhat-y)**2)\n\nw = torch.tensor(-15.0,requires_grad = True)\nb = torch.tensor(-10.0,requires_grad = True)\nx = torch.arange(-3,3,0.1).view(-1,1)\nf = 1*x - 1\ny = f + torch.randn(x.size())\n\nlr = 0.1\ncost = []\nb_ls = []\nw_ls = []\nfor epoch in range(4):\n    yhat = forward(x)\n    loss = criterian(yhat,y)\n    loss.backward()\n    w.data = w.data - lr*w.grad.data\n    w_ls.append(w.data.item())\n    w.grad.data.zero_()\n    b.data = b.data - lr*w.grad.data\n    b_ls.append(b.data.item())\n    b.grad.data.zero_()\n    cost.append(loss.item())\n    ","metadata":{"execution":{"iopub.status.busy":"2022-10-12T10:19:18.059901Z","iopub.execute_input":"2022-10-12T10:19:18.060758Z","iopub.status.idle":"2022-10-12T10:19:18.073495Z","shell.execute_reply.started":"2022-10-12T10:19:18.060714Z","shell.execute_reply":"2022-10-12T10:19:18.07236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#stochasting gradient descent\ndef forward(x):\n    y =  w*x + b\n    return y\n#cost Funtion\ndef criterian(yhat,y):\n    return torch.mean((yhat-y)**2)\n\nw = torch.tensor(-15.0,requires_grad = True)\nb = torch.tensor(-10.0,requires_grad = True)\nX = torch.arange(-3,3,0.1).view(-1,1)\nf = 1*x - 1\nY = f + torch.randn(x.size())\n\nlr = 0.1\ncost = []\nb_ls = []\nw_ls = []\nfor epoch in range(4):\n    for x,y in zip(X,Y):\n        yhat = forward(x)\n        loss = criterian(yhat,y)\n        loss.backward()\n        w.data = w.data - lr*w.grad.data\n        w_ls.append(w.data.item())\n        w.grad.data.zero_()\n        b.data = b.data - lr*w.grad.data\n        b_ls.append(b.data.item())\n        b.grad.data.zero_()\n        cost.append(loss.item())\n    ","metadata":{"execution":{"iopub.status.busy":"2022-10-12T10:30:51.351627Z","iopub.execute_input":"2022-10-12T10:30:51.352108Z","iopub.status.idle":"2022-10-12T10:30:51.377487Z","shell.execute_reply.started":"2022-10-12T10:30:51.352073Z","shell.execute_reply":"2022-10-12T10:30:51.37634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Mini Batch Gradient Descent","metadata":{}},{"cell_type":"code","source":"#Prepare Custom dataset class\nfrom torch.utils.data import Dataset\nfrom torch.utils.data import DataLoader\n\n#stochasting gradient descent\ndef forward(x):\n    y =  w*x + b\n    return y\n#cost Funtion\ndef criterian(yhat,y):\n    return torch.mean((yhat-y)**2)\n\nclass data(Dataset):\n    def __init__(self):\n        \n        self.x = torch.arange(-3,3,0.1).view(-1,1)\n        self.y = 3*self.x + torch.randn(self.x.size())\n        self.len = self.x.shape[0]\n    def __getitem__(self,index):\n        return self.x[index],self.y[index]\n    def __len__(self):\n        return self.len\nDataset = data()","metadata":{"execution":{"iopub.status.busy":"2022-10-14T08:10:37.690078Z","iopub.execute_input":"2022-10-14T08:10:37.69044Z","iopub.status.idle":"2022-10-14T08:10:37.704466Z","shell.execute_reply.started":"2022-10-14T08:10:37.690412Z","shell.execute_reply":"2022-10-14T08:10:37.703431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainloader = DataLoader(dataset = Dataset,batch_size = 5)\nlr = 0.1\nw = torch.tensor(-15.0,requires_grad = True)\nb = torch.tensor(-10.0,requires_grad = True)\nfor epoch in range(4):\n    for x,y in trainloader:\n        yhat = forward(x)\n        loss = criterian(y,yhat)\n        loss.backward()\n        w.data = w.data - lr*w.grad.data\n        b.data = b.data - lr*b.grad.data\n        w.grad.data.zero_()\n        b.grad.data.zero_()\n        ","metadata":{"execution":{"iopub.status.busy":"2022-10-14T08:10:40.835129Z","iopub.execute_input":"2022-10-14T08:10:40.835533Z","iopub.status.idle":"2022-10-14T08:10:40.871161Z","shell.execute_reply.started":"2022-10-14T08:10:40.8355Z","shell.execute_reply":"2022-10-14T08:10:40.870203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# End of the code","metadata":{}},{"cell_type":"markdown","source":"# Optimizers + train,test,validation","metadata":{}},{"cell_type":"code","source":"from torch.utils.data import Dataset, DataLoader\n\nclass Data(Dataset):\n    def __init__(self, train = True):\n        self.x = torch.arange(-3,3,0.1).view(-1,1)\n        self.y = (3*self.x + 1) + torch.randn(self.x.size())\n        self.len = self.x.shape[0]\n        if train == True:\n            self.y[0] = 0\n            self.y[50:55] = 20\n        else:\n            pass\n    def __getitem__(self,index):\n        return self.x[index],self.y[index]\n    def __len__(self):\n        return self.len       ","metadata":{"execution":{"iopub.status.busy":"2022-10-14T09:22:59.704646Z","iopub.execute_input":"2022-10-14T09:22:59.705079Z","iopub.status.idle":"2022-10-14T09:22:59.713279Z","shell.execute_reply.started":"2022-10-14T09:22:59.705042Z","shell.execute_reply":"2022-10-14T09:22:59.712092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train_data = Data()\nVal_data = Data(train = False)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-14T09:51:32.840233Z","iopub.execute_input":"2022-10-14T09:51:32.840656Z","iopub.status.idle":"2022-10-14T09:51:32.846914Z","shell.execute_reply.started":"2022-10-14T09:51:32.84062Z","shell.execute_reply":"2022-10-14T09:51:32.845856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nclass LR(nn.Module):\n    def __init__(self,in_feature,out_feature):\n        super(LR,self).__init__()\n        self.linear = nn.Linear(in_features = in_feature, out_features = out_feature)\n    def forward(self,x):\n        out = self.linear(x)\n        return out\n#criterion\ncriterion = nn.MSELoss()\ntrainloader = DataLoader(dataset = Train_data,batch_size = 1)\nepochs = 10\nlearning_rates = [0.0001,0.001,0.01,0.1,1]\nvalidation_error = torch.zeros(len(learning_rates))\ntest_error = torch.zeros(len(learning_rates))\nModels = []\n","metadata":{"execution":{"iopub.status.busy":"2022-10-14T09:23:02.439598Z","iopub.execute_input":"2022-10-14T09:23:02.440794Z","iopub.status.idle":"2022-10-14T09:23:02.448331Z","shell.execute_reply.started":"2022-10-14T09:23:02.440738Z","shell.execute_reply":"2022-10-14T09:23:02.4473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model training\nfrom torch import optim\nfor i,learning_rate in enumerate(learning_rates):\n    model = LR(1,1)\n    optimizer = optim.SGD(model.parameters(),lr = learning_rate)\n    yhat = model(Train_data.x)\n    loss = criterion(yhat, Val_data.y)\n    validation_error[i] = loss.item()\n    Models.append(model)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-10-14T09:51:49.764536Z","iopub.execute_input":"2022-10-14T09:51:49.765374Z","iopub.status.idle":"2022-10-14T09:51:49.774417Z","shell.execute_reply.started":"2022-10-14T09:51:49.765334Z","shell.execute_reply":"2022-10-14T09:51:49.773441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfor model,learning_rate in zip(Models,learning_rates):\n    yhat = model(Val_data.x)\n    plt.plot(Val_data.x.numpy(),yhat.detach().numpy(),label = 'lr:'+str(learning_rate))\nplt.plot(Val_data.x.numpy(),yhat.detach().numpy(),'or',label = 'validation data')\nplt.xlabel('x')\nplt.ylabel('y')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2022-10-14T10:09:36.594504Z","iopub.execute_input":"2022-10-14T10:09:36.595574Z","iopub.status.idle":"2022-10-14T10:09:36.828408Z","shell.execute_reply.started":"2022-10-14T10:09:36.595534Z","shell.execute_reply":"2022-10-14T10:09:36.827098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# End of the code","metadata":{}},{"cell_type":"code","source":"#Linear Regression with Torch\nw = torch.tensor(2.0,requires_grad = True)\nb = torch.tensor(-1.0,requires_grad = True)\nx = torch.tensor([[1.0],[2.0]])","metadata":{"execution":{"iopub.status.busy":"2022-10-10T10:28:45.69811Z","iopub.execute_input":"2022-10-10T10:28:45.699323Z","iopub.status.idle":"2022-10-10T10:28:45.709106Z","shell.execute_reply.started":"2022-10-10T10:28:45.699239Z","shell.execute_reply":"2022-10-10T10:28:45.707269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def forward(x):\n    y = w*x + b\n    return y","metadata":{"execution":{"iopub.status.busy":"2022-10-10T10:27:19.801707Z","iopub.execute_input":"2022-10-10T10:27:19.803211Z","iopub.status.idle":"2022-10-10T10:27:19.809616Z","shell.execute_reply.started":"2022-10-10T10:27:19.803142Z","shell.execute_reply":"2022-10-10T10:27:19.8082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = forward(x)\nprint(y)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T10:28:48.203121Z","iopub.execute_input":"2022-10-10T10:28:48.203632Z","iopub.status.idle":"2022-10-10T10:28:48.21378Z","shell.execute_reply.started":"2022-10-10T10:28:48.203595Z","shell.execute_reply":"2022-10-10T10:28:48.212053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.nn import Linear\ntorch.manual_seed(1)\nmodel = Linear(in_features = 1,out_features = 1)\n# print(list(model.parameters()))\ny = model(x)\nprint(y)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T10:32:59.987801Z","iopub.execute_input":"2022-10-10T10:32:59.988354Z","iopub.status.idle":"2022-10-10T10:32:59.999178Z","shell.execute_reply.started":"2022-10-10T10:32:59.988309Z","shell.execute_reply":"2022-10-10T10:32:59.997494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#same way we can create a custum modules\nimport torch.nn as nn\nclass LR(nn.Module):\n    def __init__(self,in_size,out_size):\n        super(LR,self).__init__()\n        self.linear = nn.Linear(in_features = in_size,out_features = out_size)\n    def forward(self,x):\n        y = self.linear(x)\n        return y\n    ","metadata":{"execution":{"iopub.status.busy":"2022-10-10T10:37:44.078094Z","iopub.execute_input":"2022-10-10T10:37:44.078662Z","iopub.status.idle":"2022-10-10T10:37:44.086111Z","shell.execute_reply.started":"2022-10-10T10:37:44.078621Z","shell.execute_reply":"2022-10-10T10:37:44.085017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = LR(1,1)\n#change the input parameter value\nmodel.state_dict()\ny = model(x)\nprint(y)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T10:41:13.405551Z","iopub.execute_input":"2022-10-10T10:41:13.406032Z","iopub.status.idle":"2022-10-10T10:41:13.416049Z","shell.execute_reply.started":"2022-10-10T10:41:13.405997Z","shell.execute_reply":"2022-10-10T10:41:13.414574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class toy_set(Dataset):\n    def __init__(self,length=100,transform = None):\n        self.x = 2*torch.ones(length,2)\n        self.y = torch.ones(length,1)\n        self.len = length\n        self.transform = transform\n    def __getitem__(self,index):\n        sample = self.x[index],self.y[index]\n        if self.transform:\n            sample = self.transform(sample)\n        return sample\n    def __len__(self):\n        return self.len","metadata":{"execution":{"iopub.status.busy":"2022-09-20T06:08:05.099175Z","iopub.execute_input":"2022-09-20T06:08:05.100006Z","iopub.status.idle":"2022-09-20T06:08:05.10613Z","shell.execute_reply.started":"2022-09-20T06:08:05.099957Z","shell.execute_reply":"2022-09-20T06:08:05.105066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ty = toy_set()\nfor i in range(3):\n    x,y = ty[i]\n    print('x:',x,'y:',y)","metadata":{"execution":{"iopub.status.busy":"2022-09-20T06:11:17.458461Z","iopub.execute_input":"2022-09-20T06:11:17.459841Z","iopub.status.idle":"2022-09-20T06:11:17.467527Z","shell.execute_reply.started":"2022-09-20T06:11:17.459791Z","shell.execute_reply":"2022-09-20T06:11:17.465779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#transform class\nclass add_mul(object):\n    def __init__(self,addx = 1,muly = 1):\n        self.addx = addx\n        self.muly = muly\n    \n    def __call__(self,sample):\n        x = sample[0]\n        y = sample[1]\n        x = x + self.addx\n        y = y * self.muly\n        sample = x,y\n        return sample","metadata":{"execution":{"iopub.status.busy":"2022-09-20T06:18:26.052012Z","iopub.execute_input":"2022-09-20T06:18:26.052325Z","iopub.status.idle":"2022-09-20T06:18:26.058377Z","shell.execute_reply.started":"2022-09-20T06:18:26.052302Z","shell.execute_reply":"2022-09-20T06:18:26.057406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#apply transform object in dataset class\nam = add_mul()\ndataset = toy_set(transform = am)\nfor i in range(3):\n    x,y = dataset[i]\n    print('x:',x,'y:',y)","metadata":{"execution":{"iopub.status.busy":"2022-09-20T06:18:28.803948Z","iopub.execute_input":"2022-09-20T06:18:28.804567Z","iopub.status.idle":"2022-09-20T06:18:28.813004Z","shell.execute_reply.started":"2022-09-20T06:18:28.804536Z","shell.execute_reply":"2022-09-20T06:18:28.812024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Multiple Linear Regression","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# End of the code segment","metadata":{}},{"cell_type":"markdown","source":"# Transform Compose","metadata":{}},{"cell_type":"code","source":"#if we want to apply multiple transform \nfrom torchvision import transforms\ndata_transform = transforms.Compose([add_mult(),mult()]) #1st parameter is the object of 1st transformer and 2nd is the object for 2nd transfomrer\nX_,y_ = data_transform(data_set[0])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Dataset for images","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import Dataset,DataLoader","metadata":{"execution":{"iopub.status.busy":"2022-09-20T06:29:15.354578Z","iopub.execute_input":"2022-09-20T06:29:15.354872Z","iopub.status.idle":"2022-09-20T06:29:15.360678Z","shell.execute_reply.started":"2022-09-20T06:29:15.354849Z","shell.execute_reply":"2022-09-20T06:29:15.359511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor = torch.tensor([7,5,4,3,2,1])","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:06.906156Z","iopub.execute_input":"2022-09-15T13:17:06.907028Z","iopub.status.idle":"2022-09-15T13:17:06.913141Z","shell.execute_reply.started":"2022-09-15T13:17:06.906971Z","shell.execute_reply":"2022-09-15T13:17:06.911491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"floattensor = torch.tensor([7,5,4,3,2,1],dtype = torch.float64)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:06.915118Z","iopub.execute_input":"2022-09-15T13:17:06.915701Z","iopub.status.idle":"2022-09-15T13:17:06.934157Z","shell.execute_reply.started":"2022-09-15T13:17:06.915665Z","shell.execute_reply":"2022-09-15T13:17:06.932577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"floattensor.tolist()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:06.937654Z","iopub.execute_input":"2022-09-15T13:17:06.938454Z","iopub.status.idle":"2022-09-15T13:17:06.950985Z","shell.execute_reply.started":"2022-09-15T13:17:06.9384Z","shell.execute_reply":"2022-09-15T13:17:06.949628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"floattensor[0] = 200\nfloattensor[3] = 22\nfloattensor[3:5] = torch.tensor([21,45])\nprint(floattensor[1:4])\nprint(floattensor[3].item())","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:06.952555Z","iopub.execute_input":"2022-09-15T13:17:06.954044Z","iopub.status.idle":"2022-09-15T13:17:06.964451Z","shell.execute_reply.started":"2022-09-15T13:17:06.954Z","shell.execute_reply":"2022-09-15T13:17:06.963182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"floattensor.dtype","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:06.966436Z","iopub.execute_input":"2022-09-15T13:17:06.967135Z","iopub.status.idle":"2022-09-15T13:17:06.975287Z","shell.execute_reply.started":"2022-09-15T13:17:06.967095Z","shell.execute_reply":"2022-09-15T13:17:06.974159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor.type()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:06.976867Z","iopub.execute_input":"2022-09-15T13:17:06.977818Z","iopub.status.idle":"2022-09-15T13:17:06.99108Z","shell.execute_reply.started":"2022-09-15T13:17:06.977771Z","shell.execute_reply":"2022-09-15T13:17:06.989577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = torch.FloatTensor([7,5,4,3,2,1])\na.type()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:06.992544Z","iopub.execute_input":"2022-09-15T13:17:06.993597Z","iopub.status.idle":"2022-09-15T13:17:07.003073Z","shell.execute_reply.started":"2022-09-15T13:17:06.993543Z","shell.execute_reply":"2022-09-15T13:17:07.001963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#1st aregument in view method represents row\n#2nd aregument in view method represents column\na_col = a.view(6,1)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:07.005547Z","iopub.execute_input":"2022-09-15T13:17:07.005958Z","iopub.status.idle":"2022-09-15T13:17:07.014081Z","shell.execute_reply.started":"2022-09-15T13:17:07.005905Z","shell.execute_reply":"2022-09-15T13:17:07.012636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a_col","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:07.018947Z","iopub.execute_input":"2022-09-15T13:17:07.019392Z","iopub.status.idle":"2022-09-15T13:17:07.028716Z","shell.execute_reply.started":"2022-09-15T13:17:07.019354Z","shell.execute_reply":"2022-09-15T13:17:07.027475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#we can use -1 as 1st argument when we don't know how many alement are available in list\nb_col = a.view(-1,1)\nb_col","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:07.030106Z","iopub.execute_input":"2022-09-15T13:17:07.031457Z","iopub.status.idle":"2022-09-15T13:17:07.041354Z","shell.execute_reply.started":"2022-09-15T13:17:07.031408Z","shell.execute_reply":"2022-09-15T13:17:07.040039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Numpy Operations**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nnumpy_array = np.array([0.0,1.0,2.0,3.0,4.0])","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:07.042962Z","iopub.execute_input":"2022-09-15T13:17:07.043361Z","iopub.status.idle":"2022-09-15T13:17:07.051452Z","shell.execute_reply.started":"2022-09-15T13:17:07.043327Z","shell.execute_reply":"2022-09-15T13:17:07.049902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#convert nupy array to tensor\ntensor = torch.from_numpy(numpy_array)\ntensor","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:07.053623Z","iopub.execute_input":"2022-09-15T13:17:07.054045Z","iopub.status.idle":"2022-09-15T13:17:07.066847Z","shell.execute_reply.started":"2022-09-15T13:17:07.054011Z","shell.execute_reply":"2022-09-15T13:17:07.065809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#convert tensor to numpy array\nnumpy_arr = tensor.numpy()\nnumpy_arr","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:07.068496Z","iopub.execute_input":"2022-09-15T13:17:07.069001Z","iopub.status.idle":"2022-09-15T13:17:07.079311Z","shell.execute_reply.started":"2022-09-15T13:17:07.068965Z","shell.execute_reply":"2022-09-15T13:17:07.07831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Pandas with Numpy**","metadata":{}},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:07.08077Z","iopub.execute_input":"2022-09-15T13:17:07.081181Z","iopub.status.idle":"2022-09-15T13:17:07.091128Z","shell.execute_reply.started":"2022-09-15T13:17:07.081138Z","shell.execute_reply":"2022-09-15T13:17:07.089858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pandas_series = pd.Series([0.1,2,0.3,10.1])\npandas_series.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:07.092984Z","iopub.execute_input":"2022-09-15T13:17:07.096239Z","iopub.status.idle":"2022-09-15T13:17:07.109388Z","shell.execute_reply.started":"2022-09-15T13:17:07.096126Z","shell.execute_reply":"2022-09-15T13:17:07.108327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#convert pandas to torch tensor\npd_to_torch = torch.from_numpy(pandas_series.values)\npd_to_torch","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:07.110673Z","iopub.execute_input":"2022-09-15T13:17:07.111348Z","iopub.status.idle":"2022-09-15T13:17:07.125969Z","shell.execute_reply.started":"2022-09-15T13:17:07.11131Z","shell.execute_reply":"2022-09-15T13:17:07.124455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Besic Operations**","metadata":{}},{"cell_type":"code","source":"#vector addition\nv = torch.tensor([1.0,0.0])\nu = torch.tensor([0.0,1.0])\nz = u+v #same for Nd Tensor also\nz","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:07.127611Z","iopub.execute_input":"2022-09-15T13:17:07.128198Z","iopub.status.idle":"2022-09-15T13:17:07.139166Z","shell.execute_reply.started":"2022-09-15T13:17:07.128163Z","shell.execute_reply":"2022-09-15T13:17:07.137862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#tensor multiplication with integer\ny = torch.tensor([1,2])\ny = 2*y #same for Nd Tensor also\ny","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:07.14058Z","iopub.execute_input":"2022-09-15T13:17:07.141587Z","iopub.status.idle":"2022-09-15T13:17:07.149666Z","shell.execute_reply.started":"2022-09-15T13:17:07.141551Z","shell.execute_reply":"2022-09-15T13:17:07.148407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# product\nu = torch.tensor([2,3])\nv = torch.tensor([4,5],dtype = torch.float64)\nz = u*v #same for Nd Tensor also\nz","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:07.151515Z","iopub.execute_input":"2022-09-15T13:17:07.152341Z","iopub.status.idle":"2022-09-15T13:17:07.164174Z","shell.execute_reply.started":"2022-09-15T13:17:07.152286Z","shell.execute_reply":"2022-09-15T13:17:07.162799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r = torch.dot(u,v)\nr","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:07.165871Z","iopub.execute_input":"2022-09-15T13:17:07.166671Z","iopub.status.idle":"2022-09-15T13:17:07.322639Z","shell.execute_reply.started":"2022-09-15T13:17:07.166622Z","shell.execute_reply":"2022-09-15T13:17:07.320797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"u = u+1\nu","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:21.059434Z","iopub.execute_input":"2022-09-15T13:17:21.059982Z","iopub.status.idle":"2022-09-15T13:17:21.071681Z","shell.execute_reply.started":"2022-09-15T13:17:21.05993Z","shell.execute_reply":"2022-09-15T13:17:21.070085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(v.mean())\nprint(v.max())\nprint(v.min())","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:24.329483Z","iopub.execute_input":"2022-09-15T13:17:24.32994Z","iopub.status.idle":"2022-09-15T13:17:24.349682Z","shell.execute_reply.started":"2022-09-15T13:17:24.329906Z","shell.execute_reply":"2022-09-15T13:17:24.348645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.linspace(-2,2,steps = 5)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:25.344672Z","iopub.execute_input":"2022-09-15T13:17:25.345336Z","iopub.status.idle":"2022-09-15T13:17:25.356591Z","shell.execute_reply.started":"2022-09-15T13:17:25.345291Z","shell.execute_reply":"2022-09-15T13:17:25.355389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = torch.linspace(0,2*np.pi,100)\ny = torch.sin(x)\nimport matplotlib.pyplot as plt\n%matplotlib inline\nplt.plot(x.numpy(),y.numpy())","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:26.554538Z","iopub.execute_input":"2022-09-15T13:17:26.555024Z","iopub.status.idle":"2022-09-15T13:17:26.84321Z","shell.execute_reply.started":"2022-09-15T13:17:26.554982Z","shell.execute_reply":"2022-09-15T13:17:26.841824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**ND Tensor**","metadata":{}},{"cell_type":"code","source":"tensor_nd = torch.tensor([[21,22,23],[31,32,33],[11,12,13]])\ntensor_nd","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:30.704333Z","iopub.execute_input":"2022-09-15T13:17:30.705356Z","iopub.status.idle":"2022-09-15T13:17:30.71464Z","shell.execute_reply.started":"2022-09-15T13:17:30.70531Z","shell.execute_reply":"2022-09-15T13:17:30.713621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(tensor_nd.shape)#returns rows and column of tensor\nprint(tensor_nd.numel())#number of element in the tensor\nprint(tensor_nd[1][0])#1st index for row and 2nd index for column\ntensor_nd.ndimension()#Dimension of the tensor (Number of nests in list)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:31.396009Z","iopub.execute_input":"2022-09-15T13:17:31.396401Z","iopub.status.idle":"2022-09-15T13:17:31.405723Z","shell.execute_reply.started":"2022-09-15T13:17:31.396371Z","shell.execute_reply":"2022-09-15T13:17:31.404379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(tensor_nd[0,0:2])#0th row and 1st and 2nd column\nprint(tensor_nd[1:3,2])#3rd column and 2nd and 3rd row","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:34.566549Z","iopub.execute_input":"2022-09-15T13:17:34.566988Z","iopub.status.idle":"2022-09-15T13:17:34.574676Z","shell.execute_reply.started":"2022-09-15T13:17:34.566953Z","shell.execute_reply":"2022-09-15T13:17:34.573589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Matrics Multiplication**","metadata":{}},{"cell_type":"code","source":"#Before Multiplication make sure that column of a matrics should be same as row of other matrics\nA = torch.tensor([[0,1,1],[1,0,1]])\nB = torch.tensor([[1,1],[1,1],[-1,1]])\nc = torch.mm(A,B)\nc","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:36.367505Z","iopub.execute_input":"2022-09-15T13:17:36.367966Z","iopub.status.idle":"2022-09-15T13:17:36.383505Z","shell.execute_reply.started":"2022-09-15T13:17:36.367927Z","shell.execute_reply":"2022-09-15T13:17:36.381868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = torch.tensor(2.0,requires_grad = True)\ny = x**2 + 2*x + 1\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:25:18.863107Z","iopub.execute_input":"2022-09-15T13:25:18.863535Z","iopub.status.idle":"2022-09-15T13:25:18.870182Z","shell.execute_reply.started":"2022-09-15T13:25:18.863503Z","shell.execute_reply":"2022-09-15T13:25:18.868709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.backward()\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:25:22.337142Z","iopub.execute_input":"2022-09-15T13:25:22.337566Z","iopub.status.idle":"2022-09-15T13:25:22.344175Z","shell.execute_reply.started":"2022-09-15T13:25:22.337529Z","shell.execute_reply":"2022-09-15T13:25:22.342915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = x.grad","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:25:40.129323Z","iopub.execute_input":"2022-09-15T13:25:40.129801Z","iopub.status.idle":"2022-09-15T13:25:40.135201Z","shell.execute_reply.started":"2022-09-15T13:25:40.129757Z","shell.execute_reply":"2022-09-15T13:25:40.133852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:25:43.124664Z","iopub.execute_input":"2022-09-15T13:25:43.125851Z","iopub.status.idle":"2022-09-15T13:25:43.134046Z","shell.execute_reply.started":"2022-09-15T13:25:43.125806Z","shell.execute_reply":"2022-09-15T13:25:43.132811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Partial Derivatives**","metadata":{}},{"cell_type":"code","source":"u = torch.tensor(1.0,requires_grad = True)\nv = torch.tensor(2.0,requires_grad = True)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:28:08.568089Z","iopub.execute_input":"2022-09-15T13:28:08.569005Z","iopub.status.idle":"2022-09-15T13:28:08.573969Z","shell.execute_reply.started":"2022-09-15T13:28:08.568949Z","shell.execute_reply":"2022-09-15T13:28:08.572791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = u*v + u**2\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:28:31.616769Z","iopub.execute_input":"2022-09-15T13:28:31.617155Z","iopub.status.idle":"2022-09-15T13:28:31.622939Z","shell.execute_reply.started":"2022-09-15T13:28:31.617125Z","shell.execute_reply":"2022-09-15T13:28:31.621683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f.backward()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:49:36.541355Z","iopub.execute_input":"2022-09-15T13:49:36.541828Z","iopub.status.idle":"2022-09-15T13:49:36.547642Z","shell.execute_reply.started":"2022-09-15T13:49:36.541789Z","shell.execute_reply":"2022-09-15T13:49:36.546284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(u.grad)\nprint(v.grad)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:50:00.922135Z","iopub.execute_input":"2022-09-15T13:50:00.922565Z","iopub.status.idle":"2022-09-15T13:50:00.929472Z","shell.execute_reply.started":"2022-09-15T13:50:00.922509Z","shell.execute_reply":"2022-09-15T13:50:00.928602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}