| Total Complexity | 9 |
| Total Lines | 36 |
| Duplicated Lines | 0 % |
| 1 | #!/usr/bin/env python |
||
| 7 | class NeuralVar(NeuralLayer): |
||
| 8 | """ |
||
| 9 | Create a constant layer with tensors. |
||
| 10 | """ |
||
| 11 | |||
| 12 | def __init__(self, dim, tensor, test_tensor=None): |
||
| 13 | """ |
||
| 14 | Create a tensor layer. |
||
| 15 | """ |
||
| 16 | super(NeuralVar, self).__init__("const") |
||
| 17 | self.output_dim = dim |
||
| 18 | self.tensor = tensor |
||
| 19 | self.test_tensor = tensor if not test_tensor else test_tensor |
||
| 20 | self.connect(0) |
||
| 21 | |||
| 22 | def apply(self, func, dim=None): |
||
| 23 | """ |
||
| 24 | Apply a function to tensors. |
||
| 25 | """ |
||
| 26 | output_dim = dim if dim else self.output_dim |
||
| 27 | return NeuralVar(output_dim, func(self.tensor), func(self.test_tensor)) |
||
| 28 | |||
| 29 | def output(self, x): |
||
| 30 | return self.tensor |
||
| 31 | |||
| 32 | def test_output(self, x): |
||
| 33 | return self.test_tensor |
||
| 34 | |||
| 35 | def set_test_value(self, value): |
||
| 36 | self.tensor.tag.test_value = value |
||
| 37 | |||
| 38 | def dim(self): |
||
| 39 | return self.output_dim |
||
| 40 | |||
| 41 | def shape(self, dim_index): |
||
| 42 | return self.tensor.shape[dim_index] |
||
| 43 |