Completed
Push — master ( 5937d6...900a69 )
by Raphael
01:44
created

NeuralVariable.__div__()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
from layer import NeuralLayer
5
from deepy.utils.decorations import neural_computation, convert_to_theano_var
6
7
8
class NeuralVariable(NeuralLayer):
9
    """
10
    Create a constant layer with tensors.
11
    """
12
13
    def __init__(self, tensor, test_tensor=None, dim=0):
14
        """
15
        Create a tensor layer.
16
        """
17
        super(NeuralVariable, self).__init__("const")
18
        self.output_dim = dim
19
        self.tensor = tensor
20
        self.test_tensor = tensor if not test_tensor else test_tensor
21
        self.initialize(0)
22
23
    def __getitem__(self, index):
24
        @neural_computation
25
        def getitem_wrapper(t, index):
26
            if type(index) == list:
27
                index = tuple(index)
28
            return t.__getitem__(index)
29
        ret = getitem_wrapper(self, index)
30
        ret.output_dim = self.dim()
31
        return ret
32
33
    def __call__(self, *args, **kwargs):
34
        normal_args, test_args, tensor_found_in_args, neural_found_in_args = convert_to_theano_var(args)
35
        normal_kwargs, test_kwargs, tensor_found_in_kwargs, neural_found_in_kwargs = convert_to_theano_var(kwargs)
36
37
        tensor_found = tensor_found_in_args or tensor_found_in_kwargs
38
39
        if tensor_found:
40
            raise Exception("Theano tensor variables can not be used together with neural variables.")
41
42
        return NeuralVariable(self.tensor(*normal_args, **normal_kwargs), self.test_tensor(*test_args, **test_kwargs), dim=self.dim())
43
44
    def __getattr__(self, name):
45
        return NeuralVariable(getattr(self.tensor, name), getattr(self.test_tensor, name), dim=self.dim())
46
47
    def apply(self, func, dim=None):
48
        """
49
        Apply a function to tensors.
50
        """
51
        output_dim = dim if dim else self.output_dim
52
        return NeuralVariable(func(self.tensor), func(self.test_tensor), output_dim)
53
54
    def compute_tensor(self, x):
55
        return self.tensor
56
57
    def compute_test_tesnor(self, x):
58
        return self.test_tensor
59
60
    def set_test_value(self, value):
61
        self.tensor.tag.test_value = value
62
63
    def dim(self):
64
        return self.output_dim
65
66
    def shape(self, dim_index):
67
        return NeuralVariable(self.tensor.shape[dim_index], self.test_tensor.shape[dim_index])
68
69
    def _other_tensor(self, other):
70
        return  other.tensor if isinstance(other, NeuralVariable) else other
71
72
    def _other_test_tensor(self, other):
73
        return other.test_tensor if isinstance(other, NeuralVariable) else other
74
75
    def __add__(self, other):
76
77
        return NeuralVariable(self.tensor + self._other_tensor(other), self.test_tensor + self._other_test_tensor(other), dim=self.dim())
78
79
    def __sub__(self, other):
80
        return NeuralVariable(self.tensor - self._other_tensor(other), self.test_tensor - self._other_test_tensor(other), dim=self.dim())
81
82
    def __mul__(self, other):
83
        return NeuralVariable(self.tensor * self._other_tensor(other), self.test_tensor * self._other_test_tensor(other), dim=self.dim())
84
85
    def __div__(self, other):
86
        return NeuralVariable(self.tensor / self._other_tensor(other), self.test_tensor / self._other_test_tensor(other), dim=self.dim())
87
88
    @property
89
    def test_value(self):
90
        if hasattr(self.tensor.tag, 'test_value'):
91
            return self.tensor.tag.test_value
92
        else:
93
            return None
94
95
    @property
96
    def tv(self):
97
        return self.test_value
98
99
    @property
100
    def ts(self):
101
        if self.test_value is not None:
102
            return self.test_value.shape
103
        else:
104
            return None