<jupyter_start><jupyter_text>[View in Colaboratory](https://colab.research.google.com/github/SKhan97/Machine-Learning---Bootcamp/blob/master/Linear_Regression_PyTorch.ipynb)<jupyter_code>from google.colab import files
files.upload() #Upload the airfoil_self_noise.dat

# http://pytorch.org/
from os import path
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())

accelerator = 'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'

!pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.3.0.post4-{platform}-linux_x86_64.whl torchvision
import torch
import numpy as np
import torch
from torch.autograd import Variable
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('airfoil_self_noise.dat', sep = '\t')
df = df.sample(frac = 1) #Reorders data to prevent predictable behaviour
print(df.head())

X = torch.Tensor(np.array(df[df.columns[0:-1]])) #All the features
Y = torch.Tensor(np.array(df[df.columns[-1]]).squeeze()) #Labels 

X =(X-X.mean(0))/X.std(0) #Normalise features such that mean = 0 and s.d. = 0

m = 1100

x_train = Variable(X[0:m])
y_train = Variable(Y[0:m]).view(-1,1) #says make it a column vector

x_test = Variable(X[m:])
y_test = Variable(Y[m:]).view(-1,1)


class linearmodel(torch.nn.Module):
  def __init__(self):
    super().__init__() #Initialises from the inherited class of torch.nn.Module
    self.linear = torch.nn.Linear(5, 1) #Number of inputs and number of outputs, does a linear combination
  
  def forward(self, x):
    x = self.linear(x);
    return x
epochs = 1000
lr = 0.5

mymodel = linearmodel()
criterion = torch.nn.MSELoss()
optimizer = torch.optim.Adam(mymodel.parameters(), lr = lr) #Parameters method inherited from torch.nn.Module

def train(epochs):
  for epoch in range(epochs):
    h = mymodel.forward(x_train)
    
    cost = criterion(h, y_train)
    #print('Epoch: ', epoch, 'Cost: ', cost.data[0])
    
    optimizer.zero_grad()
    cost.backward()
    optimizer.step()

train(epochs)
def test():
    h = mymodel.forward(x_test)
    cost = criterion(h, y_test)
    
    return cost.data[0]

test_cost = test()
print('Cost on test set: ', test_cost)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14677276746242263, 'fraction_numerical': 0.01900972590627763, 'mean_word_length': 4.533007334963325, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '103230', 'license': 'MIT', 'n_tokens_mistral': 869, 'n_tokens_neox': 832, 'n_words': 190, 'path': 'Linear Regression/Linear_Regression_PyTorch.ipynb', 'repo_name': 'SKhan97/Machine-Learning-Bootcamp', 'size': '2262'}