首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在gpflow中打印输出(张量值、形状)?

如何在gpflow中打印输出(张量值、形状)?
EN

Stack Overflow用户
提问于 2019-05-06 17:44:39
回答 1查看 125关注 0票数 2

我正在尝试在gpflow中开发一个新的模型。为了调试它,我需要知道在图形执行期间张量的形状和值。

我在tensorflow中打印张量值的基础上尝试了下面的方法,但是没有任何东西被打印到控制台。

代码语言:javascript
复制
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))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-06 20:04:07

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

代码语言:javascript
复制
@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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56002643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档