Commit 92ee84d9 authored by Mira Arabi Haddad's avatar Mira Arabi Haddad
Browse files

GROUPWORK_refactoring

parent 35f3e7b2
# External
from numpy.core.shape_base import _arrays_for_stack_dispatcher
import torch
import matplotlib.pyplot as plt
import matplotlib.patches as ptc
import math
import matplotlib.pyplot as plt
# Internal
import modules as n
from train import train_model, test_model
from train import *
from net import Network
from helper import generate_disc_set
from helper import *
# autograd globally off
torch.set_grad_enabled(False)
......@@ -18,25 +18,19 @@ train_input, train_target = generate_disc_set(1000)
test_input, test_target = generate_disc_set(1000)
# normalize train and test inputs
mean, std = train_input.mean(), train_input.std()
train_input.sub_(mean).div_(std)
test_input.sub_(mean).div_(std)
# mean, std = train_input.mean(), train_input.std()
# train_input.sub_(mean).div_(std)
# test_input.sub_(mean).div_(std)
center = (0.5, 0.5)
radius = 1/math.sqrt(2*math.pi)
# network parameters
lr = 5e-3
lr = 1e-4
gamma = 0.9
mini_batch_size = 25
nb_epochs = 500
# plt.figure()
# plt.scatter(train_input[:,0], train_input[:,1], c=train_target, cmap='RdYlGn')
# circle = plt.Circle((0.5,0.5),1/math.sqrt(2*math.pi), color='black', fill=False, lw=5)
# plt.gca().add_patch(circle)
# plt.gca().set_aspect('equal', adjustable='box')
# plt.show()
# Dictionary of networks to test
networks = {
1: [
......@@ -44,6 +38,8 @@ networks = {
n.ReLU(),
n.Linear(25, 25),
n.ReLU(),
n.Linear(25, 25),
n.ReLU(),
n.Linear(25, 1),
n.Sigmoid()
],
......@@ -52,6 +48,8 @@ networks = {
n.leakyReLU(),
n.Linear(25, 25),
n.leakyReLU(),
n.Linear(25, 25),
n.leakyReLU(),
n.Linear(25, 1),
n.Sigmoid()
],
......@@ -60,19 +58,31 @@ networks = {
n.leakyReLU(),
n.Linear(25, 25),
n.leakyReLU(),
n.Linear(25, 25),
n.leakyReLU(),
n.Linear(25, 1),
n.Tanh()
],
}
# initialize a network from the networks dictionary
model = Network(networks[2], input_size=2, output_size=1, hidden=25)
# # initialize a network from the networks dictionary
model = Network(networks[1])
# train the network
nb_errors, loss_list = train_model(model, train_input, train_target, n.BCE(), mini_batch_size, nb_epochs)
optimizer = SGD(model, mini_batch_size, lr, gamma)
loss_train, acc_train = train_model(model, train_input, train_target, n.BCE(), lr, gamma, mini_batch_size, nb_epochs, optimizer=optimizer)
# test the network
nb_errors_test = test_model(model, test_input, test_target, mini_batch_size)
acc_test, output = test_model(model, test_input, test_target, mini_batch_size)
# print training and testing accuracies
print(f'acc_train = {1-nb_errors/train_input.size(0)}')
print(f'acc_test = {1-nb_errors_test/test_input.size(0)}')
print(f' train_acc = {acc_train[-1]}')
print(f' test_acc = {acc_test}')
print(output.size())
plot_figure(test_input, test_target, center=center, radius=radius)
plot_figure(test_input, output, center=center, radius=radius)
plt.plot(range(nb_epochs), loss_train)
plt.plot(range(nb_epochs), acc_train)
plt.show()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment