Pomoc chronić Wielkiej Rafy Koralowej z TensorFlow na Kaggle Dołącz Wyzwanie

Gotowe estymatory

Zobacz na TensorFlow.org Uruchom w Google Colab Wyświetl źródło na GitHub Pobierz notatnik

Ten samouczek pokazuje, jak rozwiązać problem klasyfikacji tęczówki w TensorFlow za pomocą estymatorów. Estymator to starsza, wysokopoziomowa reprezentacja kompletnego modelu w TensorFlow. Aby uzyskać więcej informacji, zobacz Estymatory .

Najpierw najważniejsze rzeczy

Aby rozpocząć, najpierw zaimportujesz TensorFlow i kilka potrzebnych bibliotek.

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

Zestaw danych

Przykładowy program w tym dokumencie buduje i testuje model, który klasyfikuje kwiaty irysa na trzy różne gatunki na podstawie wielkości ich działek i płatków .

Wytrenujesz model przy użyciu zestawu danych Iris. Zestaw danych Iris zawiera cztery cechy i jedną etykietę . Cztery cechy identyfikują następujące cechy botaniczne poszczególnych kwiatów irysa:

  • długość kielicha
  • sepal szerokość
  • długość płatka
  • szerokość płatka

Na podstawie tych informacji możesz zdefiniować kilka stałych przydatnych do analizowania danych:

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

Następnie pobierz i przeanalizuj zestaw danych Iris za pomocą Keras i Pandas. Pamiętaj, że przechowujesz różne zestawy danych do trenowania i testowania.

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

Możesz sprawdzić swoje dane, aby zobaczyć, że masz cztery zmiennoprzecinkowe kolumny funkcji i jedną etykietę int32.

train.head()

Dla każdego zestawu danych podziel etykiety, które model będzie wytrenowany do przewidywania.

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

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

Przegląd programowania z estymatorami

Teraz, gdy masz już skonfigurowane dane, możesz zdefiniować model za pomocą estymatora TensorFlow. Estimator to dowolna klasa wywodząca się z tf.estimator.Estimator . TensorFlow udostępnia kolekcję tf.estimator (na przykład LinearRegressor ) do implementacji typowych algorytmów ML. Poza tym możesz napisać własne niestandardowe estymatory . Zaleca się korzystanie z gotowych estymatorów na początku.

Aby napisać program TensorFlow w oparciu o gotowe Estymatory, musisz wykonać następujące zadania:

  • Utwórz jedną lub więcej funkcji wejściowych.
  • Zdefiniuj kolumny elementów modelu.
  • Utwórz wystąpienie estymatora, określając kolumny funkcji i różne hiperparametry.
  • Wywołaj co najmniej jedną metodę w obiekcie Estimator, przekazując odpowiednią funkcję wejściową jako źródło danych.

Zobaczmy, jak te zadania są realizowane dla klasyfikacji tęczówki.

Utwórz funkcje wejściowe

Musisz utworzyć funkcje wejściowe, aby dostarczać dane do uczenia, oceny i przewidywania.

Funkcja wejściowa to funkcja, która zwraca obiekt tf.data.Dataset , który wyprowadza następującą dwuelementową krotkę:

  • features - Słownik Pythona, w którym:
    • Każdy klucz to nazwa funkcji.
    • Każda wartość jest tablicą zawierającą wszystkie wartości tej funkcji.
  • label — tablica zawierająca wartości etykiety dla każdego przykładu.

Aby zademonstrować format funkcji input, oto prosta implementacja:

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

Twoja funkcja wejściowa może generować słownik features i listę label w dowolny sposób. Zaleca się jednak korzystanie z interfejsu API Dataset firmy TensorFlow, który może analizować wszystkie rodzaje danych.

Interfejs Dataset API poradzi sobie z wieloma typowymi przypadkami. Na przykład, korzystając z interfejsu Dataset API, możesz łatwo odczytywać równolegle rekordy z dużej kolekcji plików i łączyć je w jeden strumień.

Aby uprościć ten przykład, załadujesz dane za pomocą pandas i zbudujesz potok wejściowy z tych danych w pamięci:

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)

Zdefiniuj kolumny funkcji

Kolumna cech to obiekt opisujący sposób, w jaki model powinien wykorzystywać surowe dane wejściowe ze słownika cech. Tworząc model estymatora, przekazujesz mu listę kolumn funkcji, które opisują wszystkie funkcje, których ma używać model. Moduł tf.feature_column udostępnia wiele opcji reprezentacji danych w modelu.

W przypadku Iris te 4 surowe funkcje są wartościami liczbowymi, więc utworzysz listę kolumn funkcji, aby poinformować model Estimator, aby reprezentował każdą z czterech funkcji jako 32-bitowe wartości zmiennoprzecinkowe. Dlatego kod do utworzenia kolumny funkcji to:

# 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))

Kolumny funkcji mogą być znacznie bardziej wyrafinowane niż te pokazane tutaj. Więcej informacji o kolumnach funkcji można znaleźć w tym przewodniku .

Teraz, gdy masz już opis, w jaki sposób model ma reprezentować surowe cechy, możesz zbudować estymator.

Utwórz estymator

Problem tęczówki to klasyczny problem klasyfikacyjny. Na szczęście TensorFlow udostępnia kilka gotowych estymatorów klasyfikatorów, w tym:

W przypadku problemu tęczówki najlepszym wyborem wydaje się tf.estimator.DNNClassifier . Oto jak utworzyłeś wystąpienie tego estymatora:

# 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}

Trenuj, oceniaj i prognozuj

Teraz, gdy masz już obiekt Estimator, możesz wywołać metody, aby wykonać następujące czynności:

  • Trenuj modelkę.
  • Oceń wyszkolony model.
  • Użyj wytrenowanego modelu, aby dokonać prognoz.

Trenuj modelkę

Trenuj model, wywołując metodę train estymatora w następujący sposób:

# 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>

Należy zauważyć, że input_fn wywołanie input_fn w lambda , aby przechwycić argumenty, jednocześnie udostępniając funkcję input, która nie przyjmuje żadnych argumentów, zgodnie z oczekiwaniami estymatora. Argument steps mówi metodzie, aby zatrzymać trenowanie po kilku krokach trenowania.

Oceń wyszkolony model

Teraz, gdy model został przeszkolony, możesz uzyskać statystyki dotyczące jego wydajności. Poniższy blok kodu ocenia dokładność wytrenowanego modelu na danych testowych:

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

W przeciwieństwie do wywołania metody train , argument steps nie został przekazany do oceny. input_fn dla eval zwraca tylko jedną epokę danych.

Słownik eval_result zawiera również średnią average_loss (średnią stratę na próbkę), loss (średnią stratę na mini-partię) oraz wartość global_step estymatora (liczbę iteracji uczących, które przeszedł).

Wykonywanie prognoz (wnioskowanie) z wytrenowanego modelu

Masz teraz wyszkolony model, który daje dobre wyniki oceny. Teraz możesz użyć wytrenowanego modelu, aby przewidzieć gatunek kwiatu tęczówki na podstawie niektórych nieoznakowanych pomiarów. Podobnie jak w przypadku treningu i oceny, prognozy dokonujesz za pomocą pojedynczego wywołania funkcji:

# 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))

Metoda predict zwraca iterację Pythona, dając słownik wyników przewidywania dla każdego przykładu. Poniższy kod wyświetla kilka prognoz i ich prawdopodobieństw:

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)