├── final rs.png ├── torvhvision ├── README.md ├── Import Modules ├── Arguments Require. └── torch.nn.module. /final rs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gowthambalan/PyTorch_linear/HEAD/final rs.png -------------------------------------------------------------------------------- /torvhvision: -------------------------------------------------------------------------------- 1 | # We can run this Python code on a Jupyter notebook 2 | # to automatically install the correct version of 3 | # PyTorch. 4 | 5 | # http://pytorch.org / from os import path 6 | from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag 7 | platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag()) 8 | 9 | accelerator = 'cu80' if path.exists('/opt / bin / nvidia-smi') else 'cpu' 10 | 11 | ! pip install -q http://download.pytorch.org / whl/{accelerator}/torch-1.3.1.post4-{platform}-linux_x86_64.whl torchvision 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PyTorch_linear 2 | 3 | we shall find out how to implement this in PyTorch, a very popular deep learning library that is being developed by Facebook. 4 | Firstly, you will need to install PyTorch into your Python environment 5 | 6 | ------------------------------------Visit pytorch.org-------------------------------------- 7 | 8 | Pytorch is the powerful Machine Learning Python Framework 9 | 10 | Logistics Regression of MNIST In Pytorch 11 | 12 |

13 | 14 |

15 | 16 | 17 | 18 | 19 | Linear Regression using PyTorch 20 | -------------------------------------------------------------------------------- /Import Modules: -------------------------------------------------------------------------------- 1 | MNIST datase--------++ 2 | 3 | pip install torch 4 | pip install torchvision --no-deps 5 | 6 | import torch 7 | import torchvision 8 | import torch.nn as tn 9 | import matplotlib.pyplot as plt 10 | import torchvision.transforms as tt 11 | import torch.utils as utils 12 | ====================================================== 13 | 14 | Dataset MNIST 15 | Number of datapoints: 60000 16 | Root location: ./data 17 | Split: Train 18 | Dataset MNIST 19 | Number of datapoints: 10000 20 | Root location: data 21 | Split: Test 22 | 23 | (, 5) 24 | 25 | import matplotlib.pyplot as plt 26 | 27 | plt.subplot(1,2,1) 28 | image, label = train_data[0] 29 | plt.imshow(image, cmap='gray') 30 | plt.title("Label of Image:{}".format(label),fontsize=20) 31 | plt.subplot(1,2,2) 32 | image, label = train_data[1] 33 | plt.imshow(image, cmap='gray') 34 | plt.title("Label of Image:{}".format(label),fontsize=20) 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Arguments Require.: -------------------------------------------------------------------------------- 1 | input_size = 28*28 #Size of image 2 | 3 | num_classes = 10 #the image number are in range 0-10 4 | 5 | num_epochs = 5 #one cycle through the full train data 6 | 7 | batch_size = 100 #sample size consider before updating the model’s weights 8 | 9 | learning_rate = 0.001 #step size to update parameter 10 | 11 | ============Loss And Optimizer========= 12 | 13 | model = LogisticRegression(input_size,num_classes) 14 | loss = tn.CrossEntropyLoss() 15 | optimizer = torch.optim.SGD(model.parameters(),lr=learning_rate) 16 | 17 | 18 | run = 0 19 | for epoch in range(num_epochs): 20 | for i,(images,labels) in enumerate(train_dataLoader): 21 | images = torch.autograd.Variable(images.view(-1,input_size)) 22 | labels = torch.autograd.Variable(labels) 23 | 24 | # Nullify gradients w.r.t. parameters 25 | optimizer.zero_grad() 26 | #forward propagation 27 | output = model(images) 28 | # compute loss based on obtained value and actual label 29 | compute_loss = loss(output,labels) 30 | # backward propagation 31 | compute_loss.backward() 32 | # update the parameters 33 | optimizer.step() 34 | run+=1 35 | 36 | if (i+1)%200 == 0: 37 | # check total accuracy of predicted value and actual label 38 | accurate = 0 39 | total = 0 40 | for images,labels in test_dataLoader: 41 | images = torch.autograd.Variable(images.view(-1,input_size)) 42 | output = model(images) 43 | _,predicted = torch.max(output.data, 1) 44 | # total labels 45 | total+= labels.size(0) 46 | 47 | # Total correct predictions 48 | accurate+= (predicted == labels).sum() 49 | accuracy_score = 100 * accurate/total 50 | 51 | print('Iteration: {}. Loss: {}. Accuracy: {}'.format(run, compute_loss.item(), accuracy_score)) 52 | 53 | print('Final Accuracy:',accuracy_score) 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /torch.nn.module.: -------------------------------------------------------------------------------- 1 | Initializing our model. 2 | Declaring the forward pass. 3 | 4 | class LinearRegressionModel(torch.nn.Module): 5 | 6 | def __init__(self): 7 | super(LinearRegressionModel, self).__init__() 8 | self.linear = torch.nn.Linear(1, 1) # One in and one out 9 | 10 | def forward(self, x): 11 | y_pred = self.linear(x) 12 | return y_pred 13 | 14 | ---------------------squared error (MSE) as our loss function and stochastic gradient descent (SGD) as our optimizer------------------ 15 | 16 | criterion = torch.nn.MSELoss(size_average = False) 17 | optimizer = torch.optim.SGD(our_model.parameters(), lr = 0.01) 18 | 19 | for epoch in range(500): 20 | 21 | # Forward pass: Compute predicted y by passing 22 | # x to the model 23 | pred_y = our_model(x_data) 24 | 25 | # Compute and print loss 26 | loss = criterion(pred_y, y_data) 27 | 28 | # Zero gradients, perform a backward pass, 29 | # and update the weights. 30 | optimizer.zero_grad() 31 | loss.backward() 32 | optimizer.step() 33 | print('epoch {}, loss {}'.format(epoch, loss.item())) 34 | 35 | input 4.0,value that is very close to 8.0 36 | 37 | predict (after training) 4 7.966438293457031 38 | 39 | import torch 40 | from torch.autograd import Variable 41 | 42 | x_data = Variable(torch.Tensor([[1.0], [2.0], [3.0]])) 43 | y_data = Variable(torch.Tensor([[2.0], [4.0], [6.0]])) 44 | 45 | 46 | class LinearRegressionModel(torch.nn.Module): 47 | 48 | def __init__(self): 49 | super(LinearRegressionModel, self).__init__() 50 | self.linear = torch.nn.Linear(1, 1) # One in and one out 51 | 52 | def forward(self, x): 53 | y_pred = self.linear(x) 54 | return y_pred 55 | 56 | # our model 57 | our_model = LinearRegressionModel() 58 | 59 | criterion = torch.nn.MSELoss(size_average = False) 60 | optimizer = torch.optim.SGD(our_model.parameters(), lr = 0.01) 61 | 62 | for epoch in range(500): 63 | 64 | # Forward pass: Compute predicted y by passing 65 | # x to the model 66 | pred_y = our_model(x_data) 67 | 68 | # Compute and print loss 69 | loss = criterion(pred_y, y_data) 70 | 71 | # Zero gradients, perform a backward pass, 72 | # and update the weights. 73 | optimizer.zero_grad() 74 | loss.backward() 75 | optimizer.step() 76 | print('epoch {}, loss {}'.format(epoch, loss.item())) 77 | 78 | new_var = Variable(torch.Tensor([[4.0]])) 79 | pred_y = our_model(new_var) 80 | print("predict (after training)", 4, our_model(new_var).item()) 81 | 82 | 83 | 84 | --------------------------------------------------------------------------------