Completed
Push — master ( c8682f...5bbe2a )
by Raphael
01:33
created

deepy.layers.NeuralVar.apply()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 6
rs 9.4286
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
from layer import NeuralLayer
5
6
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