Convert Neural Collaborative Filtering Model from TensorFlow* to the Intermediate Representation

This tutorial explains how to convert Neural Collaborative Filtering (NCF) model to Intermediate Representation (IR).

Public TensorFlow NCF model does not contain pretrained weights. To convert this model to the IR:

  1. Use the instructions from this repository to train the model.
  2. Freeze the inference graph you get on previous step in model_dir following the instructions from the Freezing Custom Models in Python* section of Converting a TensorFlow* Model. Run the following commands:
    import tensorflow as tf
    from tensorflow.python.framework import graph_io
    sess = tf.Session()
    saver = tf.train.import_meta_graph("/path/to/model/model.meta")
    saver.restore(sess, tf.train.latest_checkpoint('/path/to/model/'))
    frozen = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, \
    graph_io.write_graph(frozen, './', 'inference_graph.pb', as_text=False)

where rating/BiasAdd is an output node.

  1. Convert the model to the IR.If you look at your frozen model, you can see that it has one input that is split to four ResourceGather layers.

But as the Model Optimizer does not support such data feeding, you should skip it. Cut the edges incoming in ResourceGathers port 1:

python3 --input_model inference_graph.pb \
--input 1:embedding/embedding_lookup,1:embedding_1/embedding_lookup,\
1:embedding_2/embedding_lookup,1:embedding_3/embedding_lookup \
--input_shape [256],[256],[256],[256]

Where 256 is a batch_size you choose for your model.

Alternatively, you can do steps 2 and 3 in one command line:

python3 --input_meta_graph /path/to/model/model.meta \
--input 1:embedding/embedding_lookup,1:embedding_1/embedding_lookup,\
1:embedding_2/embedding_lookup,1:embedding_3/embedding_lookup \
--input_shape [256],[256],[256],[256] --output rating/BiasAdd