Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save harpiechoise/40f5342712549a575b178dedb549cebb to your computer and use it in GitHub Desktop.
Save harpiechoise/40f5342712549a575b178dedb549cebb to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Keras una introduccion en español :) \n",
"\n",
"Bueno aqui aprenderan a hacer un IA basica con keras este es un articulo un poco mas raro pero espero les guste ;)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"from __future__ import print_function\n",
"import numpy as np\n",
"from keras.datasets import mnist\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense, Activation\n",
"from keras.optimizers import SGD\n",
"from keras.utils import np_utils\n",
"from keras.layers import Dropout"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(1671)\n",
"\n",
"#Entrenamiento\n",
"NB_EPOCHS = 200\n",
"BATCH_SIZE = 128\n",
"VERBOSE = 1\n",
"\n",
"NB_CLASES = 10 # Numero de salidas al ser MNIST tiene 10 que son los digitos \n",
"OPTIMIZER = SGD() # Es el Descenso de gradiente sadgastico SGD\n",
"N_HIDDEN = 128 # Numero de neuronas en la capa oculta\n",
"N_HIDDEN = 128 # Neuronas en la capa oculta\n",
"VALIDATION_SPLIT = 0.2 # Cuanto del entrenamiento estara reservado para la validacion\n",
"# data: los datos seran divididos y mezclados y divididos en Baches "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"(X_train,y_train), (X_test, y_test) = mnist.load_data()\n",
"# El formato de como se descargan los datos son en dos tuplas de entrenamiento y testeo\n",
"# Los Datos De X tiene 60000 filas de 28x28 valores es si los pasas a bits son 75264000 bytes\n",
"# Entonces hacemos un reshape a 784 para dividirlas en imagenes y 28x28 son 784\n",
"\n",
"RESHAPED = 784\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"60000 Muestras para el entrenamiento\n",
"10000 Muestras para los tests\n",
"\n",
"One Hot:\n",
"\n",
"[ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]\n",
"\n",
"Normal:\n",
"\n",
"5\n"
]
}
],
"source": [
"#Para hacer el reshape devemos entrar a las variables y tienen una funcion reshape\n",
"#Para no afectar los valores para modificar los valores vamos a ver su forma\n",
"X_train.shape # (60000, 28, 28)\n",
"#Si fueran redes convolucionales no devemos hacerlo pero como es lineal lo haremos\n",
"X_train = X_train.reshape(60000,RESHAPED)\n",
"X_train.shape # (60000, 784)\n",
"#Ahora haremos lo mismo con los test los y son solamente etiquetas asique no devemos afectarlas\n",
"# (10000,28,28) devemos tener cuidado con la forma porque si no dara error\n",
"X_test = X_test.reshape(10000,RESHAPED)\n",
"\n",
"#Ahora le pondremos la etiqueta de los valores\n",
"X_train = X_train.astype('float32')\n",
"X_test = X_test.astype('float32') \n",
"# Su etiqueta de tipo de datos se actualizo a dtype=float32\n",
"# Vamos a normalizarlos por el tipo de operacion SGD ya que no devemos tener datos muy dispares\n",
"X_train /= 255\n",
"X_test /= 255\n",
"# /= es para redefinir la variable dividida por en este caso 255 seria lo mismo que decir X_train = X_train / 255\n",
"print(X_train.shape[0],'Muestras para el entrenamiento')\n",
"print(X_test.shape[0],'Muestras para los tests')\n",
"#Ahora los convertiremos de vectores de etiquetas a matrices\n",
"Y_train = np_utils.to_categorical(y_train,NB_CLASES)\n",
"Y_test = np_utils.to_categorical(y_test,NB_CLASES)\n",
"print(\"\"\"\n",
"One Hot:\n",
"\"\"\")\n",
"print(Y_train[0])\n",
"\n",
"print(\"\"\"\n",
"Normal:\n",
"\"\"\")\n",
"\n",
"print(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tenemos que pensar un poco si el dataset de mnist tiene imagenes en 28x28 necesitamos 784 entradas y si las salidas son 10 que son los digitos del 0 al 9 necesitamos tener 10 salidas"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"dense_4 (Dense) (None, 128) 100480 \n",
"_________________________________________________________________\n",
"activation_4 (Activation) (None, 128) 0 \n",
"_________________________________________________________________\n",
"dense_5 (Dense) (None, 128) 16512 \n",
"_________________________________________________________________\n",
"dropout_1 (Dropout) (None, 128) 0 \n",
"_________________________________________________________________\n",
"activation_5 (Activation) (None, 128) 0 \n",
"_________________________________________________________________\n",
"dense_6 (Dense) (None, 10) 1290 \n",
"_________________________________________________________________\n",
"dropout_2 (Dropout) (None, 10) 0 \n",
"_________________________________________________________________\n",
"activation_6 (Activation) (None, 10) 0 \n",
"=================================================================\n",
"Total params: 118,282\n",
"Trainable params: 118,282\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"model = Sequential() # Definimos el modelo en este caso secuencial\n",
"model.add(Dense(N_HIDDEN, input_shape=(RESHAPED,)))# Tenemos solo una caṕa con las salidas y la forma de la entrada son 784 es decir el tamaño de la imagen\n",
"model.add(Activation('relu')) # Vamos a usar la activacion llamada softmax que es para cuando tenemos entradas muy variables cuando llegan los picos se normalizan para tener una mayor presicion\n",
"model.add(Dense(N_HIDDEN))\n",
"model.add(Dropout(0.3))\n",
"model.add(Activation('relu'))\n",
"model.add(Dense(NB_CLASES))\n",
"model.add(Dropout(0.3))\n",
"model.add(Activation('softmax'))\n",
"model.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ahora buscamos nuestra funcion de activacion en mi caso usare la entropia cruzada categorica porque para este problema sera mucho mas efectiva su formula es la siguiente (Creo haber hablado anteriormente de la de los errores al cuadrado pero no sirve en este modelo)\n",
"\n",
"Categorical CorossEntropy:\n",
"\n",
"-t log(p)-(1-t)log(1-p)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"model.compile(loss='categorical_crossentropy',optimizer=OPTIMIZER,metrics=['accuracy'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ahora para entrenar vamos a explicar estas dos variables\n",
"\n",
"Epochs: significa epocas seran las veces en las que pasaran todos los baches de datos por la IA \n",
"\n",
"\n",
"Batch_size: es en cuanto datos se dividiran las muestras para no sobrecargar la CPU"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"48000/48000 [==============================] - 2s 42us/step - loss: 0.4256 - acc: 0.7870 - val_loss: 0.0784 - val_acc: 0.9767\n",
"Epoch 177/200\n",
"48000/48000 [==============================] - 2s 42us/step - loss: 0.4259 - acc: 0.7854 - val_loss: 0.0787 - val_acc: 0.9764\n",
"Epoch 178/200\n",
"48000/48000 [==============================] - 2s 43us/step - loss: 0.4237 - acc: 0.7883 - val_loss: 0.0788 - val_acc: 0.9762\n",
"Epoch 179/200\n",
"48000/48000 [==============================] - 2s 45us/step - loss: 0.4215 - acc: 0.7905 - val_loss: 0.0786 - val_acc: 0.9762\n",
"Epoch 180/200\n",
"48000/48000 [==============================] - 2s 51us/step - loss: 0.4293 - acc: 0.7868 - val_loss: 0.0786 - val_acc: 0.9766\n",
"Epoch 181/200\n",
"48000/48000 [==============================] - 2s 42us/step - loss: 0.4231 - acc: 0.7887 - val_loss: 0.0782 - val_acc: 0.9764\n",
"Epoch 182/200\n",
"48000/48000 [==============================] - 2s 46us/step - loss: 0.4203 - acc: 0.7870 - val_loss: 0.0781 - val_acc: 0.9764\n",
"Epoch 183/200\n",
"48000/48000 [==============================] - 2s 45us/step - loss: 0.4261 - acc: 0.7861 - val_loss: 0.0776 - val_acc: 0.9775\n",
"Epoch 184/200\n",
"48000/48000 [==============================] - 2s 44us/step - loss: 0.4243 - acc: 0.7870 - val_loss: 0.0779 - val_acc: 0.9762\n",
"Epoch 185/200\n",
"48000/48000 [==============================] - 2s 44us/step - loss: 0.4197 - acc: 0.7913 - val_loss: 0.0778 - val_acc: 0.9770\n",
"Epoch 186/200\n",
"48000/48000 [==============================] - 2s 41us/step - loss: 0.4249 - acc: 0.7853 - val_loss: 0.0779 - val_acc: 0.9766\n",
"Epoch 187/200\n",
"48000/48000 [==============================] - 2s 46us/step - loss: 0.4192 - acc: 0.7867 - val_loss: 0.0775 - val_acc: 0.9772\n",
"Epoch 188/200\n",
"48000/48000 [==============================] - 2s 42us/step - loss: 0.4258 - acc: 0.7847 - val_loss: 0.0773 - val_acc: 0.9775\n",
"Epoch 189/200\n",
"48000/48000 [==============================] - 2s 44us/step - loss: 0.4191 - acc: 0.7889 - val_loss: 0.0777 - val_acc: 0.9768\n",
"Epoch 190/200\n",
"48000/48000 [==============================] - 2s 44us/step - loss: 0.4247 - acc: 0.7862 - val_loss: 0.0775 - val_acc: 0.9776\n",
"Epoch 191/200\n",
"48000/48000 [==============================] - 2s 43us/step - loss: 0.4234 - acc: 0.7883 - val_loss: 0.0777 - val_acc: 0.9767\n",
"Epoch 192/200\n",
"48000/48000 [==============================] - 2s 43us/step - loss: 0.4206 - acc: 0.7896 - val_loss: 0.0776 - val_acc: 0.9773\n",
"Epoch 193/200\n",
"48000/48000 [==============================] - 2s 43us/step - loss: 0.4204 - acc: 0.7864 - val_loss: 0.0774 - val_acc: 0.9771\n",
"Epoch 194/200\n",
"48000/48000 [==============================] - 2s 43us/step - loss: 0.4213 - acc: 0.7855 - val_loss: 0.0774 - val_acc: 0.9769\n",
"Epoch 195/200\n",
"48000/48000 [==============================] - 2s 42us/step - loss: 0.4215 - acc: 0.7866 - val_loss: 0.0775 - val_acc: 0.9767\n",
"Epoch 196/200\n",
"48000/48000 [==============================] - 2s 41us/step - loss: 0.4164 - acc: 0.7911 - val_loss: 0.0775 - val_acc: 0.9773\n",
"Epoch 197/200\n",
"48000/48000 [==============================] - 2s 42us/step - loss: 0.4147 - acc: 0.7915 - val_loss: 0.0776 - val_acc: 0.9771\n",
"Epoch 198/200\n",
"48000/48000 [==============================] - 2s 43us/step - loss: 0.4164 - acc: 0.7905 - val_loss: 0.0776 - val_acc: 0.9770\n",
"Epoch 199/200\n",
"48000/48000 [==============================] - 2s 51us/step - loss: 0.4247 - acc: 0.7861 - val_loss: 0.0769 - val_acc: 0.9777\n",
"Epoch 200/200\n",
"48000/48000 [==============================] - 2s 46us/step - loss: 0.4201 - acc: 0.7905 - val_loss: 0.0769 - val_acc: 0.9772\n"
]
}
],
"source": [
"#En keras entrenar es facil usaremos solo un comando con los parametros explicados\n",
"\n",
"history = model.fit(X_train, Y_train, \n",
" batch_size=BATCH_SIZE, \n",
" epochs=NB_EPOCHS,\n",
" verbose=VERBOSE,\n",
" validation_split=VALIDATION_SPLIT)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10000/10000 [==============================] - 0s 39us/step\n",
"Puntaje del test: 0.075952377973\n",
"Exactitud del test: 0.9767\n"
]
}
],
"source": [
"#Ahora sacaremos el puntaje de nuestro modelo \n",
"score = model.evaluate(X_test,Y_test,verbose=VERBOSE)\n",
"print(\"Puntaje del test: \",score[0])\n",
"print(\"Exactitud del test: \",score[1]) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Felicitaciones hiciste tu primer modelo! :D "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment