Completed
Push — master ( 0a4690...96c2cd )
by Raphael
01:14
created

deepy.layers.NeuralVariable   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 42
Duplicated Lines 0 %
Metric Value
dl 0
loc 42
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A compute_tensor() 0 2 1
A apply() 0 6 2
A dim() 0 2 1
A set_test_value() 0 2 1
A __init__() 0 9 2
A shape() 0 2 1
A getitem_wrapper() 0 3 1
A compute_test_tesnor() 0 2 1
A __getitem__() 0 5 2
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
from layer import NeuralLayer
5
from deepy.utils.decorations import neural_computation
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
            return t[index]
27
        return getitem_wrapper(self, index)
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), func(self.test_tensor), output_dim)
35
36
    def compute_tensor(self, x):
37
        return self.tensor
38
39
    def compute_test_tesnor(self, x):
40
        return self.test_tensor
41
42
    def set_test_value(self, value):
43
        self.tensor.tag.test_value = value
44
45
    def dim(self):
46
        return self.output_dim
47
48
    def shape(self, dim_index):
49
        return self.tensor.shape[dim_index]
50