text stringlengths 0 4.99k |
|---|
\"race\": sorted(list(train_data[\"race\"].unique())), |
\"gender\": sorted(list(train_data[\"gender\"].unique())), |
\"native_country\": sorted(list(train_data[\"native_country\"].unique())), |
} |
# A list of the columns to ignore from the dataset. |
IGNORE_COLUMN_NAMES = [\"fnlwgt\"] |
# A list of the categorical feature names. |
CATEGORICAL_FEATURE_NAMES = list(CATEGORICAL_FEATURES_WITH_VOCABULARY.keys()) |
# A list of all the input features. |
FEATURE_NAMES = NUMERIC_FEATURE_NAMES + CATEGORICAL_FEATURE_NAMES |
# A list of column default values for each feature. |
COLUMN_DEFAULTS = [ |
[0.0] if feature_name in NUMERIC_FEATURE_NAMES + IGNORE_COLUMN_NAMES else [\"NA\"] |
for feature_name in CSV_HEADER |
] |
# The name of the target feature. |
TARGET_FEATURE_NAME = \"income_bracket\" |
# A list of the labels of the target features. |
TARGET_LABELS = [\" <=50K\", \" >50K\"] |
Create tf.data.Dataset objects for training and validation |
We create an input function to read and parse the file, and convert features and labels into a [tf.data.Dataset](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) for training and validation. We also preprocess the input by mapping the target label to an index. |
from tensorflow.keras.layers import StringLookup |
target_label_lookup = StringLookup( |
vocabulary=TARGET_LABELS, mask_token=None, num_oov_indices=0 |
) |
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))) |
return dataset.cache() |
Create model inputs |
def create_model_inputs(): |
inputs = {} |
for feature_name in FEATURE_NAMES: |
if feature_name in NUMERIC_FEATURE_NAMES: |
inputs[feature_name] = layers.Input( |
name=feature_name, shape=(), dtype=tf.float32 |
) |
else: |
inputs[feature_name] = layers.Input( |
name=feature_name, shape=(), dtype=tf.string |
) |
return inputs |
Encode input features |
def encode_inputs(inputs): |
encoded_features = [] |
for feature_name in inputs: |
if feature_name in CATEGORICAL_FEATURE_NAMES: |
vocabulary = CATEGORICAL_FEATURES_WITH_VOCABULARY[feature_name] |
# 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 |
) |
# Convert the string input values into integer indices. |
value_index = lookup(inputs[feature_name]) |
embedding_dims = int(math.sqrt(lookup.vocabulary_size())) |
# Create an embedding layer with the specified dimensions. |
embedding = layers.Embedding( |
input_dim=lookup.vocabulary_size(), output_dim=embedding_dims |
) |
# Convert the index values to embedding representations. |
encoded_feature = embedding(value_index) |
else: |
# Use the numerical features as-is. |
encoded_feature = inputs[feature_name] |
if inputs[feature_name].shape[-1] is None: |
encoded_feature = tf.expand_dims(encoded_feature, -1) |
encoded_features.append(encoded_feature) |
encoded_features = layers.concatenate(encoded_features) |
return encoded_features |
Deep Neural Decision Tree |
A neural decision tree model has two sets of weights to learn. The first set is pi, which represents the probability distribution of the classes in the tree leaves. The second set is the weights of the routing layer decision_fn, which represents the probability of going to each leave. The forward pass of the model work... |
The model expects input features as a single vector encoding all the features of an instance in the batch. This vector can be generated from a Convolution Neural Network (CNN) applied to images or dense transformations applied to structured data features. |
The model first applies a used_features_mask to randomly select a subset of input features to use. |
Then, the model computes the probabilities (mu) for the input instances to reach the tree leaves by iteratively performing a stochastic routing throughout the tree levels. |
Finally, the probabilities of reaching the leaves are combined by the class probabilities at the leaves to produce the final outputs. |
class NeuralDecisionTree(keras.Model): |
def __init__(self, depth, num_features, used_features_rate, num_classes): |
super(NeuralDecisionTree, self).__init__() |
self.depth = depth |
self.num_leaves = 2 ** depth |
self.num_classes = num_classes |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.