chainer.utils.type_check.Expr¶
-
class
chainer.utils.type_check.Expr(priority)[source]¶ Abstract syntax tree of an expression.
It represents an abstract syntax tree, and isn’t a value. You can get its actual value with
eval()function, and get syntax representation with the__str__()method. Each comparison operator (e.g.==) generates a newExprobject which represents the result of comparison between two expressions.Example
Let
xandybe instances ofExpr, then>>> x = Variable(1, 'x') >>> y = Variable(1, 'y') >>> c = (x == y)
is also an instance of
Expr. To evaluate and get its value, calleval()method:>>> c.eval() True
Call
strfunction to get a representation of the original equation:>>> str(c) 'x == y'
You can actually compare an expression with a value:
>>> (x == 1).eval() True
Note that you can’t use boolean operators such as
and, as they try to cast expressions to boolean values:>>> z = Variable(1, 'z') >>> x == y and y == z # raises an error Traceback (most recent call last): RuntimeError: Don't convert Expr to bool. Please call Expr.eval method to evaluate expression.
Methods