Table Of Contents
This sample, sampleMovieLens, is an end-to-end sample that imports a trained TensorFlow model and predicts the highest rated movie for each user. This sample demonstrates a simple movie recommender system using a multi-layer perceptron (MLP) based Neural Collaborative Filter (NCF) recommender.
Specifically, this sample demonstrates how to generate weights for a MovieLens dataset that TensorRT can then accelerate.
The network is trained in TensorFlow on the MovieLens dataset containing 6,040 users and 3,706 movies. The NCF recommender system is based off of the Neural Collaborative Filtering paper.
Each query to the network consists of a userID
and list of MovieIDs
. The network predicts the highest-rated movie for each user. As trained parameters, the network has embeddings for users and movies, and weights for a sequence of MLPs.
Specifically, this sample:
The network is converted from Tensorflow using the UFF converter (see Converting A Frozen Graph To UFF), and imported using the UFF parser. Constant layers are used to represent the trained parameters within the network, and the MLPs are implemented using MatrixMultiply layers. A TopK operation is added manually after parsing to find the highest rated movie for the given user.
The sample fills the input buffer with userIDs
and their corresponding lists of MovieIDs
, which are loaded from movielens_ratings.txt
. Then, it launches the inference to predict the rating probabilities for the movies using TensorRT.
Finally, the sample compares the outputs predicted by TensorRT with the expected outputs which are given by movielens_ratings.txt
. For each user, the MovieID
with the highest probability should match the expected highest-rated MovieID
. In the verbose mode, the sample also prints out the probability, which should be close to the expected probability.
In this sample, the following layers are used. For more information about these layers, see the TensorRT Developer Guide: Layers documentation.
Activation layer The Activation layer implements element-wise activation functions.
MatrixMultiply layer The MatrixMultiply layer implements matrix multiplication for a collection of matrices.
Scale layer The Scale layer implements a per-tensor, per-channel, or per-element affine transformation and/or exponentiation by constant values.
Shuffle layer The Shuffle layer implements a reshape and transpose operator for tensors.
TopK layer The TopK layer finds the top K
maximum (or minimum) elements along a dimension, returning a reduced tensor and a tensor of index positions.
This sample comes with a pre-trained model. However, if you want to train your own model, you would need to also convert the model weights to UFF format before you can run the sample.
Apply the
sampleMovieLensTraining.patch<tt>file to save the final result. `` patch -l -p1 < <TensorRT Install>/samples/sampleMovieLens/sampleMovieLensTraining.patch ```Train the MLP based NCF network. ``` python3 MLP.py –dataset ml-1m –epochs 20 –batch_size 256 –layers [64,32,16,8] –reg_layers [0,0,0,0] –num_neg 4 –lr 0.001 –learner adam –verbose 1 –out 1 ```
This step produces the following files in the root directory of the Git repo:
movielens_ratings.txt
: A text file which contains the lists of MovieIDs
for each user and the 10 highest-rated MovieIDs
with their probabilities.sampleMovieLens.pb
: The frozen TensorFlow graph which contains the information of the network structure and parameters.Convert the frozen .pb
file to .uff
format. ``` convert-to-uff sampleMovieLens.pb -p preprocess.py ```
The preprocess.py
script is a preprocessing step that needs to be applied to the TensorFlow graph before it can be used by TensorRT. The reason for this is that TensorFlow's concatenation operation accounts for the batch dimension while TensorRT's concatenation operation does not.
The convert-to-uff
tool is installed together with UFF installation. If you install UFF with deb/rpm, please use the convert_to_uff.py
script located in /usr/lib/python3.X/dist-packages/uff*/bin
.
sampleMovieLens.uff
file to the <TensorRT Install>/data/movielens
directory.movielens_ratings.txt
file to the <TensorRT Install>/data/movielens
directory.make
in the <TensorRT root directory>/samples/sampleMovieLens
directory. The binary named sample_movielens
will be created in the <TensorRT root directory>/bin
directory. ``` cd <TensorRT root directory>/samples/sampleMovieLens make `` Where
<TensorRT root directory>` is where you installed TensorRT.This output shows that the sample ran successfully; `PASSED`.
--help
optionsTo see the full list of available options and their descriptions, use the -h
or --help
command line option.
The following resources provide a deeper understanding about sampleMovieLens:
MovieLens
Models
Blogs
Videos
Documentation
For terms and conditions for use, reproduction, and distribution, see the TensorRT Software License Agreement documentation.
February 2019 This README.md
file was recreated, updated and reviewed.