import torch
import torch.nn as nn


class MLP(nn.Module):

    def __init__(self, num_classes=1000, mi=None, noise=1e-5):
        super(MLP, self).__init__()


        self.classifier = nn.Sequential(
            nn.Linear(784, 4096),
            nn.ReLU(inplace=True),

            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            
            nn.Linear(4096, num_classes),
        )

        self.mi = mi
        self.noise = noise

    def forward(self, x, get_res):
        #x = torch.flatten(x, 1)
        x = x.view(x.size(0), -1)
        mi = {}
        j_entropy = {}
        gram_m = {'input':{}, 'layer':{}}

        for j, elem in enumerate(self.classifier):
            x = elem(x)

            if isinstance(elem, nn.Linear):
                if get_res['gram'] and self.mi:
                    gram_m['layer'][j+i] = self.mi.get_gram_matrix(elem.weight.clone().unsqueeze(0)).mean(dim=0).cpu() #entropy

            if isinstance(elem, nn.ReLU):
                x = x + torch.rand_like(x)*self.noise

                if self.mi:
                    if get_res['mi']:
                        mi[j+i] = self.mi.tc(torch.flatten(x.clone(),1).cpu().numpy())

                    if get_res['gram']:
                        gram_m['input'][j+i] = self.mi.get_gram_matrix(x.clone()).mean(dim=0).cpu()

        if get_res['mi'] and self.mi:
            mi[j+i] = self.mi.tc(torch.flatten(x.clone(),1).cpu().numpy())

        return x, mi, gram_m

class MLP4(nn.Module):

    def __init__(self, num_classes=1000, mi=None, noise=1e-5):
        super(MLP4, self).__init__()


        self.classifier = nn.Sequential(
            nn.Linear(784, 2048),
            nn.ReLU(inplace=True),

            nn.Linear(2048, 2048),
            nn.ReLU(inplace=True),

            nn.Linear(2048, 2048),
            nn.ReLU(inplace=True),

            nn.Linear(2048, 2048),
            nn.ReLU(inplace=True),
            
            nn.Linear(2048, num_classes),
        )

        self.mi = mi
        self.noise = noise

    def forward(self, x, get_res):
        #x = torch.flatten(x, 1)
        x = x.view(x.size(0), -1)
        mi = {}
        j_entropy = {}
        gram_m = {'input':{}, 'layer':{}}

        for j, elem in enumerate(self.classifier):
            x = elem(x)

            if isinstance(elem, nn.Linear):
                if get_res['gram'] and self.mi:
                    gram_m['layer'][j+i] = self.mi.get_gram_matrix(elem.weight.clone().unsqueeze(0)).mean(dim=0).cpu() #entropy

            if isinstance(elem, nn.ReLU):
                x = x + torch.rand_like(x)*self.noise

                if self.mi:
                    if get_res['mi']:
                        mi[j+i] = self.mi.tc(torch.flatten(x.clone(),1).cpu().numpy())

                    if get_res['gram']:
                        gram_m['input'][j+i] = self.mi.get_gram_matrix(x.clone()).mean(dim=0).cpu()

        if get_res['mi'] and self.mi:
            mi[j+i] = self.mi.tc(torch.flatten(x.clone(),1).cpu().numpy())

        return x, mi, gram_m