2

我正在尝试在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))
4

1 回答 1

1

你在正确的轨道上。根据 TensorFlow 文档 [1],您需要包装tf.print()tf.control_dependencies()上下文管理器中以确保它在图形模型中运行。GPflow 目前在图模型中工作。处于开发阶段的 GPflow 2.0 将允许在 Eager 模式下使用。

@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

于 2019-05-06T12:04:06.717 回答