1 Star 0 Fork 4

kjm/ fedavg_encrypt

forked from heroding77/ fedavg_encrypt 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Models.py 5.95 KB
一键复制 编辑 原始数据 按行查看 历史
heroding77 提交于 2022-06-12 15:15 . add cifar-10 datasets
import torch
import torch.nn as nn
import torch.nn.functional as F
'''
MNIST简单全连接模型
'''
class Mnist_2NN(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(784, 200)
self.fc2 = nn.Linear(200, 200)
self.fc3 = nn.Linear(200, 10)
def forward(self, inputs):
tensor = F.relu(self.fc1(inputs))
tensor = F.relu(self.fc2(tensor))
tensor = self.fc3(tensor)
return tensor
'''
Cifar简单全连接模型
'''
class Cifar_2NN(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(3072, 200)
self.fc2 = nn.Linear(200, 200)
self.fc3 = nn.Linear(200, 10)
def forward(self, inputs):
tensor = F.relu(self.fc1(inputs))
tensor = F.relu(self.fc2(tensor))
tensor = self.fc3(tensor)
return tensor
'''
MNIST简单卷积神经网络模型
'''
class Mnist_CNN(nn.Module):
def __init__(self):
super().__init__()
# 定义每一层模型
self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=0)
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=0)
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=0)
self.fc1 = nn.Linear(3*3*64, 64)
self.fc2 = nn.Linear(64, 10)
def forward(self, inputs):
# 构造模型
tensor = inputs.view(-1, 1, 28, 28)
tensor = F.relu(self.conv1(tensor))
tensor = self.pool1(tensor)
tensor = F.relu(self.conv2(tensor))
tensor = self.pool2(tensor)
tensor = F.relu(self.conv3(tensor))
tensor = tensor.view(-1, 3*3*64)
tensor = F.relu(self.fc1(tensor))
tensor = self.fc2(tensor)
return tensor
'''
cifar简单卷积神经网络模型
'''
class Cifar_CNN(nn.Module):
def __init__(self):
super().__init__()
# 定义每一层模型
self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1)
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)
self.fc1 = nn.Linear(8*8*128, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, inputs):
# 构造模型
tensor = inputs.view(-1, 3, 32, 32)
tensor = F.relu(self.conv1(tensor))
tensor = self.pool1(tensor)
tensor = F.relu(self.conv2(tensor))
tensor = self.pool2(tensor)
tensor = F.relu(self.conv3(tensor))
# print(tensor.shape)
# raise(1)
tensor = tensor.view(-1, 8*8*128)
tensor = F.relu(self.fc1(tensor))
tensor = self.fc2(tensor)
return tensor
'''
resnet卷积神经网络模型
'''
class RestNetBasicBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride):
super(RestNetBasicBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
def forward(self, inputs):
tensor = self.conv1(inputs)
tensor = F.relu(self.bn1(tensor))
tensor = self.conv2(tensor)
tensor = self.bn2(tensor)
return F.relu(inputs + tensor)
class RestNetDownBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride):
super(RestNetDownBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride[0], padding=1)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride[1], padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
self.extra = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride[0], padding=0),
nn.BatchNorm2d(out_channels)
)
def forward(self, input):
extra_x = self.extra(input)
tensor = self.conv1(input)
tensor = F.relu(self.bn1(tensor))
tensor = self.conv2(tensor)
tensor = self.bn2(tensor)
return F.relu(extra_x + tensor)
class RestNet18(nn.Module):
def __init__(self):
super(RestNet18, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3)
self.bn1 = nn.BatchNorm2d(64)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = nn.Sequential(RestNetBasicBlock(64, 64, 1),
RestNetBasicBlock(64, 64, 1))
self.layer2 = nn.Sequential(RestNetDownBlock(64, 128, [2, 1]),
RestNetBasicBlock(128, 128, 1))
self.layer3 = nn.Sequential(RestNetDownBlock(128, 256, [2, 1]),
RestNetBasicBlock(256, 256, 1))
self.layer4 = nn.Sequential(RestNetDownBlock(256, 512, [2, 1]),
RestNetBasicBlock(512, 512, 1))
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(1, 1))
self.fc = nn.Linear(512, 10)
def forward(self, inputs):
tensor = inputs.view(-1, 3, 32, 32)
tensor = self.conv1(tensor)
tensor = self.layer1(tensor)
tensor = self.layer2(tensor)
tensor = self.layer3(tensor)
tensor = self.layer4(tensor)
tensor = self.avgpool(tensor)
tensor = tensor.reshape(tensor.shape[0], -1)
tensor = self.fc(tensor)
return tensor
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/jimmyke868/fedavg_encrypt.git
git@gitee.com:jimmyke868/fedavg_encrypt.git
jimmyke868
fedavg_encrypt
fedavg_encrypt
main

搜索帮助