In [ ]:
import numpy as np
import _pickle as pkl
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.datasets.samples_generator import make_blobs, make_circles, make_moons, make_classification
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder
from utils import plot
from utils.samples_generator import make_spiral, make_square, make_cubic, make_exp, make_log10
%matplotlib inline
In [ ]:
def linear(x, derivative=False):
return np.ones_like(x) if derivative else x
def sigmoid(x, derivative=False):
if derivative:
y = sigmoid(x)
return y*(1 - y)
return 1.0/(1.0 + np.exp(-x))
def tanh(x, derivative=False):
if derivative:
y = tanh(x)
return 1 - y**2
return (np.exp(x) - np.exp(-x))/(np.exp(x) + np.exp(-x))
def relu(x, derivative=False):
if derivative:
return np.where(x <= 0, 0, 1)
return np.maximum(0, x)
def leaky_relu(x, derivative=False):
alpha = 0.1
if derivative:
return np.where(x <= 0, alpha, 1)
return np.where(x <= 0, alpha*x, x)
def elu(x, derivative=False):
alpha = 1.0
if derivative:
y = elu(x)
return np.where(x <= 0, y + alpha, 1)
return np.where(x <= 0, alpha*(np.exp(x) - 1), x)
In [ ]:
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)
In [ ]:
In [ ]:
In [ ]:
def neg_log_likelihood(y_oh, y_pred, derivative=False):
k = np.nonzero(y_pred * y_oh)
pk = y_pred[k]
if derivative:
y_pred[k] = (-1.0 / pk)
return y_pred
return np.mean(-np.log(pk))
def softmax_neg_log_likelihood(y_oh, y_pred, derivative=False):
y_softmax = softmax(y_pred)
if derivative:
return -(y_oh - y_softmax) / y_oh.shape[0]
return neg_log_likelihood(y_oh, y_softmax)
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
x = np.array([[0.05, 0.10]])
y = np.array([[0.01, 0.99]])
w1 = np.array([[0.15, 0.20], [0.25, 0.30]])
b1 = np.array([[0.35]])
w2 = np.array([[0.40, 0.45], [0.50, 0.55]])
b2 = np.array([[0.60]])
# insira sua rede aqui!
In [ ]:
x = np.array([[0.1, 0.2, 0.7]])
y = np.array([[1, 0, 0]])
D_in, D_out = x.shape[1], y.shape[1]
w1 = np.array([[0.1, 0.2, 0.3], [0.3, 0.2, 0.7], [0.4, 0.3, 0.9]])
b1 = np.ones((1,3))
w2 = np.array([[0.2, 0.3, 0.5], [0.3, 0.5, 0.7], [0.6, 0.4, 0.8]])
b2 = np.ones((1,3))
w3 = np.array([[0.1, 0.4, 0.8], [0.3, 0.7, 0.2], [0.5, 0.2, 0.9]])
b3 = np.ones((1,3))
# insira sua rede aqui!
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
data = np.loadtxt('data/medidas.csv', delimiter=',', skiprows=1)
print(data.shape)
x, y = data[:,0].reshape(-1,1), data[:,1].reshape(-1,1)
print(x.shape, y.shape)
plt.scatter(x, y)
In [ ]:
minmax = MinMaxScaler(feature_range=(-1, 1))
x = minmax.fit_transform(x.astype(np.float64))
print(x.min(), x.max())
plt.scatter(x, y)
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.scatter(x, y)
plt.plot(x, nn.predict(x), c='green')
In [ ]:
data = np.loadtxt('data/notas.csv', delimiter=',', skiprows=1)
print(data.shape)
x, y = data[:,:-1], data[:,-1].reshape(-1,1)
print(x.shape, y.shape)
In [ ]:
minmax = MinMaxScaler(feature_range=(-1, 1))
x = minmax.fit_transform(x.astype(np.float64))
print(x.min(axis=0), x.max(axis=0))
plt.scatter(x, y)
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
In [ ]:
x, y = make_square(n_samples=100, x_min=-10, x_max=10, a=1, b=1, c=1, noise=10)
print(x.shape, y.shape)
plt.scatter(x, y)
In [ ]:
minmax = MinMaxScaler(feature_range=(-1, 1))
x = minmax.fit_transform(x.astype(np.float64))
print(x.min(axis=0), x.max(axis=0))
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.scatter(x, y)
plt.plot(x, nn.predict(x), c='green')
In [ ]:
x, y = make_cubic(n_samples=100, x_min=-4, x_max=4, a=1, b=0, c=-10, d=0, noise=3)
print(x.shape, y.shape)
plt.scatter(x, y)
In [ ]:
minmax = MinMaxScaler(feature_range=(-1, 1))
x = minmax.fit_transform(x.astype(np.float64))
print(x.min(axis=0), x.max(axis=0))
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.scatter(x, y)
plt.plot(x, nn.predict(x), c='green')
In [ ]:
x, y = make_log10(n_samples=100, x_min=1, x_max=100, noise=0.3)
print(x.shape, y.shape)
plt.scatter(x, y)
In [ ]:
minmax = MinMaxScaler(feature_range=(-1, 1))
x = minmax.fit_transform(x.astype(np.float64))
print(x.min(axis=0), x.max(axis=0))
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.scatter(x, y)
plt.plot(x, nn.predict(x), c='green')
In [ ]:
x, y = make_exp(n_samples=100, x_min=0, x_max=5, noise=10)
print(x.shape, y.shape)
plt.scatter(x, y)
In [ ]:
minmax = MinMaxScaler(feature_range=(-1, 1))
x = minmax.fit_transform(x.astype(np.float64))
print(x.min(axis=0), x.max(axis=0))
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.scatter(x, y)
plt.plot(x, nn.predict(x), c='green')
In [ ]:
x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 0, 0, 1]).reshape(-1, 1)
print(x.shape, y.shape)
plt.scatter(x[:,0], x[:,1], c=list(np.array(y).ravel()), s=15, cmap='bwr')
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
y_pred = nn.predict(x)
print('Predições:', y_pred, sep='\n')
print('Acurácia: {:.2f}%'.format(100*accuracy_score(y, y_pred > 0.5)))
plot.classification_predictions(x, y, is_binary=True, nn=nn, cmap='bwr')
In [ ]:
x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 1, 1, 0]).reshape(-1, 1)
print(x.shape, y.shape)
plt.scatter(x[:,0], x[:,1], c=list(np.array(y).ravel()), s=15, cmap='bwr')
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
y_pred = nn.predict(x)
print('Predições:', y_pred, sep='\n')
print('Acurácia: {:.2f}%'.format(100*accuracy_score(y, y_pred > 0.5)))
plot.classification_predictions(x, y, is_binary=True, nn=nn, cmap='bwr')
In [ ]:
x, y = make_blobs(n_samples=100, n_features=2, centers=2, random_state=1234)
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')
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
y_pred = nn.predict(x)
threshold = 0 if nn.layers[-1].activation == linear else 0.5
print('Acurácia: {:.2f}%'.format(100*accuracy_score(y, y_pred >= threshold)))
plot.classification_predictions(x, y, is_binary=True, nn=nn, threshold=threshold, cmap='bwr')
In [ ]:
x, y = make_blobs(n_samples=500, n_features=2, cluster_std=0.9, centers=[(-3, -3), (3, 3), (-3, 3), (3, -3)], random_state=1234)
y = y.reshape(-1, 1)
y = np.where(y >= 2, 1, 0)
print(x.shape, y.shape)
plt.scatter(x[:,0], x[:,1], c=list(np.array(y).ravel()), s=15, cmap='bwr')
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
y_pred = nn.predict(x)
print('Acurácia: {:.2f}%'.format(100*accuracy_score(y, y_pred > 0.5)))
plot.classification_predictions(x, y, is_binary=True, nn=nn, threshold=0.5, cmap='bwr')
In [ ]:
x, y = make_circles(n_samples=500, noise=0.1, factor=0.4, random_state=1234)
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')
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
y_pred = nn.predict(x)
print('Acurácia: {:.2f}%'.format(100*accuracy_score(y, y_pred > 0.5)))
plot.classification_predictions(x, y, is_binary=True, nn=nn, threshold=0.5, cmap='bwr')
In [ ]:
x, y = make_moons(200, 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')
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
y_pred = nn.predict(x)
print('Acurácia: {:.2f}%'.format(100*accuracy_score(y, y_pred > 0.5)))
plot.classification_predictions(x, y, is_binary=True, nn=nn, threshold=0.5, cmap='bwr')
In [ ]:
x, y = make_spiral(n_samples=100, n_class=2, radius=5, laps=1.75)
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')
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
y_pred = nn.predict(x)
print('Acurácia: {:.2f}%'.format(100*accuracy_score(y, y_pred > 0.5)))
plot.classification_predictions(x, y, is_binary=True, nn=nn, threshold=0.5, cmap='bwr')
In [ ]:
x, y = make_blobs(n_samples=300, n_features=2, centers=[(0, -3), (-3, 3), (3, 3)], random_state=1234)
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)
In [ ]:
onehot = OneHotEncoder(sparse=False)
y_onehot = onehot.fit_transform(y)
print(y_onehot[::60])
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
y_pred = np.argmax(nn.predict(x), axis=1)
print('Acurácia: {:.2f}%'.format(100*accuracy_score(y, y_pred)))
plot.classification_predictions(x, y, is_binary=False, nn=nn)
In [ ]:
x, y = make_blobs(n_samples=400, n_features=2, centers=[(-3, 0), (3, 0), (0, 3), (0, -3)], random_state=1234)
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)
In [ ]:
onehot = OneHotEncoder(sparse=False)
y_onehot = onehot.fit_transform(y)
print(y_onehot[::70])
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
y_pred = np.argmax(nn.predict(x), axis=1)
print('Acurácia: {:.2f}%'.format(100*accuracy_score(y, y_pred)))
plot.classification_predictions(x, y, is_binary=False, nn=nn)
In [ ]:
x, y = make_spiral(n_samples=100, n_class=5, radius=1, laps=0.5)
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)
In [ ]:
onehot = OneHotEncoder(sparse=False)
y_onehot = onehot.fit_transform(y)
print(y_onehot[::100])
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
y_pred = np.argmax(nn.predict(x), axis=1)
print('Acurácia: {:.2f}%'.format(100*accuracy_score(y, y_pred)))
plot.classification_predictions(x, y, is_binary=False, nn=nn)
In [ ]:
x, y = make_classification(n_samples=100, n_classes=4, n_features=2, n_clusters_per_class=1, n_redundant=0, n_repeated=0, random_state=1234)
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)
In [ ]:
onehot = OneHotEncoder(sparse=False)
y_onehot = onehot.fit_transform(y)
print(y_onehot[::10])
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
y_pred = np.argmax(nn.predict(x), axis=1)
print('Acurácia: {:.2f}%'.format(100*accuracy_score(y, y_pred)))
plot.classification_predictions(x, y, is_binary=False, nn=nn)
In [ ]:
data = load_iris()
x, y = data.data[:, 2:], data.target.reshape(-1,1)
print(data.feature_names)
print(data.target_names)
print(x.shape, y.shape)
plt.scatter(x[:,0], x[:,1], c=list(np.array(y).ravel()), s=15, cmap=plt.cm.viridis)
In [ ]:
onehot = OneHotEncoder(sparse=False)
y_onehot = onehot.fit_transform(y)
print(y_onehot[::20])
In [ ]:
input_dim, output_dim = x.shape[1], y.shape[1]
# insira sua rede aqui!
y_pred = np.argmax(nn.predict(x), axis=1)
print('Acurácia: {:.2f}%'.format(100*accuracy_score(y, y_pred)))
plot.classification_predictions(x, y, is_binary=False, nn=nn)
Regularization:
Momentum:
Learning Rate Decay:
Batch Normalization:
Gradient Checking: