deepy/core/tensor_conversion.py
def convert_to_theano_var(obj):
    """
    Convert neural vars to theano vars.
    :param obj: NeuralVariable or list or dict or tuple
    :return: theano var, test var, tensor found, neural var found
    """
    from deepy.core.neural_var import NeuralVariable
    if type(obj) == tuple:
        return tuple(convert_to_theano_var(list(obj)))
    if type(obj) == list:
        unpacked_list = map(convert_to_theano_var, obj)
        normal_list = []
        test_list = []
        theano_var_found = False
        neural_var_found = False
        for normal_var, tensor_found, neural_found in unpacked_list:
            normal_list.append(normal_var)
            if tensor_found: theano_var_found = True
            if neural_found: neural_var_found = True
        return normal_list, theano_var_found, neural_var_found
    elif type(obj) == dict:
        normal_map = {}
        theano_var_found = False
        neural_var_found = False
        for key in obj:
            normal_var, tensor_found, neural_found = convert_to_theano_var(obj[key])
            normal_map[key] = normal_var
            if tensor_found: theano_var_found = True
            if neural_found: neural_var_found = True
        return normal_map, theano_var_found, neural_var_found
    elif type(obj) == MapDict:
        normal_map = {}
        theano_var_found = False
        neural_var_found = False
        for key in obj:
            normal_var, tensor_found, neural_found = convert_to_theano_var(obj[key])
            normal_map[key] = normal_var
            if tensor_found: theano_var_found = True
            if neural_found: neural_var_found = True
        return MapDict(normal_map), theano_var_found, neural_var_found
    elif type(obj) == NeuralVariable:
        theano_tensor = obj.tensor
        theano_tensor.tag.last_dim = obj.dim()
        return theano_tensor, False, True
    elif type(obj) == TensorVariable:
        return obj, True, False
    elif type(obj) == slice:
        normal_args = []
        theano_var_found = False
        neural_var_found = False
        for arg in [obj.start, obj.stop, obj.step]:
            normal_var, tensor_found, neural_found = convert_to_theano_var(arg)
            normal_args.append(normal_var)
            if tensor_found: theano_var_found = True
            if neural_found: neural_var_found = True
        return slice(*normal_args), theano_var_found, neural_var_found
    else:
        return obj, False, False
def convert_to_theano_var(obj):
    """
    Convert neural vars to theano vars.
    :param obj: NeuralVariable or list or dict or tuple
    :return: theano var, test var, tensor found, neural var found
    """
    from deepy.core.neural_var import NeuralVariable
    if type(obj) == tuple:
        return tuple(convert_to_theano_var(list(obj)))
    if type(obj) == list:
        unpacked_list = map(convert_to_theano_var, obj)
        normal_list = []
        test_list = []
        theano_var_found = False
        neural_var_found = False
        for normal_var, tensor_found, neural_found in unpacked_list:
            normal_list.append(normal_var)
            if tensor_found: theano_var_found = True
            if neural_found: neural_var_found = True
        return normal_list, theano_var_found, neural_var_found
    elif type(obj) == dict:
        normal_map = {}
        theano_var_found = False
        neural_var_found = False
        for key in obj:
            normal_var, tensor_found, neural_found = convert_to_theano_var(obj[key])
            normal_map[key] = normal_var
            if tensor_found: theano_var_found = True
            if neural_found: neural_var_found = True
        return normal_map, theano_var_found, neural_var_found
    elif type(obj) == MapDict:
        normal_map = {}
        theano_var_found = False
        neural_var_found = False
        for key in obj:
            normal_var, tensor_found, neural_found = convert_to_theano_var(obj[key])
            normal_map[key] = normal_var
            if tensor_found: theano_var_found = True
            if neural_found: neural_var_found = True
        return MapDict(normal_map), theano_var_found, neural_var_found
    elif type(obj) == NeuralVariable:
        theano_tensor = obj.tensor
        theano_tensor.tag.last_dim = obj.dim()
        return theano_tensor, False, True
    elif type(obj) == TensorVariable:
        return obj, True, False
    elif type(obj) == slice:
        normal_args = []
        theano_var_found = False
        neural_var_found = False
        for arg in [obj.start, obj.stop, obj.step]:
            normal_var, tensor_found, neural_found = convert_to_theano_var(arg)
            normal_args.append(normal_var)
            if tensor_found: theano_var_found = True
            if neural_found: neural_var_found = True
        return slice(*normal_args), theano_var_found, neural_var_found
    else:
        return obj, False, False
Convert neural vars to theano vars.
    :param obj: NeuralVariable or list or dict or tuple
    :return: theano var, test var, tensor found, neural var found