Как я могу напечатать вывод (тензорные значения, фигуры) в gpflow?

Я пытаюсь разработать новую модель в gpflow, Чтобы отладить его, мне нужно знать формы и значения тензоров во время выполнения графа.

Я попытался ниже на основе значений тензор печати в tensorflow, но ничего не выводится на консоль.

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))

1 ответ

Решение

Вы на правильном пути. Согласно документам TensorFlow [1], вам нужно обернуть tf.print() в tf.control_dependencies() контекстный менеджер, чтобы убедиться, что он работает, когда в графической модели. GPflow в настоящее время работает в графовой модели. GPflow 2.0, который является разработкой, позволит использовать его в активном режиме.

@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

Другие вопросы по тегам