text
stringlengths
0
4.99k
train_data_file, shuffle=True, batch_size=batch_size
)
model.fit(train_dataset, epochs=num_epochs)
print(\"Model training finished\")
print(\"Evaluating the model on the test data...\")
test_dataset = get_dataset_from_csv(test_data_file, batch_size=batch_size)
_, accuracy = model.evaluate(test_dataset)
print(f\"Test accuracy: {round(accuracy * 100, 2)}%\")
Experiment 1: train a decision tree model
In this experiment, we train a single neural decision tree model where we use all input features.
num_trees = 10
depth = 10
used_features_rate = 1.0
num_classes = len(TARGET_LABELS)
def create_tree_model():
inputs = create_model_inputs()
features = encode_inputs(inputs)
features = layers.BatchNormalization()(features)
num_features = features.shape[1]
tree = NeuralDecisionTree(depth, num_features, used_features_rate, num_classes)
outputs = tree(features)
model = keras.Model(inputs=inputs, outputs=outputs)
return model
tree_model = create_tree_model()
run_experiment(tree_model)
123/123 [==============================] - 3s 9ms/step - loss: 0.5326 - sparse_categorical_accuracy: 0.7838
Epoch 2/10
123/123 [==============================] - 1s 9ms/step - loss: 0.3406 - sparse_categorical_accuracy: 0.8469
Epoch 3/10
123/123 [==============================] - 1s 9ms/step - loss: 0.3254 - sparse_categorical_accuracy: 0.8499
Epoch 4/10
123/123 [==============================] - 1s 9ms/step - loss: 0.3188 - sparse_categorical_accuracy: 0.8539
Epoch 5/10
123/123 [==============================] - 1s 9ms/step - loss: 0.3137 - sparse_categorical_accuracy: 0.8573
Epoch 6/10
123/123 [==============================] - 1s 9ms/step - loss: 0.3091 - sparse_categorical_accuracy: 0.8581
Epoch 7/10
123/123 [==============================] - 1s 9ms/step - loss: 0.3039 - sparse_categorical_accuracy: 0.8596
Epoch 8/10
123/123 [==============================] - 1s 9ms/step - loss: 0.2991 - sparse_categorical_accuracy: 0.8633
Epoch 9/10
123/123 [==============================] - 1s 9ms/step - loss: 0.2935 - sparse_categorical_accuracy: 0.8667
Epoch 10/10
123/123 [==============================] - 1s 9ms/step - loss: 0.2877 - sparse_categorical_accuracy: 0.8708
Model training finished
Evaluating the model on the test data...
62/62 [==============================] - 1s 5ms/step - loss: 0.3314 - sparse_categorical_accuracy: 0.8471
Test accuracy: 84.71%
Experiment 2: train a forest model
In this experiment, we train a neural decision forest with num_trees trees where each tree uses randomly selected 50% of the input features. You can control the number of features to be used in each tree by setting the used_features_rate variable. In addition, we set the depth to 5 instead of 10 compared to the previous experiment.
num_trees = 25
depth = 5
used_features_rate = 0.5
def create_forest_model():
inputs = create_model_inputs()
features = encode_inputs(inputs)
features = layers.BatchNormalization()(features)
num_features = features.shape[1]
forest_model = NeuralDecisionForest(
num_trees, depth, num_features, used_features_rate, num_classes
)
outputs = forest_model(features)
model = keras.Model(inputs=inputs, outputs=outputs)
return model
forest_model = create_forest_model()
run_experiment(forest_model)
Start training the model...
Epoch 1/10
123/123 [==============================] - 9s 7ms/step - loss: 0.5523 - sparse_categorical_accuracy: 0.7872
Epoch 2/10
123/123 [==============================] - 1s 6ms/step - loss: 0.3435 - sparse_categorical_accuracy: 0.8465
Epoch 3/10
123/123 [==============================] - 1s 6ms/step - loss: 0.3260 - sparse_categorical_accuracy: 0.8514
Epoch 4/10
123/123 [==============================] - 1s 6ms/step - loss: 0.3197 - sparse_categorical_accuracy: 0.8533
Epoch 5/10
123/123 [==============================] - 1s 6ms/step - loss: 0.3160 - sparse_categorical_accuracy: 0.8535
Epoch 6/10
123/123 [==============================] - 1s 6ms/step - loss: 0.3133 - sparse_categorical_accuracy: 0.8545
Epoch 7/10
123/123 [==============================] - 1s 6ms/step - loss: 0.3110 - sparse_categorical_accuracy: 0.8556
Epoch 8/10