Skip to content

Commit

Permalink
fix: dtype conversion and typo
Browse files Browse the repository at this point in the history
  • Loading branch information
anas-rz committed Jul 31, 2023
1 parent 5076d50 commit 96d004d
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions examples/keras_io/structured_data/deep_neural_decision_forests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
## Setup
"""

import os

os.environ["KERAS_BACKEND"] = "tensorflow"

import keras_core as keras
from keras_core import layers
from keras_core.layers import StringLookup
Expand All @@ -50,8 +46,8 @@
import math


_dtype = "float32"
keras.config.set_floatx(_dtype)
_dtype = 'float32'
# keras.config.set_floatx(_dtype)

"""
## Prepare the data
Expand Down Expand Up @@ -171,33 +167,30 @@
# Create a lookup to convert a string values to an integer indices.
# Since we are not using a mask token, nor expecting any out of vocabulary
# (oov) token, we set mask_token to None and num_oov_indices to 0.
lookup = StringLookup(vocabulary=vocabulary, mask_token=None, num_oov_indices=0)
lookup = StringLookup(
vocabulary=vocabulary, mask_token=None, num_oov_indices=0
)
lookup_dict[feature_name] = lookup


def encode_categorical(batch_x, batch_y):
for feature_name in CATEGORICAL_FEATURE_NAMES:
batch_x[feature_name] = lookup_dict[feature_name](batch_x[feature_name])
batch_x[feature_name]= lookup_dict[feature_name](batch_x[feature_name])

return batch_x, batch_y


def get_dataset_from_csv(csv_file_path, shuffle=False, batch_size=128):
dataset = (
tf_data.experimental.make_csv_dataset(
csv_file_path,
batch_size=batch_size,
column_names=CSV_HEADER,
column_defaults=COLUMN_DEFAULTS,
label_name=TARGET_FEATURE_NAME,
num_epochs=1,
header=False,
na_value="?",
shuffle=shuffle,
)
.map(lambda features, target: (features, target_label_lookup(target)))
.map(encode_categorical)
)
dataset = tf_data.experimental.make_csv_dataset(
csv_file_path,
batch_size=batch_size,
column_names=CSV_HEADER,
column_defaults=COLUMN_DEFAULTS,
label_name=TARGET_FEATURE_NAME,
num_epochs=1,
header=False,
na_value="?",
shuffle=shuffle,
).map(lambda features, target: (features, target_label_lookup(target))).map(encode_categorical)

return dataset.cache()

Expand Down Expand Up @@ -283,18 +276,17 @@ def __init__(self, depth, num_features, used_features_rate, num_classes):
# Create a mask for the randomly selected features.
num_used_features = int(num_features * used_features_rate)
one_hot = np.eye(num_features)
sampled_feature_indicies = np.random.choice(
sampled_feature_indices = np.random.choice(
np.arange(num_features), num_used_features, replace=False
)
self.used_features_mask = one_hot[sampled_feature_indicies]
self.used_features_mask = ops.convert_to_tensor(one_hot[sampled_feature_indices], dtype=_dtype)

# Initialize the weights of the classes in leaves.
self.pi = self.add_weight(
initializer="random_normal",
initializer='random_normal',
shape=[self.num_leaves, self.num_classes],
dtype=_dtype,
trainable=True,
)
trainable=True,)

# Initialize the stochastic routing layer.
self.decision_fn = layers.Dense(
Expand Down Expand Up @@ -468,4 +460,4 @@ def create_forest_model():
"""
You can use the trained model hosted on [Hugging Face Hub](https://huggingface.co/keras-io/neural-decision-forest)
and try the demo on [Hugging Face Spaces](https://huggingface.co/spaces/keras-io/Neural-Decision-Forest).
"""
"""

0 comments on commit 96d004d

Please sign in to comment.