1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
|
4
|
|
|
import theano.tensor as TT |
5
|
|
|
|
6
|
|
|
from deepy.core.tensor_conversion import neural_computation, convert_to_theano_var |
7
|
|
|
from deepy.layers.layer import NeuralLayer |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class NeuralVariable(NeuralLayer): |
11
|
|
|
""" |
12
|
|
|
Create a constant layer with tensors. |
13
|
|
|
""" |
14
|
|
|
|
15
|
|
|
def __init__(self, tensor, dim=0): |
16
|
|
|
""" |
17
|
|
|
Create a tensor layer. |
18
|
|
|
:type tensor: theano.tensor.var.TensorVariable |
19
|
|
|
""" |
20
|
|
|
super(NeuralVariable, self).__init__("const") |
21
|
|
|
self.output_dim = dim |
22
|
|
|
self.tensor = tensor |
23
|
|
|
self.init(0) |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def __getattr__(self, name): |
27
|
|
|
return NeuralVariable(getattr(self.tensor, name), dim=self.dim()) |
28
|
|
|
|
29
|
|
|
def apply(self, func, dim=None): |
30
|
|
|
""" |
31
|
|
|
Apply a function to tensors. |
32
|
|
|
""" |
33
|
|
|
output_dim = dim if dim else self.output_dim |
34
|
|
|
return NeuralVariable(func(self.tensor), output_dim) |
35
|
|
|
|
36
|
|
|
def compute_tensor(self, x): |
37
|
|
|
return self.tensor |
38
|
|
|
|
39
|
|
|
def set_test_value(self, value): |
40
|
|
|
self.tensor.tag.test_value = value |
41
|
|
|
|
42
|
|
|
def dim(self): |
43
|
|
|
return self.output_dim |
44
|
|
|
|
45
|
|
|
def _other_tensor(self, other): |
46
|
|
|
return other.tensor if isinstance(other, NeuralVariable) else other |
47
|
|
|
|
48
|
|
|
def __eq__(self, other): |
49
|
|
|
return NeuralVariable(TT.eq(self.tensor, self._other_tensor(other)), dim=self.dim()) |
50
|
|
|
|
51
|
|
|
def __ne__(self, other): |
52
|
|
|
return NeuralVariable(TT.neq(self.tensor, self._other_tensor(other)), dim=self.dim()) |
53
|
|
|
|
54
|
|
|
def __add__(self, other): |
55
|
|
|
return NeuralVariable(self.tensor + self._other_tensor(other), dim=self.dim()) |
56
|
|
|
|
57
|
|
|
def __sub__(self, other): |
58
|
|
|
return NeuralVariable(self.tensor - self._other_tensor(other), dim=self.dim()) |
59
|
|
|
|
60
|
|
|
def __mul__(self, other): |
61
|
|
|
return NeuralVariable(self.tensor * self._other_tensor(other), dim=self.dim()) |
62
|
|
|
|
63
|
|
|
def __div__(self, other): |
64
|
|
|
return NeuralVariable(self.tensor / self._other_tensor(other), dim=self.dim()) |
65
|
|
|
|
66
|
|
|
def __neg__(self): |
67
|
|
|
return NeuralVariable(- self.tensor, dim=self.dim()) |
68
|
|
|
|
69
|
|
|
def __radd__(self, other): |
70
|
|
|
return NeuralVariable(self._other_tensor(other) + self.tensor, dim=self.dim()) |
71
|
|
|
|
72
|
|
|
def __rsub__(self, other): |
73
|
|
|
return NeuralVariable(self._other_tensor(other) - self.tensor, dim=self.dim()) |
74
|
|
|
|
75
|
|
|
def __rmul__(self, other): |
76
|
|
|
return NeuralVariable(self._other_tensor(other) * self.tensor, dim=self.dim()) |
77
|
|
|
|
78
|
|
|
def __rdiv__(self, other): |
79
|
|
|
return NeuralVariable(self._other_tensor(other) / self.tensor, dim=self.dim()) |
80
|
|
|
|
81
|
|
|
def __getitem__(self, index): |
82
|
|
|
@neural_computation |
83
|
|
|
def getitem_wrapper(t, index): |
84
|
|
|
if type(index) == list: |
85
|
|
|
index = tuple(index) |
86
|
|
|
return t.__getitem__(index) |
87
|
|
|
ret = getitem_wrapper(self, index) |
88
|
|
|
if (hasattr(ret.tensor, 'tag') and hasattr(ret.tensor.tag, 'test_value') |
89
|
|
|
and ret.tensor.tag.test_value is not None and len(ret.tensor.tag.test_value.shape) > 0): |
90
|
|
|
ret.output_dim = ret.tensor.tag.test_value.shape[-1] |
91
|
|
|
else: |
92
|
|
|
ret.output_dim = self.dim() |
93
|
|
|
return ret |
94
|
|
|
|
95
|
|
|
def __call__(self, *args, **kwargs): |
96
|
|
|
normal_args, tensor_found_in_args, neural_found_in_args = convert_to_theano_var(args) |
97
|
|
|
normal_kwargs, tensor_found_in_kwargs, neural_found_in_kwargs = convert_to_theano_var(kwargs) |
98
|
|
|
|
99
|
|
|
tensor_found = tensor_found_in_args or tensor_found_in_kwargs |
100
|
|
|
|
101
|
|
|
if tensor_found: |
102
|
|
|
raise Exception("Theano tensor variables can not be used together with neural variables.") |
103
|
|
|
|
104
|
|
|
return NeuralVariable(self.tensor(*normal_args, **normal_kwargs), dim=self.dim()) |
105
|
|
|
|
106
|
|
|
def debug_monitor(self, name=""): |
107
|
|
|
from deepy.debug import monitor_var_sum |
108
|
|
|
self.tensor += monitor_var_sum(self.tensor, name=name) |
109
|
|
|
|
110
|
|
|
@property |
111
|
|
|
def test_value(self): |
112
|
|
|
if hasattr(self.tensor.tag, 'test_value'): |
113
|
|
|
return self.tensor.tag.test_value |
114
|
|
|
else: |
115
|
|
|
return None |
116
|
|
|
|
117
|
|
|
@property |
118
|
|
|
def ndim(self): |
119
|
|
|
return self.tensor.ndim |
120
|
|
|
|
121
|
|
|
@property |
122
|
|
|
def tv(self): |
123
|
|
|
return self.test_value |
124
|
|
|
|
125
|
|
|
@property |
126
|
|
|
def ts(self): |
127
|
|
|
if self.test_value is not None: |
128
|
|
|
return self.test_value.shape |
129
|
|
|
else: |
130
|
|
|
return None |