Capítulo 6: Introducción a las redes neuronales y el aprendizaje profundo
6.4 Ejercicios Prácticos
Ejercicio 1: Implementar un Perceptrón
Implementa un perceptrón simple utilizando Python. Utilízalo para clasificar un conjunto de datos binario de tu elección. Puedes crear tu propio conjunto de datos o utilizar uno sencillo de una biblioteca como Scikit-learn.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Create a binary classification dataset
X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# Split the dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
# Define the Perceptron model
class Perceptron:
def __init__(self, learning_rate=0.01, n_iters=1000):
self.lr = learning_rate
self.n_iters = n_iters
self.activation_func = self._unit_step_func
self.weights = None
self.bias = None
def fit(self, X, y):
n_samples, n_features = X.shape
# init parameters
self.weights = np.zeros(n_features)
self.bias = 0
y_ = np.where(y <= 0, -1, 1)
for _ in range(self.n_iters):
for idx, x_i in enumerate(X):
linear_output = np.dot(x_i, self.weights) + self.bias
y_predicted = self.activation_func(linear_output)
# Perceptron update rule
update = self.lr * (y_[idx] - y_predicted)
self.weights += update * x_i
self.bias += update
def predict(self, X):
linear_output = np.dot(X, self.weights) + self.bias
y_predicted = self.activation_func(linear_output)
return y_predicted
def _unit_step_func(self, x):
return np.where(x >= 0, 1, -1)
# Training the Perceptron model
p = Perceptron(learning_rate=0.01, n_iters=1000)
p.fit(X_train, y_train)
# Making predictions on the test data
predictions = p.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, predictions)
print("Perceptron classification accuracy: ", accuracy)
Ejercicio 2: Implementar el Descenso de Gradiente
Implementa el algoritmo de descenso de gradiente desde cero en Python. Utilízalo para encontrar el mínimo de una función simple (como f(x) = x^2 + 5x + 6) y muestra gráficamente los pasos del algoritmo a lo largo del camino.
import numpy as np
import matplotlib.pyplot as plt
# Define the function and its derivative
def f(x):
return x**2 + 5*x + 6
def df(x):
return 2*x + 5
# Gradient descent algorithm
def gradient_descent(x_start, learning_rate, n_iters):
x = x_start
history = [x]
for _ in range(n_iters):
grad = df(x)
x -= learning_rate * grad
history.append(x)
return history
# Run the algorithm and plot the steps
history = gradient_descent(x_start=-10, learning_rate=0.1, n_iters=50)
plt.plot(history, [f(x) for x in history], 'o-')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Gradient Descent Steps')
plt.show()
Ejercicio 3: Técnicas de Regularización
Usando un conjunto de datos de tu elección, construye un modelo de aprendizaje profundo con Keras. Aplica regularización L1, L2 y dropout, y compara los resultados. ¿Cuál de los métodos funciona mejor para tu conjunto de datos?
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.regularizers import l1, l2
# Create a Sequential model
model = Sequential()
# Add an input layer and a hidden layer with L1 regularization
model.add(Dense(32, input_dim=8, activation='relu', kernel_regularizer=l1(0.01)))
# Add dropout layer
model.add(Dropout(0.5))
# Add an output layer with L2 regularization
model.add(Dense(1, activation='sigmoid', kernel_regularizer=l2(0.01)))
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, y, epochs=150, batch_size=10)
Ejercicio 4: Early Stopping y Dropout
Usando un conjunto de datos de tu elección, construye un modelo de aprendizaje profundo con Keras. Implementa la detención temprana (early stopping) y el dropout, y observa cómo afectan al rendimiento del modelo.
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.callbacks import EarlyStopping
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
# Create a binary classification dataset
X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# Split the dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
# Create a Sequential model
model = Sequential()
# Add an input layer and a hidden layer
model.add(Dense(32, input_dim=20, activation='relu'))
# Add dropout layer
model.add(Dropout(0.5))
# Add an output layer
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Define the early stopping monitor
early_stopping_monitor = EarlyStopping(patience=3)
# Fit the model
model.fit(X_train, y_train, epochs=150, batch_size=10, validation_split=0.2, callbacks=[early_stopping_monitor])
En este ejercicio, observarás cómo la detención temprana (early stopping) y el dropout pueden ayudar a prevenir el sobreajuste y mejorar la capacidad del modelo para generalizar a nuevos datos.
6.4 Ejercicios Prácticos
Ejercicio 1: Implementar un Perceptrón
Implementa un perceptrón simple utilizando Python. Utilízalo para clasificar un conjunto de datos binario de tu elección. Puedes crear tu propio conjunto de datos o utilizar uno sencillo de una biblioteca como Scikit-learn.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Create a binary classification dataset
X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# Split the dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
# Define the Perceptron model
class Perceptron:
def __init__(self, learning_rate=0.01, n_iters=1000):
self.lr = learning_rate
self.n_iters = n_iters
self.activation_func = self._unit_step_func
self.weights = None
self.bias = None
def fit(self, X, y):
n_samples, n_features = X.shape
# init parameters
self.weights = np.zeros(n_features)
self.bias = 0
y_ = np.where(y <= 0, -1, 1)
for _ in range(self.n_iters):
for idx, x_i in enumerate(X):
linear_output = np.dot(x_i, self.weights) + self.bias
y_predicted = self.activation_func(linear_output)
# Perceptron update rule
update = self.lr * (y_[idx] - y_predicted)
self.weights += update * x_i
self.bias += update
def predict(self, X):
linear_output = np.dot(X, self.weights) + self.bias
y_predicted = self.activation_func(linear_output)
return y_predicted
def _unit_step_func(self, x):
return np.where(x >= 0, 1, -1)
# Training the Perceptron model
p = Perceptron(learning_rate=0.01, n_iters=1000)
p.fit(X_train, y_train)
# Making predictions on the test data
predictions = p.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, predictions)
print("Perceptron classification accuracy: ", accuracy)
Ejercicio 2: Implementar el Descenso de Gradiente
Implementa el algoritmo de descenso de gradiente desde cero en Python. Utilízalo para encontrar el mínimo de una función simple (como f(x) = x^2 + 5x + 6) y muestra gráficamente los pasos del algoritmo a lo largo del camino.
import numpy as np
import matplotlib.pyplot as plt
# Define the function and its derivative
def f(x):
return x**2 + 5*x + 6
def df(x):
return 2*x + 5
# Gradient descent algorithm
def gradient_descent(x_start, learning_rate, n_iters):
x = x_start
history = [x]
for _ in range(n_iters):
grad = df(x)
x -= learning_rate * grad
history.append(x)
return history
# Run the algorithm and plot the steps
history = gradient_descent(x_start=-10, learning_rate=0.1, n_iters=50)
plt.plot(history, [f(x) for x in history], 'o-')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Gradient Descent Steps')
plt.show()
Ejercicio 3: Técnicas de Regularización
Usando un conjunto de datos de tu elección, construye un modelo de aprendizaje profundo con Keras. Aplica regularización L1, L2 y dropout, y compara los resultados. ¿Cuál de los métodos funciona mejor para tu conjunto de datos?
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.regularizers import l1, l2
# Create a Sequential model
model = Sequential()
# Add an input layer and a hidden layer with L1 regularization
model.add(Dense(32, input_dim=8, activation='relu', kernel_regularizer=l1(0.01)))
# Add dropout layer
model.add(Dropout(0.5))
# Add an output layer with L2 regularization
model.add(Dense(1, activation='sigmoid', kernel_regularizer=l2(0.01)))
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, y, epochs=150, batch_size=10)
Ejercicio 4: Early Stopping y Dropout
Usando un conjunto de datos de tu elección, construye un modelo de aprendizaje profundo con Keras. Implementa la detención temprana (early stopping) y el dropout, y observa cómo afectan al rendimiento del modelo.
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.callbacks import EarlyStopping
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
# Create a binary classification dataset
X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# Split the dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
# Create a Sequential model
model = Sequential()
# Add an input layer and a hidden layer
model.add(Dense(32, input_dim=20, activation='relu'))
# Add dropout layer
model.add(Dropout(0.5))
# Add an output layer
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Define the early stopping monitor
early_stopping_monitor = EarlyStopping(patience=3)
# Fit the model
model.fit(X_train, y_train, epochs=150, batch_size=10, validation_split=0.2, callbacks=[early_stopping_monitor])
En este ejercicio, observarás cómo la detención temprana (early stopping) y el dropout pueden ayudar a prevenir el sobreajuste y mejorar la capacidad del modelo para generalizar a nuevos datos.
6.4 Ejercicios Prácticos
Ejercicio 1: Implementar un Perceptrón
Implementa un perceptrón simple utilizando Python. Utilízalo para clasificar un conjunto de datos binario de tu elección. Puedes crear tu propio conjunto de datos o utilizar uno sencillo de una biblioteca como Scikit-learn.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Create a binary classification dataset
X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# Split the dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
# Define the Perceptron model
class Perceptron:
def __init__(self, learning_rate=0.01, n_iters=1000):
self.lr = learning_rate
self.n_iters = n_iters
self.activation_func = self._unit_step_func
self.weights = None
self.bias = None
def fit(self, X, y):
n_samples, n_features = X.shape
# init parameters
self.weights = np.zeros(n_features)
self.bias = 0
y_ = np.where(y <= 0, -1, 1)
for _ in range(self.n_iters):
for idx, x_i in enumerate(X):
linear_output = np.dot(x_i, self.weights) + self.bias
y_predicted = self.activation_func(linear_output)
# Perceptron update rule
update = self.lr * (y_[idx] - y_predicted)
self.weights += update * x_i
self.bias += update
def predict(self, X):
linear_output = np.dot(X, self.weights) + self.bias
y_predicted = self.activation_func(linear_output)
return y_predicted
def _unit_step_func(self, x):
return np.where(x >= 0, 1, -1)
# Training the Perceptron model
p = Perceptron(learning_rate=0.01, n_iters=1000)
p.fit(X_train, y_train)
# Making predictions on the test data
predictions = p.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, predictions)
print("Perceptron classification accuracy: ", accuracy)
Ejercicio 2: Implementar el Descenso de Gradiente
Implementa el algoritmo de descenso de gradiente desde cero en Python. Utilízalo para encontrar el mínimo de una función simple (como f(x) = x^2 + 5x + 6) y muestra gráficamente los pasos del algoritmo a lo largo del camino.
import numpy as np
import matplotlib.pyplot as plt
# Define the function and its derivative
def f(x):
return x**2 + 5*x + 6
def df(x):
return 2*x + 5
# Gradient descent algorithm
def gradient_descent(x_start, learning_rate, n_iters):
x = x_start
history = [x]
for _ in range(n_iters):
grad = df(x)
x -= learning_rate * grad
history.append(x)
return history
# Run the algorithm and plot the steps
history = gradient_descent(x_start=-10, learning_rate=0.1, n_iters=50)
plt.plot(history, [f(x) for x in history], 'o-')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Gradient Descent Steps')
plt.show()
Ejercicio 3: Técnicas de Regularización
Usando un conjunto de datos de tu elección, construye un modelo de aprendizaje profundo con Keras. Aplica regularización L1, L2 y dropout, y compara los resultados. ¿Cuál de los métodos funciona mejor para tu conjunto de datos?
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.regularizers import l1, l2
# Create a Sequential model
model = Sequential()
# Add an input layer and a hidden layer with L1 regularization
model.add(Dense(32, input_dim=8, activation='relu', kernel_regularizer=l1(0.01)))
# Add dropout layer
model.add(Dropout(0.5))
# Add an output layer with L2 regularization
model.add(Dense(1, activation='sigmoid', kernel_regularizer=l2(0.01)))
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, y, epochs=150, batch_size=10)
Ejercicio 4: Early Stopping y Dropout
Usando un conjunto de datos de tu elección, construye un modelo de aprendizaje profundo con Keras. Implementa la detención temprana (early stopping) y el dropout, y observa cómo afectan al rendimiento del modelo.
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.callbacks import EarlyStopping
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
# Create a binary classification dataset
X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# Split the dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
# Create a Sequential model
model = Sequential()
# Add an input layer and a hidden layer
model.add(Dense(32, input_dim=20, activation='relu'))
# Add dropout layer
model.add(Dropout(0.5))
# Add an output layer
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Define the early stopping monitor
early_stopping_monitor = EarlyStopping(patience=3)
# Fit the model
model.fit(X_train, y_train, epochs=150, batch_size=10, validation_split=0.2, callbacks=[early_stopping_monitor])
En este ejercicio, observarás cómo la detención temprana (early stopping) y el dropout pueden ayudar a prevenir el sobreajuste y mejorar la capacidad del modelo para generalizar a nuevos datos.
6.4 Ejercicios Prácticos
Ejercicio 1: Implementar un Perceptrón
Implementa un perceptrón simple utilizando Python. Utilízalo para clasificar un conjunto de datos binario de tu elección. Puedes crear tu propio conjunto de datos o utilizar uno sencillo de una biblioteca como Scikit-learn.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Create a binary classification dataset
X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# Split the dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
# Define the Perceptron model
class Perceptron:
def __init__(self, learning_rate=0.01, n_iters=1000):
self.lr = learning_rate
self.n_iters = n_iters
self.activation_func = self._unit_step_func
self.weights = None
self.bias = None
def fit(self, X, y):
n_samples, n_features = X.shape
# init parameters
self.weights = np.zeros(n_features)
self.bias = 0
y_ = np.where(y <= 0, -1, 1)
for _ in range(self.n_iters):
for idx, x_i in enumerate(X):
linear_output = np.dot(x_i, self.weights) + self.bias
y_predicted = self.activation_func(linear_output)
# Perceptron update rule
update = self.lr * (y_[idx] - y_predicted)
self.weights += update * x_i
self.bias += update
def predict(self, X):
linear_output = np.dot(X, self.weights) + self.bias
y_predicted = self.activation_func(linear_output)
return y_predicted
def _unit_step_func(self, x):
return np.where(x >= 0, 1, -1)
# Training the Perceptron model
p = Perceptron(learning_rate=0.01, n_iters=1000)
p.fit(X_train, y_train)
# Making predictions on the test data
predictions = p.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, predictions)
print("Perceptron classification accuracy: ", accuracy)
Ejercicio 2: Implementar el Descenso de Gradiente
Implementa el algoritmo de descenso de gradiente desde cero en Python. Utilízalo para encontrar el mínimo de una función simple (como f(x) = x^2 + 5x + 6) y muestra gráficamente los pasos del algoritmo a lo largo del camino.
import numpy as np
import matplotlib.pyplot as plt
# Define the function and its derivative
def f(x):
return x**2 + 5*x + 6
def df(x):
return 2*x + 5
# Gradient descent algorithm
def gradient_descent(x_start, learning_rate, n_iters):
x = x_start
history = [x]
for _ in range(n_iters):
grad = df(x)
x -= learning_rate * grad
history.append(x)
return history
# Run the algorithm and plot the steps
history = gradient_descent(x_start=-10, learning_rate=0.1, n_iters=50)
plt.plot(history, [f(x) for x in history], 'o-')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Gradient Descent Steps')
plt.show()
Ejercicio 3: Técnicas de Regularización
Usando un conjunto de datos de tu elección, construye un modelo de aprendizaje profundo con Keras. Aplica regularización L1, L2 y dropout, y compara los resultados. ¿Cuál de los métodos funciona mejor para tu conjunto de datos?
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.regularizers import l1, l2
# Create a Sequential model
model = Sequential()
# Add an input layer and a hidden layer with L1 regularization
model.add(Dense(32, input_dim=8, activation='relu', kernel_regularizer=l1(0.01)))
# Add dropout layer
model.add(Dropout(0.5))
# Add an output layer with L2 regularization
model.add(Dense(1, activation='sigmoid', kernel_regularizer=l2(0.01)))
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, y, epochs=150, batch_size=10)
Ejercicio 4: Early Stopping y Dropout
Usando un conjunto de datos de tu elección, construye un modelo de aprendizaje profundo con Keras. Implementa la detención temprana (early stopping) y el dropout, y observa cómo afectan al rendimiento del modelo.
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.callbacks import EarlyStopping
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
# Create a binary classification dataset
X, y = make_classification(n_samples=1000, n_features=20, n_informative=15, n_redundant=5, random_state=7)
# Split the dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
# Create a Sequential model
model = Sequential()
# Add an input layer and a hidden layer
model.add(Dense(32, input_dim=20, activation='relu'))
# Add dropout layer
model.add(Dropout(0.5))
# Add an output layer
model.add(Dense(1, activation='sigmoid'))
# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Define the early stopping monitor
early_stopping_monitor = EarlyStopping(patience=3)
# Fit the model
model.fit(X_train, y_train, epochs=150, batch_size=10, validation_split=0.2, callbacks=[early_stopping_monitor])
En este ejercicio, observarás cómo la detención temprana (early stopping) y el dropout pueden ayudar a prevenir el sobreajuste y mejorar la capacidad del modelo para generalizar a nuevos datos.