text
stringlengths
0
4.99k
model = create_model()
Run training and evaluation experiment
# Compile the model.
model.compile(
optimizer=keras.optimizers.Adagrad(learning_rate=0.01),
loss=keras.losses.MeanSquaredError(),
metrics=[keras.metrics.MeanAbsoluteError()],
)
# Read the training data.
train_dataset = get_dataset_from_csv(\"train_data.csv\", shuffle=True, batch_size=265)
# Fit the model with the training data.
model.fit(train_dataset, epochs=5)
# Read the test data.
test_dataset = get_dataset_from_csv(\"test_data.csv\", batch_size=265)
# Evaluate the model on the test data.
_, rmse = model.evaluate(test_dataset, verbose=0)
print(f\"Test MAE: {round(rmse, 3)}\")
Epoch 1/5
1598/1598 [==============================] - 46s 27ms/step - loss: 1.6617 - mean_absolute_error: 0.9981
Epoch 2/5
1598/1598 [==============================] - 43s 27ms/step - loss: 1.0282 - mean_absolute_error: 0.8101
Epoch 3/5
1598/1598 [==============================] - 43s 27ms/step - loss: 0.9609 - mean_absolute_error: 0.7812
Epoch 4/5
1598/1598 [==============================] - 43s 27ms/step - loss: 0.9272 - mean_absolute_error: 0.7675
Epoch 5/5
1598/1598 [==============================] - 43s 27ms/step - loss: 0.9062 - mean_absolute_error: 0.7588
Test MAE: 0.761
You should achieve a Mean Absolute Error (MAE) at or around 0.7 on the test data.
Conclusion
The BST model uses the Transformer layer in its architecture to capture the sequential signals underlying users’ behavior sequences for recommendation.
You can try training this model with different configurations, for example, by increasing the input sequence length and training the model for a larger number of epochs. In addition, you can try including other features like movie release year and customer zipcode, and including cross features like sex X genre.
Using Gated Residual and Variable Selection Networks for income level prediction.
Introduction
This example demonstrates the use of Gated Residual Networks (GRN) and Variable Selection Networks (VSN), proposed by Bryan Lim et al. in Temporal Fusion Transformers (TFT) for Interpretable Multi-horizon Time Series Forecasting, for structured data classification. GRNs give the flexibility to the model to apply non-linear processing only where needed. VSNs allow the model to softly remove any unnecessary noisy inputs which could negatively impact performance. Together, those techniques help improving the learning capacity of deep neural network models.
Note that this example implements only the GRN and VSN components described in in the paper, rather than the whole TFT model, as GRN and VSN can be useful on their own for structured data learning tasks.
To run the code you need to use TensorFlow 2.3 or higher.
The dataset
This example uses the United States Census Income Dataset provided by the UC Irvine Machine Learning Repository. The task is binary classification to determine whether a person makes over 50K a year.
The dataset includes ~300K instances with 41 input features: 7 numerical features and 34 categorical features.
Setup
import math
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
Prepare the data
First we load the data from the UCI Machine Learning Repository into a Pandas DataFrame.
# Column names.
CSV_HEADER = [
\"age\",
\"class_of_worker\",
\"detailed_industry_recode\",
\"detailed_occupation_recode\",
\"education\",
\"wage_per_hour\",
\"enroll_in_edu_inst_last_wk\",
\"marital_stat\",
\"major_industry_code\",
\"major_occupation_code\",
\"race\",
\"hispanic_origin\",
\"sex\",
\"member_of_a_labor_union\",
\"reason_for_unemployment\",
\"full_or_part_time_employment_stat\",
\"capital_gains\",
\"capital_losses\",
\"dividends_from_stocks\",
\"tax_filer_stat\",
\"region_of_previous_residence\",
\"state_of_previous_residence\",
\"detailed_household_and_family_stat\",
\"detailed_household_summary_in_household\",
\"instance_weight\",
\"migration_code-change_in_msa\",
\"migration_code-change_in_reg\",
\"migration_code-move_within_reg\",
\"live_in_this_house_1_year_ago\",
\"migration_prev_res_in_sunbelt\",
\"num_persons_worked_for_employer\",
\"family_members_under_18\",
\"country_of_birth_father\",
\"country_of_birth_mother\",