def eval(node, clean_up=True): """ It evaluates a node that has taken a numpy array as input. Note that sequences are not supported yet by this method Examples: Plus with two matrices >>> print (cntk.eval(cntk.ops.plus([[-30.,40.], [1.,2.]], [[-30.,40.], [1.,2.]]))) # [array([[[-60., 80.], [2., 4.]]])] Times with broadcast of a scalar over a matrix >>> print (cntk.eval(cntk.ops.element_times([[-30.,40.], [1.,2.]], 5))) # [array([[[-150., 200.], [5., 10.]]])] Args: node (:class:`cntk.graph.ComputationNode`): the node to evaluate clean_up (bool): whether the temporary directory should be removed when the context is left Returns: NumPy array containing the result """ from cntk.context import get_new_context # call a helper method to get a context with get_new_context() as ctx: ctx.clean_up = clean_up return ctx.eval(node)
def eval(node, clean_up=True): """ It evaluates a node that has taken a numpy array as input. Note that sequences are not supported yet by this method Examples: Plus with two matrices >>> print (cntk.eval(cntk.ops.plus([[-30.,40.], [1.,2.]], [[-30.,40.], [1.,2.]]))) # [array([[[-60., 80.], [2., 4.]]])] Times with broadcast of a scalar over a matrix >>> print (cntk.eval(cntk.ops.element_times([[-30.,40.], [1.,2.]], 5))) # [array([[[-150., 200.], [5., 10.]]])] Args: node (:class:`cntk.graph.ComputationNode`): the node to evaluate clean_up (bool): whether the temporary directory should be removed when the context is left Returns: NumPy array containing the result """ from cntk.context import get_new_context from cntk.ops import input_numpy, constant from cntk.graph import ComputationNode, _InputComputationNodeBase import numpy as np # call a helper method to get a context with get_new_context() as ctx: ctx.clean_up = clean_up first = True # The params are passed as arryas, e.g. plus([1,2], [3,4]), and we need to # wrap them with input and parameter nodes. if node.params: for p in node.params: if p in node.inputs: val = getattr(node, p) if not isinstance(val, ComputationNode): # One param needs to be an Input() node. This will be fixed in # CNTK soon, so that we can remove this workaround and evaluate a # network with no inputs. if first: ir = input_numpy([val], alias=p, name=p) setattr(node, p, ir) first = False else: setattr(node, p, constant(getattr(node, p), name=p)) else: if isinstance(val, _InputComputationNodeBase) and first: first = False return ctx.eval(node)
def unittest_helper(root_node, input_numpy, expected, device_id=-1, precision="float", clean_up=True, backward_pass=False, input_node=None): from cntk.context import get_new_context with get_new_context() as ctx: ctx.clean_up = clean_up ctx.device_id = device_id ctx.precision = precision assert not ctx.input_nodes result = ctx.eval(root_node, input_numpy, backward_pass, input_node) assert len(result) == len(expected) for res, exp in zip(result, expected): assert np.allclose(res, exp, atol=TOLERANCE_ABSOLUTE) assert res.shape == AA(exp).shape