#!/usr/bin/env python # coding: utf-8 # #
Diplomado en Inteligencia Artificial y Aprendizaje Profundo
# #
Introducción a la API Sequential de Keras
# ## Profesores # 1. Alvaro Mauricio Montenegro Díaz, ammontenegrod@unal.edu.co # 2. Daniel Mauricio Montenegro Reyes, dextronomo@gmail.com # 3. Campo Elías Pardo Turriago, cepardot@unal.edu.co # ## Asesora Medios y Marketing digital # # 4. Maria del Pilar Montenegro, pmontenegro88@gmail.com # ## Asistentes # 5. Oleg Jarma, ojarmam@unal.edu.co # 6. Laura Lizarazo, ljlizarazore@unal.edu.co # ## Contenido # * [Introducción](#Introducción) # * [Prepara datos de MNIST](#Prepara-datos-de-MNIST) # * [Modelo Sequential de Keras](#Modelo-Sequential-de-Keras) # * [Summary y Plot Model](#Summary-y-Plot-Model) # * [Entrenamiento y evaluación del modelo](#Entrenamiento-y-evaluación-del-modelo) # # ## Introducción # Este es un notebook de Google Colaboratory. Los programas de Python se executan directamente en tu navegador, una gran manera de aprender y utilizar TensorFlow. Para poder seguir este tutorial, ejecuta este notebook en Google Colab presionando el boton en la parte superior de esta pagina. # # In[1]: from __future__ import absolute_import, division, print_function import tensorflow as tf print('Version de Tensorflow = ', tf.__version__) # ## Prepara datos de MNIST # In[2]: mnist = tf.keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() x_train, x_test = x_train/255.0, x_test/255.0 # ## Modelo Sequential de Keras # # In[3]: import tensorflow.keras.layers as layer model = tf.keras.models.Sequential([ layer.Flatten(input_shape=(28,28)), #784 layer.Dense(128, activation='relu'), layer.Dropout(0.2), layer.Dense(10, activation='softmax') ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # ## Summary y Plot Model # In[4]: model.summary() # In[5]: from tensorflow.keras.utils import plot_model plot_model(model, to_file='../Imagenes/mnist_dense.png', show_shapes=True, show_dtype=False) # Puede necesitar instalar [graphviz](https://www.graphviz.org/). pip install pydot pip install pydot_ng pip install graphviz #pip install plot-model # In[ ]: #from plot_model import plot_model #plot_model(model) # ## Entrenamiento y evaluación del modelo # In[6]: history = model.fit(x_train, y_train, epochs=5) # In[7]: model.evaluate(x_test, y_test, verbose=2) # ## Primeros gráficos # ### Extrae datos # In[20]: import matplotlib.pyplot as plt import pandas as pd hist = pd.DataFrame(history.history) hist['epoch'] = history.epoch hist # ### Pérdida # In[27]: plt.figure() plt.xlabel('Epoca') plt.ylabel('Loss: Entropía cruzada') plt.plot(hist['epoch'], hist['loss'], label='Error en entrenamiento') plt.ylim([0,0.5]) plt.legend() plt.show() # ### Exactitud # In[29]: plt.figure() plt.xlabel('Epoca') plt.ylabel('Exactitud') plt.plot(hist['epoch'], hist['accuracy'], label='Exactitud en entrenamiento') plt.ylim([0.9,1]) plt.legend() plt.show() # In[ ]: