How to make predictions with Tensorflow Ranking

Viewed 288

I'm currently using the TensorFlow Ranking module for a recommendation task. I'm reproducing this tutorial, using exactly the same code and the same database, the famous dataset from Yahoo for learning to rank

The model runs perfectly and generates the expected results, but as you may notice, the tutorial doesn't say how to make predictions, I've tried the solution posted in this question and also the following function

def predict_input_fn(features):
        
        iterator_initializer_hook = IteratorInitializerHook()
        features_placeholder = {
            k: tf.compat.v1.placeholder(v.dtype, v.shape)
            for k, v in six.iteritems(features)
        }
    
        #labels_placeholder = tf.compat.v1.placeholder(labels.dtype, labels.shape)
        dataset = tf.data.Dataset.from_tensors(
            (features_placeholder))
        iterator = tf.compat.v1.data.make_initializable_iterator(dataset)
    
        feed_dict = {}
        feed_dict.update(
            {features_placeholder[k]: features[k] for k in features_placeholder})
        iterator_initializer_hook.iterator_initializer_fn = (
            lambda sess: sess.run(iterator.initializer, feed_dict=feed_dict))
        features = iterator.get_next()
        return features
    

  gen_predictions = estimator.predict(input_fn=lambda: predict_input_fn(features_vali)) 

Where features_vali comes from this line of code on the tutorial

features_vali, labels_vali = load_libsvm_data(FLAGS.vali_path,FLAGS.list_size)

Everything I have tried generates the same error:

ValueError: Cannot reshape a tensor with 630600 elements to shape [6306,1] (6306 elements) for > 'transform/encoding_layer/1/Reshape' (op: 'Reshape') with input shapes: [6306,100,1], 2 and > with input tensors computed as partial shapes: input 1 = [6306,1].

6306 are the queries that I want to predict and 100 is the list_size parameter (i.e 100 documents by query)

I'm pretty sure that I'm missing something in predict_input_fn, but I can't find the error

0 Answers
Related