Skip to content
Pablo Rodriguez

Content Based Tensorflow

TensorFlow Implementation of Content-Based Filtering

Section titled “TensorFlow Implementation of Content-Based Filtering”

Neural Network Architecture Implementation

Section titled “Neural Network Architecture Implementation”

The content-based filtering system uses a user network and a movie network, implemented very similarly to how we have previously implemented neural networks with dense layers.

# User Network - Sequential Model
user_NN = tf.keras.models.Sequential([
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(32) # Final layer outputs 32 numbers
])

The user network:

  • Uses a sequential model
  • Has two dense hidden layers with specified number of units
  • Final layer has 32 units and outputs 32 numbers (v_u)
  • Hidden layers use ReLU activation function
# Movie/Item Network - Sequential Model
item_NN = tf.keras.models.Sequential([
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(32) # Final layer outputs 32 numbers
])

The movie network:

  • Also uses sequential model with dense hidden layers
  • Final layer outputs 32 numbers (v_m)
  • Same structure as user network in this example
# Extract user input features and compute user vector
input_user = tf.keras.layers.Input(shape=(num_user_features))
vu = user_NN(input_user)
vu = tf.linalg.l2_normalize(vu, axis=1) # Normalize to length 1

This code:

  • Extracts input features for the user
  • Feeds them to the user network defined above to compute v_u
  • Normalizes the vector v_u to have length one using L2 normalization
# Extract item input features and compute movie vector
input_item = tf.keras.layers.Input(shape=(num_item_features))
vm = item_NN(input_item)
vm = tf.linalg.l2_normalize(vm, axis=1) # Normalize to length 1

This code:

  • Extracts item/movie features
  • Feeds them to the item neural network
  • Computes the movie vector v_m
  • Normalizes that vector to have length one
# Compute dot product between user and movie vectors
output = tf.keras.layers.Dot(axes=1)([vu, vm])

After computing v_u and v_m, we take the dot product using:

  • Keras special layer type: tf.keras.layers.Dot
  • Takes dot product between the vectors v_u and v_m
  • Gives the output of the neural network (final prediction)
# Define overall model with inputs and outputs
model = tf.keras.Model([input_user, input_item], output)

This tells Keras:

  • The overall model inputs are the user features and movie/item features
  • The output is the dot product result defined above
  • Creates a unified model from the two separate networks
# Configure training
cost_fn = tf.keras.losses.MeanSquaredError()
opt = keras.optimizers.Adam(learning_rate=0.01)
model.compile(optimizer=opt, loss=cost_fn)

The cost function used to train this model is the mean squared error cost function, which is very similar to the collaborative filtering approach.

L2 Normalization

The L2 normalization step that normalizes the length of vectors v_u and v_m makes the algorithm work better. TensorFlow has the l2_normalize function that normalizes the vector, also called normalizing the L2 norm of the vector.

The implementation demonstrates how to:

  • Take multiple neural networks (user and movie networks)
  • Put them together to work in concert
  • Build a larger, more complex system
  • Use the inner product of outputs for final predictions
  • All parameters of both user and movie networks are trained simultaneously
  • No separate training procedure for individual networks
  • Single cost function optimizes the entire combined system
  • Networks are judged on how well v_u and v_m predict ratings

These code snippets represent the key components for implementing content-based filtering as a neural network. The complete implementation includes:

  • Data preprocessing and feature engineering
  • Model training with the defined architecture
  • Prediction generation for new users and items
  • Similar item finding using the learned vectors

The TensorFlow implementation provides a flexible framework for building content-based recommender systems that can incorporate rich user and item features while maintaining computational efficiency and scalability.