<reponame>Yutong-Dai/FLBase<filename>src/flbase/models/MLP.py<gh_stars>0
'''
# File: CNNMnist.py
# Project: models
# Created Date: 2021-12-16 5:11
# Author: <NAME> <EMAIL>
# -----
# Last Modified: 2022-04-22 9:18
# Modified By: <NAME> <EMAIL>
# 
# This code is published under the MIT License.
# -----
# HISTORY:
# Date      	By 	Comments
# ----------	---	----------------------------------------------------------
'''
from ..model import Model
import torch.nn as nn
import torch.nn.functional as F

"""
Ref: https://github.com/AshwinRJ/Federated-Learning-PyTorch/blob/master/src/models.py
"""

class MLP(Model):
    def __init__(self, config):
        super().__init__(config, None, None)
        dim = config['dim']
        self.fc1 = nn.Linear(2, 64)
        self.fc2 = nn.Linear(64, dim * 4)
        self.fc3 = nn.Linear(dim * 4, dim * 2)
        self.feature_embedding = nn.Linear(dim * 2, dim)

    def forward(self, x, normalize=False):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        embedding = self.feature_embedding(x)
        if normalize:
            normalized_embedding = embedding / torch.norm(embedding, dim=1).view(-1,1)
            return normalized_embedding      
        else:
            return embedding    

class MLPCE(Model):
    def __init__(self, config):
        super().__init__(config, None, None)
        dim = config['dim']
        self.fc1 = nn.Linear(2, 64)
        self.fc2 = nn.Linear(64, dim * 4)
        self.fc3 = nn.Linear(dim * 4, dim * 2)
        self.W = nn.Linear(dim * 2, 2)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        logits = self.W(x)
        return logits  

# class MLP(nn.Module):
#     def __init__(self, dim):
#         super().__init__()
#         self.fc1 = nn.Linear(2, 64)
#         self.fc2 = nn.Linear(64, dim * 4)
#         self.fc3 = nn.Linear(dim * 4, dim * 2)
#         self.feature_embedding = nn.Linear(dim * 2, dim)

#     def forward(self, x, normalize=False):
#         x = F.relu(self.fc1(x))
#         x = F.relu(self.fc2(x))
#         x = F.relu(self.fc3(x))
#         embedding = self.feature_embedding(x)
#         if normalize:
#             normalized_embedding = embedding / torch.norm(embedding, dim=1).view(-1,1)
#             return normalized_embedding      
#         else:
#             return embedding    


# class MLPCE(nn.Module):
#     def __init__(self, dim):
#         super().__init__()
#         self.fc1 = nn.Linear(2, 64)
#         self.fc2 = nn.Linear(64, dim * 4)
#         self.fc3 = nn.Linear(dim * 4, dim * 2)
#         self.W = nn.Linear(dim * 2, 2)

#     def forward(self, x):
#         x = F.relu(self.fc1(x))
#         x = F.relu(self.fc2(x))
#         x = F.relu(self.fc3(x))
#         logits = self.W(x)
#         return logits  
'''
# File: CNNMnist.py
# Project: models
# Created Date: 2021-12-16 5:11
# Author: <NAME> <EMAIL>
# -----
# Last Modified: 2022-04-22 9:18
# Modified By: <NAME> <EMAIL>
# 
# This code is published under the MIT License.
# -----
# HISTORY:
# Date      	By 	Comments
# ----------	---	----------------------------------------------------------
'''
from ..model import Model
import torch.nn as nn
import torch.nn.functional as F

"""
Ref: https://github.com/AshwinRJ/Federated-Learning-PyTorch/blob/master/src/models.py
"""

class MLP(Model):
    def __init__(self, config):
        super().__init__(config, None, None)
        dim = config['dim']
        self.fc1 = nn.Linear(2, 64)
        self.fc2 = nn.Linear(64, dim * 4)
        self.fc3 = nn.Linear(dim * 4, dim * 2)
        self.feature_embedding = nn.Linear(dim * 2, dim)

    def forward(self, x, normalize=False):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        embedding = self.feature_embedding(x)
        if normalize:
            normalized_embedding = embedding / torch.norm(embedding, dim=1).view(-1,1)
            return normalized_embedding      
        else:
            return embedding    

class MLPCE(Model):
    def __init__(self, config):
        super().__init__(config, None, None)
        dim = config['dim']
        self.fc1 = nn.Linear(2, 64)
        self.fc2 = nn.Linear(64, dim * 4)
        self.fc3 = nn.Linear(dim * 4, dim * 2)
        self.W = nn.Linear(dim * 2, 2)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        logits = self.W(x)
        return logits  

# class MLP(nn.Module):
#     def __init__(self, dim):
#         super().__init__()
#         self.fc1 = nn.Linear(2, 64)
#         self.fc2 = nn.Linear(64, dim * 4)
#         self.fc3 = nn.Linear(dim * 4, dim * 2)
#         self.feature_embedding = nn.Linear(dim * 2, dim)

#     def forward(self, x, normalize=False):
#         x = F.relu(self.fc1(x))
#         x = F.relu(self.fc2(x))
#         x = F.relu(self.fc3(x))
#         embedding = self.feature_embedding(x)
#         if normalize:
#             normalized_embedding = embedding / torch.norm(embedding, dim=1).view(-1,1)
#             return normalized_embedding      
#         else:
#             return embedding    


# class MLPCE(nn.Module):
#     def __init__(self, dim):
#         super().__init__()
#         self.fc1 = nn.Linear(2, 64)
#         self.fc2 = nn.Linear(64, dim * 4)
#         self.fc3 = nn.Linear(dim * 4, dim * 2)
#         self.W = nn.Linear(dim * 2, 2)

#     def forward(self, x):
#         x = F.relu(self.fc1(x))
#         x = F.relu(self.fc2(x))
#         x = F.relu(self.fc3(x))
#         logits = self.W(x)
#         return logits
# File: CNNMnist.py # Project: models # Created Date: 2021-12-16 5:11 # Author: <NAME> <EMAIL> # ----- # Last Modified: 2022-04-22 9:18 # Modified By: <NAME> <EMAIL> #  # This code is published under the MIT License. # ----- # HISTORY: # Date      	By 	Comments # ----------	---	---------------------------------------------------------- Ref: https://github.com/AshwinRJ/Federated-Learning-PyTorch/blob/master/src/models.py # class MLP(nn.Module): #     def __init__(self, dim): #         super().__init__() #         self.fc1 = nn.Linear(2, 64) #         self.fc2 = nn.Linear(64, dim * 4) #         self.fc3 = nn.Linear(dim * 4, dim * 2) #         self.feature_embedding = nn.Linear(dim * 2, dim) #     def forward(self, x, normalize=False): #         x = F.relu(self.fc1(x)) #         x = F.relu(self.fc2(x)) #         x = F.relu(self.fc3(x)) #         embedding = self.feature_embedding(x) #         if normalize: #             normalized_embedding = embedding / torch.norm(embedding, dim=1).view(-1,1) #             return normalized_embedding #         else: #             return embedding # class MLPCE(nn.Module): #     def __init__(self, dim): #         super().__init__() #         self.fc1 = nn.Linear(2, 64) #         self.fc2 = nn.Linear(64, dim * 4) #         self.fc3 = nn.Linear(dim * 4, dim * 2) #         self.W = nn.Linear(dim * 2, 2) #     def forward(self, x): #         x = F.relu(self.fc1(x)) #         x = F.relu(self.fc2(x)) #         x = F.relu(self.fc3(x)) #         logits = self.W(x) #         return logits