# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import torch
from torch import nn
import torch.nn.functional as F





class CNNCifar(nn.Module):
    def __init__(self, num_classes):
        super(CNNCifar, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, num_classes)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x



# +
net_glob = CNNCifar(num_classes = 10).to("cuda")
inp = torch.randn([1,3,32,32]).cuda()

# log_probs = net_glob(inp)
# inp = torch.randn([3,32,32]).cuda()
flops1 = FlopCountAnalysis(net_glob, inp)
print("Total FLOPs: " + str(flops1.total()))
print(flop_count_table(flops1))
# -



class MLP_from_CNN(nn.Module):
    def __init__(self):
        super(MLP_from_CNN, self).__init__()
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
    def forward(self, x):
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x    


net_glob = MLP_from_CNN().to("cuda")
inp = torch.randn(400,1).cuda()
flops1 = FlopCountAnalysis(net_glob, inp)
print(flops1.total())
print(flop_count_table(flops1))


# +
class CNNCifar2(nn.Module):
    def __init__(self, num_classes):
        super(CNNCifar2, self).__init__()
        self.conv1 = nn.Conv2d(3, 5, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(5, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, num_classes)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x
net_glob = CNNCifar2(num_classes = 10).to("cuda")
inp = torch.randn([1,3,32,32]).cuda()

# log_probs = net_glob(inp)
# inp = torch.randn([3,32,32]).cuda()
flops1 = FlopCountAnalysis(net_glob, inp)
print("Total FLOPs: " + str(flops1.total()))
print(flop_count_table(flops1))
# -









class MLP(nn.Module):
    def __init__(self, dim_in, dim_hidden, dim_out):
        super(MLP, self).__init__()
        self.layer_input = nn.Linear(dim_in, dim_hidden)
        self.relu = nn.ReLU()
        self.dropout = nn.Dropout()
        self.layer_hidden = nn.Linear(dim_hidden, dim_out)

    def forward(self, x):
        x = x.view(-1, x.shape[1]*x.shape[-2]*x.shape[-1])
        x = self.layer_input(x)
        x = self.dropout(x)
        x = self.relu(x)
        x = self.layer_hidden(x)
        return x



from fvcore.nn import FlopCountAnalysis
from  fvcore.nn import flop_count_table

net_glob = MLP(dim_in=784, dim_hidden= 0, dim_out=10).to("cuda")
inp = torch.randn(784,1).cuda()
flops1 = FlopCountAnalysis(net_glob, inp)
flops1.total()

print(flop_count_table(flops1))

n1 = MLP(dim_in=784, dim_hidden=150, dim_out=10).to("cuda")
inp = torch.randn(784,1).cuda()
flops2 = FlopCountAnalysis(n1, inp)
flops2.total()

#GlobalNet 200 neurons in hidden layer
print(flop_count_table(flops1))

#N2 N3 N4 150 neurons in hidden layer
print(flop_count_table(flops2))





setting_array = [8,0,0,2]

# +
a = 10 * setting_array[0]
b = a + 10 * setting_array[1]
c = b + 10 * setting_array[2]


print(0,a)
print(a,b)
print(b,c)
print(d,100)
# -

setting = str(setting_array).replace(",","").replace(" ","").replace("[","").replace("]","")

with open('temp.txt', 'a+') as f:
    print('printing to a file.', file=f)



import winsound

duration = 1000  # milliseconds
freq = 940  # Hz
winsound.Beep(freq, 500)
winsound.Beep(38, 500)
winsound.Beep(freq, 500)
winsound.Beep(38, 500)
winsound.Beep(freq, 500)
winsound.Beep(38, 500)
winsound.Beep(freq, 500)
winsound.Beep(38, 500)
winsound.Beep(freq, 500)
winsound.Beep(38, 500)
winsound.Beep(freq, 500)
winsound.Beep(38, 500)








import os

dir = './save/non-iid-mnist/Update_every_round'
os.listdir(dir)

ll = list(os.listdir(dir))

ll

for e in ll:
    if e.endswith('.txt'):
        id = (e.split('_')[-1][0:4])
        with open(dir +'/' + e) as f:
#             print(e)
            print(id)
            for line in f:
                if line.startswith("Tr"):
#                     print(line)
#                     print(line[-6:-1])
#                     print("Testing Accuracy: "+line[-6:-1])
                    print("Training Accuracy: "+line[-6:-1])
#                     pass



StarcoderdataJupyterScriptsDedupFiltered