This file is indexed.

/usr/share/pyshared/mdp/hinet/layer.py is in python-mdp 3.3-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"""
Module for Layers.

Note that additional args and kwargs for train or execute are currently not
supported.
"""

import mdp
from mdp import numx

# TODO: maybe turn self.nodes into a read only property with self._nodes

# TODO: Find a better way to deal with additional args for train/execute?
#    Maybe split them by default, but can be disabled via switch?

class Layer(mdp.Node):
    """Layers are nodes which consist of multiple horizontally parallel nodes.

    The incoming data is split up according to the dimensions of the internal
    nodes. For example if the first node has an input_dim of 50 and the second
    node 100 then the layer will have an input_dim of 150. The first node gets
    x[:,:50], the second one x[:,50:].

    Any additional arguments are forwarded unaltered to each node.
    Warning: This might change in the next release (2.5).

    Since they are nodes themselves layers can be stacked in a flow (e.g. to
    build a layered network). If one would like to use flows instead of nodes
    inside of a layer one can use a FlowNode.
    """

    def __init__(self, nodes, dtype=None):
        """Setup the layer with the given list of nodes.

        The input and output dimensions for the nodes must be already set
        (the output dimensions for simplicity reasons). The training phases for
        the nodes are allowed to differ.

        Keyword arguments:
        nodes -- List of the nodes to be used.
        """
        self.nodes = nodes
        # check nodes properties and get the dtype
        dtype = self._check_props(dtype)
        # calculate the the dimensions
        self.node_input_dims = numx.zeros(len(self.nodes))
        input_dim = 0
        for index, node in enumerate(nodes):
            input_dim += node.input_dim
            self.node_input_dims[index] = node.input_dim
        output_dim = self._get_output_dim_from_nodes()
        super(Layer, self).__init__(input_dim=input_dim,
                                    output_dim=output_dim,
                                    dtype=dtype)

    def _get_output_dim_from_nodes(self):
        """Calculate the output_dim from the nodes and return it.

        If the output_dim of a node is not set the None is returned.
        """
        output_dim = 0
        for node in self.nodes:
            if node.output_dim is not None:
                output_dim += node.output_dim
            else:
                return None
        return output_dim

    def _check_props(self, dtype):
        """Check the compatibility of the properties of the internal nodes.

        Return the found dtype and check the dimensions.

        dtype -- The specified layer dtype.
        """
        dtype_list = []  # the dtypes for all the nodes
        for i, node in enumerate(self.nodes):
            # input_dim for each node must be set
            if node.input_dim is None:
                err = ("input_dim must be set for every node. " +
                       "Node #%d (%s) does not comply." % (i, node))
                raise mdp.NodeException(err)
            if node.dtype is not None:
                dtype_list.append(node.dtype)
        # check that the dtype is None or the same for every node
        nodes_dtype = None
        nodes_dtypes = set(dtype_list)
        nodes_dtypes.discard(None)
        if len(nodes_dtypes) > 1:
            err = ("All nodes must have the same dtype (found: %s)." %
                   nodes_dtypes)
            raise mdp.NodeException(err)
        elif len(nodes_dtypes) == 1:
            nodes_dtype = list(nodes_dtypes)[0]
        # check that the nodes dtype matches the specified dtype
        if nodes_dtype and dtype:
            if not numx.dtype(nodes_dtype) == numx.dtype(dtype):
                err = ("Cannot set dtype to %s: " %
                       numx.dtype(nodes_dtype).name +
                       "an internal node requires %s" % numx.dtype(dtype).name)
                raise mdp.NodeException(err)
        elif nodes_dtype and not dtype:
            dtype = nodes_dtype
        return dtype

    def _set_dtype(self, t):
        for node in self.nodes:
            node.dtype = t
        self._dtype = t

    def _get_supported_dtypes(self):
        # we supported the minimal common dtype set
        types = set(mdp.utils.get_dtypes('All'))
        for node in self.nodes:
            types = types.intersection(node.get_supported_dtypes())
        return list(types)

    def is_trainable(self):
        return any(node.is_trainable() for node in self.nodes)

    def is_invertible(self):
        return all(node.is_invertible() for node in self.nodes)

    def _get_train_seq(self):
        """Return the train sequence.

        The length is set by the node with maximum length.
        """
        max_train_length = 0
        for node in self.nodes:
            node_length = len(node._get_train_seq())
            if node_length > max_train_length:
                max_train_length = node_length
        return ([[self._train, self._stop_training]] * max_train_length)

    def _train(self, x, *args, **kwargs):
        """Perform single training step by training the internal nodes."""
        start_index = 0
        stop_index = 0
        for node in self.nodes:
            start_index = stop_index
            stop_index += node.input_dim
            if node.is_training():
                node.train(x[:, start_index : stop_index], *args, **kwargs)

    def _stop_training(self, *args, **kwargs):
        """Stop training of the internal nodes."""
        for node in self.nodes:
            if node.is_training():
                node.stop_training(*args, **kwargs)
        if self.output_dim is None:
            self.output_dim = self._get_output_dim_from_nodes()

    def _pre_execution_checks(self, x):
        """Make sure that output_dim is set and then perform normal checks."""
        if self.output_dim is None:
            # first make sure that the output_dim is set for all nodes
            in_start = 0
            in_stop = 0
            for node in self.nodes:
                in_start = in_stop
                in_stop += node.input_dim
                node._pre_execution_checks(x[:,in_start:in_stop])
            self.output_dim = self._get_output_dim_from_nodes()
            if self.output_dim is None:
                err = "output_dim must be set at this point for all nodes"
                raise mdp.NodeException(err)
        super(Layer, self)._pre_execution_checks(x)

    def _execute(self, x, *args, **kwargs):
        """Process the data through the internal nodes."""
        in_start = 0
        in_stop = 0
        out_start = 0
        out_stop = 0
        y = None
        for node in self.nodes:
            out_start = out_stop
            out_stop += node.output_dim
            in_start = in_stop
            in_stop += node.input_dim
            if y is None:
                node_y = node.execute(x[:,in_start:in_stop], *args, **kwargs)
                y = numx.zeros([node_y.shape[0], self.output_dim],
                               dtype=node_y.dtype)
                y[:,out_start:out_stop] = node_y
            else:
                y[:,out_start:out_stop] = node.execute(x[:,in_start:in_stop],
                                                        *args, **kwargs)
        return y

    def _inverse(self, x, *args, **kwargs):
        """Combine the inverse of all the internal nodes."""
        in_start = 0
        in_stop = 0
        out_start = 0
        out_stop = 0
        y = None
        for node in self.nodes:
            # compared with execute, input and output are switched
            out_start = out_stop
            out_stop += node.input_dim
            in_start = in_stop
            in_stop += node.output_dim
            if y is None:
                node_y = node.inverse(x[:,in_start:in_stop], *args, **kwargs)
                y = numx.zeros([node_y.shape[0], self.input_dim],
                               dtype=node_y.dtype)
                y[:,out_start:out_stop] = node_y
            else:
                y[:,out_start:out_stop] = node.inverse(x[:,in_start:in_stop],
                                                        *args, **kwargs)
        return y

    ## container methods ##

    def __len__(self):
        return len(self.nodes)

    def __getitem__(self, key):
        return self.nodes.__getitem__(key)

    def __contains__(self, item):
        return self.nodes.__contains__(item)

    def __iter__(self):
        return self.nodes.__iter__()


class CloneLayer(Layer):
    """Layer with a single node instance that is used multiple times.

    The same single node instance is used to build the layer, so
    Clonelayer(node, 3) executes in the same way as Layer([node]*3).
    But Layer([node]*3) would have a problem when closing a training phase,
    so one has to use CloneLayer.

    A CloneLayer can be used for weight sharing in the training phase. It might
    be also useful for reducing the memory footprint use during the execution
    phase (since only a single node instance is needed).
    """

    def __init__(self, node, n_nodes=1, dtype=None):
        """Setup the layer with the given list of nodes.

        Keyword arguments:
        node -- Node to be cloned.
        n_nodes -- Number of repetitions/clones of the given node.
        """
        super(CloneLayer, self).__init__((node,) * n_nodes, dtype=dtype)
        self.node = node  # attribute for convenience

    def _stop_training(self, *args, **kwargs):
        """Stop training of the internal node."""
        if self.node.is_training():
            self.node.stop_training(*args, **kwargs)
        if self.output_dim is None:
            self.output_dim = self._get_output_dim_from_nodes()

class SameInputLayer(Layer):
    """SameInputLayer is a layer were all nodes receive the full input.

    So instead of splitting the input according to node dimensions, all nodes
    receive the complete input data.
    """

    def __init__(self, nodes, dtype=None):
        """Setup the layer with the given list of nodes.

        The input dimensions for the nodes must all be equal, the output
        dimensions can differ (but must be set as well for simplicity reasons).

        Keyword arguments:
        nodes -- List of the nodes to be used.
        """
        self.nodes = nodes
        # check node properties and get the dtype
        dtype = self._check_props(dtype)
        # check that the input dimensions are all the same
        input_dim = self.nodes[0].input_dim
        for node in self.nodes:
            if not node.input_dim == input_dim:
                err = "The nodes have different input dimensions."
                raise mdp.NodeException(err)
        output_dim = self._get_output_dim_from_nodes()
        # intentionally use MRO above Layer, not SameInputLayer
        super(Layer, self).__init__(input_dim=input_dim,
                                    output_dim=output_dim,
                                    dtype=dtype)

    @staticmethod
    def is_invertible():
        return False

    def _train(self, x, *args, **kwargs):
        """Perform single training step by training the internal nodes."""
        for node in self.nodes:
            if node.is_training():
                node.train(x, *args, **kwargs)

    def _pre_execution_checks(self, x):
        """Make sure that output_dim is set and then perform nromal checks."""
        if self.output_dim is None:
            # first make sure that the output_dim is set for all nodes
            for node in self.nodes:
                node._pre_execution_checks(x)
            self.output_dim = self._get_output_dim_from_nodes()
            if self.output_dim is None:
                err = "output_dim must be set at this point for all nodes"
                raise mdp.NodeException(err)
        # intentionally use MRO above Layer, not SameInputLayer
        super(Layer, self)._pre_execution_checks(x)

    def _execute(self, x, *args, **kwargs):
        """Process the data through the internal nodes."""
        out_start = 0
        out_stop = 0
        y = None
        for node in self.nodes:
            out_start = out_stop
            out_stop += node.output_dim
            if y is None:
                node_y = node.execute(x, *args, **kwargs)
                y = numx.zeros([node_y.shape[0], self.output_dim],
                               dtype=node_y.dtype)
                y[:,out_start:out_stop] = node_y
            else:
                y[:,out_start:out_stop] = node.execute(x, *args, **kwargs)
        return y