theano - print value of TensorVariable

后端 未结 5 2015
夕颜
夕颜 2020-12-29 19:02

How can I print the numerical value of a theano TensorVariable? I\'m new to theano, so please be patient :)

I have a function where I get y

5条回答
  •  爱一瞬间的悲伤
    2020-12-29 19:24

    Use theano.printing.Print to add print operator to your computational graph.

    Example:

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

    Output:

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

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

提交回复
热议问题