deepy/tensor/functions.py
def var(tensor_type, last_dim=0, test_shape=None):
    """
    Wrap a Theano tensor into the variable for defining neural network.
    :param last_dim: last dimension of tensor, 0 indicates that the last dimension is flexible
    :rtype: deepy.core.neural_var.NeuralVariable
    """
    # Create tensor
    from deepy.core.neural_var import NeuralVariable
    from deepy.core.env import env
    from theano.tensor.var import TensorVariable
    if isinstance(tensor_type, NeuralVariable):
        var = tensor_type
        if last_dim != 0:
            var.output_dim = last_dim
    elif isinstance(tensor_type, TensorVariable):
        var = NeuralVariable(tensor_type, dim=last_dim)
    elif isinstance(tensor_type, str):
        theano_tensor = getattr(TT, tensor_type)()
        var = NeuralVariable(theano_tensor, dim=last_dim)
    else:
        raise Exception("tensor_type shall be a string or a NeuralVariable")
    # Set test value
    if test_shape:
        if type(test_shape) != list and type(test_shape) != tuple:
            # May be it's a value
            var.set_test_value(test_shape)
        else:
            test_val = env.numpy_rand.rand(*test_shape)
            if len(test_shape) > 0:
                test_val = test_val.astype(var.tensor.dtype)
            elif var.tensor.dtype.startswith("int"):
                test_val = 1
            var.set_test_value(test_val)
    else:
        # Create a general test_shape
        dims = [(d + 1) * 3 for d in range(var.tensor.ndim)]
        if var.dim() != 0:
            dims[-1] = var.dim()
        test_val = env.numpy_rand.rand(*dims)
        if len(dims) > 0:
            test_val = test_val.astype(var.tensor.dtype)
        elif var.tensor.dtype.startswith("int"):
            test_val = 1
        var.set_test_value(test_val)
    return var
def var(tensor_type, last_dim=0, test_shape=None):
    """
    Wrap a Theano tensor into the variable for defining neural network.
    :param last_dim: last dimension of tensor, 0 indicates that the last dimension is flexible
    :rtype: deepy.core.neural_var.NeuralVariable
    """
    # Create tensor
    from deepy.core.neural_var import NeuralVariable
    from deepy.core.env import env
    from theano.tensor.var import TensorVariable
    if isinstance(tensor_type, NeuralVariable):
        var = tensor_type
        if last_dim != 0:
            var.output_dim = last_dim
    elif isinstance(tensor_type, TensorVariable):
        var = NeuralVariable(tensor_type, dim=last_dim)
    elif isinstance(tensor_type, str):
        theano_tensor = getattr(TT, tensor_type)()
        var = NeuralVariable(theano_tensor, dim=last_dim)
    else:
        raise Exception("tensor_type shall be a string or a NeuralVariable")
    # Set test value
    if test_shape:
        if type(test_shape) != list and type(test_shape) != tuple:
            # May be it's a value
            var.set_test_value(test_shape)
        else:
            test_val = env.numpy_rand.rand(*test_shape)
            if len(test_shape) > 0:
                test_val = test_val.astype(var.tensor.dtype)
            elif var.tensor.dtype.startswith("int"):
                test_val = 1
            var.set_test_value(test_val)
    else:
        # Create a general test_shape
        dims = [(d + 1) * 3 for d in range(var.tensor.ndim)]
        if var.dim() != 0:
            dims[-1] = var.dim()
        test_val = env.numpy_rand.rand(*dims)
        if len(dims) > 0:
            test_val = test_val.astype(var.tensor.dtype)
        elif var.tensor.dtype.startswith("int"):
            test_val = 1
        var.set_test_value(test_val)
    return var
Wrap a Theano tensor into the variable for defining neural network.
    :param last_dim: last dimension of tensor, 0 indicates that the last dimension is flexible
    :rtype: deepy.core.neural_var.NeuralVariable