first commit
This commit is contained in:
commit
dd9bfc760d
1 file changed
+238
@@ -0,0 +1,238 @@
|
||||
import argparse
|
||||
import os
|
||||
import time
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Parámetros
|
||||
# ---------------------------------------------------------
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--threads",
|
||||
type=int,
|
||||
default=4,
|
||||
help="Número de hilos usados por TensorFlow"
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
THREADS = args.threads
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Importar TensorFlow
|
||||
# ---------------------------------------------------------
|
||||
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Configuración de hilos
|
||||
# ---------------------------------------------------------
|
||||
|
||||
tf.config.threading.set_intra_op_parallelism_threads(THREADS)
|
||||
|
||||
tf.config.threading.set_inter_op_parallelism_threads(1)
|
||||
|
||||
|
||||
print("=" * 60)
|
||||
|
||||
print("DEMOSTRACIÓN DE TENSORFLOW Y MULTIHILO")
|
||||
|
||||
print("=" * 60)
|
||||
|
||||
print(f"PID del proceso Python: {os.getpid()}")
|
||||
|
||||
print(f"Hilos TensorFlow configurados: {THREADS}")
|
||||
|
||||
print(f"CPU disponibles: {os.cpu_count()}")
|
||||
|
||||
print("=" * 60)
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Crear dataset sintético
|
||||
# ---------------------------------------------------------
|
||||
|
||||
NUM_SAMPLES = 100000
|
||||
|
||||
NUM_FEATURES = 100
|
||||
|
||||
NUM_CLASSES = 10
|
||||
|
||||
|
||||
print("\nGenerando dataset...")
|
||||
|
||||
|
||||
np.random.seed(42)
|
||||
|
||||
|
||||
X = np.random.randn(
|
||||
NUM_SAMPLES,
|
||||
NUM_FEATURES
|
||||
).astype("float32")
|
||||
|
||||
|
||||
# Creamos una matriz que simula una función real
|
||||
W_real = np.random.randn(
|
||||
NUM_FEATURES,
|
||||
NUM_CLASSES
|
||||
).astype("float32")
|
||||
|
||||
|
||||
logits = X @ W_real
|
||||
|
||||
|
||||
y = np.argmax(
|
||||
logits,
|
||||
axis=1
|
||||
).astype("int32")
|
||||
|
||||
|
||||
print("Dataset generado")
|
||||
|
||||
print("Entradas:", X.shape)
|
||||
|
||||
print("Etiquetas:", y.shape)
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Dataset TensorFlow
|
||||
# ---------------------------------------------------------
|
||||
|
||||
dataset = tf.data.Dataset.from_tensor_slices(
|
||||
(X, y)
|
||||
)
|
||||
|
||||
|
||||
dataset = dataset.shuffle(
|
||||
10000
|
||||
)
|
||||
|
||||
|
||||
dataset = dataset.batch(
|
||||
256
|
||||
)
|
||||
|
||||
|
||||
dataset = dataset.prefetch(
|
||||
tf.data.AUTOTUNE
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Crear red neuronal
|
||||
# ---------------------------------------------------------
|
||||
|
||||
model = tf.keras.Sequential([
|
||||
|
||||
tf.keras.layers.Input(
|
||||
shape=(NUM_FEATURES,)
|
||||
),
|
||||
|
||||
tf.keras.layers.Dense(
|
||||
512,
|
||||
activation="relu"
|
||||
),
|
||||
|
||||
tf.keras.layers.Dense(
|
||||
256,
|
||||
activation="relu"
|
||||
),
|
||||
|
||||
tf.keras.layers.Dense(
|
||||
128,
|
||||
activation="relu"
|
||||
),
|
||||
|
||||
tf.keras.layers.Dense(
|
||||
NUM_CLASSES,
|
||||
activation="softmax"
|
||||
)
|
||||
|
||||
])
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Compilar modelo
|
||||
# ---------------------------------------------------------
|
||||
|
||||
model.compile(
|
||||
|
||||
optimizer="adam",
|
||||
|
||||
loss="sparse_categorical_crossentropy",
|
||||
|
||||
metrics=["accuracy"]
|
||||
|
||||
)
|
||||
|
||||
|
||||
model.summary()
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Entrenamiento
|
||||
# ---------------------------------------------------------
|
||||
|
||||
print("\nIniciando entrenamiento...")
|
||||
|
||||
print(
|
||||
"Mientras se ejecuta revise:"
|
||||
)
|
||||
|
||||
print(
|
||||
"htop"
|
||||
)
|
||||
|
||||
print(
|
||||
"ps -L -p",
|
||||
os.getpid()
|
||||
)
|
||||
|
||||
|
||||
inicio = time.time()
|
||||
|
||||
|
||||
history = model.fit(
|
||||
|
||||
dataset,
|
||||
|
||||
epochs=8,
|
||||
|
||||
verbose=1
|
||||
|
||||
)
|
||||
|
||||
|
||||
fin = time.time()
|
||||
|
||||
|
||||
# ---------------------------------------------------------
|
||||
# Resultados
|
||||
# ---------------------------------------------------------
|
||||
|
||||
tiempo = fin - inicio
|
||||
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
|
||||
print("ENTRENAMIENTO TERMINADO")
|
||||
|
||||
print("=" * 60)
|
||||
|
||||
print(
|
||||
f"Hilos utilizados: {THREADS}"
|
||||
)
|
||||
|
||||
print(
|
||||
f"Tiempo total: {tiempo:.2f} segundos"
|
||||
)
|
||||
|
||||
print(
|
||||
f"Accuracy final: "
|
||||
f"{history.history['accuracy'][-1]:.4f}"
|
||||
)
|
||||
|
||||
print("=" * 60)
|
||||
Reference in new issue
Block a user