{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-09-12T10:17:44.747815Z","iopub.execute_input":"2024-09-12T10:17:44.748278Z","iopub.status.idle":"2024-09-12T10:17:45.270056Z","shell.execute_reply.started":"2024-09-12T10:17:44.748232Z","shell.execute_reply":"2024-09-12T10:17:45.268583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importamos las librerías necesarias\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n# Cargar el dataset CIFAR-10\n(X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()\n\n# Clases de CIFAR-10\nclass_names = ['Avión', 'Automóvil', 'Pájaro', 'Gato', 'Ciervo', \n               'Perro', 'Rana', 'Caballo', 'Barco', 'Camión']\n\n# Normalizar los valores de las imágenes entre 0 y 1\nX_train, X_test = X_train / 255.0, X_test / 255.0\n\n# Visualización de 5 imágenes con sus etiquetas\ndef visualizar_imagenes(X, y, class_names, num_imagenes=5):\n    plt.figure(figsize=(10, 2))\n    for i in range(num_imagenes):\n        plt.subplot(1, num_imagenes, i+1)\n        plt.imshow(X[i])\n        plt.title(class_names[y[i][0]])\n        plt.axis('off')\n    plt.show()\n\nvisualizar_imagenes(X_train, y_train, class_names)\n\n# Definir el modelo\nmodel = models.Sequential()\n\n# Primera capa convolucional\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\n# Segunda capa convolucional\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\n# Tercera capa convolucional\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\n\n# Aplanar las características para pasarlas a las capas densas\nmodel.add(layers.Flatten())\n\n# Agregar una capa densa totalmente conectada\nmodel.add(layers.Dense(64, activation='relu'))\n\n# Capa de salida con softmax para clasificación multiclase\nmodel.add(layers.Dense(10, activation='softmax'))\n\n# Compilar el modelo\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Entrenar el modelo\nhistory = model.fit(X_train, y_train, epochs=10, \n                    validation_data=(X_test, y_test))\n\n# Evaluar el modelo\ntest_loss, test_acc = model.evaluate(X_test, y_test, verbose=2)\nprint(f'\\nPrecisión en el conjunto de prueba: {test_acc:.2f}')\n\n# Visualizar los resultados de entrenamiento\nplt.plot(history.history['accuracy'], label='Precisión de entrenamiento')\nplt.plot(history.history['val_accuracy'], label='Precisión de validación')\nplt.xlabel('Época')\nplt.ylabel('Precisión')\nplt.legend(loc='lower right')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-09-12T10:17:45.272502Z","iopub.execute_input":"2024-09-12T10:17:45.273169Z","iopub.status.idle":"2024-09-12T10:24:35.276743Z","shell.execute_reply.started":"2024-09-12T10:17:45.273108Z","shell.execute_reply":"2024-09-12T10:24:35.275500Z"},"trusted":true},"execution_count":null,"outputs":[]}]}