Ayuda a proteger la Gran Barrera de Coral con TensorFlow en Kaggle Únete Challenge

Estimadores prediseñados

Ver en TensorFlow.org Ejecutar en Google Colab Ver fuente en GitHub Descargar libreta

Este tutorial le muestra cómo resolver el problema de clasificación de Iris en TensorFlow usando estimadores. Un estimador es una representación de alto nivel de TensorFlow heredada de un modelo completo. Para más detalles ver Estimadores .

Lo primero es lo primero

Para comenzar, primero importará TensorFlow y una serie de bibliotecas que necesitará.

import tensorflow as tf

import pandas as pd
2021-07-09 01:21:17.647127: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0

el conjunto de datos

El programa de ejemplo de este documento crea y prueba un modelo que clasifica las flores de iris en tres especies diferentes según el tamaño de sus sépalos y pétalos .

Entrenará un modelo utilizando el conjunto de datos de Iris. El conjunto de datos de Iris contiene cuatro características y una etiqueta . Las cuatro características identifican las siguientes características botánicas de las flores individuales de Iris:

  • longitud del sépalo
  • anchura del sépalo
  • longitud del pétalo
  • ancho de pétalo

En función de esta información, puede definir algunas constantes útiles para analizar los datos:

CSV_COLUMN_NAMES = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species']
SPECIES = ['Setosa', 'Versicolor', 'Virginica']

A continuación, descargue y analice el conjunto de datos de Iris con Keras y Pandas. Tenga en cuenta que mantiene distintos conjuntos de datos para entrenamiento y prueba.

train_path = tf.keras.utils.get_file(
    "iris_training.csv", "https://storage.googleapis.com/download.tensorflow.org/data/iris_training.csv")
test_path = tf.keras.utils.get_file(
    "iris_test.csv", "https://storage.googleapis.com/download.tensorflow.org/data/iris_test.csv")

train = pd.read_csv(train_path, names=CSV_COLUMN_NAMES, header=0)
test = pd.read_csv(test_path, names=CSV_COLUMN_NAMES, header=0)
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/iris_training.csv
8192/2194 [================================================================================================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/iris_test.csv
8192/573 [============================================================================================================================================================================================================================================================================================================================================================================================================================================] - 0s 0us/step

Puede inspeccionar sus datos para ver que tiene cuatro columnas de características flotantes y una etiqueta int32.

train.head()

Para cada uno de los conjuntos de datos, divida las etiquetas, que el modelo será entrenado para predecir.

train_y = train.pop('Species')
test_y = test.pop('Species')

# The label column has now been removed from the features.
train.head()

Descripción general de la programación con Estimadores

Ahora que tiene los datos configurados, puede definir un modelo usando un Estimador de TensorFlow. Un estimador es cualquier clase derivada de tf.estimator.Estimator . TensorFlow proporciona una colección de tf.estimator (por ejemplo, LinearRegressor ) para implementar algoritmos de ML comunes. Más allá de eso, puede escribir sus propios Estimadores personalizados . Se recomienda usar Estimadores prefabricados cuando recién comienza.

Para escribir un programa de TensorFlow basado en Estimadores prefabricados, debe realizar las siguientes tareas:

  • Cree una o más funciones de entrada.
  • Defina las columnas de características del modelo.
  • Cree una instancia de un Estimador, especificando las columnas de características y varios hiperparámetros.
  • Llame a uno o más métodos en el objeto Estimator, pasando la función de entrada adecuada como fuente de los datos.

Veamos cómo se implementan esas tareas para la clasificación de Iris.

Crear funciones de entrada

Debe crear funciones de entrada para proporcionar datos para el entrenamiento, la evaluación y la predicción.

Una función de entrada es una función que devuelve un objeto tf.data.Dataset que genera la siguiente tupla de dos elementos:

  • features : un diccionario de Python en el que:
    • Cada tecla es el nombre de una función.
    • Cada valor es una matriz que contiene todos los valores de esa característica.
  • label : una matriz que contiene los valores de la etiqueta para cada ejemplo.

Solo para demostrar el formato de la función de entrada, aquí hay una implementación simple:

def input_evaluation_set():
    features = {'SepalLength': np.array([6.4, 5.0]),
                'SepalWidth':  np.array([2.8, 2.3]),
                'PetalLength': np.array([5.6, 3.3]),
                'PetalWidth':  np.array([2.2, 1.0])}
    labels = np.array([2, 1])
    return features, labels

Su función de entrada puede generar el diccionario de features y la lista de label de la forma que desee. Sin embargo, se recomienda usar la API de conjunto de datos de TensorFlow, que puede analizar todo tipo de datos.

La API de conjunto de datos puede manejar muchos casos comunes por usted. Por ejemplo, con la API de conjunto de datos, puede leer fácilmente registros de una gran colección de archivos en paralelo y unirlos en una sola secuencia.

Para simplificar las cosas en este ejemplo, cargará los datos con pandas y creará una canalización de entrada a partir de estos datos en memoria:

def input_fn(features, labels, training=True, batch_size=256):
    """An input function for training or evaluating"""
    # Convert the inputs to a Dataset.
    dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))

    # Shuffle and repeat if you are in training mode.
    if training:
        dataset = dataset.shuffle(1000).repeat()

    return dataset.batch(batch_size)

Definir las columnas de características

Una columna de características es un objeto que describe cómo el modelo debe usar los datos de entrada sin procesar del diccionario de características. Cuando crea un modelo de Estimator, le pasa una lista de columnas de características que describen cada una de las características que desea que use el modelo. El módulo tf.feature_column proporciona muchas opciones para representar datos en el modelo.

Para Iris, las 4 características sin procesar son valores numéricos, por lo que creará una lista de columnas de características para indicarle al modelo Estimator que represente cada una de las cuatro características como valores de punto flotante de 32 bits. Por lo tanto, el código para crear la columna de características es:

# Feature columns describe how to use the input.
my_feature_columns = []
for key in train.keys():
    my_feature_columns.append(tf.feature_column.numeric_column(key=key))

Las columnas de características pueden ser mucho más sofisticadas que las que se muestran aquí. Puede leer más sobre las columnas de características en esta guía .

Ahora que tiene la descripción de cómo quiere que el modelo represente las características sin procesar, puede construir el estimador.

Crear una instancia de un estimador

El problema de Iris es un problema de clasificación clásico. Afortunadamente, TensorFlow proporciona varios Estimadores clasificadores prefabricados, que incluyen:

Para el problema de Iris, tf.estimator.DNNClassifier parece ser la mejor opción. Así es como creaste una instancia de este Estimador:

# Build a DNN with 2 hidden layers with 30 and 10 hidden nodes each.
classifier = tf.estimator.DNNClassifier(
    feature_columns=my_feature_columns,
    # Two hidden layers of 30 and 10 nodes respectively.
    hidden_units=[30, 10],
    # The model must choose between 3 classes.
    n_classes=3)
2021-07-09 01:21:19.558010: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcuda.so.1
2021-07-09 01:21:20.231408: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:20.232032: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1733] Found device 0 with properties: 
pciBusID: 0000:00:05.0 name: NVIDIA Tesla V100-SXM2-16GB computeCapability: 7.0
coreClock: 1.53GHz coreCount: 80 deviceMemorySize: 15.78GiB deviceMemoryBandwidth: 836.37GiB/s
2021-07-09 01:21:20.232063: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0
2021-07-09 01:21:20.235269: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcublas.so.11
2021-07-09 01:21:20.235347: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcublasLt.so.11
2021-07-09 01:21:20.236422: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcufft.so.10
2021-07-09 01:21:20.236742: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcurand.so.10
2021-07-09 01:21:20.237708: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcusolver.so.11
2021-07-09 01:21:20.238567: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcusparse.so.11
2021-07-09 01:21:20.238730: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudnn.so.8
2021-07-09 01:21:20.238818: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:20.239441: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:20.240009: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1871] Adding visible gpu devices: 0
2021-07-09 01:21:20.240696: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-07-09 01:21:20.241238: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:20.241818: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1733] Found device 0 with properties: 
pciBusID: 0000:00:05.0 name: NVIDIA Tesla V100-SXM2-16GB computeCapability: 7.0
coreClock: 1.53GHz coreCount: 80 deviceMemorySize: 15.78GiB deviceMemoryBandwidth: 836.37GiB/s
2021-07-09 01:21:20.241888: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:20.242488: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:20.243024: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1871] Adding visible gpu devices: 0
2021-07-09 01:21:20.243060: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0
2021-07-09 01:21:20.804599: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1258] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-07-09 01:21:20.804631: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1264]      0 
2021-07-09 01:21:20.804639: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1277] 0:   N 
2021-07-09 01:21:20.804832: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:20.805481: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:20.806065: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:20.806673: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1418] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 14646 MB memory) -> physical GPU (device: 0, name: NVIDIA Tesla V100-SXM2-16GB, pci bus id: 0000:00:05.0, compute capability: 7.0)
INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpseh3yn6n
INFO:tensorflow:Using config: {'_model_dir': '/tmp/tmpseh3yn6n', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true
graph_options {
  rewrite_options {
    meta_optimizer_iterations: ONE
  }
}
, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_experimental_max_worker_delay_secs': None, '_session_creation_timeout_secs': 7200, '_checkpoint_save_graph_def': True, '_service': None, '_cluster_spec': ClusterSpec({}), '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}

Entrenar, evaluar y predecir

Ahora que tiene un objeto Estimator, puede llamar a métodos para hacer lo siguiente:

  • Entrena al modelo.
  • Evalúe el modelo entrenado.
  • Utilice el modelo entrenado para hacer predicciones.

entrenar al modelo

Entrena el modelo llamando al método de train del Estimator de la siguiente manera:

# Train the Model.
classifier.train(
    input_fn=lambda: input_fn(train, train_y, training=True),
    steps=5000)
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/training/training_util.py:236: Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version.
Instructions for updating:
Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.
INFO:tensorflow:Calling model_fn.
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/keras/optimizer_v2/adagrad.py:88: calling Constant.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
2021-07-09 01:21:21.556477: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:21.556846: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1733] Found device 0 with properties: 
pciBusID: 0000:00:05.0 name: NVIDIA Tesla V100-SXM2-16GB computeCapability: 7.0
coreClock: 1.53GHz coreCount: 80 deviceMemorySize: 15.78GiB deviceMemoryBandwidth: 836.37GiB/s
2021-07-09 01:21:21.556958: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:21.557247: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:21.557501: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1871] Adding visible gpu devices: 0
2021-07-09 01:21:21.557538: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1258] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-07-09 01:21:21.557545: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1264]      0 
2021-07-09 01:21:21.557551: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1277] 0:   N 
2021-07-09 01:21:21.557647: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:21.557945: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:21.558204: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1418] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 14646 MB memory) -> physical GPU (device: 0, name: NVIDIA Tesla V100-SXM2-16GB, pci bus id: 0000:00:05.0, compute capability: 7.0)
2021-07-09 01:21:21.573557: I tensorflow/core/platform/profile_utils/cpu_utils.cc:114] CPU Frequency: 2000179999 Hz
INFO:tensorflow:Calling checkpoint listeners before saving checkpoint 0...
INFO:tensorflow:Saving checkpoints for 0 into /tmp/tmpseh3yn6n/model.ckpt.
INFO:tensorflow:Calling checkpoint listeners after saving checkpoint 0...
2021-07-09 01:21:21.918584: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcublas.so.11
INFO:tensorflow:loss = 1.0744461, step = 0
2021-07-09 01:21:22.316159: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcublasLt.so.11
INFO:tensorflow:global_step/sec: 334.538
INFO:tensorflow:loss = 0.9813716, step = 100 (0.300 sec)
INFO:tensorflow:global_step/sec: 394.633
INFO:tensorflow:loss = 0.9640286, step = 200 (0.253 sec)
INFO:tensorflow:global_step/sec: 405.418
INFO:tensorflow:loss = 0.870553, step = 300 (0.247 sec)
INFO:tensorflow:global_step/sec: 414.009
INFO:tensorflow:loss = 0.7720107, step = 400 (0.241 sec)
INFO:tensorflow:global_step/sec: 419.099
INFO:tensorflow:loss = 0.7458334, step = 500 (0.239 sec)
INFO:tensorflow:global_step/sec: 413.423
INFO:tensorflow:loss = 0.73442066, step = 600 (0.242 sec)
INFO:tensorflow:global_step/sec: 407.202
INFO:tensorflow:loss = 0.6953498, step = 700 (0.246 sec)
INFO:tensorflow:global_step/sec: 399.47
INFO:tensorflow:loss = 0.6968536, step = 800 (0.250 sec)
INFO:tensorflow:global_step/sec: 417.937
INFO:tensorflow:loss = 0.669106, step = 900 (0.239 sec)
INFO:tensorflow:global_step/sec: 415.499
INFO:tensorflow:loss = 0.6549559, step = 1000 (0.241 sec)
INFO:tensorflow:global_step/sec: 399.254
INFO:tensorflow:loss = 0.644197, step = 1100 (0.251 sec)
INFO:tensorflow:global_step/sec: 381.26
INFO:tensorflow:loss = 0.628752, step = 1200 (0.262 sec)
INFO:tensorflow:global_step/sec: 380.687
INFO:tensorflow:loss = 0.6085156, step = 1300 (0.263 sec)
INFO:tensorflow:global_step/sec: 385.335
INFO:tensorflow:loss = 0.6050654, step = 1400 (0.259 sec)
INFO:tensorflow:global_step/sec: 379.021
INFO:tensorflow:loss = 0.5857471, step = 1500 (0.264 sec)
INFO:tensorflow:global_step/sec: 383.237
INFO:tensorflow:loss = 0.5734547, step = 1600 (0.261 sec)
INFO:tensorflow:global_step/sec: 379.347
INFO:tensorflow:loss = 0.5768546, step = 1700 (0.264 sec)
INFO:tensorflow:global_step/sec: 385.847
INFO:tensorflow:loss = 0.5602146, step = 1800 (0.259 sec)
INFO:tensorflow:global_step/sec: 374.918
INFO:tensorflow:loss = 0.5603363, step = 1900 (0.268 sec)
INFO:tensorflow:global_step/sec: 379.286
INFO:tensorflow:loss = 0.53942347, step = 2000 (0.263 sec)
INFO:tensorflow:global_step/sec: 387.69
INFO:tensorflow:loss = 0.5318261, step = 2100 (0.258 sec)
INFO:tensorflow:global_step/sec: 373.274
INFO:tensorflow:loss = 0.519292, step = 2200 (0.268 sec)
INFO:tensorflow:global_step/sec: 368.826
INFO:tensorflow:loss = 0.51804626, step = 2300 (0.271 sec)
INFO:tensorflow:global_step/sec: 381.156
INFO:tensorflow:loss = 0.49958432, step = 2400 (0.262 sec)
INFO:tensorflow:global_step/sec: 380.416
INFO:tensorflow:loss = 0.47292516, step = 2500 (0.263 sec)
INFO:tensorflow:global_step/sec: 379.532
INFO:tensorflow:loss = 0.4866906, step = 2600 (0.264 sec)
INFO:tensorflow:global_step/sec: 367.405
INFO:tensorflow:loss = 0.4665504, step = 2700 (0.272 sec)
INFO:tensorflow:global_step/sec: 393.807
INFO:tensorflow:loss = 0.46227247, step = 2800 (0.254 sec)
INFO:tensorflow:global_step/sec: 389.936
INFO:tensorflow:loss = 0.44966495, step = 2900 (0.257 sec)
INFO:tensorflow:global_step/sec: 376.884
INFO:tensorflow:loss = 0.44808808, step = 3000 (0.265 sec)
INFO:tensorflow:global_step/sec: 392.111
INFO:tensorflow:loss = 0.43817097, step = 3100 (0.255 sec)
INFO:tensorflow:global_step/sec: 392.087
INFO:tensorflow:loss = 0.43738297, step = 3200 (0.255 sec)
INFO:tensorflow:global_step/sec: 397.546
INFO:tensorflow:loss = 0.42564616, step = 3300 (0.252 sec)
INFO:tensorflow:global_step/sec: 399.665
INFO:tensorflow:loss = 0.41426587, step = 3400 (0.250 sec)
INFO:tensorflow:global_step/sec: 398.991
INFO:tensorflow:loss = 0.41321295, step = 3500 (0.251 sec)
INFO:tensorflow:global_step/sec: 400.251
INFO:tensorflow:loss = 0.41148052, step = 3600 (0.250 sec)
INFO:tensorflow:global_step/sec: 392.045
INFO:tensorflow:loss = 0.40983573, step = 3700 (0.255 sec)
INFO:tensorflow:global_step/sec: 387.784
INFO:tensorflow:loss = 0.39185163, step = 3800 (0.258 sec)
INFO:tensorflow:global_step/sec: 385.973
INFO:tensorflow:loss = 0.38712424, step = 3900 (0.259 sec)
INFO:tensorflow:global_step/sec: 397.847
INFO:tensorflow:loss = 0.3770343, step = 4000 (0.251 sec)
INFO:tensorflow:global_step/sec: 399.684
INFO:tensorflow:loss = 0.39354473, step = 4100 (0.250 sec)
INFO:tensorflow:global_step/sec: 394.732
INFO:tensorflow:loss = 0.37436056, step = 4200 (0.253 sec)
INFO:tensorflow:global_step/sec: 394.382
INFO:tensorflow:loss = 0.37443662, step = 4300 (0.254 sec)
INFO:tensorflow:global_step/sec: 381.682
INFO:tensorflow:loss = 0.35983646, step = 4400 (0.262 sec)
INFO:tensorflow:global_step/sec: 386.426
INFO:tensorflow:loss = 0.3579504, step = 4500 (0.259 sec)
INFO:tensorflow:global_step/sec: 387.776
INFO:tensorflow:loss = 0.35766554, step = 4600 (0.258 sec)
INFO:tensorflow:global_step/sec: 393.279
INFO:tensorflow:loss = 0.3629043, step = 4700 (0.254 sec)
INFO:tensorflow:global_step/sec: 396.422
INFO:tensorflow:loss = 0.34867007, step = 4800 (0.253 sec)
INFO:tensorflow:global_step/sec: 390.771
INFO:tensorflow:loss = 0.33946946, step = 4900 (0.255 sec)
INFO:tensorflow:Calling checkpoint listeners before saving checkpoint 5000...
INFO:tensorflow:Saving checkpoints for 5000 into /tmp/tmpseh3yn6n/model.ckpt.
INFO:tensorflow:Calling checkpoint listeners after saving checkpoint 5000...
INFO:tensorflow:Loss for final step: 0.34298706.
<tensorflow_estimator.python.estimator.canned.dnn.DNNClassifierV2 at 0x7f8a375c6610>

Tenga en cuenta que envuelve su llamada input_fn en una lambda para capturar los argumentos mientras proporciona una función de entrada que no toma argumentos, como lo espera el Estimador. El argumento de steps le dice al método que detenga el entrenamiento después de una serie de pasos de entrenamiento.

Evaluar el modelo entrenado

Ahora que el modelo ha sido entrenado, puede obtener algunas estadísticas sobre su rendimiento. El siguiente bloque de código evalúa la precisión del modelo entrenado en los datos de prueba:

eval_result = classifier.evaluate(
    input_fn=lambda: input_fn(test, test_y, training=False))

print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))
INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Starting evaluation at 2021-07-09T01:21:35
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from /tmp/tmpseh3yn6n/model.ckpt-5000
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Inference Time : 0.20065s
INFO:tensorflow:Finished evaluation at 2021-07-09-01:21:35
INFO:tensorflow:Saving dict for global step 5000: accuracy = 0.93333334, average_loss = 0.40179652, global_step = 5000, loss = 0.40179652
2021-07-09 01:21:35.538566: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:35.538955: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1733] Found device 0 with properties: 
pciBusID: 0000:00:05.0 name: NVIDIA Tesla V100-SXM2-16GB computeCapability: 7.0
coreClock: 1.53GHz coreCount: 80 deviceMemorySize: 15.78GiB deviceMemoryBandwidth: 836.37GiB/s
2021-07-09 01:21:35.539101: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:35.539507: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:35.539833: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1871] Adding visible gpu devices: 0
2021-07-09 01:21:35.539878: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1258] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-07-09 01:21:35.539886: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1264]      0 
2021-07-09 01:21:35.539892: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1277] 0:   N 
2021-07-09 01:21:35.540030: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:35.540370: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:35.540714: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1418] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 14646 MB memory) -> physical GPU (device: 0, name: NVIDIA Tesla V100-SXM2-16GB, pci bus id: 0000:00:05.0, compute capability: 7.0)
INFO:tensorflow:Saving 'checkpoint_path' summary for global step 5000: /tmp/tmpseh3yn6n/model.ckpt-5000

Test set accuracy: 0.933

A diferencia de la llamada al método de train , no pasó el argumento de steps para evaluar. El input_fn para eval solo produce una sola época de datos.

El diccionario eval_result también contiene la average_loss promedio (pérdida media por muestra), la loss (pérdida media por mini-lote) y el valor del paso global del global_step (el número de iteraciones de entrenamiento a las que se sometió).

Hacer predicciones (inferir) a partir del modelo entrenado

Ahora tiene un modelo entrenado que produce buenos resultados de evaluación. Ahora puede usar el modelo entrenado para predecir la especie de una flor de iris en función de algunas medidas no etiquetadas. Al igual que con el entrenamiento y la evaluación, haces predicciones usando una sola llamada de función:

# Generate predictions from the model
expected = ['Setosa', 'Versicolor', 'Virginica']
predict_x = {
    'SepalLength': [5.1, 5.9, 6.9],
    'SepalWidth': [3.3, 3.0, 3.1],
    'PetalLength': [1.7, 4.2, 5.4],
    'PetalWidth': [0.5, 1.5, 2.1],
}

def input_fn(features, batch_size=256):
    """An input function for prediction."""
    # Convert the inputs to a Dataset without labels.
    return tf.data.Dataset.from_tensor_slices(dict(features)).batch(batch_size)

predictions = classifier.predict(
    input_fn=lambda: input_fn(predict_x))

El método de predict devuelve un iterable de Python, lo que genera un diccionario de resultados de predicción para cada ejemplo. El siguiente código imprime algunas predicciones y sus probabilidades:

for pred_dict, expec in zip(predictions, expected):
    class_id = pred_dict['class_ids'][0]
    probability = pred_dict['probabilities'][class_id]

    print('Prediction is "{}" ({:.1f}%), expected "{}"'.format(
        SPECIES[class_id], 100 * probability, expec))
INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from /tmp/tmpseh3yn6n/model.ckpt-5000
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
Prediction is "Setosa" (87.5%), expected "Setosa"
Prediction is "Versicolor" (52.7%), expected "Versicolor"
Prediction is "Virginica" (64.5%), expected "Virginica"
2021-07-09 01:21:35.958955: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:35.959406: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1733] Found device 0 with properties: 
pciBusID: 0000:00:05.0 name: NVIDIA Tesla V100-SXM2-16GB computeCapability: 7.0
coreClock: 1.53GHz coreCount: 80 deviceMemorySize: 15.78GiB deviceMemoryBandwidth: 836.37GiB/s
2021-07-09 01:21:35.959597: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:35.960061: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:35.960403: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1871] Adding visible gpu devices: 0
2021-07-09 01:21:35.960461: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1258] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-07-09 01:21:35.960471: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1264]      0 
2021-07-09 01:21:35.960482: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1277] 0:   N 
2021-07-09 01:21:35.960646: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:35.961092: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-07-09 01:21:35.961439: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1418] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 14646 MB memory) -> physical GPU (device: 0, name: NVIDIA Tesla V100-SXM2-16GB, pci bus id: 0000:00:05.0, compute capability: 7.0)