<jupyter_start><jupyter_text>Red neuronal Perceptrón multicapa<jupyter_code>#%tensorflow_version 2.x
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder
%matplotlib inline
tf.__version__
from tensorflow.keras.datasets.fashion_mnist import load_data
fashion_mnist = load_data()
#read_data_sets("exercice/data", one_hot = True)<jupyter_output><empty_output><jupyter_text>Separando el conjunto de datos en Entrenamiento y prueba<jupyter_code>(x_train, y_train), (x_test, y_test)=fashion_mnist
x_train.shape
imagendemo=x_train[0,:,:]
plt.imshow(imagendemo,cmap='gray')<jupyter_output><empty_output><jupyter_text>Las etiquetas numéricas pueden ser transformadas al nombre de la clase correspondiente usando el siguiente diccionario<jupyter_code>label_dict = {
 0: "T-shirt/top",
 1: "Trouser",
 2: "Pullover",
 3: "Dress",
 4: "Coat",
 5: "Sandal",
 6: "Shirt",
 7: "Sneaker",
 8: "Bag",
 9: "Ankle boot"
}
label_dict[y_train[0]]
x_train=x_train.reshape(-1,28*28).astype('float32')/255
x_test=x_test.reshape(-1,28*28).astype('float32')/255
x_train.shape
x_train.shape<jupyter_output><empty_output><jupyter_text>Las salidas y deben se codificadas en one hot<jupyter_code># onehot encode
onehot_encoder = OneHotEncoder(sparse=False)
y_train = y_train.reshape(len(y_train), 1)
y_train_onehot = onehot_encoder.fit_transform(y_train)

y_test = y_test.reshape(len(y_test), 1)
y_test_onehot = onehot_encoder.fit_transform(y_test)

y_train_onehot.shape<jupyter_output><empty_output><jupyter_text>Declarando la arquitecturaGenerando función<jupyter_code>class DNN_model(object):
    def __init__(self,
                 n_nodes_hl1=500,
                 n_nodes_hl2=500,
                 n_nodes_hl3=500,
                 n_classes=10):
        self.h1LW = tf.Variable(np.random.randn(784, n_nodes_hl1),name="hl1weigths",dtype="float32")
        self.h1LB = tf.Variable(np.random.randn(n_nodes_hl1),name="hl1bias",dtype="float32")
        self.h2LW = tf.Variable(np.random.randn(n_nodes_hl1, n_nodes_hl2),name="hl2weigths",dtype="float32")
        self.h2LB = tf.Variable(np.random.randn(n_nodes_hl2),name="hl2bias",dtype="float32")
        self.h3LW = tf.Variable(np.random.randn(n_nodes_hl2, n_nodes_hl3),name="hl3weigths",dtype="float32")
        self.h3LB = tf.Variable(np.random.randn(n_nodes_hl3),name="hl3bias",dtype="float32")
        self.outW = tf.Variable(np.random.randn(n_nodes_hl3, n_classes),name="outweigths",dtype="float32")
        self.outB = tf.Variable(np.random.randn(n_classes),name="outbias",dtype="float32")
        self.trainable_variables =[self.h1LW,self.h1LB,self.h2LW,self.h2LB,self.h3LW,self.h3LB,self.outW,self.outB]          
    def __call__(self,x): 
        # Declarando la arquitectura

        l1 = tf.add(tf.matmul(x,self.h1LW), self.h1LB)
        l1 = tf.nn.relu(l1)

        l2 = tf.add(tf.matmul(l1,self.h2LW), self.h2LB)
        l2 = tf.nn.relu(l2)

        l3 = tf.add(tf.matmul(l2,self.h3LW), self.h3LB)
        l3 = tf.nn.relu(l3)

        output = tf.add(tf.matmul(l3,self.outW),self.outB)
        return output
  
DNN = DNN_model()
DNN(x_train[24:30])<jupyter_output><empty_output><jupyter_text>Seleccionar un optimizador<jupyter_code>#optimizador = tf.keras.optimizers.Adam(learning_rate=0.001 )
optimizador = tf.compat.v1.train.AdamOptimizer(learning_rate=0.001)<jupyter_output><empty_output><jupyter_text>Definir las metricas a usar<jupyter_code>train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.CategoricalAccuracy(name='train_accuracy')

test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.CategoricalAccuracy(name='test_accuracy')<jupyter_output><empty_output><jupyter_text>Calculo de gradientes y ajuste<jupyter_code>@tf.function
def train_step(model,tdata, labels):
    with tf.GradientTape() as tape:
        predictions = model(tdata)
        #calculo de una funcion de error 
        loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits( labels, predictions ))
   
    gradients = tape.gradient(loss, model.trainable_variables)
    capped_grads_and_vars = [(grad,model.trainable_variables[index]) for index, grad in enumerate(gradients)]
    optimizador.apply_gradients(capped_grads_and_vars)
    
    train_loss(loss)
    train_accuracy(labels, predictions)
@tf.function
def test_step(model,tdata, labels):
    predictions = model(tdata)
    t_loss =  tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels, predictions))

    test_loss(t_loss)
    test_accuracy(labels, predictions)

class batch_loader:
    def __init__(self,data_x,data_y,batch_size=100):
        self.x=data_x
        self.y=data_y
        self.index=0
        self.len=len(data_x)
        self.batch_size=batch_size
    def __iter__(self):
        return self
    
    def __len__(self):
        return self.len

    def __next__(self):
        if self.index+self.batch_size<=self.len:
            to_return=(self.x[self.index:self.index+self.batch_size,:],self.y[self.index:self.index+self.batch_size,:])
            self.index+=self.batch_size
            return to_return
        elif self.index<self.batch_size:
            to_return=(self.x[self.index:,:],self.y[self.index:,:])
            self.index=self.len
            return to_return
        else:
            raise StopIteration<jupyter_output><empty_output><jupyter_text>función de entrenamiento  y prueba<jupyter_code>def fitting(model,train_x,train_y,test_x,test_y,EPOCHS,batch_size=100):
    for epoch in range(EPOCHS):
        i=0
        for batch_x, batch_y in batch_loader(train_x,train_y,batch_size):
              train_step(model,batch_x,batch_y)

        test_step(model,test_x,test_y)

        template = 'Epoch {:02}, Perdida: {:.2f}, Exactitud: {:.2f}, Perdida de prueba: {:.2f}, Exactitud de prueba: {:.2f}'
        print(template.format(epoch+1,
                             train_loss.result(),
                            train_accuracy.result()*100,
                            test_loss.result(),
                            test_accuracy.result()*100))
        train_loss.reset_states()
        train_accuracy.reset_states()
        test_loss.reset_states()
        test_accuracy.reset_states()

  
    
fitting(DNN,x_train,y_train_onehot,x_test,y_test_onehot,100,100)<jupyter_output>Epoch 01, Perdida: 3808.97, Exactitud: 73.91, Perdida de prueba: 1955.91, Exactitud de prueba: 78.89
Epoch 02, Perdida: 1407.21, Exactitud: 80.96, Perdida de prueba: 1420.51, Exactitud de prueba: 80.76
Epoch 03, Perdida: 963.76, Exactitud: 83.07, Perdida de prueba: 1097.08, Exactitud de prueba: 82.41
Epoch 04, Perdida: 716.75, Exactitud: 84.50, Perdida de prueba: 952.80, Exactitud de prueba: 82.54
Epoch 05, Perdida: 570.51, Exactitud: 85.46, Perdida de prueba: 844.69, Exactitud de prueba: 83.46
Epoch 06, Perdida: 455.72, Exactitud: 86.64, Perdida de prueba: 823.83, Exactitud de prueba: 82.69
Epoch 07, Perdida: 393.64, Exactitud: 87.24, Perdida de prueba: 740.26, Exactitud de prueba: 83.79
Epoch 08, Perdida: 322.51, Exactitud: 88.19, Perdida de prueba: 724.75, Exactitud de prueba: 82.68
Epoch 09, Perdida: 277.75, Exactitud: 88.74, Perdida de prueba: 771.78, Exactitud de prueba: 82.61
Epoch 10, Perdida: 259.05, Exactitud: 89.06, Perdida de prueba: 671.53, Exactitud de prueba: 83.33
Epoch[...]
{'fraction_non_alphanumeric': 0.12399618892064788, 'fraction_numerical': 0.04872737171634681, 'mean_word_length': 4.142057382785165, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '303317', 'license': 'MIT', 'n_tokens_mistral': 3111, 'n_tokens_neox': 2848, 'n_words': 451, 'path': 'tensorflow-vanilla/Vanilla-TF2.ipynb', 'repo_name': 'HectorJuarezL/machine-learning-codes', 'size': '7347'}