Created
December 13, 2022 17:58
-
-
Save RodolfoFerro/29105f2204217d26a6ff73fe793e7f83 to your computer and use it in GitHub Desktop.
TCJ 2022
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"private_outputs": true, | |
"provenance": [], | |
"authorship_tag": "ABX9TyN43xf/kEt0ZNgh4KFm4ooQ", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/RodolfoFerro/29105f2204217d26a6ff73fe793e7f83/tcj-2022.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Entrenamiento de una neurona" | |
], | |
"metadata": { | |
"id": "R2SGcAw4oqAq" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "2NKx40hxqmo4" | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"\n", | |
"\n", | |
"class TrainableNeuron():\n", | |
" def __init__(self, n):\n", | |
" \"\"\"Class constructor.\n", | |
" \n", | |
" Parameters\n", | |
" ----------\n", | |
" n : int\n", | |
" Input size.\n", | |
" \"\"\"\n", | |
" \n", | |
" np.random.seed(123)\n", | |
" self.synaptic_weights = 2 * np.random.random((n, 1)) - 1 # TODO. Use np.random.random((n, 1)) to gen values in (-1, 1)\n", | |
"\n", | |
" def __sigmoid(self, x):\n", | |
" \"\"\"Sigmoid function.\n", | |
" \n", | |
" Parameters\n", | |
" ----------\n", | |
" x : float\n", | |
" Input value to sigmoid function.\n", | |
" \"\"\"\n", | |
" \n", | |
" # Return result of sigmoid function f(z) = 1 / (1 + e^(-z))\n", | |
" return 1 / (1 + np.exp(-x))\n", | |
"\n", | |
" def __sigmoid_derivative(self, x):\n", | |
" \"\"\"Derivative of the Sigmoid function.\n", | |
" \n", | |
" Parameters\n", | |
" ----------\n", | |
" x : float\n", | |
" Input value to evaluated sigmoid function.\"\"\"\n", | |
"\n", | |
" # Return the derivate of sigmoid function x * (1 - x)\n", | |
" return x * (1 - x)\n", | |
"\n", | |
" def train(self, training_inputs, training_output, epochs):\n", | |
" \"\"\"Training function.\n", | |
" \n", | |
" Parameters\n", | |
" ----------\n", | |
" training_inputs : list\n", | |
" List of features for training.\n", | |
" training_outputs : list\n", | |
" List of labels for training.\n", | |
" epochs : int\n", | |
" Number of iterations for training.\n", | |
" \n", | |
" Returns\n", | |
" -------\n", | |
" history : list\n", | |
" A list containing the training history.\n", | |
" \"\"\"\n", | |
"\n", | |
" # Historial de entrenamiento\n", | |
" history = []\n", | |
"\n", | |
" # Transposición de vector de muestras\n", | |
" real_output = training_output.reshape((len(training_inputs), 1))\n", | |
"\n", | |
"\n", | |
" for iteration in range(epochs):\n", | |
" # Predicción de valores\n", | |
" predicted_output = self.predict(training_inputs)\n", | |
" \n", | |
" # Error simple\n", | |
" error = real_output - predicted_output\n", | |
" \n", | |
" # Ajuste de pesos\n", | |
" adjustment = np.dot(training_inputs.T, error *\n", | |
" self.__sigmoid_derivative(predicted_output))\n", | |
" self.synaptic_weights += adjustment\n", | |
"\n", | |
" history.append(np.sum(error))\n", | |
" \n", | |
" return history\n", | |
"\n", | |
" def predict(self, inputs):\n", | |
" \"\"\"Prediction function. Applies input function to inputs tensor.\n", | |
" \n", | |
" Parameters\n", | |
" ----------\n", | |
" inputs : list\n", | |
" List of inputs to apply sigmoid function.\n", | |
" \"\"\"\n", | |
" # TODO: Apply self.__sigmoid to np.dot of (inputs, self.synaptic_weights)\n", | |
" return self.__sigmoid(np.dot(inputs, self.synaptic_weights))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "BYW9aYSCxc1q" | |
}, | |
"outputs": [], | |
"source": [ | |
"# Training samples:\n", | |
"input_values = [(0, 1), (1, 0), (0, 0), (1, 1)] # TODO. Define the input values as a list of tuples\n", | |
"output_values = [1, 1, 0, 1] # TODO. Define the desired outputs\n", | |
"\n", | |
"training_inputs = np.array(input_values)\n", | |
"training_output = np.array(output_values).T.reshape((len(output_values), 1))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "cThkcQGMxrX8" | |
}, | |
"outputs": [], | |
"source": [ | |
"neuron = TrainableNeuron(2) # TODO Instantiate Trainable Neuron\n", | |
"\n", | |
"print('Pesos iniciales (aleatorios):')\n", | |
"neuron.synaptic_weights" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "WnuCP6eHxtQk" | |
}, | |
"outputs": [], | |
"source": [ | |
"# Modifiquemos el número de épocas de entranemiento para ver el\n", | |
"# performance de la neurona.\n", | |
"epochs = 10000\n", | |
"\n", | |
"# Entrenamos la neurona por tantas épocas\n", | |
"history = neuron.train(training_inputs, training_output, epochs)\n", | |
"\n", | |
"print('Pesos después del entrenamiento (aleatorios):')\n", | |
"neuron.synaptic_weights" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "8vhWL1nLnZ-R" | |
}, | |
"outputs": [], | |
"source": [ | |
"import plotly.express as px\n", | |
"\n", | |
"\n", | |
"eje_x = np.arange(len(history))\n", | |
"\n", | |
"fig = px.line(\n", | |
" x=eje_x,\n", | |
" y=history,\n", | |
" title='Historia de entrenamiento',\n", | |
" labels=dict(x='Épocas', y='Error')\n", | |
")\n", | |
"fig.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "_rp9_fj1FKkT" | |
}, | |
"outputs": [], | |
"source": [ | |
"import plotly.graph_objects as go\n", | |
"\n", | |
"\n", | |
"# Construcción de una rejilla\n", | |
"x = np.linspace(-0.1, 1.1, 201)\n", | |
"y = np.linspace(-0.1, 1.1, 201)\n", | |
"xy = np.meshgrid(x, y)\n", | |
"zz = np.array(list(zip(*(x.flat for x in xy))))\n", | |
"\n", | |
"# Predicción en la rejilla de valores\n", | |
"surface = neuron.predict(zz).flatten()\n", | |
"\n", | |
"fig = go.Figure(data=[go.Scatter3d(\n", | |
" x=zz[:, 0],\n", | |
" y=zz[:, 1],\n", | |
" z=surface,\n", | |
" mode='markers',\n", | |
" marker=dict(\n", | |
" size=1,\n", | |
" color=surface,\n", | |
" colorscale='Viridis',\n", | |
" opacity=0.8\n", | |
" )\n", | |
")])\n", | |
"\n", | |
"# Tight layout\n", | |
"fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))\n", | |
"fig.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# Problema de separabilidad lineal" | |
], | |
"metadata": { | |
"id": "R4BLI6Jxom0H" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import numpy as np\n", | |
"\n", | |
"\n", | |
"x = np.array([(0, 0), (1, 0), (0, 1), (1, 1)])\n", | |
"y = np.array([0, 1, 1, 0])" | |
], | |
"metadata": { | |
"id": "GbI7sI3GRLvO" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import tensorflow as tf\n", | |
"\n", | |
"\n", | |
"np.random.seed(321)\n", | |
"model = tf.keras.models.Sequential([\n", | |
" tf.keras.layers.Dense(2, activation='tanh', input_shape=(2, )),\n", | |
" tf.keras.layers.Dense(1, activation='sigmoid')\n", | |
"])" | |
], | |
"metadata": { | |
"id": "7UId24PBOUdL" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model.predict([[1, 1]])" | |
], | |
"metadata": { | |
"id": "YDrqviMejqWL" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"loss = tf.keras.losses.MeanSquaredError()" | |
], | |
"metadata": { | |
"id": "mFFUMXpxZo0x" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"$$ \\mathrm{MSE}=\\frac{1}{N}\\cdot\\sum_{i=1}^N \\left(y_i- \\hat{y}_i \\right )^2 $$" | |
], | |
"metadata": { | |
"id": "BP04BMThkScl" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"loss([1], [0])" | |
], | |
"metadata": { | |
"id": "0A56ubLybjZL" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"optimizer = tf.keras.optimizers.SGD(learning_rate=0.6)\n", | |
"\n", | |
"model.compile(optimizer=optimizer, loss='mse', metrics=[loss])" | |
], | |
"metadata": { | |
"id": "MFK38I_GbOyN" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"model.summary()" | |
], | |
"metadata": { | |
"id": "wT5kqY2YcbVv" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"history = model.fit(x, y, epochs=1000)" | |
], | |
"metadata": { | |
"id": "fDQ4fPJxbtbO" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import plotly.express as px\n", | |
"\n", | |
"\n", | |
"losses = history.history['loss']\n", | |
"eje_x = np.arange(len(losses))\n", | |
"\n", | |
"fig = px.line(\n", | |
" x=eje_x,\n", | |
" y=losses,\n", | |
" title='Historia de entrenamiento',\n", | |
" labels=dict(x='Épocas', y='Error')\n", | |
")\n", | |
"fig.show()" | |
], | |
"metadata": { | |
"id": "cHBNrHSMXKhW" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# Construcción de una rejilla\n", | |
"x = np.linspace(-0.1, 1.1, 201)\n", | |
"y = np.linspace(-0.1, 1.1, 201)\n", | |
"xy = np.meshgrid(x, y)\n", | |
"zz = np.array(list(zip(*(x.flat for x in xy))))\n", | |
"\n", | |
"# Predicción en la rejilla de valores\n", | |
"surface = model.predict(zz)\n", | |
"surface = surface.flatten()" | |
], | |
"metadata": { | |
"id": "hvSrcyLDaTxc" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import plotly.graph_objects as go\n", | |
"\n", | |
"\n", | |
"fig = go.Figure(data=[go.Scatter3d(\n", | |
" x=zz[:, 0],\n", | |
" y=zz[:, 1],\n", | |
" z=surface,\n", | |
" mode='markers',\n", | |
" marker=dict(\n", | |
" size=1,\n", | |
" color=surface,\n", | |
" colorscale='Viridis',\n", | |
" opacity=0.8\n", | |
" )\n", | |
")])\n", | |
"\n", | |
"# Tight layout\n", | |
"fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))\n", | |
"fig.show()" | |
], | |
"metadata": { | |
"id": "Z98QLtC8cZH5" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"---" | |
], | |
"metadata": { | |
"id": "9tNPxEQFn7Io" | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment