【问题标题】:theano - print value of TensorVariabletheano - TensorVariable 的打印值
【发布时间】:2013-07-03 10:13:41
【问题描述】:

如何打印 theano TensorVariable 的数值? 我是theano的新手,所以请耐心等待:)

我有一个函数,我将y 作为参数。 现在我想将这个y 的形状调试打印到控制台。 使用

print y.shape

导致控制台输出(我期待数字,即(2,4,4)):

Shape.0

或者我如何打印例如以下代码的数值结果(这会计算y 中有多少值大于最大值的一半):

errorCount = T.sum(T.gt(T.abs_(y),T.max(y)/2.0))

errorCount 应该是一个数字,因为T.sum 会汇总所有值。 但是使用

print errCount

给了我(预计像134):

Sum.0

【问题讨论】:

标签: python debugging theano


【解决方案1】:

如果 y 是一个 theano 变量,则 y.shape 将是一个 theano 变量。所以这是正常的

print y.shape

返回:

Shape.0

如果你想计算表达式 y.shape,你可以这样做:

y.shape.eval()

如果y.shape 不输入计算自身(它只依赖于共享变量和常量)。否则,如果 y 依赖于 x Theano 变量,您可以像这样传递输入值:

y.shape.eval(x=numpy.random.rand(...))

sum 也是如此。 Theano 图是符号变量,在您使用 theano.function 编译它或在它们上调用 eval() 之前不会进行计算。

编辑:根据docs,theano 较新版本中的语法是

y.shape.eval({x: numpy.random.rand(...)})

【讨论】:

    【解决方案2】:

    对于未来的读者:之前的答案非常好。 但是,我发现 'tag.test_value' 机制更有利于调试目的(请参阅theano-debug-faq):

    from theano import config
    from theano import tensor as T
    config.compute_test_value = 'raise'
    import numpy as np    
    #define a variable, and use the 'tag.test_value' option:
    x = T.matrix('x')
    x.tag.test_value = np.random.randint(100,size=(5,5))
    
    #define how y is dependent on x:
    y = x*x
    
    #define how some other value (here 'errorCount') depends on y:
    errorCount = T.sum(y)
    
    #print the tag.test_value result for debug purposes!
    errorCount.tag.test_value
    

    对我来说,这更有帮助;例如,检查正确的尺寸等。

    【讨论】:

      【解决方案3】:

      打印张量变量的值。

      执行以下操作:

      print tensor[dimension].eval() # 这将打印张量中该位置的内容/值

      例如,对于一维张量:

      print tensor[0].eval()
      

      【讨论】:

        【解决方案4】:

        使用theano.printing.Print 将打印运算符添加到您的计算图中。

        例子:

        import numpy
        import theano
        
        x = theano.tensor.dvector('x')
        
        x_printed = theano.printing.Print('this is a very important value')(x)
        
        f = theano.function([x], x * 5)
        f_with_print = theano.function([x], x_printed * 5)
        
        #this runs the graph without any printing
        assert numpy.all( f([1, 2, 3]) == [5, 10, 15])
        
        #this runs the graph with the message, and value printed
        assert numpy.all( f_with_print([1, 2, 3]) == [5, 10, 15])
        

        输出:

        this is a very important value __str__ = [ 1. 2. 3.]

        来源:Theano 1.0 docs: “How do I Print an Intermediate Value in a Function?”

        【讨论】:

          【解决方案5】:

          我发现@zuuz 的回答很有帮助, 对于价值观,

          print(your_variable.tag.test_value)
          

          对于形状,它应该更新为,

          print(np.shape(your_variable.tag.test_value))
          

          【讨论】:

          • *** AttributeError: 'scratchpad' 对象没有属性 'test_value'
          猜你喜欢
          • 1970-01-01
          • 2017-06-01
          • 2016-08-20
          • 1970-01-01
          • 2014-09-04
          • 2018-09-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多