tensorflow / decision-forests

A collection of state-of-the-art algorithms for the training, serving and interpretation of Decision Forest models in Keras.
Apache License 2.0
663 stars 110 forks source link

I run example ,but got error #122

Closed whk6688 closed 2 years ago

whk6688 commented 2 years ago

Traceback (most recent call last): File "mydf.py", line 29, in model.fit(x=train_ds) File "/home/wanghaikuan/anaconda3/envs/python37/lib/python3.7/site-packages/tensorflow_decision_forests/keras/core.py", line 1535, in fit class_weight=class_weight) File "/home/wanghaikuan/anaconda3/envs/python37/lib/python3.7/site-packages/tensorflow_decision_forests/keras/core.py", line 1668, in _fit_implementation iterator) File "/home/wanghaikuan/anaconda3/envs/python37/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py", line 587, in _method_wrapper result = method(self, *args, **kwargs) File "/home/wanghaikuan/anaconda3/envs/python37/lib/python3.7/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler raise e.with_traceback(filtered_tb) from None File "/home/wanghaikuan/anaconda3/envs/python37/lib/python3.7/site-packages/tensorflow_decision_forests/keras/core.py", line 1554, in _consumes_training_examples_until_eof num_examples += self.train_step(data) File "/home/wanghaikuan/anaconda3/envs/python37/lib/python3.7/site-packages/tensorflow_decision_forests/keras/core.py", line 1027, in train_step return self.collect_data_step(data, is_training_example=True) File "/home/wanghaikuan/anaconda3/envs/python37/lib/python3.7/site-packages/tensorflow_decision_forests/keras/core.py", line 1236, in collect_data_step if not self._is_trained: tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: Using a symbolic tf.Tensor as a Python bool is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.

-------------------------------------------------------- my code is : import numpy as np import pandas as pd import tensorflow as tf import tensorflow_decision_forests as tfdf

print("Found TF-DF v" + tfdf.version)

dataset_path = tf.keras.utils.get_file( "adult.csv", "https://raw.githubusercontent.com/google/yggdrasil-decision-forests/" "main/yggdrasil_decision_forests/test_data/dataset/adult.csv")

dataset_df = pd.read_csv(dataset_path) # "df" for Pandas's DataFrame.

print("First 3 examples:") print(dataset_df.head(3))

test_indices = np.random.rand(len(dataset_df)) < 0.30 test_ds_pd = dataset_df[test_indices] train_ds_pd = dataset_df[~test_indices] print(f"{len(train_ds_pd)} examples in training" f", {len(test_ds_pd)} examples for testing.")

train_ds = tfdf.keras.pd_dataframe_to_tf_dataset(train_ds_pd, label="income") test_ds = tfdf.keras.pd_dataframe_to_tf_dataset(test_ds_pd, label="income")

model = tfdf.keras.RandomForestModel(verbose=2) model.fit(x=train_ds)

i need you help, thanks

rstz commented 2 years ago

Hi,

thank you for reporting this! Could you please tell us your operating system, the tfdf version and the tensorflow version you're using?

whk6688 commented 2 years ago

tensorflow 2.9.1 tensorflow-decision-forests 0.2.7 tensorflow-estimator 2.9.0 tensorflow-io-gcs-filesystem 0.26.0

Linux version 4.18.0-147.el8.x86_64 (mockbuild@x86-vm-09.build.eng.bos.redhat.com) (gcc version 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)) #1 SMP Thu Sep 26 15:52:44 UTC 2019

tensorflow-decision-forests 0.2.7

whk6688 commented 2 years ago

can you help me?

achoum commented 2 years ago

Hi @whk6688,

This error could to indicate that TensorFlow is running in TensorFlow v1 compatibility mode, while TF-DF training is only compatible with TensorFlow v2.

Is it possible that your python env or that other libraries loaded in your colab kernel are depending on TensorFlow v1 (and therefore load TensorFlow with v1 compatibility mode)?

Things to try:

a = tf.Variable(False, trainable=False, name="is_trained")
if  a:
  print("A is true")
else:
  print("A is false")
whk6688 commented 2 years ago

yes, i have other projects which use tf v1. when i install tensorflow_decision_forests, it seems install tf v2 automatically. can you tell me how to forbid tf v1? thanks

whk6688 commented 2 years ago

the result is : 'A is true'

rstz commented 2 years ago

I believe that pip's --force-reinstall might be what you're looking force. Since this is more of a Python question, I'll close this discussion for now.