我正在尝试在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))发布于 2019-05-06 20:04:07
你在正确的轨道上。根据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_calchttps://stackoverflow.com/questions/56002643
复制相似问题