Skip to content

Instantly share code, notes, and snippets.

@guilhermefgs
guilhermefgs / init_theta_nn.py
Last active October 27, 2019 06:08
Inicialização aleatória dos parâmetros theta em redes neurais
def randomInit(input_layer_size, hidden_layer_size, num_labels):
# Parâmetros da primeira camada (incluindo bias)
# As matrizes têm formato (num_entradas, num_saidas)
size1 = (hidden_layer_size, input_layer_size+1)
theta1_ini = np.random.normal(0, .1, size=size1)
# Parâmetros da primeira camada (incluindo bias)
size2 = (num_labels, hidden_layer_size+1)
theta2_ini = np.random.normal(0, .1, size=size2)
@guilhermefgs
guilhermefgs / norm_formating.py
Last active October 27, 2019 03:50
Normalização e formatação
# Mudando o formato das entradas para que cada imagem seja uma lista, ao invés de uma matriz
x_train = x_train.reshape(x_train.shape[0], 28*28)
x_test = x_test.reshape(x_test.shape[0], 28*28)
# Converte os valores para float, para que eles possam ter parte decimal.
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# Divide pelo valor RGB máximo, para que a entrada tenha valores entre 0 e 1.
x_train /= 255
@guilhermefgs
guilhermefgs / label_mapping.py
Last active October 27, 2019 03:50
label mapping
# Cada linha da matriz identidade corresponde a uma das labels
mapping = np.identity(10, dtype=int)
y_train_classif = y_train
y_test_classif = y_test
y_train = np.array([mapping[y] for y in y_train])
y_test = np.array([mapping[y] for y in y_test])
print(y_train_classif[0], y_train[0])
@guilhermefgs
guilhermefgs / sigmoide.py
Created October 26, 2019 21:06
sigmoide e sigmoide gradient functions
def sigmoid(z):
return 1/(1 + np.exp(-z))
def sigmoidGrad(z):
return (sigmoid(z))*(1-sigmoid(z))
@guilhermefgs
guilhermefgs / mnisttf.py
Last active October 26, 2019 14:58 — forked from ogyalcin/mnisttf.py
Import Tensorflow and MNIST Dataset
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# MATPLOTLIB
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# lê o arquivo (note que em matplotlib deve ser em png)
img = mpimg.imaread('<nome_do_arquivo>.png')
print(img)
imgplot = plt.imshow(img) # mostra a imagem
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.