def neural_computation(original_func, prefer_tensor=False):
    """
    An annotation to enable theano-based fucntions to be called with NeuralVar.
    :param original_func:
    :param prefer_tensor: a switch to return tensors when no inputs
    :return:
    """

    def wrapper(*args, **kwargs):
        normal_args, tensor_found_in_args, neural_found_in_args = convert_to_theano_var(args)
        normal_kwargs, tensor_found_in_kwargs, neural_found_in_kwargs = convert_to_theano_var(kwargs)

        tensor_found = tensor_found_in_args or tensor_found_in_kwargs
        neural_found = neural_found_in_args or neural_found_in_kwargs

        if tensor_found and neural_found:
            raise Exception("Theano tensor variables can not be used together with neural variables.")

        normal_result = original_func(*normal_args, **normal_kwargs)

        if tensor_found or (not neural_found and prefer_tensor):
            # No neural variables are inputted, so output tensors
            return normal_result
        else:
            # Output neural variables, auto set output_dim
            result_var = convert_to_neural_var(normal_result)
            if (isinstance(normal_result, TensorVariable) and
                    hasattr(normal_result.tag, "test_value") and
                    hasattr(normal_result.tag.test_value, "shape") and
                    normal_result.tag.test_value.shape):
                result_var.output_dim = normal_result.tag.test_value.shape[-1]
            return result_var
    return wrapper
def neural_computation(original_func, prefer_tensor=False):
    """
    An annotation to enable theano-based fucntions to be called with NeuralVar.
    :param original_func:
    :param prefer_tensor: a switch to return tensors when no inputs
    :return:
    """

    def wrapper(*args, **kwargs):
        normal_args, tensor_found_in_args, neural_found_in_args = convert_to_theano_var(args)
        normal_kwargs, tensor_found_in_kwargs, neural_found_in_kwargs = convert_to_theano_var(kwargs)

        tensor_found = tensor_found_in_args or tensor_found_in_kwargs
        neural_found = neural_found_in_args or neural_found_in_kwargs

        if tensor_found and neural_found:
            raise Exception("Theano tensor variables can not be used together with neural variables.")

        normal_result = original_func(*normal_args, **normal_kwargs)

        if tensor_found or (not neural_found and prefer_tensor):
            # No neural variables are inputted, so output tensors
            return normal_result
        else:
            # Output neural variables, auto set output_dim
            result_var = convert_to_neural_var(normal_result)
            if (isinstance(normal_result, TensorVariable) and
                    hasattr(normal_result.tag, "test_value") and
                    hasattr(normal_result.tag.test_value, "shape") and
                    normal_result.tag.test_value.shape):
                result_var.output_dim = normal_result.tag.test_value.shape[-1]
            return result_var
    return wrapper
def neural_computation ( original_func , prefer_tensor = False ) : def wrapper ( * args , * * kwargs ) : normal_args , tensor_found_in_args , neural_found_in_args = convert_to_theano_var ( args ) normal_kwargs , tensor_found_in_kwargs , neural_found_in_kwargs = convert_to_theano_var ( kwargs ) tensor_found = tensor_found_in_args or tensor_found_in_kwargs neural_found = neural_found_in_args or neural_found_in_kwargs if tensor_found and neural_found : raise Exception ( "Theano tensor variables can not be used together with neural variables." ) normal_result = original_func ( * normal_args , * * normal_kwargs ) if tensor_found or ( not neural_found and prefer_tensor ) : # No neural variables are inputted, so output tensors return normal_result else : # Output neural variables, auto set output_dim result_var = convert_to_neural_var ( normal_result ) if ( isinstance ( normal_result , TensorVariable ) and hasattr ( normal_result . tag , "test_value" ) and hasattr ( normal_result . tag . test_value , "shape" ) and normal_result . tag . test_value . shape ) : result_var . output_dim = normal_result . tag . test_value . shape [ - 1 ] return result_var return wrapper
An annotation to enable theano-based fucntions to be called with NeuralVar.
    :param original_func:
    :param prefer_tensor: a switch to return tensors when no inputs
    :return:
An annotation to enable theano - based fucntions to be called with NeuralVar . : param original_func : : param prefer_tensor : a switch to return tensors when no inputs : return :