Jonathan
Jonathan

Reputation: 123

How can I print output (tensor values, shapes) in gpflow?

I am trying to develop a new model within gpflow. In order to debug it I need to know shapes and values of tensors during execution of the graph.

I tried the below based on printing tensor values in tensorflow, but nothing is printed to the console.

import numpy as np
import sys
import gpflow
from gpflow.mean_functions import MeanFunction
from gpflow.decors import params_as_tensors

class Log(MeanFunction):
    """
    :math:`y_i = \log(x_i)`
    """

    def __init__(self):
        MeanFunction.__init__(self)

    @params_as_tensors
    def __call__(self, X):
        # I want to figure out the shape of X here
        tf.print(tf.shape(X), output_stream=sys.stdout)
        # Returns the natural logarithm of the input
        return tf.log(X)

# Test gpflow implementation
sess = tf.InteractiveSession()

with sess.as_default(), sess.graph.as_default():
    X = np.random.uniform(size=[100, 1])
    y = np.random.uniform(size=[100, 1])

    m = gpflow.models.GPR(X=X, Y=y, mean_function=Log(), kern=gpflow.kernels.RBF(input_dim=1))

Upvotes: 2

Views: 186

Answers (1)

Mark van der Wilk
Mark van der Wilk

Reputation: 807

You're on the right track. According to the TensorFlow docs [1], you need to wrap tf.print() in a tf.control_dependencies() context manager to make sure it's run, when in graph model. GPflow currently works in graph model. GPflow 2.0, which is indevelopment, will allow usage in eager mode.

@params_as_tensors
def __call__(self, X): 
    # I want to figure out the shape of X here 
    print_op = tf.print(tf.shape(X), output_stream=sys.stdout) 
    with tf.control_dependencies([print_op]): 
        log_calc = tf.log(X) 
    # Returns the natural logarithm of the input 
    return log_calc

[1] https://www.tensorflow.org/api_docs/python/tf/print

Upvotes: 1

Related Questions