Escolar Documentos
Profissional Documentos
Cultura Documentos
ipynb - Colaboratory
Ano: 2023
Observações: Ao avaliar o impacto das técnicas é importante deixar a rede neural com a mesma
arquitetura, ou seja, com o mesmo tamanho, quantidade de neurônios e camadas. Assim, ficará
mais justo a observação do impacto das técnicas nas redes neurais. Também, para cada
questão utilize um problema diferente. E, em cada questão, deixe um bloco para o código da
arquitetura, outro com o gráfico dos resultados e por fim seu comentário sobre o
comportamento observado. Gere um PDF e envie no classroom.
keyboard_arrow_down Importações
import os
os.makedirs('utils_ex', exist_ok=True)
!wget -q https://github.com/mafaldasalomao/pavic_treinamento_ml/raw/main/utils/plot.py -O
!wget -q https://github.com/mafaldasalomao/pavic_treinamento_ml/raw/main/utils/samples_ge
import numpy as np
import _pickle as pkl
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.datasets import make_blobs, make_circles, make_moons, make_classification
%matplotlib inline
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 1/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
# Auxiliar
def softmax(x, y_oh=None, derivative=False):
if derivative:
y_pred = softmax(x)
k = np.nonzero(y_pred * y_oh)
pk = y_pred[k]
y_pred[k] = pk * (1.0 - pk)
return y_pred
exp = np.exp(x)
return exp / np.sum(exp, axis=1, keepdims=True)
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 2/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
##### Regressão
def mae(y, y_pred, derivative=False):
if derivative:
return np.where(y_pred > y, 1, -1) / y.shape[0]
return np.mean(np.abs(y - y_pred))
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 3/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
#Inicialização de pesos
def zeros(rows, cols): #in, out
return np.zeros((rows, cols))
keyboard_arrow_down Regularização
def l1_regularization(weights, derivative=False):
if derivative:
weights = [np.where(w < 0, -1, w) for w in weights]
return np.array([np.where(w > 0, 1, w) for w in weights])
return np.sum([np.sum(np.abs(w)) for w in weights])
def l2_regularization(weights, derivative=False):
if derivative:
return weights
return 0.5 * np.sum(weights**2)
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 4/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
if is_training:
layer._pop_mean = layer.bn_decay * layer._pop_mean + (1.0 - layer.bn_decay) * mu
layer._pop_var = layer.bn_decay * layer._pop_var + (1.0 - layer.bn_decay) * var
layer._bn_cache = (x, x_norm, mu, var)
return out
m = layer._activ_inp.shape[0]
x_mu = x - mu
std_inv = 1. / np.sqrt(var + 1e-8)
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 5/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
class Layer():
def __init__(self, input_dim, output_dim,
weights_initializer=random_normal,
bias_initializer=ones, dropout_prob=0,
reg_func=l2_regularization,
reg_strength=0.0,
batch_norm=False,
bn_decay=0.9,
is_trainable=True,
activation=linear):
self.input = None
self.weights = weights_initializer(output_dim, input_dim)
self.biases = bias_initializer(1, output_dim)
self.activation = activation
self.dropout_prob = dropout_prob
self.reg_func = reg_func
self.reg_strength = reg_strength
self._dropout_mask = None
self._activ_inp, self._activ_out = None, None
self._dweights, self._dbiases, self._prev_dweights = None, None, 0.0
self.is_trainable = is_trainable
self.batch_norm = batch_norm
self.bn_decay = bn_decay
self.gamma, self.betta = ones(1, output_dim), zeros(1, output_dim)
class NeuralNetwork():
def __init__(self, cost_func=mse, learning_rate=1e-3,
lr_decay_method=none_decay,
lr_decay_rate=0.0,
lr_decay_steps=1,
patience=np.inf,
momentum=0.0):
self.layers = []
self.cost_func = cost_func
self.momentum = momentum
self.learning_rate = self.lr_initial = learning_rate
self.lr_decay_method = lr_decay_method
self.lr_decay_rate = lr_decay_rate
self.lr_decay_steps = lr_decay_steps
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 7/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
if epoch % verbose == 0:
loss_train = self.cost_func(y_train, self.predict(x_train))
loss_reg = (1.0 / y_train.shape[0]) * np.sum([layer.reg_strength * layer.reg_func
print("Epoch: {0:=4}/{1} loss_train: {2:.8f}+{3:.8f} = {4:.8f} loss_val= {5:.8f}"
#Save model
def save(self, filepath):
pkl.dump(self, open(filepath, 'wb'), -1)
#Load model
def load(filepath):
return pkl.load(open(filepath, 'rb'))
if layer.batch_norm:
layer.gamma = layer.gamma - self.learning_rate * layer._dgamma
layer.betta = layer.betta - self.learning_rate * layer._dbetta
keyboard_arrow_down Questões
Questão 01 - Com suas palavras defina: Overfitting, underfitting, vetor gradiente, taxa de
aprendizado, épocas, camadas, neurônio, função de custo e função de ativação.
Resp. 1:
Overfitting: é quando o modelo é maior e os dados são menores. O modelo se ajusta acima do
necessário, não sendo capaz portanto de generalizar o problema para quando houver novos
exemplos de entrada. Portanto o acerto tende a ser exageradamente grande no treino, mas
tende a falhar para novos dados.
Underfitting: é quando o modelo é menor e os dados são maiores. Neste sentido, o modelo
tende a modelar o problema de forma muito errônea, por que o seu ajuste não é capaz de
satisfazer a complexidade do problema. Tende a errar muito no treino, e o acerto no teste tende
a ser maior, por que a liberdade para os dados satisfezerem o modelo é maior (o que não é
conveniente para o aprendizado da máquina).
Vetor gradiente: é um vetor que nos dá a direção para que o ponto na curva da função de custo
deve ser deslocada, no sentido de achar o menor nível. O vetor gradiente é normal em relação ao
ponto na curva, e o seu valor negativo nos diz a direção para onde o erro da máquina aprendiz é
minimizado.
Taxa de aprendizado: é o tamanho da passada que o algoritmo deve dar repetidamente até
chegar no resultado ideal. Quando o tamanho da passada é pequeno, muitas iterações
(passadas) são necessárias pra chegar ao resultado. Quando a passada é muito grande chega-
se ao resultado mais rápido, mas perde-se na precisão do modelo, devido ao risco do tamanho
do passo incidir na extrapolação do valor ideal.
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 9/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
pesos (se houver), configura uma época. A época configura uma iteração por um conjunto de
indivíduos OU pacotes OU mini-pacotes, obtidos pela rede até a devolução de um resultado ou
atualizações de pesos. Geralmente itera-se sucessivas vezes suas operações até que uma
pausa definida seja alcançada ou até que um valor satisfaça o pretendido.
Neurônio: é uma estrutura em uma rede neural usado para armazenar indíviduos ou resultados
de operações entre pesos e indivíduos na rede.
Função de custo: é a função que ajuda o aprendizado do algoritmo, calculando o quanto que o
resultado se afasta do valor real.
Questão 02 - Construa uma rede neural totalmente conectada, treine-a e teste-a com pelo
menos dois métodos de inicialização de pesos diferentes. Em seguida, comente o que foi
possível observar de diferente entre os métodos escolhidos.
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 10/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
plt.subplot(1, 3, 1)
plt.scatter(x, y)
# train
nn.fit(x, y, epochs=5000, verbose=500)
# plot
plt.subplot(1, 3, 2)
plt.scatter(x, y)
plt.title('RANDOM NORMAL')
plt.plot(x, nn.predict(x), c='purple');
print('#'*20)
##### ONES
nn = NeuralNetwork(cost_func=mse, learning_rate=0.1)
nn.layers.append(Layer(input_dim=input_dim, output_dim=10, weights_initializer=ones, acti
nn.layers.append(Layer(10, 10, weights_initializer=ones, activation=sigmoid))
nn.layers.append(Layer(10, output_dim, weights_initializer=ones, activation=linear))
# train
nn.fit(x, y, epochs=5000, verbose=500)
# plot
plt.subplot(1, 3, 3)
plt.scatter(x, y)
plt.title('ONES')
plt.plot(x, nn.predict(x), c='purple');
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 11/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
(100, 1) (100, 1)
Epoch: 0/5000 loss_train: 52.02215732+0.00000000 = 52.02215732 loss_val= 52.022157
Epoch: 500/5000 loss_train: 11.58474496+0.00000000 = 11.58474496 loss_val= 11.584744
Epoch: 1000/5000 loss_train: 3.50942976+0.00000000 = 3.50942976 loss_val= 3.50942976
Epoch: 1500/5000 loss_train: 2.62644518+0.00000000 = 2.62644518 loss_val= 2.62644518
Epoch: 2000/5000 loss_train: 2.21694160+0.00000000 = 2.21694160 loss_val= 2.21694160
Epoch: 2500/5000 loss_train: 1.93857296+0.00000000 = 1.93857296 loss_val= 1.93857296
Epoch: 3000/5000 loss_train: 1.76738271+0.00000000 = 1.76738271 loss_val= 1.76738271
Epoch: 3500/5000 loss_train: 1.66114758+0.00000000 = 1.66114758 loss_val= 1.66114758
Epoch: 4000/5000 loss_train: 1.59073900+0.00000000 = 1.59073900 loss_val= 1.59073900
Epoch: 4500/5000 loss_train: 1.54010967+0.00000000 = 1.54010967 loss_val= 1.54010967
Epoch: 5000/5000 loss_train: 1.49754506+0.00000000 = 1.49754506 loss_val= 1.49754506
####################
Epoch: 0/5000 loss_train: 51.85490543+0.00000000 = 51.85490543 loss_val= 51.854905
Epoch: 500/5000 loss_train: 51.27017807+0.00000000 = 51.27017807 loss_val= 51.270178
Epoch: 1000/5000 loss_train: 51.27013299+0.00000000 = 51.27013299 loss_val= 51.270132
Epoch: 1500/5000 loss_train: 51.27008458+0.00000000 = 51.27008458 loss_val= 51.270084
Epoch: 2000/5000 loss_train: 51.27003147+0.00000000 = 51.27003147 loss_val= 51.270031
Epoch: 2500/5000 loss_train: 51.26997194+0.00000000 = 51.26997194 loss_val= 51.269971
Epoch: 3000/5000 loss_train: 51.26990371+0.00000000 = 51.26990371 loss_val= 51.269903
Epoch: 3500/5000 loss_train: 51.26982366+0.00000000 = 51.26982366 loss_val= 51.269823
Epoch: 4000/5000 loss_train: 51.26972738+0.00000000 = 51.26972738 loss_val= 51.269727
Epoch: 4500/5000 loss_train: 51.26960831+0.00000000 = 51.26960831 loss_val= 51.269608
Epoch: 5000/5000 loss_train: 51.26945633+0.00000000 = 51.26945633 loss_val= 51.269456
Acima pode-se ver exemplos de treinamento de duas redes semelhantes com uma inicializando
pesos de forma aleatória e a outra rede iniciando pesos com 1's. A inicialização aleatória trouxe
melhor resultado devido a capacidade dos pesos fornecerem mais liberdade para o modelo
realizar os ajustes nos dados.
Questão 03 - Construa uma rede neural totalmente conectada, treine-a e teste-a com
diferentes camadas de ativação. Em seguida, comente o que foi possível observar de diferente
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 12/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
# Gerando dados
x, y = make_log10(n_samples=100, x_min=1, x_max=100, noise=0.3)
minmax = MinMaxScaler(feature_range=(-1, 1))
x = minmax.fit_transform(x.astype(np.float64))
plt.figure(figsize=(12, 6))
plt.subplot(1,3,1)
plt.title('LOG-10')
plt.scatter(x, y);
# SIGMOID
nn = NeuralNetwork(cost_func=mae, learning_rate=1e-1)
# train
nn.fit(x, y, epochs=5000, verbose=500)
# plot
plt.subplot(1, 3, 2)
plt.scatter(x, y)
plt.title('SIGMOID')
plt.plot(x, nn.predict(x), c='purple');
print('#'*20)
nn = NeuralNetwork(cost_func=mae, learning_rate=1e-1)
# train
nn.fit(x, y, epochs=5000, verbose=500)
# plot
plt.subplot(1, 3, 3)
plt.scatter(x, y)
plt.title('TANH')
plt.plot(x, nn.predict(x), c='purple');
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 13/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 14/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
Questão 04 - Construa uma rede neural totalmente conectada, treine-a e teste-a sem/com pelo
menos um dos métodos de regularização (L1, L2). Em seguida, comente o que foi possível
observar de diferente entre uma rede com método de regularização e outra sem (mantenha a
mesma quantidade de camadas).
print(x.shape, y.shape)
(1000, 2) (1000, 1)
plt.figure(figsize=(12, 6))
plt.subplot(1,2,1)
plt.scatter(x[:, 0], x[:,1])
plt.subplot(1,2,2)
plt.scatter(x[:, 0], x[:,1], c=list(np.array(y).ravel()), s=15, cmap='bwr')
<matplotlib.collections.PathCollection at 0x7efc5bd8db10>
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 15/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
import numpy as np
import matplotlib.pyplot as plt
def __softmax(x):
exp = np.exp(x)
return exp / np.sum(exp, axis=1, keepdims=True)
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 16/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
plt.figure(figsize=(12,6))
########################## NO REG
print("No regularization")
nn = NeuralNetwork(cost_func=binary_cross_entropy, learning_rate=0.1)
nn.layers.append(Layer(input_dim, 4, activation=tanh))
nn.layers.append(Layer(4, output_dim, activation=sigmoid))
y_pred = nn.predict(x)
print("")
acc = 100*accuracy_score(y, y_pred >= 0.5)
print("Acc: {:.2f}%".format(acc))
# plt.subplot(1,3,1)
classification_predictions_custom(x, y, is_binary=True, nn=nn, threshold=0.5, cmap='bwr',
########################## REG L1
print("Reg. L1")
nn = NeuralNetwork(cost_func=binary_cross_entropy, learning_rate=0.1)
nn.layers.append(Layer(input_dim, 4, reg_strength=1, reg_func=l1_regularization, activati
nn.layers.append(Layer(4, output_dim, reg_strength=1, reg_func=l1_regularization, activat
y_pred = nn.predict(x)
print("")
acc = 100*accuracy_score(y, y_pred >= 0.5)
print("Acc: {:.2f}%".format(acc))
# plt.subplot(1,3,2)
classification_predictions_custom(x, y, is_binary=True, nn=nn, threshold=0.5, title="L1",
########################## REG L2
print("Reg. L2")
nn = NeuralNetwork(cost_func=binary_cross_entropy, learning_rate=0.1)
nn.layers.append(Layer(input_dim, 4, reg_strength=1, reg_func=l1_regularization, activati
nn.layers.append(Layer(4, output_dim, reg_strength=1, reg_func=l1_regularization, activat
y_pred = nn.predict(x)
print("")
acc = 100*accuracy_score(y, y_pred >= 0.5)
print("Acc: {:.2f}%".format(acc))
# plt.subplot(1,3,3)
classification_predictions_custom(x, y, is_binary=True, nn=nn, threshold=0.5, title="L2",
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 17/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
No regularization
Epoch: 0/5000 loss_train: 1.25499428+0.00000000 = 1.25499428 loss_val= 1.25499428
Epoch: 300/5000 loss_train: 0.50035096+0.00000000 = 0.50035096 loss_val= 0.50035096
Epoch: 600/5000 loss_train: 0.47104859+0.00000000 = 0.47104859 loss_val= 0.47104859
Epoch: 900/5000 loss_train: 0.45824521+0.00000000 = 0.45824521 loss_val= 0.45824521
Epoch: 1200/5000 loss_train: 0.44121130+0.00000000 = 0.44121130 loss_val= 0.44121130
Epoch: 1500/5000 loss_train: 0.32592766+0.00000000 = 0.32592766 loss_val= 0.32592766
Epoch: 1800/5000 loss_train: 0.17397664+0.00000000 = 0.17397664 loss_val= 0.17397664
Epoch: 2100/5000 loss_train: 0.12822026+0.00000000 = 0.12822026 loss_val= 0.12822026
Epoch: 2400/5000 loss_train: 0.10520166+0.00000000 = 0.10520166 loss_val= 0.10520166
Epoch: 2700/5000 loss_train: 0.09066580+0.00000000 = 0.09066580 loss_val= 0.09066580
Epoch: 3000/5000 loss_train: 0.08066445+0.00000000 = 0.08066445 loss_val= 0.08066445
Epoch: 3300/5000 loss_train: 0.07338149+0.00000000 = 0.07338149 loss_val= 0.07338149
Epoch: 3600/5000 loss_train: 0.06784440+0.00000000 = 0.06784440 loss_val= 0.06784440
Epoch: 3900/5000 loss_train: 0.06348810+0.00000000 = 0.06348810 loss_val= 0.06348810
Epoch: 4200/5000 loss_train: 0.05996379+0.00000000 = 0.05996379 loss_val= 0.05996379
Epoch: 4500/5000 loss_train: 0.05704555+0.00000000 = 0.05704555 loss_val= 0.05704555
Epoch: 4800/5000 loss_train: 0.05458118+0.00000000 = 0.05458118 loss_val= 0.05458118
Acc: 99.20%
Reg. L1
Epoch: 0/5000 loss_train: 1.16225572+0.01135547 = 1.17361119 loss_val= 1.16225572
Epoch: 300/5000 loss_train: 0.56666175+0.01129756 = 0.57795931 loss_val= 0.56666175
Epoch: 600/5000 loss_train: 0.35651010+0.01705934 = 0.37356943 loss_val= 0.35651010
Epoch: 900/5000 loss_train: 0.22100639+0.02180753 = 0.24281392 loss_val= 0.22100639
Epoch: 1200/5000 loss_train: 0.15758751+0.02506111 = 0.18264862 loss_val= 0.15758751
Epoch: 1500/5000 loss_train: 0.12403943+0.02735860 = 0.15139803 loss_val= 0.12403943
Epoch: 1800/5000 loss_train: 0.10394831+0.02910691 = 0.13305522 loss_val= 0.10394831
Epoch: 2100/5000 loss_train: 0.09076833+0.03048246 = 0.12125079 loss_val= 0.09076833
Epoch: 2400/5000 loss_train: 0.08151950+0.03159980 = 0.11311930 loss_val= 0.08151950
Epoch: 2700/5000 loss_train: 0.07468847+0.03252880 = 0.10721728 loss_val= 0.07468847
Epoch: 3000/5000 loss_train: 0.06943716+0.03331523 = 0.10275238 loss_val= 0.06943716
Epoch: 3300/5000 loss_train: 0.06526839+0.03399044 = 0.09925883 loss_val= 0.06526839
Epoch: 3600/5000 loss_train: 0.06187063+0.03457694 = 0.09644757 loss_val= 0.06187063
Epoch: 3900/5000 loss_train: 0.05903965+0.03509113 = 0.09413078 loss_val= 0.05903965
Epoch: 4200/5000 loss_train: 0.05663660+0.03554562 = 0.09218222 loss_val= 0.05663660
Epoch: 4500/5000 loss_train: 0.05456406+0.03595011 = 0.09051417 loss_val= 0.05456406
Epoch: 4800/5000 loss_train: 0.05275187+0.03631211 = 0.08906399 loss_val= 0.05275187
Acc: 99.30%
Reg. L2
Epoch: 0/5000 loss_train: 0.83712390+0.01131259 = 0.84843648 loss_val= 0.83712390
Epoch: 300/5000 loss_train: 0.51195874+0.01297594 = 0.52493468 loss_val= 0.51195874
Epoch: 600/5000 loss_train: 0.47224830+0.01509715 = 0.48734545 loss_val= 0.47224830
Epoch: 900/5000 loss_train: 0.45692211+0.01639424 = 0.47331635 loss_val= 0.45692211
Epoch: 1200/5000 loss_train: 0.44806017+0.01735986 = 0.46542003 loss_val= 0.44806017
Epoch: 1500/5000 loss_train: 0.44203160+0.01814479 = 0.46017639 loss_val= 0.44203160
Epoch: 1800/5000 loss_train: 0.43764067+0.01880240 = 0.45644307 loss_val= 0.43764067
Epoch: 2100/5000 loss_train: 0.43429940+0.01937348 = 0.45367288 loss_val= 0.43429940
Epoch: 2400/5000 loss_train: 0.43166165+0.01991236 = 0.45157401 loss_val= 0.43166165
Epoch: 2700/5000 loss_train: 0.42946812+0.02040003 = 0.44986815 loss_val= 0.42946812
Epoch: 3000/5000 loss_train: 0.42754075+0.02084857 = 0.44838932 loss_val= 0.42754075
Epoch: 3300/5000 loss_train: 0.42573809+0.02126642 = 0.44700451 loss_val= 0.42573809
Epoch: 3600/5000 loss_train: 0.42391697+0.02166277 = 0.44557974 loss_val= 0.42391697
Epoch: 3900/5000 loss_train: 0.42186947+0.02204725 = 0.44391672 loss_val= 0.42186947
Comentário: Podemos perceber
Epoch: 4200/5000 nos resultados
loss_train: acima, para separação
0.41909523+0.02243179 de círculos,
= 0.44152702 loss_val=a 0.41909523
Epoch: 4500/5000
regularização loss_train:
L1 performando 0.41354388+0.02283853
melhor em relação às técnicas= com
0.43638241
ausêncialoss_val= 0.41354388
de regularização e
Epoch: 4800/5000 loss_train: 0.38809625+0.02335534 = 0.41145159 loss_val= 0.38809625
regularização L2. Obtendo uma acurácia de 99.3% (L2). Porém, devido ao crescimento da
Acc: 85.20%
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 18/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
acurácia não ter sido tão significativa, custando em desempenho para o algoritmo, a técnica
sem regularização se mostra mais viável para este caso, com 99.2% de acurácia (NO REG).
Questão 05 - Construa uma rede neural totalmente conectada, treine-a e teste-a com
tamanhos de batchs diferentes. Em seguida, comente o comportamento da rede com
diferentes batchs sizes.
print(x.shape, y.shape)
plt.scatter(x, y)
(1000, 1) (1000, 1)
<matplotlib.collections.PathCollection at 0x7efc4c0cac50>
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 19/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
plt.figure(figsize=(12, 6))
plt.subplot(1,5,1)
plt.title('LOG-10')
plt.scatter(x, y);
# SIGMOID
nn = NeuralNetwork(cost_func=mae, learning_rate=1e-1)
# train
nn.fit(x, y, epochs=5000, verbose=500)
# plot
plt.subplot(1, 5, 2)
plt.scatter(x, y)
plt.title('LOG-10')
plt.plot(x, nn.predict(x), c='purple');
# train
nn.fit(x, y, epochs=5000, verbose=500, batch_size=10)
# plot
plt.subplot(1, 5, 3)
plt.scatter(x, y)
plt.title('LOG-10 BATCHSIZE-10')
plt.plot(x, nn.predict(x), c='purple');
# train
nn.fit(x, y, epochs=5000, verbose=500, batch_size=20)
# plot
plt.subplot(1, 5, 4)
plt.scatter(x, y)
plt.title('LOG-10 BATCHSIZE-20')
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 20/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
# train
nn.fit(x, y, epochs=5000, verbose=500, batch_size=200)
# plot
plt.subplot(1, 5, 5)
plt.scatter(x, y)
plt.title('LOG-10 BATCHSIZE-200')
plt.plot(x, nn.predict(x), c='purple');
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 21/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 22/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
(200, 2) (200, 1)
<matplotlib.collections.PathCollection at 0x7efc4b92e350>
y_pred = nn.predict(x)
plt.figure(figsize=(12, 6))
#plt.subplot(1, 2, 1)
classification_predictions_custom(x, y, is_binary=True, nn=nn, threshold=0.5, cmap='bwr',
y_pred = nn.predict(x)
#plt.subplot(1, 2, 2)
classification_predictions_custom(x, y, is_binary=True, nn=nn, threshold=0.5, cmap='bwr',
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 23/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
x, y = make_moons(1000, noise=0.20)
y = y.reshape(-1, 1)
print(x.shape, y.shape)
plt.scatter(x[:,0], x[:,1], c=list(np.array(y).ravel()), s=15, cmap='bwr')
(1000, 2) (1000, 1)
<matplotlib.collections.PathCollection at 0x7efc4c2efd60>
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 25/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
y_pred = nn.predict(x)
plt.figure(figsize=(12, 6))
#plt.subplot(1, 2, 1)
classification_predictions_custom(x, y, is_binary=True, nn=nn, threshold=0.5, cmap='bwr',
y_pred = nn.predict(x)
#plt.subplot(1, 2, 2)
classification_predictions_custom(x, y, is_binary=True, nn=nn, threshold=0.5, cmap='bwr',
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 26/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 27/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
x, y = make_blobs(n_samples=400, n_features=2, centers=[(-3, 0), (3, 0), (0, 3), (0, -3)]
y = y.reshape(-1, 1)
print(x.shape, y.shape)
plt.scatter(x[:,0], x[:,1], c=list(np.array(y).ravel()), s=15, cmap=plt.cm.viridis)
onehot = OneHotEncoder(sparse=False)
y_onehot = onehot.fit_transform(y)
print(y_onehot[::70])
(400, 2) (400, 1)
[[0. 0. 1. 0.]
[1. 0. 0. 0.]
[0. 0. 1. 0.]
[1. 0. 0. 0.]
[0. 0. 0. 1.]
[1. 0. 0. 0.]]
/usr/local/lib/python3.10/dist-packages/sklearn/preprocessing/_encoders.py:868: Futur
warnings.warn(
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 28/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
plt.figure(figsize=(12, 6))
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 29/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
Questão 09 - Construa uma rede neural totalmente conectada, treine-a e teste-a sem/com
momentum. Em seguida, comente o que foi possível observar de diferente entre uma rede com
momentum e outra sem (mantenha a mesma quantidade de camadas).
print(x.shape, y.shape)
plt.scatter(x[:,0], x[:,1], c=list(np.array(y).ravel()), s=15, cmap=plt.cm.viridis)
onehot = OneHotEncoder(sparse=False)
y_onehot = onehot.fit_transform(y)
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 30/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
(100, 2) (100, 1)
/usr/local/lib/python3.10/dist-packages/sklearn/preprocessing/_encoders.py:868: Futur
warnings.warn(
plt.figure(figsize=(12, 6))
#BUILD
nn = NeuralNetwork(cost_func=softmax_neg_log_likelihood, learning_rate=1e-2)
nn.layers.append(Layer(input_dim, 30,dropout_prob=0.7, activation=relu))
nn.layers.append(Layer(30, 20,dropout_prob=0.7, activation=relu))
nn.layers.append(Layer(20, output_dim, activation=linear))
#TRAIN
nn.fit(x, y_onehot, epochs=10000, verbose=1000)
#EVALUATION
y_pred = np.argmax(nn.predict(x), axis=1)
#BUILD
nn = NeuralNetwork(cost_func=softmax_neg_log_likelihood, learning_rate=1e-2, momentum=0.4
nn.layers.append(Layer(input_dim, 30,dropout_prob=0.7, activation=relu))
nn.layers.append(Layer(30, 20,dropout_prob=0.7, activation=relu))
nn.layers.append(Layer(20, output_dim, activation=linear))
#TRAIN
nn.fit(x, y_onehot, epochs=10000, verbose=1000)
#EVALUATION
y_pred = np.argmax(nn.predict(x), axis=1)
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 31/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 32/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
atingiu uma acurácia de 88%. O momentum aplica uma "força" na direção da curva de erro
durante o processo de aprendizado, ajudando o aprendiz a se ajustar mais rápido.
Questão 10 - Construa uma rede neural totalmente conectada, treine-a e teste-a sem/com
early stopping. Em seguida, comente o impacto causado por early stopping e sua utilidade.
print(x.shape, y.shape)
plt.scatter(x[:,0], x[:,1], c=list(np.array(y).ravel()), s=15, cmap=plt.cm.viridis)
(1000, 2) (1000, 1)
<matplotlib.collections.PathCollection at 0x7fbcc84d2ce0>
onehot = OneHotEncoder(sparse=False)
y_onehot = onehot.fit_transform(y)
/usr/local/lib/python3.10/dist-packages/sklearn/preprocessing/_encoders.py:868: Futur
warnings.warn(
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 33/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
#BUILD
nn = NeuralNetwork(cost_func=softmax_neg_log_likelihood, learning_rate=1e-2, momentum=0.2
nn.layers.append(Layer(input_dim, 30,dropout_prob=0.7, activation=tanh))
nn.layers.append(Layer(30, 20,dropout_prob=0.7, activation=relu))
nn.layers.append(Layer(20, output_dim, activation=linear))
#TRAIN
nn.fit(x, y_onehot, epochs=10000, verbose=1000)
#EVALUATION
y_pred = np.argmax(nn.predict(x), axis=1)
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 34/35
14/12/2023, 21:48 Relatorio_ML_PAVIC_Wallison.ipynb - Colaboratory
#BUILD
nn = NeuralNetwork(cost_func=softmax_neg_log_likelihood, learning_rate=1e-2, momentum=0.2
nn.layers.append(Layer(input_dim, 30,dropout_prob=0.7, activation=tanh))
nn.layers.append(Layer(30, 20,dropout_prob=0.7, activation=relu))
nn.layers.append(Layer(20, output_dim, activation=linear))
#TRAIN
nn.fit(x, y_onehot, epochs=10000, verbose=1000)
#EVALUATION
y_pred = np.argmax(nn.predict(x), axis=1)
https://colab.research.google.com/drive/1o0CBMEn3IFfrskD1t4FTzz-JIPgIv7_l#scrollTo=wxqeRBU1WlVJ&printMode=true 35/35