<repo_name>MedhaChirumarri/Deep-learning_pattern-recognition<file_sep>/Method1.py
import pandas as pd 
import numpy as np
import math
import glob
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
import torch
import torch.nn.functional as F
import torch.nn as nn
from sklearn.model_selection import train_test_split


def f(path,x):
    
    all_files = glob.glob(path + "/*.csv")
    
    li = []
    
    for filename in all_files:
        df = pd.read_csv(filename, index_col=None, header=0,encoding='utf-16')
        li.append(df)
    
    frame = pd.concat(li, axis=0, ignore_index=True)
    frame["class"]=[x for i in range(len(frame))]
    return (frame)

data1=f("/content/kan.zip",0)
data2=f("/content/hin.zip",1)
data3=f("/content/asm.zip",2)
data4=f("/content/ben.zip",0)
data5=f("/content/guj",2)
data1=data1.append(data2)
data=data3.append(data1)
data6=data4.append(data5)
Y=np.asarray(data["class"])
X=np.asarray(data.drop("class",axis=1))
Y1=np.asarray(data6["class"])
X1=np.asarray(data6.drop("class",axis=1))
#print(type(X))
#X_train, X_test, Y_train, Y_test = train_test_split(X,Y,test_size=0.3,random_state=0)
#X_train, Y_train, X_test, Y_test = map(torch.tensor, (X_train, Y_train, X_test, Y_test))
X,Y=map(torch.tensor,(X,Y))
X1,Y1=map(torch.tensor,(X1,Y1))

def accuracy(y_hat, y):
  pred = torch.argmax(y_hat, dim=1)
  return (pred == y).float().mean()


def fit(epochs = 1000, learning_rate = 1):
  loss_arr = []
  acc_arr = []
  for epoch in range(epochs):
   # y_hat = fn(X_train)
   # loss = F.cross_entropy(y_hat, Y_train)
    #loss_arr.append(loss.item())
    #acc_arr.append(accuracy(y_hat, Y_train))
    #y_hat = fn(X_train)
    y_hat = fn(X)
    
    loss = F.cross_entropy(y_hat, Y)
    loss_arr.append(loss.item())
    acc_arr.append(accuracy(y_hat, Y))

    loss.backward()
    with torch.no_grad():
      for param in fn.parameters():
        param -= learning_rate * param.grad
      fn.zero_grad()
        
  plt.plot(loss_arr, 'r-')
  plt.plot(acc_arr, 'b-')
  plt.show()      
  print('acurracy', acc_arr[-1])
  print('Loss after training', loss_arr[-1])

class FirstNetwork(nn.Module):
  
  def __init__(self):
    super().__init__()
    torch.manual_seed(0)
    self.weights1 = nn.Parameter(torch.randn(80,700) / math.sqrt(2))
    self.bias1 = nn.Parameter(torch.zeros(700))
    self.weights2 = nn.Parameter(torch.randn(700, 500) / math.sqrt(2))
    self.bias2 = nn.Parameter(torch.zeros(500))
    self.weights3 = nn.Parameter(torch.randn(500, 5) / math.sqrt(2))
    self.bias3 = nn.Parameter(torch.zeros(5))
    
  def forward(self, X):
    a1 = torch.matmul(X, self.weights1.double()) + self.bias1
    h1 = a1.sigmoid()
    a2 = torch.matmul(h1, self.weights2.double()) + self.bias2
    h2 = a2.sigmoid()
    a3=torch.matmul(h2, self.weights3.double()) + self.bias3
    h3 = a3.exp()/a3.exp().sum(-1).unsqueeze(-1)
    return h3

fn = FirstNetwork()
fit()

y1=fn(X1)
acc=accuracy(y1,Y1)
print(acc)

<file_sep>/README.md
# FFN
DL code
<file_sep>/SRP2.PY
import pandas as pd 
import numpy as np
import glob
import torch
import torch.nn as nn
import torch.optim as optim
import time



folders = glob.glob("/home/system/Desktop/SRP/train/*")
Xtrain=[]
Ytrain=[]
k=0
for folder in folders:
    for filename in glob.glob(folder+"/*.csv"):
        df = pd.read_csv(filename, index_col=None, header=0,encoding='utf-16')
        Ytrain.append(np.array(k))
        Xtrain.append(np.array(df))
    k=k+1

print(len(Xtrain))
folders = glob.glob("/home/system/Desktop/SRP/test/*")
Xtest=[]
Ytest=[]
k=0
check_p=0
m=0
for folder in folders:
    for filename in glob.glob(folder+"/*.csv"):
        df = pd.read_csv(filename, index_col=None, header=0,encoding='utf-16')
        Ytest.append(np.array(k))
        Xtest.append(np.array(df))
    k=k+1

print(len(Xtest))

def eval(net,criterion,batch_size,topk, X, Y):
    correct = 0
    files,langs = batched_dataloader(batch_size, X, Y,False)
    hidden = net.init_hidden(batch_size)
    output, hidden = net(files, hidden)
    val, indices = output.topk(topk)
    for i,j in zip(indices,langs):
        if j in i:
            correct += 1
    accuracy = correct/batch_size
    loss=criterion(output, langs)
    return accuracy,loss

def batched_dataloader(batch_size, X, Y,train=True):
    files = []
    langs = []
    file_len=[]
    if train==True:
        for i in range(batch_size):
            index = np.random.randint(len(X))
            file, lang = X[index], Y[index]
            file_len.append(len(file))
            files.append(torch.tensor(file))
            langs.append(lang)
        
    else:
        langs=Y
        file_len=[len(i) for i in X]
        files=[torch.tensor(i) for i in X]
    
    langs=np.array(langs)
    files=nn.utils.rnn.pad_sequence(files, batch_first=True)
    files=np.array(files)
    
    
    k=np.empty([max(file_len),batch_size,80])
    for i in range(max(file_len)):
        for j in range(batch_size):
            k[i][j]=np.array(files[j][i])
            
    files=torch.from_numpy(k).float()
    langs=torch.from_numpy(langs).long()
    files = nn.utils.rnn.pack_padded_sequence(files,file_len, enforce_sorted = False)
    
    return files, langs


def train_batch(net, opt, criterion, batch_size):
    
    opt.zero_grad()
    
    files, langs = batched_dataloader(batch_size, Xtrain, Ytrain)
    
    hidden = net.init_hidden(batch_size)
    output, hidden = net(files,hidden)
    loss = criterion(output, langs)
    
    loss.backward()
    opt.step()
    print("loss:",loss)
    print(" ")
    return loss



def train_setup(net, lr = 0.01, n_batches = 100, batch_size = 10, momentum = 0.9):
    criterion = nn.NLLLoss()
    opt = optim.SGD(net.parameters(), lr=lr, momentum=momentum)
    
    loss_arr = np.zeros(n_batches + 1)
    
    for i in range(n_batches):
        print("batch:",i+1)
        loss_arr[i+1] = (loss_arr[i]*i + train_batch(net, opt, criterion, batch_size))/(i + 1)
    print("avg_loss_array:",loss_arr)
    print(" ")
    print('Top-3:', eval(net, criterion,len(Xtest), 3, Xtest, Ytest), 'Top-2:', eval(net, criterion,len(Xtest), 2, Xtest, Ytest))
                      

class LSTM_net(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(LSTM_net, self).__init__()
        self.hidden_size = hidden_size
        self.lstm_cell = nn.LSTM(input_size, hidden_size)
        self.h2o = nn.Linear(hidden_size, output_size)
        self.softmax = nn.LogSoftmax(dim=1)
    
    def forward(self, input, hidden):
        out, hidden = self.lstm_cell(input, hidden)
        output = self.h2o(hidden[0].view(-1, self.hidden_size))
        output = self.softmax(output)
        return output, hidden
    
    def init_hidden(self, batch_size = 1):
        return (torch.zeros(1, batch_size, self.hidden_size), torch.zeros(1, batch_size, self.hidden_size))
    
    
n_hidden = 128
tic = time.time()
net = LSTM_net(80, n_hidden, 9)
train_setup(net, lr=1, n_batches=20, batch_size = 128)
toc = time.time()
print(" ")
print('Time taken', toc - tic)<file_sep>/SRP3.py
import pandas as pd 
import numpy as np
import glob
import torch
import torch.nn as nn
import torch.optim as optim
import time



folders = glob.glob("/home/system/Desktop/SRP/train/*")
Xtrain=[]
Ytrain=[]
k=0
for folder in folders:
    for filename in glob.glob(folder+"/*.csv"):
        df = pd.read_csv(filename, index_col=None, header=0,encoding='utf-16')
        Ytrain.append(np.array([k]))
        Xtrain.append(np.array(df))
    k=k+1

print(len(Xtrain))
folders = glob.glob("/home/system/Desktop/SRP/test/*")
Xtest=[]
Ytest=[]
k=0
for folder in folders:
    for filename in glob.glob(folder+"/*.csv"):
        df = pd.read_csv(filename, index_col=None, header=0,encoding='utf-16')
        Ytest.append(np.array([k]))
        Xtest.append(np.array(df))
    k=k+1

print(len(Xtest))

def eval(net,criterion,batch_size,topk, X, Y):
    files=np.array(X)
    langs=np.array(Y)
    correct = 0
    total_loss=0
    for file,lang in zip(files,langs):

        hidden = net.init_hidden()
        
        file=torch.from_numpy(file).float()
        file=file.view(file.size()[0],1,80)
        lang=torch.from_numpy(lang).long()
        output, hidden = net(file, hidden)
        loss = criterion(output, lang)
        total_loss+=loss
        val, indices = output.topk(topk)
        if lang in indices:
            correct += 1
    accuracy = correct/batch_size
    return accuracy,total_loss/batch_size


def batched_dataloader(batch_size, X, Y):
    files = []
    langs = []
    
    for i in range(batch_size):
        index = np.random.randint(len(X))
        file, lang = X[index], Y[index]
        files.append(file)
        langs.append(lang)
        
    files=np.array(files)
    langs=np.array(langs)
    return files, langs


def train(net, opt, criterion, batch_size):
    
    opt.zero_grad()
    total_loss = 0
    
    files,langs = batched_dataloader(batch_size, Xtrain, Ytrain)
    
    total_loss = 0
    for file,lang in zip(files,langs):

        hidden = net.init_hidden()
        
        file=torch.from_numpy(file).float()
        file=file.view(file.size()[0],1,80)
        lang=torch.from_numpy(lang).long()
        output, hidden = net(file, hidden)
        loss = criterion(output, lang)
        loss.backward(retain_graph=True)
        total_loss += loss
            
    print("loss :",total_loss/batch_size)
    print(" ")
    opt.step()     
    return total_loss/batch_size



def train_setup(net, lr = 0.01, n_batches = 100, batch_size = 10, momentum = 0.9):
    criterion = nn.NLLLoss()
    opt = optim.SGD(net.parameters(), lr=lr, momentum=momentum)
    
    loss_arr = np.zeros(n_batches + 1)
    
    for i in range(n_batches):
        print("batch:",i+1)
        loss_arr[i+1] = (loss_arr[i]*i + train(net, opt, criterion, batch_size))/(i + 1)
    print("avg_loss_arr:",loss_arr)
    print(" ")
    print('Top-3:', eval(net,criterion ,len(Xtest), 3, Xtest, Ytest), 'Top-2:', eval(net,criterion, len(Xtest), 2, Xtest, Ytest))
                      

class LSTM_net(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(LSTM_net, self).__init__()
        self.hidden_size = hidden_size
        self.lstm_cell = nn.LSTM(input_size, hidden_size)
        self.h2o = nn.Linear(hidden_size, output_size)
        self.softmax = nn.LogSoftmax(dim=1)
    
    def forward(self, input, hidden):
        out, hidden = self.lstm_cell(input, hidden)
        output = self.h2o(hidden[0].view(-1, self.hidden_size))
        output = self.softmax(output)
        return output, hidden
    
    def init_hidden(self, batch_size = 1):
        return (torch.zeros(1, batch_size, self.hidden_size), torch.zeros(1, batch_size, self.hidden_size))
    
    
n_hidden = 128
tic = time.time()
net = LSTM_net(80, n_hidden, 9)
train_setup(net, lr=1, n_batches=20, batch_size = 128)
toc = time.time()
print(" ")
print('Time taken', toc - tic)<file_sep>/method3.py
import warnings
import pandas as pd
warnings.filterwarnings('ignore')
import torch
import torch.nn.functional as F
from sklearn.model_selection import train_test_split
import torch.nn as nn
from torch import optim
import numpy as np

data1=pd.read_csv("/content/hin13.csv",encoding="utf-16")
data2=pd.read_csv("/content/kan12.csv",encoding="utf-16")
data1["class"]=[1 for i in range(len(data1))]
data2["class"]=[2 for i in range(len(data2))]
data=data1.append(data2)
Y=np.asarray(data["class"])
X=np.asarray(data.drop("class",axis=1))

X_train, X_test, Y_train, Y_test = train_test_split(X,Y,test_size=0.3,random_state=0)
X_train, Y_train, X_test, Y_test = map(torch.tensor, (X_train, Y_train, X_test, Y_test))
X_train=X_train.float()


def fit_v2(x, y, model, opt, loss_fn, epochs = 1000):
  
  for epoch in range(epochs):
    loss = loss_fn(model(x), y)

    loss.backward()
    opt.step()
    opt.zero_grad()
    
  return loss.item()


class FirstNetwork_v2(nn.Module):
  
  def __init__(self):
    super().__init__()
    torch.manual_seed(0)
    self.net = nn.Sequential(
        nn.Linear(80, 700).float(), 
        nn.Sigmoid(), 
        nn.Linear(700, 500).float(), 
        nn.Sigmoid(),
        nn.Linear(500,5).float(),
        nn.Softmax()
    )

  def forward(self, X):
    return self.net((X))

fn = FirstNetwork_v2()
loss_fn = F.cross_entropy
opt = optim.SGD(fn.parameters(), lr=1)
print(fit_v2(X_train, Y_train, fn, opt, loss_fn))

fn = FirstNetwork_v2()
fit_v1()
<file_sep>/SRP1.PY
import pandas as pd 
import numpy as np
import glob
import torch
import torch.nn as nn
import torch.optim as optim



folders = glob.glob("/home/system/Desktop/SRP/test/*")
X=[]
Y=[]
k=0
for folder in folders:
    for filename in glob.glob(folder+"/*.csv"):
        df = pd.read_csv(filename, index_col=None, header=0,encoding='utf-16')
        Y.append(np.array(k))
        X.append(np.array(df))
    k=k+1



def batched_dataloader(batch_size, X, Y):
    files = []
    langs = []
    file_len=[]
    for i in range(batch_size):
        index = np.random.randint(len(X))
        file, lang = X[index], Y[index]
        file_len.append(len(file))
        files.append(torch.tensor(file))
        langs.append(lang)
    langs=np.array(langs)
    files=nn.utils.rnn.pad_sequence(files, batch_first=True)
    files=np.array(files)
    files=torch.from_numpy(files).float()
    langs=torch.from_numpy(langs).long()
    return files, langs


def train(net, opt, criterion, batch_size):
    
    opt.zero_grad()
    
    files, langs = batched_dataloader(batch_size, X, Y)
    
    hidden = net.init_hidden(batch_size)
    output, hidden = net(files,hidden)
    loss = criterion(output, langs)
    
    loss.backward()
    opt.step()
    print("loss:",loss)
    return loss



def train_setup(net, lr = 0.01, n_batches = 100, batch_size = 10, momentum = 0.9):
    criterion = nn.NLLLoss()
    opt = optim.SGD(net.parameters(), lr=lr, momentum=momentum)
    
    loss_arr = np.zeros(n_batches + 1)
    
    for i in range(n_batches):
        print(i)
        loss_arr[i+1] = (loss_arr[i]*i + train(net, opt, criterion, batch_size))/(i + 1)
    print(loss_arr)
            
    #print('Top-1:', eval(net, len(X), 1, X, Y), 'Top-2:', eval(net, len(X), 2, X, Y))
                      

class LSTM_net(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(LSTM_net, self).__init__()
        self.hidden_size = hidden_size
        self.lstm_cell = nn.LSTM(input_size, hidden_size)
        self.h2o = nn.Linear(hidden_size, output_size)
        self.softmax = nn.LogSoftmax(dim=1)
    
    def forward(self, input, hidden):
        out, hidden = self.lstm_cell(input.view(input.size()[1],64,80), hidden)
        output = self.h2o(hidden[0].view(-1, self.hidden_size))
        output = self.softmax(output)
        return output.view(64,9), hidden
    
    def init_hidden(self, batch_size = 1):
        return (torch.zeros(1, batch_size, self.hidden_size), torch.zeros(1, batch_size, self.hidden_size))
    
    
n_hidden = 128
net = LSTM_net(80, n_hidden, 9)
train_setup(net, lr=0.15, n_batches=50, batch_size = 64)
<file_sep>/Method2.py
#method 2

import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch import optim
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split


def accuracy(y_hat, y):
  pred = torch.argmax(y_hat, dim=1)
  return (pred == y).float().mean()

data1=pd.read_csv("/hin13.csv",encoding="utf-16")
data2=pd.read_csv("/kan12.csv",encoding="utf-16")
data1["class"]=[1 for i in range(len(data1))]
data2["class"]=[2 for i in range(len(data2))]
data=data1.append(data2)

Y=np.asarray(data["class"])
X=np.asarray(data.drop("class",axis=1))

X_train, X_test, Y_train, Y_test = train_test_split(X,Y,test_size=0.3,random_state=0)
X_train, Y_train, X_test, Y_test = map(torch.tensor, (X_train, Y_train, X_test, Y_test))
X_train=X_train.float()
def fit_v1(epochs = 1000, learning_rate = 1):
  loss_arr = []
  acc_arr = []
  opt = optim.SGD(fn.parameters(), lr=learning_rate)
  
  for epoch in range(epochs):
    y_hat = fn(X_train)
    loss = F.cross_entropy(y_hat, Y_train)
    loss_arr.append(loss.item())
    acc_arr.append(accuracy(y_hat, Y_train))

    loss.backward()
    opt.step()
    opt.zero_grad()
        
  plt.plot(loss_arr, 'r-')
  plt.plot(acc_arr, 'b-')
  plt.show()      
  print('Loss before training', loss_arr[0])
  print('Loss after training', loss_arr[-1])
  
class FirstNetwork_v1(nn.Module):
  
  def __init__(self):
    super().__init__()
    torch.manual_seed(0)
    self.lin1 = nn.Linear(80, 700)
    self.lin2 = nn.Linear(700, 500)
    self.lin3 = nn.Linear(500,5)
    
  def forward(self, X):
    a1 = self.lin1(X)
    h1 = a1.sigmoid()
    a2 = self.lin2(h1)
    h2= a2.sigmoid()
    a3 = self.lin3(h2)
    h3 = a3.exp()/a3.exp().sum(-1).unsqueeze(-1)
    return h3

fn = FirstNetwork_v1()
fit_v1()

fn = FirstNetwork_v1()
fit()
