TensorFlow Decision Forests modellerini TensorFlow.js ile çalıştırma

Bu talimatlar, bir TF-DF modelinin nasıl eğitileceğini ve TensorFlow.js kullanılarak web üzerinde nasıl çalıştırılacağını açıklamaktadır.

Detaylı talimatlar

TF-DF'de bir model eğitin

Bu öğreticiyi denemek için öncelikle bir TF-DF modeline ihtiyacınız var. Kendi modelinizi kullanabilir veya Yeni Başlayanlar için eğitimle bir model eğitebilirsiniz.

Bir modeli Google Colab'da hızlı bir şekilde eğitmek istiyorsanız aşağıdaki kod pasajını kullanabilirsiniz.

!pip install tensorflow_decision_forests -U -qq
import tensorflow as tf
import tensorflow_decision_forests as tfdf
import pandas as pd

# Download the dataset, load it into a pandas dataframe and convert it to TensorFlow format.
!wget -q https://storage.googleapis.com/download.tensorflow.org/data/palmer_penguins/penguins.csv -O /tmp/penguins.csv
dataset_df = pd.read_csv("/tmp/penguins.csv")
train_ds = tfdf.keras.pd_dataframe_to_tf_dataset(dataset_df, label="species")

# Create and train the model
model_1 = tfdf.keras.GradientBoostedTreesModel()
model_1.fit(train_ds)

Modeli dönüştür

İleriye dönük talimatlarda TF-DF modelinizi /tmp/my_saved_model yolu altına kaydettiğiniz varsayılmaktadır. Modeli TensorFlow.js'ye dönüştürmek için aşağıdaki parçacığı çalıştırın.

!pip install tensorflow tensorflow_decision_forests 'tensorflowjs>=4.4.0'
!pip install tf_keras

# Prepare and load the model with TensorFlow
import tensorflow as tf
import tensorflowjs as tfjs
from google.colab import files

# Save the model in the SavedModel format
tf.saved_model.save(model_1, "/tmp/my_saved_model")

# Convert the SavedModel to TensorFlow.js and save as a zip file
tfjs.converters.tf_saved_model_conversion_v2.convert_tf_saved_model("/tmp/my_saved_model", "./tfjs_model")

# Download the converted TFJS model
!zip -r tfjs_model.zip tfjs_model/
files.download("tfjs_model.zip")

Google Colab çalışmayı tamamladığında dönüştürülen TFJS modelini zip dosyası olarak indirir. Bir sonraki adımda kullanmadan önce bu dosyayı açın.

Sıkıştırılmış bir Tensorflow.js modeli bir dizi dosyadan oluşur. Örnek model aşağıdakileri içerir:

  • varlıklar.zip
  • grup1-shard1of1.bin
  • model.json

Web'de Tensorflow.js modelini kullanın

TFJS bağımlılıklarını yüklemek ve TFDF modelini çalıştırmak için bu şablonu kullanın. Modelinizin sunulduğu model yolunu değiştirin ve executiveAsync'e verilen tensörü değiştirin.

  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.5.0/dist/tf.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-tfdf/dist/tf-tfdf.min.js"></script>
  <script>
    (async () =>{
      // Load the model.
      // Tensorflow.js currently needs the absolute path to the model including the full origin.
      const model = await tfdf.loadTFDFModel('https://path/to/unzipped/model/model.json');
      // Perform an inference
      const result = await model.executeAsync({
            "island": tf.tensor(["Torgersen"]),
            "bill_length_mm": tf.tensor([39.1]),
            "bill_depth_mm": tf.tensor([17.3]),
            "flipper_length_mm": tf.tensor([3.1]),
            "body_mass_g": tf.tensor([1000.0]),
            "sex": tf.tensor(["Female"]),
            "year": tf.tensor([2007], [1], 'int32'),
      });
      // The result is a 6-dimensional vector, the first half may be ignored
      result.print();
    })();
  </script>

Sorunuz mu var?

TensorFlow Decision Forests belgelerine ve TensorFlow.js belgelerine göz atın.