TensorFlow.js के साथ TensorFlow निर्णय वन मॉडल चलाना

ये निर्देश बताते हैं कि टीएफ-डीएफ मॉडल को कैसे प्रशिक्षित किया जाए और इसे TensorFlow.js का उपयोग करके वेब पर कैसे चलाया जाए।

विस्तृत निर्देश

टीएफ-डीएफ में एक मॉडल को प्रशिक्षित करें

इस ट्यूटोरियल को आज़माने के लिए, आपको सबसे पहले एक TF-DF मॉडल की आवश्यकता होगी। आप अपने स्वयं के मॉडल का उपयोग कर सकते हैं या शुरुआती ट्यूटोरियल के साथ किसी मॉडल को प्रशिक्षित कर सकते हैं।

यदि आप Google Colab में किसी मॉडल को शीघ्रता से प्रशिक्षित करना चाहते हैं, तो आप निम्नलिखित कोड स्निपेट का उपयोग कर सकते हैं।

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

मॉडल परिवर्तित करें

आगे जाने वाले निर्देश मानते हैं कि आपने अपना TF-DF मॉडल पथ /tmp/my_saved_model के अंतर्गत सहेजा है। मॉडल को TensorFlow.js में बदलने के लिए निम्नलिखित स्निपेट चलाएँ।

!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 चलना समाप्त हो जाता है, तो यह परिवर्तित TFJS मॉडल को ज़िप फ़ाइल के रूप में डाउनलोड करता है। अगले चरण में उपयोग करने से पहले इस फ़ाइल को अनज़िप करें।

एक अनज़िप्ड Tensorflow.js मॉडल में कई फ़ाइलें होती हैं। उदाहरण मॉडल में निम्नलिखित शामिल हैं:

  • संपत्ति.ज़िप
  • Group1-shard1of1.bin
  • मॉडल.json

वेब पर Tensorflow.js मॉडल का उपयोग करें

TFJS निर्भरताएँ लोड करने और TFDF मॉडल चलाने के लिए इस टेम्पलेट का उपयोग करें। मॉडल पथ को उस स्थान पर बदलें जहां आपका मॉडल परोसा जाता है औरexecuteAsync को दिए गए टेंसर को संशोधित करें।

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

प्रशन?

TensorFlow डिसीज़न फ़ॉरेस्ट दस्तावेज़ और TensorFlow.js दस्तावेज़ देखें।