def plot_layers(weights, tied_weights=False, channels=1):
    '''Create a plot of weights, visualized as "bottom-level" pixel arrays.'''
    if hasattr(weights[0], 'get_value'):
        weights = [w.get_value() for w in weights]
    k = min(len(weights), 9)
    imgs = np.eye(weights[0].shape[0])
    for i, weight in enumerate(weights[:-1]):
        imgs = np.dot(weight.T, imgs)
        plot_images(imgs,
                    100 + 10 * k + i + 1,
                    channels=channels,
                    title='Layer {}'.format(i+1))
    weight = weights[-1]
    n = weight.shape[1] / channels
    if int(np.sqrt(n)) ** 2 != n:
        return
    if tied_weights:
        imgs = np.dot(weight.T, imgs)
        plot_images(imgs,
                    100 + 10 * k + k,
                    channels=channels,
                    title='Layer {}'.format(k))
    else:
        plot_images(weight,
                    100 + 10 * k + k,
                    channels=channels,
                    title='Decoding weights')

def
plot_layers
(
weights
,
tied_weights
=
False
,
channels
=
1
)
:
if
hasattr
(
weights
[
0
]
,
'get_value'
)
:
weights
=
[
w
.
get_value
(
)
for
w
in
weights
]
k
=
min
(
len
(
weights
)
,
9
)
imgs
=
np
.
eye
(
weights
[
0
]
.
shape
[
0
]
)
for
i
,
weight
in
enumerate
(
weights
[
:
1
]
)
:
imgs
=
np
.
dot
(
weight
.
T
,
imgs
)
plot_images
(
imgs
,
100
+
10
*
k
+
i
+
1
,
channels
=
channels
,
title
=
'Layer {}'
.
format
(
i
+
1
)
)
weight
=
weights
[
1
]
n
=
weight
.
shape
[
1
]
/
channels
if
int
(
np
.
sqrt
(
n
)
)
**
2
!=
n
:
return
if
tied_weights
:
imgs
=
np
.
dot
(
weight
.
T
,
imgs
)
plot_images
(
imgs
,
100
+
10
*
k
+
k
,
channels
=
channels
,
title
=
'Layer {}'
.
format
(
k
)
)
else
:
plot_images
(
weight
,
100
+
10
*
k
+
k
,
channels
=
channels
,
title
=
'Decoding weights'
)

Create a plot of weights visualized as bottom - level pixel arrays .


def plot_filters(filters):
    '''Create a plot of conv filters, visualized as pixel arrays.'''
    imgs = filters.get_value()

    N, channels, x, y = imgs.shape
    n = int(np.sqrt(N))
    assert n * n == N, 'filters must contain a square number of rows!'
    assert channels == 1 or channels == 3, 'can only plot grayscale or rgb filters!'

    img = np.zeros(((y+1) * n - 1, (x+1) * n - 1, channels), dtype=imgs[0].dtype)
    for i, pix in enumerate(imgs):
        r, c = divmod(i, n)
        img[r * (y+1):(r+1) * (y+1) - 1,
            c * (x+1):(c+1) * (x+1) - 1] = pix.transpose((1, 2, 0))

    img -= img.min()
    img /= img.max()

    ax = plt.gcf().add_subplot(111)
    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)
    ax.set_frame_on(False)
    ax.imshow(img.squeeze(), cmap=plt.cm.gray)

Create a plot of conv filters visualized as pixel arrays .


def batches(arrays, steps=100, batch_size=64, rng=None):
    '''Create a callable that generates samples from a dataset.

    Parameters
    ----------
    arrays : list of ndarray (time-steps, data-dimensions)
        Arrays of data. Rows in these arrays are assumed to correspond to time
        steps, and columns to variables. Multiple arrays can be given; in such
        a case, these arrays usually correspond to [input, output]---for
        example, for a recurrent regression problem---or [input, output,
        weights]---for a weighted regression or classification problem.
    steps : int, optional
        Generate samples of this many time steps. Defaults to 100.
    batch_size : int, optional
        Generate this many samples per call. Defaults to 64. This must match the
        batch_size parameter that was used when creating the recurrent network
        that will process the data.
    rng : :class:`numpy.random.RandomState` or int, optional
        A random number generator, or an integer seed for a random number
        generator. If not provided, the random number generator will be created
        with an automatically chosen seed.

    Returns
    -------
    callable :
        A callable that can be used inside a dataset for training a recurrent
        network.
    '''
    assert batch_size >= 2, 'batch_size must be at least 2!'
    assert isinstance(arrays, (tuple, list)), 'arrays must be a tuple or list!'

    if rng is None or isinstance(rng, int):
        rng = np.random.RandomState(rng)

    def sample():
        xs = [np.zeros((batch_size, steps, a.shape[1]), a.dtype) for a in arrays]
        for i in range(batch_size):
            j = rng.randint(len(arrays[0]) - steps)
            for x, a in zip(xs, arrays):
                x[i] = a[j:j+steps]
        return xs

    return sample

def
batches
(
arrays
,
steps
=
100
,
batch_size
=
64
,
rng
=
)
:
assert
batch_size
>=
2
,
'batch_size must be at least 2!'
assert
isinstance
(
arrays
,
(
tuple
,
list
)
)
,
'arrays must be a tuple or list!'
if
rng
is
or
isinstance
(
rng
,
int
)
:
rng
=
np
.
random
.
RandomState
(
rng
)
def
sample
(
)
:
xs
=
[
np
.
zeros
(
(
batch_size
,
steps
,
a
.
shape
[
1
]
)
,
a
.
dtype
)
for
a
in
arrays
]
for
i
in
range
(
batch_size
)
:
j
=
rng
.
randint
(
len
(
arrays
[
0
]
)
steps
)
for
x
,
a
in
zip
(
xs
,
arrays
)
:
x
[
i
]
=
a
[
j
:
j
+
steps
]
return
xs
return
sample

Create a callable that generates samples from a dataset .


def encode(self, txt):
        '''Encode a text string by replacing characters with alphabet index.

        Parameters
        ----------
        txt : str
            A string to encode.

        Returns
        -------
        classes : list of int
            A sequence of alphabet index values corresponding to the given text.
        '''
        return list(self._fwd_index.get(c, 0) for c in txt)

def encode ( self , txt ) : return list ( self . _fwd_index . get ( c , 0 ) for c in txt )

Encode a text string by replacing characters with alphabet index .


def classifier_batches(self, steps, batch_size, rng=None):
        '''Create a callable that returns a batch of training data.

        Parameters
        ----------
        steps : int
            Number of time steps in each batch.
        batch_size : int
            Number of training examples per batch.
        rng : :class:`numpy.random.RandomState` or int, optional
            A random number generator, or an integer seed for a random number
            generator. If not provided, the random number generator will be
            created with an automatically chosen seed.

        Returns
        -------
        batch : callable
            A callable that, when called, returns a batch of data that can be
            used to train a classifier model.
        '''
        assert batch_size >= 2, 'batch_size must be at least 2!'

        if rng is None or isinstance(rng, int):
            rng = np.random.RandomState(rng)

        T = np.arange(steps)

        def batch():
            inputs = np.zeros((batch_size, steps, 1 + len(self.alpha)), 'f')
            outputs = np.zeros((batch_size, steps), 'i')
            for b in range(batch_size):
                offset = rng.randint(len(self.text) - steps - 1)
                enc = self.encode(self.text[offset:offset + steps + 1])
                inputs[b, T, enc[:-1]] = 1
                outputs[b, T] = enc[1:]
            return [inputs, outputs]

        return batch

def
classifier_batches
(
self
,
steps
,
batch_size
,
rng
=
)
:
assert
batch_size
>=
2
,
'batch_size must be at least 2!'
if
rng
is
or
isinstance
(
rng
,
int
)
:
rng
=
np
.
random
.
RandomState
(
rng
)
T
=
np
.
arange
(
steps
)
def
batch
(
)
:
inputs
=
np
.
zeros
(
(
batch_size
,
steps
,
1
+
len
(
self
.
alpha
)
)
,
'f'
)
outputs
=
np
.
zeros
(
(
batch_size
,
steps
)
,
'i'
)
for
b
in
range
(
batch_size
)
:
offset
=
rng
.
randint
(
len
(
self
.
text
)
steps
1
)
enc
=
self
.
encode
(
self
.
text
[
offset
:
offset
+
steps
+
1
]
)
inputs
[
b
,
T
,
enc
[
:
1
]
]
=
1
outputs
[
b
,
T
]
=
enc
[
1
:
]
return
[
inputs
,
outputs
]
return
batch

Create a callable that returns a batch of training data .


def predict_sequence(self, labels, steps, streams=1, rng=None):
        '''Draw a sequential sample of class labels from this network.

        Parameters
        ----------
        labels : list of int
            A list of integer class labels to get the classifier started.
        steps : int
            The number of time steps to sample.
        streams : int, optional
            Number of parallel streams to sample from the model. Defaults to 1.
        rng : :class:`numpy.random.RandomState` or int, optional
            A random number generator, or an integer seed for a random number
            generator. If not provided, the random number generator will be
            created with an automatically chosen seed.

        Yields
        ------
        label(s) : int or list of int
            Yields at each time step an integer class label sampled sequentially
            from the model. If the number of requested streams is greater than
            1, this will be a list containing the corresponding number of class
            labels.
        '''
        if rng is None or isinstance(rng, int):
            rng = np.random.RandomState(rng)
        offset = len(labels)
        batch = max(2, streams)
        inputs = np.zeros((batch, offset + steps, self.layers[0].output_size), 'f')
        inputs[:, np.arange(offset), labels] = 1
        for i in range(offset, offset + steps):
            chars = []
            for pdf in self.predict_proba(inputs[:i])[:, -1]:
                try:
                    c = rng.multinomial(1, pdf).argmax(axis=-1)
                except ValueError:
                    # sometimes the pdf triggers a normalization error. just
                    # choose greedily in this case.
                    c = pdf.argmax(axis=-1)
                chars.append(int(c))
            inputs[np.arange(batch), i, chars] = 1
            yield chars[0] if streams == 1 else chars

Draw a sequential sample of class labels from this network .


def add_conv_weights(self, name, mean=0, std=None, sparsity=0):
        '''Add a convolutional weight array to this layer's parameters.

        Parameters
        ----------
        name : str
            Name of the parameter to add.
        mean : float, optional
            Mean value for randomly-initialized weights. Defaults to 0.
        std : float, optional
            Standard deviation of initial matrix values. Defaults to
            :math:`1 / sqrt(n_i + n_o)`.
        sparsity : float, optional
            Fraction of weights to set to zero. Defaults to 0.
        '''
        nin = self.input_size
        nout = self.output_size
        mean = self.kwargs.get(
            'mean_{}'.format(name),
            self.kwargs.get('mean', mean))
        std = self.kwargs.get(
            'std_{}'.format(name),
            self.kwargs.get('std', std or 1 / np.sqrt(nin + nout)))
        sparsity = self.kwargs.get(
            'sparsity_{}'.format(name),
            self.kwargs.get('sparsity', sparsity))
        arr = np.zeros((nout, nin) + self.filter_size, util.FLOAT)
        for r in range(self.filter_size[0]):
            for c in range(self.filter_size[1]):
                arr[:, :, r, c] = util.random_matrix(
                    nout, nin, mean, std, sparsity=sparsity, rng=self.rng)
        self._params.append(theano.shared(arr, name=self._fmt(name)))

def add_conv_weights ( self , name , mean = 0 , std = None , sparsity = 0 ) : nin = self . input_size nout = self . output_size mean = self . kwargs . get ( 'mean_{}' . format ( name ) , self . kwargs . get ( 'mean' , mean ) ) std = self . kwargs . get ( 'std_{}' . format ( name ) , self . kwargs . get ( 'std' , std or 1 / np . sqrt ( nin + nout ) ) ) sparsity = self . kwargs . get ( 'sparsity_{}' . format ( name ) , self . kwargs . get ( 'sparsity' , sparsity ) ) arr = np . zeros ( ( nout , nin ) + self . filter_size , util . FLOAT ) for r in range ( self . filter_size [ 0 ] ) : for c in range ( self . filter_size [ 1 ] ) : arr [ : , : , r , c ] = util . random_matrix ( nout , nin , mean , std , sparsity = sparsity , rng = self . rng ) self . _params . append ( theano . shared ( arr , name = self . _fmt ( name ) ) )

Add a convolutional weight array to this layer s parameters .


def encode(self, x, layer=None, sample=False, **kwargs):
        '''Encode a dataset using the hidden layer activations of our network.

        Parameters
        ----------
        x : ndarray
            A dataset to encode. Rows of this dataset capture individual data
            points, while columns represent the variables in each data point.

        layer : str, optional
            The name of the hidden layer output to use. By default, we use
            the "middle" hidden layer---for example, for a 4,2,4 or 4,3,2,3,4
            autoencoder, we use the layer with size 2.

        sample : bool, optional
            If True, then draw a sample using the hidden activations as
            independent Bernoulli probabilities for the encoded data. This
            assumes the hidden layer has a logistic sigmoid activation function.

        Returns
        -------
        ndarray :
            The given dataset, encoded by the appropriate hidden layer
            activation.
        '''
        enc = self.feed_forward(x, **kwargs)[self._find_output(layer)]
        if sample:
            return np.random.binomial(n=1, p=enc).astype(np.uint8)
        return enc

def encode ( self , x , layer = None , sample = False , * * kwargs ) : enc = self . feed_forward ( x , * * kwargs ) [ self . _find_output ( layer ) ] if sample : return np . random . binomial ( n = 1 , p = enc ) . astype ( np . uint8 ) return enc

Encode a dataset using the hidden layer activations of our network .


def decode(self, z, layer=None, **kwargs):
        '''Decode an encoded dataset by computing the output layer activation.

        Parameters
        ----------
        z : ndarray
            A matrix containing encoded data from this autoencoder.
        layer : int or str or :class:`Layer <layers.Layer>`, optional
            The index or name of the hidden layer that was used to encode `z`.

        Returns
        -------
        decoded : ndarray
            The decoded dataset.
        '''
        key = self._find_output(layer)
        if key not in self._functions:
            regs = regularizers.from_kwargs(self, **kwargs)
            outputs, updates = self.build_graph(regs)
            self._functions[key] = theano.function(
                [outputs[key]],
                [outputs[self.layers[-1].output_name]],
                updates=updates)
        return self._functions[key](z)[0]

def decode ( self , z , layer = None , * * kwargs ) : key = self . _find_output ( layer ) if key not in self . _functions : regs = regularizers . from_kwargs ( self , * * kwargs ) outputs , updates = self . build_graph ( regs ) self . _functions [ key ] = theano . function ( [ outputs [ key ] ] , [ outputs [ self . layers [ - 1 ] . output_name ] ] , updates = updates ) return self . _functions [ key ] ( z ) [ 0 ]

Decode an encoded dataset by computing the output layer activation .


def _find_output(self, layer):
        '''Find a layer output name for the given layer specifier.

        Parameters
        ----------
        layer : None, int, str, or :class:`theanets.layers.Layer`
            A layer specification. If this is None, the "middle" layer in the
            network will be used (i.e., the layer at the middle index in the
            list of network layers). If this is an integer, the corresponding
            layer in the network's layer list will be used. If this is a string,
            the layer with the corresponding name will be returned.

        Returns
        -------
        name : str
            The fully-scoped output name for the desired layer.
        '''
        if layer is None:
            layer = len(self.layers) // 2
        if isinstance(layer, int):
            layer = self.layers[layer]
        if isinstance(layer, util.basestring):
            try:
                layer = [l for l in self.layers if l.name == layer][0]
            except IndexError:
                pass
        if isinstance(layer, layers.Layer):
            layer = layer.output_name
        return layer

def _find_output ( self , layer ) : if layer is None : layer = len ( self . layers ) // 2 if isinstance ( layer , int ) : layer = self . layers [ layer ] if isinstance ( layer , util . basestring ) : try : layer = [ l for l in self . layers if l . name == layer ] [ 0 ] except IndexError : pass if isinstance ( layer , layers . Layer ) : layer = layer . output_name return layer

Find a layer output name for the given layer specifier .


def score(self, x, w=None, **kwargs):
        '''Compute R^2 coefficient of determination for a given input.

        Parameters
        ----------
        x : ndarray (num-examples, num-inputs)
            An array containing data to be fed into the network. Multiple
            examples are arranged as rows in this array, with columns containing
            the variables for each example.

        Returns
        -------
        r2 : float
            The R^2 correlation between the prediction of this netork and its
            input. This can serve as one measure of the information loss of the
            autoencoder.
        '''
        return super(Autoencoder, self).score(x, x, w=w, **kwargs)

def score ( self , x , w = None , * * kwargs ) : return super ( Autoencoder , self ) . score ( x , x , w = w , * * kwargs )

Compute R^2 coefficient of determination for a given input .


def predict(self, x, **kwargs):
        '''Compute a greedy classification for the given set of data.

        Parameters
        ----------
        x : ndarray (num-examples, num-variables)
            An array containing examples to classify. Examples are given as the
            rows in this array.

        Returns
        -------
        k : ndarray (num-examples, )
            A vector of class index values, one per row of input data.
        '''
        outputs = self.feed_forward(x, **kwargs)
        return outputs[self.layers[-1].output_name].argmax(axis=-1)

def predict ( self , x , * * kwargs ) : outputs = self . feed_forward ( x , * * kwargs ) return outputs [ self . layers [ - 1 ] . output_name ] . argmax ( axis = - 1 )

Compute a greedy classification for the given set of data .


def predict_proba(self, x, **kwargs):
        '''Compute class posterior probabilities for the given set of data.

        Parameters
        ----------
        x : ndarray (num-examples, num-variables)
            An array containing examples to predict. Examples are given as the
            rows in this array.

        Returns
        -------
        p : ndarray (num-examples, num-classes)
            An array of class posterior probability values, one per row of input
            data.
        '''
        return self.feed_forward(x, **kwargs)[self.layers[-1].output_name]

def predict_proba ( self , x , * * kwargs ) : return self . feed_forward ( x , * * kwargs ) [ self . layers [ - 1 ] . output_name ]

Compute class posterior probabilities for the given set of data .


def predict_logit(self, x, **kwargs):
        '''Compute the logit values that underlie the softmax output.

        Parameters
        ----------
        x : ndarray (num-examples, num-variables)
            An array containing examples to classify. Examples are given as the
            rows in this array.

        Returns
        -------
        l : ndarray (num-examples, num-classes)
            An array of posterior class logit values, one row of logit values
            per row of input data.
        '''
        return self.feed_forward(x, **kwargs)[self.layers[-1].full_name('pre')]

def predict_logit ( self , x , * * kwargs ) : return self . feed_forward ( x , * * kwargs ) [ self . layers [ - 1 ] . full_name ( 'pre' ) ]

Compute the logit values that underlie the softmax output .


def score(self, x, y, w=None, **kwargs):
        '''Compute the mean accuracy on a set of labeled data.

        Parameters
        ----------
        x : ndarray (num-examples, num-variables)
            An array containing examples to classify. Examples are given as the
            rows in this array.
        y : ndarray (num-examples, )
            A vector of integer class labels, one for each row of input data.
        w : ndarray (num-examples, )
            A vector of weights, one for each row of input data.

        Returns
        -------
        score : float
            The (possibly weighted) mean accuracy of the model on the data.
        '''
        eq = y == self.predict(x, **kwargs)
        if w is not None:
            return (w * eq).sum() / w.sum()
        return eq.mean()

def score ( self , x , y , w = None , * * kwargs ) : eq = y == self . predict ( x , * * kwargs ) if w is not None : return ( w * eq ) . sum ( ) / w . sum ( ) return eq . mean ( )

Compute the mean accuracy on a set of labeled data .


def batch_at(features, labels, seq_begins, seq_lengths):
    '''Extract a single batch of data to pass to the model being trained.

    Parameters
    ----------
    features, labels : ndarray
        Arrays of the input features and target labels.
    seq_begins : ndarray
        Array of the start offsets of the speech segments to include.
    seq_lengths : ndarray
        Array of the lengths of the speech segments to include in the batch.

    Returns
    -------
    features, labels, mask : ndarrays
        A triple of arrays for training a network. The first element contains
        input features, the second contains target labels, and the third
        contains a "mask" consisting of ones where there is valid data and zeros
        everywhere else.
    '''
    length = seq_lengths.max()
    feat = np.zeros((BATCH_SIZE, length, features.shape[-1]), 'f')
    labl = np.zeros((BATCH_SIZE, length), 'i')
    mask = np.zeros((BATCH_SIZE, length), 'f')
    for b, (begin, length) in enumerate(zip(seq_begins, seq_lengths)):
        feat[b, :length] = features[begin:begin+length]
        labl[b, :length] = labels[begin:begin+length]
        mask[b, :length] = 1
    return [feat, labl, mask]

def batch_at ( features , labels , seq_begins , seq_lengths ) : length = seq_lengths . max ( ) feat = np . zeros ( ( BATCH_SIZE , length , features . shape [ - 1 ] ) , 'f' ) labl = np . zeros ( ( BATCH_SIZE , length ) , 'i' ) mask = np . zeros ( ( BATCH_SIZE , length ) , 'f' ) for b , ( begin , length ) in enumerate ( zip ( seq_begins , seq_lengths ) ) : feat [ b , : length ] = features [ begin : begin + length ] labl [ b , : length ] = labels [ begin : begin + length ] mask [ b , : length ] = 1 return [ feat , labl , mask ]

Extract a single batch of data to pass to the model being trained .


def batches(dataset):
    '''Returns a callable that chooses sequences from netcdf data.'''
    seq_lengths = dataset.variables['seqLengths'].data
    seq_begins = np.concatenate(([0], np.cumsum(seq_lengths)[:-1]))

    def sample():
        chosen = np.random.choice(
            list(range(len(seq_lengths))), BATCH_SIZE, replace=False)
        return batch_at(dataset.variables['inputs'].data,
                        dataset.variables['targetClasses'].data,
                        seq_begins[chosen],
                        seq_lengths[chosen])

    return sample

def batches ( dataset ) : seq_lengths = dataset . variables [ 'seqLengths' ] . data seq_begins = np . concatenate ( ( [ 0 ] , np . cumsum ( seq_lengths ) [ : - 1 ] ) ) def sample ( ) : chosen = np . random . choice ( list ( range ( len ( seq_lengths ) ) ) , BATCH_SIZE , replace = False ) return batch_at ( dataset . variables [ 'inputs' ] . data , dataset . variables [ 'targetClasses' ] . data , seq_begins [ chosen ] , seq_lengths [ chosen ] ) return sample

Returns a callable that chooses sequences from netcdf data .


def load(self, path):
        '''Load a saved network from a pickle file on disk.

        This method sets the ``network`` attribute of the experiment to the
        loaded network model.

        Parameters
        ----------
        filename : str
            Load the keyword arguments and parameters of a network from a pickle
            file at the named path. If this name ends in ".gz" then the input
            will automatically be gunzipped; otherwise the input will be treated
            as a "raw" pickle.

        Returns
        -------
        network : :class:`Network <graph.Network>`
            A newly-constructed network, with topology and parameters loaded
            from the given pickle file.
        '''
        self.network = graph.Network.load(path)
        return self.network

def load ( self , path ) : self . network = graph . Network . load ( path ) return self . network

Load a saved network from a pickle file on disk .


def random_matrix(rows, cols, mean=0, std=1, sparsity=0, radius=0, diagonal=0, rng=None):
    '''Create a matrix of randomly-initialized weights.

    Parameters
    ----------
    rows : int
        Number of rows of the weight matrix -- equivalently, the number of
        "input" units that the weight matrix connects.
    cols : int
        Number of columns of the weight matrix -- equivalently, the number
        of "output" units that the weight matrix connects.
    mean : float, optional
        Draw initial weight values from a normal with this mean. Defaults to 0.
    std : float, optional
        Draw initial weight values from a normal with this standard deviation.
        Defaults to 1.
    sparsity : float in (0, 1), optional
        If given, ensure that the given fraction of the weight matrix is
        set to zero. Defaults to 0, meaning all weights are nonzero.
    radius : float, optional
        If given, rescale the initial weights to have this spectral radius.
        No scaling is performed by default.
    diagonal : float, optional
        If nonzero, create a matrix containing all zeros except for this value
        along the diagonal. If nonzero, other arguments (except for rows and
        cols) will be ignored.
    rng : :class:`numpy.random.RandomState` or int, optional
        A random number generator, or an integer seed for a random number
        generator. If not provided, the random number generator will be created
        with an automatically chosen seed.

    Returns
    -------
    matrix : numpy array
        An array containing random values. These often represent the weights
        connecting each "input" unit to each "output" unit in a layer.
    '''
    if rng is None or isinstance(rng, int):
        rng = np.random.RandomState(rng)
    arr = mean + std * rng.randn(rows, cols)
    if 1 > sparsity > 0:
        k = min(rows, cols)
        mask = rng.binomial(n=1, p=1 - sparsity, size=(rows, cols)).astype(bool)
        mask[:k, :k] |= np.eye(k).astype(bool)
        arr *= mask
    if radius > 0:
        # rescale weights to have the appropriate spectral radius.
        u, s, vT = np.linalg.svd(arr, full_matrices=False)
        arr = np.dot(np.dot(u, np.diag(radius * s / abs(s[0]))), vT)
    if diagonal != 0:
        # generate a diagonal weight matrix. ignore other options.
        arr = diagonal * np.eye(max(rows, cols))[:rows, :cols]
    return arr.astype(FLOAT)

Create a matrix of randomly - initialized weights .


def random_vector(size, mean=0, std=1, rng=None):
    '''Create a vector of randomly-initialized values.

    Parameters
    ----------
    size : int
        Length of vecctor to create.
    mean : float, optional
        Mean value for initial vector values. Defaults to 0.
    std : float, optional
        Standard deviation for initial vector values. Defaults to 1.
    rng : :class:`numpy.random.RandomState` or int, optional
        A random number generator, or an integer seed for a random number
        generator. If not provided, the random number generator will be created
        with an automatically chosen seed.

    Returns
    -------
    vector : numpy array
        An array containing random values. This often represents the bias for a
        layer of computation units.
    '''
    if rng is None or isinstance(rng, int):
        rng = np.random.RandomState(rng)
    return (mean + std * rng.randn(size)).astype(FLOAT)

def random_vector ( size , mean = 0 , std = 1 , rng = None ) : if rng is None or isinstance ( rng , int ) : rng = np . random . RandomState ( rng ) return ( mean + std * rng . randn ( size ) ) . astype ( FLOAT )

Create a vector of randomly - initialized values .


def outputs_matching(outputs, patterns):
    '''Get the outputs from a network that match a pattern.

    Parameters
    ----------
    outputs : dict or sequence of (str, theano expression)
        Output expressions to filter for matches. If this is a dictionary, its
        ``items()`` will be processed for matches.
    patterns : sequence of str
        A sequence of glob-style patterns to match against. Any parameter
        matching any pattern in this sequence will be included in the match.

    Yields
    ------
    matches : pair of str, theano expression
        Generates a sequence of (name, expression) pairs. The name is the name
        of the output that matched, and the expression is the symbolic output in
        the network graph.
    '''
    if isinstance(patterns, basestring):
        patterns = (patterns, )
    if isinstance(outputs, dict):
        outputs = outputs.items()
    for name, expr in outputs:
        for pattern in patterns:
            if fnmatch.fnmatch(name, pattern):
                yield name, expr
                break

def outputs_matching ( outputs , patterns ) : if isinstance ( patterns , basestring ) : patterns = ( patterns , ) if isinstance ( outputs , dict ) : outputs = outputs . items ( ) for name , expr in outputs : for pattern in patterns : if fnmatch . fnmatch ( name , pattern ) : yield name , expr break

Get the outputs from a network that match a pattern .


def params_matching(layers, patterns):
    '''Get the parameters from a network that match a pattern.

    Parameters
    ----------
    layers : list of :class:`theanets.layers.Layer`
        A list of network layers to retrieve parameters from.
    patterns : sequence of str
        A sequence of glob-style patterns to match against. Any parameter
        matching any pattern in this sequence will be included in the match.

    Yields
    ------
    matches : pair of str, theano expression
        Generates a sequence of (name, expression) pairs. The name is the name
        of the parameter that matched, and the expression represents the
        parameter symbolically.
    '''
    if isinstance(patterns, basestring):
        patterns = (patterns, )
    for layer in layers:
        for param in layer.params:
            name = param.name
            for pattern in patterns:
                if fnmatch.fnmatch(name, pattern):
                    yield name, param
                    break

def params_matching ( layers , patterns ) : if isinstance ( patterns , basestring ) : patterns = ( patterns , ) for layer in layers : for param in layer . params : name = param . name for pattern in patterns : if fnmatch . fnmatch ( name , pattern ) : yield name , param break

Get the parameters from a network that match a pattern .


def from_kwargs(graph, **kwargs):
    '''Construct common regularizers from a set of keyword arguments.

    Keyword arguments not listed below will be passed to
    :func:`Regularizer.build` if they specify the name of a registered
    :class:`Regularizer`.

    Parameters
    ----------
    graph : :class:`theanets.graph.Network`
        A network graph to regularize.

    regularizers : dict or tuple/list of :class:`Regularizer`, optional
        If this is a list or a tuple, the contents of the list will be returned
        as the regularizers. This is to permit custom lists of regularizers to
        be passed easily.

        If this is a dict, its contents will be added to the other keyword
        arguments passed in.

    rng : int or theano RandomStreams, optional
        If an integer is provided, it will be used to seed the random number
        generators for the dropout or noise regularizers. If a theano
        RandomStreams object is provided, it will be used directly. Defaults to
        13.

    input_dropout : float, optional
        Apply dropout to input layers in the network graph, with this dropout
        rate. Defaults to 0 (no dropout).

    hidden_dropout : float, optional
        Apply dropout to hidden layers in the network graph, with this dropout
        rate. Defaults to 0 (no dropout).

    output_dropout : float, optional
        Apply dropout to the output layer in the network graph, with this
        dropout rate. Defaults to 0 (no dropout).

    input_noise : float, optional
        Apply noise to input layers in the network graph, with this standard
        deviation. Defaults to 0 (no noise).

    hidden_noise : float, optional
        Apply noise to hidden layers in the network graph, with this standard
        deviation. Defaults to 0 (no noise).

    output_noise : float, optional
        Apply noise to the output layer in the network graph, with this
        standard deviation. Defaults to 0 (no noise).

    Returns
    -------
    regs : list of :class:`Regularizer`
        A list of regularizers to apply to the given network graph.
    '''
    if 'regularizers' in kwargs:
        regs = kwargs['regularizers']
        if isinstance(regs, (tuple, list)):
            return regs
        if isinstance(regs, dict):
            kwargs.update(regs)

    regs = []

    rng = kwargs.get('rng', 13)

    def pattern(ls):
        return tuple(l.output_name for l in ls)

    inputs = pattern([l for l in graph.layers if isinstance(l, layers.Input)])
    hiddens = pattern(graph.layers[1:-1])
    outputs = pattern([graph.layers[-1]])

    # create regularizers for different types of canned dropout.
    spec = {inputs: kwargs.get('input_dropout', 0),
            hiddens: kwargs.get('hidden_dropout', 0),
            outputs: kwargs.get('output_dropout', 0)}
    spec.update(kwargs.get('dropout', {}))
    for pattern, w in spec.items():
        if w:
            regs.append(BernoulliDropout(pattern=pattern, weight=w, rng=rng))

    # create regularizers for different types of canned noise.
    spec = {inputs: kwargs.get('input_noise', 0),
            hiddens: kwargs.get('hidden_noise', 0),
            outputs: kwargs.get('output_noise', 0)}
    spec.update(kwargs.get('noise', {}))
    for pattern, w in spec.items():
        if w:
            regs.append(GaussianNoise(pattern=pattern, weight=w, rng=rng))

    # create regularizers based on other keyword arguments.
    for key, value in kwargs.items():
        if Regularizer.is_registered(key):
            if not isinstance(value, dict):
                value = dict(weight=value)
            regs.append(Regularizer.build(key, **value))

    return regs

Construct common regularizers from a set of keyword arguments .


def variables(self):
        '''A list of Theano variables used in this loss.'''
        result = [self._target]
        if self._weights is not None:
            result.append(self._weights)
        return result

def variables ( self ) : result = [ self . _target ] if self . _weights is not None : result . append ( self . _weights ) return result

A list of Theano variables used in this loss .


def accuracy(self, outputs):
        '''Build a Theano expression for computing the accuracy of graph output.

        Parameters
        ----------
        outputs : dict of Theano expressions
            A dictionary mapping network output names to Theano expressions
            representing the outputs of a computation graph.

        Returns
        -------
        acc : Theano expression
            A Theano expression representing the accuracy of the output compared
            to the target data.
        '''
        output = outputs[self.output_name]
        predict = TT.argmax(output, axis=-1)
        correct = TT.eq(predict, self._target)
        acc = correct.mean()
        if self._weights is not None:
            acc = (self._weights * correct).sum() / self._weights.sum()
        return acc

def accuracy ( self , outputs ) : output = outputs [ self . output_name ] predict = TT . argmax ( output , axis = - 1 ) correct = TT . eq ( predict , self . _target ) acc = correct . mean ( ) if self . _weights is not None : acc = ( self . _weights * correct ) . sum ( ) / self . _weights . sum ( ) return acc

Build a Theano expression for computing the accuracy of graph output .


def _scan(self, inputs, outputs, name='scan', step=None, constants=None):
        '''Helper method for defining a basic loop in theano.

        Parameters
        ----------
        inputs : sequence of theano expressions
            Inputs to the scan operation.
        outputs : sequence of output specifiers
            Specifiers for the outputs of the scan operation. This should be a
            sequence containing:
            - None for values that are output by the scan but not tapped as
              inputs,
            - an integer or theano scalar (``ndim == 0``) indicating the batch
              size for initial zero state,
            - a theano tensor variable (``ndim > 0``) containing initial state
              data, or
            - a dictionary containing a full output specifier. See
              ``outputs_info`` in the Theano documentation for ``scan``.
        name : str, optional
            Name of the scan variable to create. Defaults to ``'scan'``.
        step : callable, optional
            The callable to apply in the loop. Defaults to :func:`self._step`.
        constants : sequence of tensor, optional
            A sequence of parameters, if any, needed by the step function.

        Returns
        -------
        output(s) : theano expression(s)
            Theano expression(s) representing output(s) from the scan.
        updates : sequence of update tuples
            A sequence of updates to apply inside a theano function.
        '''
        init = []
        for i, x in enumerate(outputs):
            ndim = getattr(x, 'ndim', -1)
            if x is None or isinstance(x, dict) or ndim > 0:
                init.append(x)
                continue
            if isinstance(x, int) or ndim == 0:
                init.append(TT.repeat(theano.shared(
                    np.zeros((1, self.output_size), util.FLOAT),
                    name=self._fmt('init{}'.format(i))), x, axis=0))
                continue
            raise ValueError('cannot handle input {} for scan!'.format(x))
        return theano.scan(
            step or self._step,
            name=self._fmt(name),
            sequences=inputs,
            outputs_info=init,
            non_sequences=constants,
            go_backwards='back' in self.kwargs.get('direction', '').lower(),
            truncate_gradient=self.kwargs.get('bptt_limit', -1),
        )

def
_scan
(
self
,
inputs
,
outputs
,
name
=
'scan'
,
step
=
,
constants
=
)
:
init
=
[
]
for
i
,
x
in
enumerate
(
outputs
)
:
ndim
=
getattr
(
x
,
'ndim'
,
1
)
if
x
is
or
isinstance
(
x
,
dict
)
or
ndim
>
0
:
init
.
append
(
x
)
continue
if
isinstance
(
x
,
int
)
or
ndim
==
0
:
init
.
append
(
TT
.
repeat
(
theano
.
shared
(
np
.
zeros
(
(
1
,
self
.
output_size
)
,
util
.
FLOAT
)
,
name
=
self
.
_fmt
(
'init{}'
.
format
(
i
)
)
)
,
x
,
axis
=
0
)
)
continue
raise
ValueError
(
'cannot handle input {} for scan!'
.
format
(
x
)
)
return
theano
.
scan
(
step
or
self
.
_step
,
name
=
self
.
_fmt
(
name
)
,
sequences
=
inputs
,
outputs_info
=
init
,
non_sequences
=
constants
,
go_backwards
=
'back'
in
self
.
kwargs
.
get
(
'direction'
,
''
)
.
lower
(
)
,
truncate_gradient
=
self
.
kwargs
.
get
(
'bptt_limit'
,
1
)
,
)

Helper method for defining a basic loop in theano .


def build(name, layer, **kwargs):
    '''Construct an activation function by name.

    Parameters
    ----------
    name : str or :class:`Activation`
        The name of the type of activation function to build, or an
        already-created instance of an activation function.
    layer : :class:`theanets.layers.Layer`
        The layer to which this activation will be applied.
    kwargs : dict
        Additional named arguments to pass to the activation constructor.

    Returns
    -------
    activation : :class:`Activation`
        A neural network activation function instance.
    '''
    if isinstance(name, Activation):
        return name

    if '+' in name:
        return functools.reduce(
            Compose, (build(n, layer, **kwargs) for n in name.split('+')))

    act = COMMON.get(name)
    if act is not None:
        act.name = name
        act.params = []
        return act

    if name.lower().startswith('maxout') and ':' in name:
        name, pieces = name.split(':', 1)
        kwargs['pieces'] = int(pieces)
    kwargs['name'] = name
    kwargs['layer'] = layer
    return Activation.build(name, **kwargs)

def build ( name , layer , * * kwargs ) : if isinstance ( name , Activation ) : return name if '+' in name : return functools . reduce ( Compose , ( build ( n , layer , * * kwargs ) for n in name . split ( '+' ) ) ) act = COMMON . get ( name ) if act is not None : act . name = name act . params = [ ] return act if name . lower ( ) . startswith ( 'maxout' ) and ':' in name : name , pieces = name . split ( ':' , 1 ) kwargs [ 'pieces' ] = int ( pieces ) kwargs [ 'name' ] = name kwargs [ 'layer' ] = layer return Activation . build ( name , * * kwargs )

Construct an activation function by name .


def reservoir(xs, n, rng):
        '''Select a random sample of n items from xs.'''
        pool = []
        for i, x in enumerate(xs):
            if len(pool) < n:
                pool.append(x / np.linalg.norm(x))
                continue
            j = rng.randint(i + 1)
            if j < n:
                pool[j] = x / np.linalg.norm(x)
        # if the pool still has fewer than n items, pad with distorted random
        # duplicates from the source data.
        L = len(pool)
        S = np.std(pool, axis=0)
        while len(pool) < n:
            x = pool[rng.randint(L)]
            pool.append(x + S * rng.randn(*x.shape))
        return np.array(pool, dtype=pool[0].dtype)

Select a random sample of n items from xs .


def set_loss(self, *args, **kwargs):
        '''Clear the current loss functions from the network and add a new one.

        All parameters and keyword arguments are passed to :func:`add_loss`
        after clearing the current losses.
        '''
        self.losses = []
        self.add_loss(*args, **kwargs)

def set_loss ( self , * args , * * kwargs ) : self . losses = [ ] self . add_loss ( * args , * * kwargs )

Clear the current loss functions from the network and add a new one .


def itertrain(self, train, valid=None, algo='rmsprop', subalgo='rmsprop',
                  save_every=0, save_progress=None, **kwargs):
        '''Train our network, one batch at a time.

        This method yields a series of ``(train, valid)`` monitor pairs. The
        ``train`` value is a dictionary mapping names to monitor values
        evaluated on the training dataset. The ``valid`` value is also a
        dictionary mapping names to values, but these values are evaluated on
        the validation dataset.

        Because validation might not occur every training iteration, the
        validation monitors might be repeated for multiple training iterations.
        It is probably most helpful to think of the validation monitors as being
        the "most recent" values that have been computed.

        After training completes, the network attribute of this class will
        contain the trained network parameters.

        Parameters
        ----------
        train : :class:`Dataset <downhill.dataset.Dataset>` or list
            A dataset to use when training the network. If this is a
            ``downhill.Dataset`` instance, it will be used directly as the
            training datset. If it is a list of numpy arrays or a list of
            callables, it will be converted to a ``downhill.Dataset`` and then
            used as the training set.
        valid : :class:`Dataset <downhill.dataset.Dataset>` or list, optional
            If this is provided, it will be used as a validation dataset. If not
            provided, the training set will be used for validation. (This is not
            recommended!)
        algo : str, optional
            An optimization algorithm to use for training our network. If not
            provided, :class:`RMSProp <downhill.adaptive.RMSProp>` will be used.
        subalgo : str, optional
            An optimization algorithm to use for a trainer that requires a
            "sub-algorithm," sugh as an unsupervised pretrainer. Defaults to
            :class:`RMSProp <downhill.adaptive.RMSProp>`.
        save_every : int or float, optional
            If this is nonzero and ``save_progress`` is not None, then the model
            being trained will be saved periodically. If this is a float, it is
            treated as a number of minutes to wait between savings. If it is an
            int, it is treated as the number of training epochs to wait between
            savings. Defaults to 0.
        save_progress : str or file handle, optional
            If this is not None, and ``save_progress`` is nonzero, then save the
            model periodically during training. This parameter gives either (a)
            the full path of a file to save the model, or (b) a file-like object
            where the model should be saved. If it is a string and the given
            name contains a "{}" format specifier, it will be filled with the
            integer Unix timestamp at the time the model is saved. Defaults to
            None, which does not save models.

        Yields
        ------
        training : dict
            A dictionary of monitor values computed using the training dataset,
            at the conclusion of training. This dictionary will at least contain
            a 'loss' key that indicates the value of the loss function. Other
            keys may be available depending on the trainer being used.
        validation : dict
            A dictionary of monitor values computed using the validation
            dataset, at the conclusion of training.
        '''
        if 'rng' not in kwargs:
            kwargs['rng'] = self._rng

        def create_dataset(data, **kwargs):
            name = kwargs.get('name', 'dataset')
            s = '{}_batches'.format(name)
            return downhill.Dataset(
                data,
                name=name,
                batch_size=kwargs.get('batch_size', 32),
                iteration_size=kwargs.get('iteration_size', kwargs.get(s)),
                axis=kwargs.get('axis', 0),
                rng=kwargs['rng'])

        # set up datasets ...
        if valid is None:
            valid = train
        if not isinstance(valid, downhill.Dataset):
            valid = create_dataset(valid, name='valid', **kwargs)
        if not isinstance(train, downhill.Dataset):
            train = create_dataset(train, name='train', **kwargs)

        if 'algorithm' in kwargs:
            warnings.warn(
                'please use the "algo" keyword arg instead of "algorithm"',
                DeprecationWarning)
            algo = kwargs.pop('algorithm')
            if isinstance(algo, (list, tuple)):
                algo = algo[0]

        # set up trainer ...
        if isinstance(algo, util.basestring):
            algo = algo.lower()
            if algo == 'sample':
                algo = trainer.SampleTrainer(self)
            elif algo.startswith('layer') or algo.startswith('sup'):
                algo = trainer.SupervisedPretrainer(subalgo, self)
            elif algo.startswith('pre') or algo.startswith('unsup'):
                algo = trainer.UnsupervisedPretrainer(subalgo, self)
            else:
                algo = trainer.DownhillTrainer(algo, self)

        # set up check to save model ...
        def needs_saving(elapsed, iteration):
            if save_progress is None:
                return False
            if isinstance(save_every, float):
                return elapsed > 60 * save_every
            if isinstance(save_every, int):
                return iteration % save_every == 0
            return False

        # train it!
        start = time.time()
        for i, monitors in enumerate(algo.itertrain(train, valid, **kwargs)):
            yield monitors
            now = time.time()
            if i and needs_saving(now - start, i):
                filename_or_handle = save_progress
                if isinstance(filename_or_handle, util.basestring):
                    filename_or_handle = save_progress.format(int(now))
                self.save(filename_or_handle)
                start = now


def train(self, *args, **kwargs):
        '''Train the network until the trainer converges.

        All arguments are passed to :func:`itertrain`.

        Returns
        -------
        training : dict
            A dictionary of monitor values computed using the training dataset,
            at the conclusion of training. This dictionary will at least contain
            a 'loss' key that indicates the value of the loss function. Other
            keys may be available depending on the trainer being used.
        validation : dict
            A dictionary of monitor values computed using the validation
            dataset, at the conclusion of training.
        '''
        monitors = None
        for monitors in self.itertrain(*args, **kwargs):
            pass
        return monitors

def train ( self , * args , * * kwargs ) : monitors = None for monitors in self . itertrain ( * args , * * kwargs ) : pass return monitors

Train the network until the trainer converges .


def _hash(self, regularizers=()):
        '''Construct a string key for representing a computation graph.

        This key will be unique for a given (a) network topology, (b) set of
        losses, and (c) set of regularizers.

        Returns
        -------
        key : str
            A hash representing the computation graph for the current network.
        '''
        def add(s):
            h.update(str(s).encode('utf-8'))
        h = hashlib.md5()
        for l in self.layers:
            add('{}{}{}'.format(l.__class__.__name__, l.name, l.output_shape))
        for l in self.losses:
            add('{}{}'.format(l.__class__.__name__, l.weight))
        for r in regularizers:
            add('{}{}{}'.format(r.__class__.__name__, r.weight, r.pattern))
        return h.hexdigest()

def _hash ( self , regularizers = ( ) ) : def add ( s ) : h . update ( str ( s ) . encode ( 'utf-8' ) ) h = hashlib . md5 ( ) for l in self . layers : add ( '{}{}{}' . format ( l . __class__ . __name__ , l . name , l . output_shape ) ) for l in self . losses : add ( '{}{}' . format ( l . __class__ . __name__ , l . weight ) ) for r in regularizers : add ( '{}{}{}' . format ( r . __class__ . __name__ , r . weight , r . pattern ) ) return h . hexdigest ( )

Construct a string key for representing a computation graph .


def build_graph(self, regularizers=()):
        '''Connect the layers in this network to form a computation graph.

        Parameters
        ----------
        regularizers : list of :class:`theanets.regularizers.Regularizer`
            A list of the regularizers to apply while building the computation
            graph.

        Returns
        -------
        outputs : list of Theano variables
            A list of expressions giving the output of each layer in the graph.
        updates : list of update tuples
            A list of updates that should be performed by a Theano function that
            computes something using this graph.
        '''
        key = self._hash(regularizers)
        if key not in self._graphs:
            util.log('building computation graph')
            for loss in self.losses:
                loss.log()
            for reg in regularizers:
                reg.log()
            outputs = {}
            updates = []
            for layer in self.layers:
                out, upd = layer.connect(outputs)
                for reg in regularizers:
                    reg.modify_graph(out)
                outputs.update(out)
                updates.extend(upd)
            self._graphs[key] = outputs, updates
        return self._graphs[key]

def
build_graph
(
self
,
regularizers
=
(
)
)
:
key
=
self
.
_hash
(
regularizers
)
if
key
not
in
self
.
_graphs
:
util
.
log
(
'building computation graph'
)
for
loss
in
self
.
losses
:
loss
.
log
(
)
for
reg
in
regularizers
:
reg
.
log
(
)
outputs
=
{
}
updates
=
[
]
for
layer
in
self
.
layers
:
out
,
upd
=
layer
.
connect
(
outputs
)
for
reg
in
regularizers
:
reg
.
modify_graph
(
out
)
outputs
.
update
(
out
)
updates
.
extend
(
upd
)
self
.
_graphs
[
key
]
=
outputs
,
updates
return
self
.
_graphs
[
key
]

Connect the layers in this network to form a computation graph .


def inputs(self):
        '''A list of Theano variables for feedforward computations.'''
        return [l.input for l in self.layers if isinstance(l, layers.Input)]

def inputs ( self ) : return [ l . input for l in self . layers if isinstance ( l , layers . Input ) ]

A list of Theano variables for feedforward computations .


def variables(self):
        '''A list of Theano variables for loss computations.'''
        result = self.inputs
        seen = set(i.name for i in result)
        for loss in self.losses:
            for v in loss.variables:
                if v.name not in seen:
                    result.append(v)
                    seen.add(v.name)
        return result

def variables ( self ) : result = self . inputs seen = set ( i . name for i in result ) for loss in self . losses : for v in loss . variables : if v . name not in seen : result . append ( v ) seen . add ( v . name ) return result

A list of Theano variables for loss computations .


def find(self, which, param):
        '''Get a parameter from a layer in the network.

        Parameters
        ----------
        which : int or str
            The layer that owns the parameter to return.

            If this is an integer, then 0 refers to the input layer, 1 refers
            to the first hidden layer, 2 to the second, and so on.

            If this is a string, the layer with the corresponding name, if any,
            will be used.

        param : int or str
            Name of the parameter to retrieve from the specified layer, or its
            index in the parameter list of the layer.

        Raises
        ------
        KeyError
            If there is no such layer, or if there is no such parameter in the
            specified layer.

        Returns
        -------
        param : Theano shared variable
            A shared parameter variable from the indicated layer.
        '''
        for i, layer in enumerate(self.layers):
            if which == i or which == layer.name:
                return layer.find(param)
        raise KeyError(which)

def find ( self , which , param ) : for i , layer in enumerate ( self . layers ) : if which == i or which == layer . name : return layer . find ( param ) raise KeyError ( which )

Get a parameter from a layer in the network .


def feed_forward(self, x, **kwargs):
        '''Compute a forward pass of all layers from the given input.

        All keyword arguments are passed directly to :func:`build_graph`.

        Parameters
        ----------
        x : ndarray (num-examples, num-variables)
            An array containing data to be fed into the network. Multiple
            examples are arranged as rows in this array, with columns containing
            the variables for each example.

        Returns
        -------
        layers : list of ndarray (num-examples, num-units)
            The activation values of each layer in the the network when given
            input `x`. For each of the hidden layers, an array is returned
            containing one row per input example; the columns of each array
            correspond to units in the respective layer. The "output" of the
            network is the last element of this list.
        '''
        regs = regularizers.from_kwargs(self, **kwargs)
        key = self._hash(regs)
        if key not in self._functions:
            outputs, updates = self.build_graph(regs)
            labels, exprs = list(outputs.keys()), list(outputs.values())
            util.log('compiling feed_forward function')
            self._functions[key] = (labels, theano.function(
                self.inputs, exprs, updates=updates))
        labels, f = self._functions[key]
        return dict(zip(labels, f(x)))

def
feed_forward
(
self
,
x
,
*
*
kwargs
)
:
regs
=
regularizers
.
from_kwargs
(
self
,
*
*
kwargs
)
key
=
self
.
_hash
(
regs
)
if
key
not
in
self
.
_functions
:
outputs
,
updates
=
self
.
build_graph
(
regs
)
labels
,
exprs
=
list
(
outputs
.
keys
(
)
)
,
list
(
outputs
.
values
(
)
)
util
.
log
(
'compiling feed_forward function'
)
self
.
_functions
[
key
]
=
(
labels
,
theano
.
function
(
self
.
inputs
,
exprs
,
updates
=
updates
)
)
labels
,
f
=
self
.
_functions
[
key
]
return
dict
(
zip
(
labels
,
f
(
x
)
)
)

Compute a forward pass of all layers from the given input .


def predict(self, x, **kwargs):
        '''Compute a forward pass of the inputs, returning the network output.

        All keyword arguments end up being passed to :func:`build_graph`.

        Parameters
        ----------
        x : ndarray (num-examples, num-variables)
            An array containing data to be fed into the network. Multiple
            examples are arranged as rows in this array, with columns containing
            the variables for each example.

        Returns
        -------
        y : ndarray (num-examples, num-variables)
            Returns the values of the network output units when given input `x`.
            Rows in this array correspond to examples, and columns to output
            variables.
        '''
        return self.feed_forward(x, **kwargs)[self.layers[-1].output_name]

def predict ( self , x , * * kwargs ) : return self . feed_forward ( x , * * kwargs ) [ self . layers [ - 1 ] . output_name ]

Compute a forward pass of the inputs returning the network output .


def score(self, x, y, w=None, **kwargs):
        '''Compute R^2 coefficient of determination for a given labeled input.

        Parameters
        ----------
        x : ndarray (num-examples, num-inputs)
            An array containing data to be fed into the network. Multiple
            examples are arranged as rows in this array, with columns containing
            the variables for each example.
        y : ndarray (num-examples, num-outputs)
            An array containing expected target data for the network. Multiple
            examples are arranged as rows in this array, with columns containing
            the variables for each example.

        Returns
        -------
        r2 : float
            The R^2 correlation between the prediction of this netork and its
            target output.
        '''
        u = y - self.predict(x, **kwargs)
        v = y - y.mean()
        if w is None:
            w = np.ones_like(u)
        return 1 - (w * u * u).sum() / (w * v * v).sum()

def score ( self , x , y , w = None , * * kwargs ) : u = y - self . predict ( x , * * kwargs ) v = y - y . mean ( ) if w is None : w = np . ones_like ( u ) return 1 - ( w * u * u ) . sum ( ) / ( w * v * v ) . sum ( )

Compute R^2 coefficient of determination for a given labeled input .


def save(self, filename_or_handle):
        '''Save the state of this network to a pickle file on disk.

        Parameters
        ----------
        filename_or_handle : str or file handle
            Save the state of this network to a pickle file. If this parameter
            is a string, it names the file where the pickle will be saved. If it
            is a file-like object, this object will be used for writing the
            pickle. If the filename ends in ".gz" then the output will
            automatically be gzipped.
        '''
        if isinstance(filename_or_handle, util.basestring):
            opener = gzip.open if filename_or_handle.lower().endswith('.gz') else open
            handle = opener(filename_or_handle, 'wb')
        else:
            handle = filename_or_handle
        pickle.dump(self, handle, -1)
        if isinstance(filename_or_handle, util.basestring):
            handle.close()
        util.log('saved model to {}', filename_or_handle)

def
save
(
self
,
filename_or_handle
)
:
if
isinstance
(
filename_or_handle
,
util
.
basestring
)
:
opener
=
gzip
.
open
if
filename_or_handle
.
lower
(
)
.
endswith
(
'.gz'
)
else
open
handle
=
opener
(
filename_or_handle
,
'wb'
)
else
:
handle
=
filename_or_handle
pickle
.
dump
(
self
,
handle
,
1
)
if
isinstance
(
filename_or_handle
,
util
.
basestring
)
:
handle
.
close
(
)
util
.
log
(
'saved model to {}'
,
filename_or_handle
)

Save the state of this network to a pickle file on disk .


def load(cls, filename_or_handle):
        '''Load a saved network from disk.

        Parameters
        ----------
        filename_or_handle : str or file handle
            Load the state of this network from a pickle file. If this parameter
            is a string, it names the file where the pickle will be saved. If it
            is a file-like object, this object will be used for reading the
            pickle. If the filename ends in ".gz" then the output will
            automatically be gunzipped.
        '''
        assert not isinstance(cls, Network), \
            'cannot load an instance! say instead: net = Network.load(source)'
        if isinstance(filename_or_handle, util.basestring):
            opener = gzip.open if filename_or_handle.lower().endswith('.gz') else open
            handle = opener(filename_or_handle, 'rb')
        else:
            handle = filename_or_handle
        model = pickle.load(handle)
        if isinstance(filename_or_handle, util.basestring):
            handle.close()
        util.log('loaded model from {}', filename_or_handle)
        return model


def loss(self, **kwargs):
        '''Return a variable representing the regularized loss for this network.

        The regularized loss includes both the :ref:`loss computation <losses>`
        for the network as well as any :ref:`regularizers <regularizers>` that
        are in place.

        Keyword arguments are passed directly to
        :func:`theanets.regularizers.from_kwargs`.

        Returns
        -------
        loss : Theano expression
            A Theano expression representing the loss of this network.
        '''
        regs = regularizers.from_kwargs(self, **kwargs)
        outputs, _ = self.build_graph(regs)
        return sum(l.weight * l(outputs) for l in self.losses) + \
            sum(r.weight * r.loss(self.layers, outputs) for r in regs)

def loss ( self , * * kwargs ) : regs = regularizers . from_kwargs ( self , * * kwargs ) outputs , _ = self . build_graph ( regs ) return sum ( l . weight * l ( outputs ) for l in self . losses ) + sum ( r . weight * r . loss ( self . layers , outputs ) for r in regs )

Return a variable representing the regularized loss for this network .


def updates(self, **kwargs):
        '''Return expressions to run as updates during network training.

        Returns
        -------
        updates : list of (parameter, expression) pairs
            A list of named parameter update expressions for this network.
        '''
        regs = regularizers.from_kwargs(self, **kwargs)
        _, updates = self.build_graph(regs)
        return updates

def updates ( self , * * kwargs ) : regs = regularizers . from_kwargs ( self , * * kwargs ) _ , updates = self . build_graph ( regs ) return updates

Return expressions to run as updates during network training .


def output_size(self):
        '''Number of "neurons" in this layer's default output.'''
        shape = self.output_shape
        if shape is None:
            raise util.ConfigurationError(
                'undefined output size for layer "{}"'.format(self.name))
        return shape[-1]

def
output_size
(
self
)
:
shape
=
self
.
output_shape
if
shape
is
:
raise
util
.
ConfigurationError
(
'undefined output size for layer "{}"'
.
format
(
self
.
name
)
)
return
shape
[
1
]

Number of neurons in this layer s default output .


def connect(self, inputs):
        '''Create Theano variables representing the outputs of this layer.

        Parameters
        ----------
        inputs : dict of Theano expressions
            Symbolic inputs to this layer, given as a dictionary mapping string
            names to Theano expressions. Each string key should be of the form
            "{layer_name}:{output_name}" and refers to a specific output from
            a specific layer in the graph.

        Returns
        -------
        outputs : dict
            A dictionary mapping names to Theano expressions for the outputs
            from this layer.
        updates : sequence of (parameter, expression) tuples
            Updates that should be performed by a Theano function that computes
            something using this layer.
        '''
        outputs, updates = self.transform(inputs)
        # transform the outputs to be a list of ordered pairs if needed.
        if isinstance(outputs, dict):
            outputs = sorted(outputs.items())
        if isinstance(outputs, (TT.TensorVariable, SS.SparseVariable)):
            outputs = [('out', outputs)]
        outs = {self.full_name(name): expr for name, expr in outputs}
        return outs, updates

Create Theano variables representing the outputs of this layer .


def bind(self, graph, reset=True, initialize=True):
        '''Bind this layer into a computation graph.

        This method is a wrapper for performing common initialization tasks. It
        calls :func:`resolve`, :func:`setup`, and :func:`log`.

        Parameters
        ----------
        graph : :class:`Network <theanets.graph.Network>`
            A computation network in which this layer is to be bound.
        reset : bool, optional
            If ``True`` (the default), reset the resolved layers for this layer.
        initialize : bool, optional
            If ``True`` (the default), initialize the parameters for this layer
            by calling :func:`setup`.

        Raises
        ------
        theanets.util.ConfigurationError :
            If an input cannot be resolved.
        '''
        if reset:
            for k in self._input_shapes:
                self._input_shapes[k] = None
            for k in self._output_shapes:
                self._output_shapes[k] = None
        self.resolve_inputs(graph.layers)
        self.resolve_outputs()
        self.activate = activations.build(
            self.kwargs.get('activation', 'relu'), self)
        if initialize:
            self.setup()
        self.log()

def bind ( self , graph , reset = True , initialize = True ) : if reset : for k in self . _input_shapes : self . _input_shapes [ k ] = None for k in self . _output_shapes : self . _output_shapes [ k ] = None self . resolve_inputs ( graph . layers ) self . resolve_outputs ( ) self . activate = activations . build ( self . kwargs . get ( 'activation' , 'relu' ) , self ) if initialize : self . setup ( ) self . log ( )

Bind this layer into a computation graph .


def resolve_inputs(self, layers):
        '''Resolve the names of inputs for this layer into shape tuples.

        Parameters
        ----------
        layers : list of :class:`Layer`
            A list of the layers that are available for resolving inputs.

        Raises
        ------
        theanets.util.ConfigurationError :
            If an input cannot be resolved.
        '''
        resolved = {}
        for name, shape in self._input_shapes.items():
            if shape is None:
                name, shape = self._resolve_shape(name, layers)
            resolved[name] = shape
        self._input_shapes = resolved

def resolve_inputs ( self , layers ) : resolved = { } for name , shape in self . _input_shapes . items ( ) : if shape is None : name , shape = self . _resolve_shape ( name , layers ) resolved [ name ] = shape self . _input_shapes = resolved

Resolve the names of inputs for this layer into shape tuples .


def resolve_outputs(self):
        '''Resolve the names of outputs for this layer into shape tuples.'''
        input_shape = None
        for i, shape in enumerate(self._input_shapes.values()):
            if i == 0:
                input_shape = shape
            if len(input_shape) != len(shape) or any(
                    a is not None and b is not None and a != b
                    for a, b in zip(input_shape[:-1], shape[:-1])):
                raise util.ConfigurationError(
                    'layer "{}" incompatible input shapes {}'
                    .format(self.name, self._input_shapes))
        size = self.kwargs.get('size')
        shape = self.kwargs.get('shape')
        if shape is not None:
            pass
        elif size is not None:
            shape = tuple(input_shape[:-1]) + (size, )
        else:
            raise util.ConfigurationError(
                'layer "{}" does not specify a size'.format(self.name))
        self._output_shapes['out'] = shape

Resolve the names of outputs for this layer into shape tuples .


def log(self):
        '''Log some information about this layer.'''
        inputs = ', '.join('"{0}" {1}'.format(*ns) for ns in self._input_shapes.items())
        util.log('layer {0.__class__.__name__} "{0.name}" {0.output_shape} {1} from {2}',
                 self, getattr(self.activate, 'name', self.activate), inputs)
        util.log('learnable parameters: {}', self.log_params())


def log_params(self):
        '''Log information about this layer's parameters.'''
        total = 0
        for p in self.params:
            shape = p.get_value().shape
            util.log('parameter "{}" {}', p.name, shape)
            total += np.prod(shape)
        return total

def
log_params
(
self
)
:
total
=
0
for
p
in
self
.
params
:
shape
=
p
.
get_value
(
)
.
shape
util
.
log
(
'parameter "{}" {}'
,
p
.
name
,
shape
)
total
+=
np
.
prod
(
shape
)
return
total

Log information about this layer s parameters .


def _fmt(self, string):
        '''Helper method to format our name into a string.'''
        if '{' not in string:
            string = '{}.' + string
        return string.format(self.name)

def _fmt ( self , string ) : if '{' not in string : string = '{}.' + string return string . format ( self . name )

Helper method to format our name into a string .


def _resolve_shape(self, name, layers):
        '''Given a list of layers, find the layer output with the given name.

        Parameters
        ----------
        name : str
            Name of a layer to resolve.
        layers : list of :class:`theanets.layers.base.Layer`
            A list of layers to search in.

        Raises
        ------
        util.ConfigurationError :
            If there is no such layer, or if there are more than one.

        Returns
        -------
        name : str
            The fully-scoped name of the desired output.
        shape : tuple of None and/or int
            The shape of the named output.
        '''
        matches = [l for l in layers if name.split(':')[0] == l.name]
        if len(matches) != 1:
            raise util.ConfigurationError(
                'layer "{}" cannot resolve "{}" using {}'
                .format(self.name, name, [l.name for l in layers]))
        name = name if ':' in name else matches[0].output_name
        return name, matches[0]._output_shapes[name.split(':')[1]]

Given a list of layers find the layer output with the given name .


def find(self, key):
        '''Get a shared variable for a parameter by name.

        Parameters
        ----------
        key : str or int
            The name of the parameter to look up, or the index of the parameter
            in our parameter list. These are both dependent on the
            implementation of the layer.

        Returns
        -------
        param : shared variable
            A shared variable containing values for the given parameter.

        Raises
        ------
        KeyError
            If a param with the given name does not exist.
        '''
        name = self._fmt(str(key))
        for i, p in enumerate(self._params):
            if key == i or name == p.name:
                return p
        raise KeyError(key)

def find ( self , key ) : name = self . _fmt ( str ( key ) ) for i , p in enumerate ( self . _params ) : if key == i or name == p . name : return p raise KeyError ( key )

Get a shared variable for a parameter by name .


def add_bias(self, name, size, mean=0, std=1):
        '''Helper method to create a new bias vector.

        Parameters
        ----------
        name : str
            Name of the parameter to add.
        size : int
            Size of the bias vector.
        mean : float, optional
            Mean value for randomly-initialized biases. Defaults to 0.
        std : float, optional
            Standard deviation for randomly-initialized biases. Defaults to 1.
        '''
        mean = self.kwargs.get('mean_{}'.format(name), mean)
        std = self.kwargs.get('std_{}'.format(name), std)
        self._params.append(theano.shared(
            util.random_vector(size, mean, std, rng=self.rng),
            name=self._fmt(name)))

def add_bias ( self , name , size , mean = 0 , std = 1 ) : mean = self . kwargs . get ( 'mean_{}' . format ( name ) , mean ) std = self . kwargs . get ( 'std_{}' . format ( name ) , std ) self . _params . append ( theano . shared ( util . random_vector ( size , mean , std , rng = self . rng ) , name = self . _fmt ( name ) ) )

Helper method to create a new bias vector .


def to_spec(self):
        '''Create a specification dictionary for this layer.

        Returns
        -------
        spec : dict
            A dictionary specifying the configuration of this layer.
        '''
        spec = dict(**self.kwargs)
        spec.update(
            form=self.__class__.__name__.lower(),
            name=self.name,
            activation=self.kwargs.get('activation', 'relu'),
        )
        return spec

def to_spec ( self ) : spec = dict ( * * self . kwargs ) spec . update ( form = self . __class__ . __name__ . lower ( ) , name = self . name , activation = self . kwargs . get ( 'activation' , 'relu' ) , ) return spec

Create a specification dictionary for this layer .


def loggabor(self, x_pos, y_pos, sf_0, B_sf, theta, B_theta, preprocess=True):
        """
        Returns the envelope of a LogGabor

        Note that the convention for coordinates follows that of matrices:
        the origin is at the top left of the image, and coordinates are first
        the rows (vertical axis, going down) then the columns (horizontal axis,
        going right).

        """

        env = np.multiply(self.band(sf_0, B_sf), self.orientation(theta, B_theta))
        if not(x_pos==0.) and not(y_pos==0.): # bypass translation whenever none is needed
              env = env.astype(np.complex128) * self.trans(x_pos*1., y_pos*1.)
        if preprocess : env *= self.f_mask # retina processing
        # normalizing energy:
        env /= np.sqrt((np.abs(env)**2).mean())
        # in the case a a single bump (see ``orientation``), we should compensate the fact that the distribution gets complex:
        env *= np.sqrt(2.)
        return env


def loggabor_image(self, x_pos, y_pos, theta, sf_0, phase, B_sf, B_theta):
        """
        Returns the image of a LogGabor

        Note that the convention for coordinates follows that of matrices:
        the origin is at the top left of the image, and coordinates are first
        the rows (vertical axis, going down) then the columns (horizontal axis,
        going right).

        """

        FT_lg = self.loggabor(x_pos, y_pos, sf_0=sf_0, B_sf=B_sf, theta=theta, B_theta=B_theta)
        FT_lg = FT_lg * np.exp(1j * phase)
        return self.invert(FT_lg, full=False)

def loggabor_image ( self , x_pos , y_pos , theta , sf_0 , phase , B_sf , B_theta ) : FT_lg = self . loggabor ( x_pos , y_pos , sf_0 = sf_0 , B_sf = B_sf , theta = theta , B_theta = B_theta ) FT_lg = FT_lg * np . exp ( 1j * phase ) return self . invert ( FT_lg , full = False )


def add_tier(self, name, tier_type='IntervalTier', number=None):
        """Add an IntervalTier or a TextTier on the specified location.

        :param str name: Name of the tier, duplicate names is allowed.
        :param str tier_type: Type of the tier.
        :param int number: Place to insert the tier, when ``None`` the number
            is generated and the tier will be placed on the bottom.
        :returns: The created tier.
        :raises ValueError: If the number is out of bounds.
        """
        if number is None:
            number = 1 if not self.tiers else len(self.tiers)+1
        elif number < 1 or number > len(self.tiers):
            raise ValueError('Number not in [1..{}]'.format(len(self.tiers)))
        elif tier_type not in Tier.P_TIERS:
            raise ValueError('tier_type has to be in {}'.format(self.P_TIERS))
        self.tiers.insert(number-1,
                          Tier(self.xmin, self.xmax, name, tier_type))
        return self.tiers[number-1]

def
add_tier
(
self
,
name
,
tier_type
=
'IntervalTier'
,
number
=
)
:
if
number
is
:
number
=
1
if
not
self
.
tiers
else
len
(
self
.
tiers
)
+
1
elif
number
<
1
or
number
>
len
(
self
.
tiers
)
:
raise
ValueError
(
'Number not in [1..{}]'
.
format
(
len
(
self
.
tiers
)
)
)
elif
tier_type
not
in
Tier
.
P_TIERS
:
raise
ValueError
(
'tier_type has to be in {}'
.
format
(
self
.
P_TIERS
)
)
self
.
tiers
.
insert
(
number
1
,
Tier
(
self
.
xmin
,
self
.
xmax
,
name
,
tier_type
)
)
return
self
.
tiers
[
number
1
]

Add an IntervalTier or a TextTier on the specified location .


def remove_tier(self, name_num):
        """Remove a tier, when multiple tiers exist with that name only the
        first is removed.

        :param name_num: Name or number of the tier to remove.
        :type name_num: int or str
        :raises IndexError: If there is no tier with that number.
        """
        if isinstance(name_num, int):
            del(self.tiers[name_num-1])
        else:
            self.tiers = [i for i in self.tiers if i.name != name_num]

def remove_tier ( self , name_num ) : if isinstance ( name_num , int ) : del ( self . tiers [ name_num - 1 ] ) else : self . tiers = [ i for i in self . tiers if i . name != name_num ]

Remove a tier when multiple tiers exist with that name only the first is removed .


def get_tier(self, name_num):
        """Gives a tier, when multiple tiers exist with that name only the
        first is returned.

        :param name_num: Name or number of the tier to return.
        :type name_num: int or str
        :returns: The tier.
        :raises IndexError: If the tier doesn't exist.
        """
        return self.tiers[name_num - 1] if isinstance(name_num, int) else\
            [i for i in self.tiers if i.name == name_num][0]

def get_tier ( self , name_num ) : return self . tiers [ name_num - 1 ] if isinstance ( name_num , int ) else [ i for i in self . tiers if i . name == name_num ] [ 0 ]

Gives a tier when multiple tiers exist with that name only the first is returned .


def to_eaf(self, skipempty=True, pointlength=0.1):
        """Convert the object to an pympi.Elan.Eaf object

        :param int pointlength: Length of respective interval from points in
                                seconds
        :param bool skipempty: Skip the empty annotations
        :returns: :class:`pympi.Elan.Eaf` object
        :raises ImportError: If the Eaf module can't be loaded.
        :raises ValueError: If the pointlength is not strictly positive.
        """
        from pympi.Elan import Eaf
        eaf_out = Eaf()
        if pointlength <= 0:
            raise ValueError('Pointlength should be strictly positive')
        for tier in self.get_tiers():
            eaf_out.add_tier(tier.name)
            for ann in tier.get_intervals(True):
                if tier.tier_type == 'TextTier':
                    ann = (ann[0], ann[0]+pointlength, ann[1])
                if ann[2].strip() or not skipempty:
                    eaf_out.add_annotation(tier.name, int(round(ann[0]*1000)),
                                           int(round(ann[1]*1000)), ann[2])
        return eaf_out

Convert the object to an pympi . Elan . Eaf object


def add_point(self, point, value, check=True):
        """Add a point to the TextTier

        :param int point: Time of the point.
        :param str value: Text of the point.
        :param bool check: Flag to check for overlap.
        :raises Exception: If overlap or wrong tiertype.
        """
        if self.tier_type != 'TextTier':
            raise Exception('Tiertype must be TextTier.')
        if check and any(i for i in self.intervals if i[0] == point):
                raise Exception('No overlap is allowed')
        self.intervals.append((point, value))

def
add_point
(
self
,
point
,
value
,
check
=
True
)
:
if
self
.
tier_type
!=
'TextTier'
:
raise
Exception
(
'Tiertype must be TextTier.'
)
if
check
and
any
(
i
for
i
in
self
.
intervals
if
i
[
0
]
==
point
)
:
raise
Exception
(
'No overlap is allowed'
)
self
.
intervals
.
append
(
(
point
,
value
)
)


def add_interval(self, begin, end, value, check=True):
        """Add an interval to the IntervalTier.

        :param float begin: Start time of the interval.
        :param float end: End time of the interval.
        :param str value: Text of the interval.
        :param bool check: Flag to check for overlap.
        :raises Exception: If overlap, begin > end or wrong tiertype.
        """
        if self.tier_type != 'IntervalTier':
            raise Exception('Tiertype must be IntervalTier')
        if check:
            if any(i for i in self.intervals if begin < i[1] and end > i[0]):
                raise Exception('No overlap is allowed')
            if begin > end:
                raise Exception('Begin must be smaller then end')
        self.intervals.append((begin, end, value))

def
add_interval
(
self
,
begin
,
end
,
value
,
check
=
True
)
:
if
self
.
tier_type
!=
'IntervalTier'
:
raise
Exception
(
'Tiertype must be IntervalTier'
)
if
check
:
if
any
(
i
for
i
in
self
.
intervals
if
begin
<
i
[
1
]
and
end
>
i
[
0
]
)
:
raise
Exception
(
'No overlap is allowed'
)
if
begin
>
end
:
raise
Exception
(
'Begin must be smaller then end'
)
self
.
intervals
.
append
(
(
begin
,
end
,
value
)
)


def remove_interval(self, time):
        """Remove an interval, if no interval is found nothing happens.

        :param int time: Time of the interval.
        :raises TierTypeException: If the tier is not a IntervalTier.
        """
        if self.tier_type != 'IntervalTier':
            raise Exception('Tiertype must be IntervalTier.')
        self.intervals = [i for i in self.intervals
                          if not(i[0] <= time and i[1] >= time)]

def
remove_interval
(
self
,
time
)
:
if
self
.
tier_type
!=
'IntervalTier'
:
raise
Exception
(
'Tiertype must be IntervalTier.'
)
self
.
intervals
=
[
i
for
i
in
self
.
intervals
if
not
(
i
[
0
]
<=
time
and
i
[
1
]
>=
time
)
]

Remove an interval if no interval is found nothing happens .


def remove_point(self, time):
        """Remove a point, if no point is found nothing happens.

        :param int time: Time of the point.
        :raises TierTypeException: If the tier is not a TextTier.
        """
        if self.tier_type != 'TextTier':
            raise Exception('Tiertype must be TextTier.')
        self.intervals = [i for i in self.intervals if i[0] != time]

def
remove_point
(
self
,
time
)
:
if
self
.
tier_type
!=
'TextTier'
:
raise
Exception
(
'Tiertype must be TextTier.'
)
self
.
intervals
=
[
i
for
i
in
self
.
intervals
if
i
[
0
]
!=
time
]

Remove a point if no point is found nothing happens .


def get_intervals(self, sort=False):
        """Give all the intervals or points.

        :param bool sort: Flag for yielding the intervals or points sorted.
        :yields: All the intervals
        """
        for i in sorted(self.intervals) if sort else self.intervals:
            yield i

def get_intervals ( self , sort = False ) : for i in sorted ( self . intervals ) if sort else self . intervals : yield i


def get_all_intervals(self):
        """Returns the true list of intervals including the empty intervals."""
        ints = sorted(self.get_intervals(True))
        if self.tier_type == 'IntervalTier':
            if not ints:
                ints.append((self.xmin, self.xmax, ''))
            else:
                if ints[0][0] > self.xmin:
                    ints.insert(0, (self.xmin, ints[0][0], ''))
                if ints[-1][1] < self.xmax:
                    ints.append((ints[-1][1], self.xmax, ''))
                p = ints[-1]
                for index, i in reversed(list(enumerate(ints[:-1], 1))):
                    if p[0] - i[1] != 0:
                        ints.insert(index, (i[1], p[0], ''))
                    p = i
        return ints

def get_all_intervals ( self ) : ints = sorted ( self . get_intervals ( True ) ) if self . tier_type == 'IntervalTier' : if not ints : ints . append ( ( self . xmin , self . xmax , '' ) ) else : if ints [ 0 ] [ 0 ] > self . xmin : ints . insert ( 0 , ( self . xmin , ints [ 0 ] [ 0 ] , '' ) ) if ints [ - 1 ] [ 1 ] < self . xmax : ints . append ( ( ints [ - 1 ] [ 1 ] , self . xmax , '' ) ) p = ints [ - 1 ] for index , i in reversed ( list ( enumerate ( ints [ : - 1 ] , 1 ) ) ) : if p [ 0 ] - i [ 1 ] != 0 : ints . insert ( index , ( i [ 1 ] , p [ 0 ] , '' ) ) p = i return ints

Returns the true list of intervals including the empty intervals .


def indent(el, level=0):
    """Function to pretty print the xml, meaning adding tabs and newlines.

    :param ElementTree.Element el: Current element.
    :param int level: Current level.
    """
    i = '\n' + level * '\t'
    if len(el):
        if not el.text or not el.text.strip():
            el.text = i+'\t'
        if not el.tail or not el.tail.strip():
            el.tail = i
        for elem in el:
            indent(elem, level+1)
        if not el.tail or not el.tail.strip():
            el.tail = i
    else:
        if level and (not el.tail or not el.tail.strip()):
            el.tail = i

def indent ( el , level = 0 ) : i = '\n' + level * '\t' if len ( el ) : if not el . text or not el . text . strip ( ) : el . text = i + '\t' if not el . tail or not el . tail . strip ( ) : el . tail = i for elem in el : indent ( elem , level + 1 ) if not el . tail or not el . tail . strip ( ) : el . tail = i else : if level and ( not el . tail or not el . tail . strip ( ) ) : el . tail = i

Function to pretty print the xml meaning adding tabs and newlines .


def add_annotation(self, id_tier, start, end, value='', svg_ref=None):
        """Add an annotation.

        :param str id_tier: Name of the tier.
        :param int start: Start time of the annotation.
        :param int end: End time of the annotation.
        :param str value: Value of the annotation.
        :param str svg_ref: Svg reference.
        :raises KeyError: If the tier is non existent.
        :raises ValueError: If one of the values is negative or start is bigger
                            then end or if the tiers already contains ref
                            annotations.
        """
        if self.tiers[id_tier][1]:
            raise ValueError('Tier already contains ref annotations...')
        if start == end:
            raise ValueError('Annotation length is zero...')
        if start > end:
            raise ValueError('Annotation length is negative...')
        if start < 0:
            raise ValueError('Start is negative...')
        start_ts = self.generate_ts_id(start)
        end_ts = self.generate_ts_id(end)
        aid = self.generate_annotation_id()
        self.annotations[aid] = id_tier
        self.tiers[id_tier][0][aid] = (start_ts, end_ts, value, svg_ref)


def add_cv_entry(self, cv_id, cve_id, values, ext_ref=None):
        """Add an entry to a controlled vocabulary.

        :param str cv_id: Name of the controlled vocabulary to add an entry.
        :param str cve_id: Name of the entry.
        :param list values: List of values of the form:
            ``(value, lang_ref, description)`` where description can be
            ``None``.
        :param str ext_ref: External reference.
        :throws KeyError: If there is no controlled vocabulary with that id.
        :throws ValueError: If a language in one of the entries doesn't exist.
        """
        for value, lang_ref, description in values:
            if lang_ref not in self.languages:
                raise ValueError('Language not present: {}'.format(lang_ref))
        self.controlled_vocabularies[cv_id][1][cve_id] = (values, ext_ref)

def
add_cv_entry
(
self
,
cv_id
,
cve_id
,
values
,
ext_ref
=
)
:
for
value
,
lang_ref
,
description
in
values
:
if
lang_ref
not
in
self
.
languages
:
raise
ValueError
(
'Language not present: {}'
.
format
(
lang_ref
)
)
self
.
controlled_vocabularies
[
cv_id
]
[
1
]
[
cve_id
]
=
(
values
,
ext_ref
)

Add an entry to a controlled vocabulary .


def add_cv_description(self, cv_id, lang_ref, description=None):
        """Add a description to a controlled vocabulary.

        :param str cv_id: Name of the controlled vocabulary to add the
            description.
        :param str lang_ref: Language reference.
        :param str description: Description, this can be none.
        :throws KeyError: If there is no controlled vocabulary with that id.
        :throws ValueError: If the language provided doesn't exist.
        """
        if lang_ref not in self.languages:
            raise ValueError('Language not present: {}'.format(lang_ref))
        self.controlled_vocabularies[cv_id][0].append((lang_ref, description))

def
add_cv_description
(
self
,
cv_id
,
lang_ref
,
description
=
)
:
if
lang_ref
not
in
self
.
languages
:
raise
ValueError
(
'Language not present: {}'
.
format
(
lang_ref
)
)
self
.
controlled_vocabularies
[
cv_id
]
[
0
]
.
append
(
(
lang_ref
,
description
)
)

Add a description to a controlled vocabulary .


def add_external_ref(self, eid, etype, value):
        """Add an external reference.

        :param str eid: Name of the external reference.
        :param str etype: Type of the external reference, has to be in
            ``['iso12620', 'ecv', 'cve_id', 'lexen_id', 'resource_url']``.
        :param str value: Value of the external reference.
        :throws KeyError: if etype is not in the list of possible types.
        """
        if etype not in self.ETYPES:
            raise KeyError('etype not in {}'.format(self.ETYPES))
        self.external_refs[eid] = (etype, value)

def
add_external_ref
(
self
,
eid
,
etype
,
value
)
:
if
etype
not
in
self
.
ETYPES
:
raise
KeyError
(
'etype not in {}'
.
format
(
self
.
ETYPES
)
)
self
.
external_refs
[
eid
]
=
(
etype
,
value
)


def add_language(self, lang_id, lang_def=None, lang_label=None):
        """Add a language.

        :param str lang_id: ID of the language.
        :param str lang_def: Definition of the language(preferably ISO-639-3).
        :param str lang_label: Label of the language.
        """
        self.languages[lang_id] = (lang_def, lang_label)

def add_language ( self , lang_id , lang_def = None , lang_label = None ) : self . languages [ lang_id ] = ( lang_def , lang_label )


def add_lexicon_ref(self, lrid, name, lrtype, url, lexicon_id,
                        lexicon_name, datcat_id=None, datcat_name=None):
        """Add lexicon reference.

        :param str lrid: Lexicon reference internal ID.
        :param str name: Lexicon reference display name.
        :param str lrtype: Lexicon reference service type.
        :param str url: Lexicon reference service location
        :param str lexicon_id: Lexicon reference service id.
        :param str lexicon_name: Lexicon reference service name.
        :param str datacat_id: Lexicon reference identifier of data category.
        :param str datacat_name: Lexicon reference name of data category.
        """
        self.lexicon_refs[lrid] = {
            'LEX_REF_ID': lrid,
            'NAME': name,
            'TYPE': lrtype,
            'URL': url,
            'LEXICON_ID': lexicon_id,
            'LEXICON_NAME': lexicon_name,
            'DATCAT_ID': datcat_id,
            'DATCAT_NAME': datcat_name
            }

def add_lexicon_ref ( self , lrid , name , lrtype , url , lexicon_id , lexicon_name , datcat_id = None , datcat_name = None ) : self . lexicon_refs [ lrid ] = { 'LEX_REF_ID' : lrid , 'NAME' : name , 'TYPE' : lrtype , 'URL' : url , 'LEXICON_ID' : lexicon_id , 'LEXICON_NAME' : lexicon_name , 'DATCAT_ID' : datcat_id , 'DATCAT_NAME' : datcat_name }


def add_linguistic_type(self, lingtype, constraints=None,
                            timealignable=True, graphicreferences=False,
                            extref=None, param_dict=None):
        """Add a linguistic type.

        :param str lingtype: Name of the linguistic type.
        :param str constraints: Constraint name.
        :param bool timealignable: Flag for time alignable.
        :param bool graphicreferences: Flag for graphic references.
        :param str extref: External reference.
        :param dict param_dict: TAG attributes, when this is not ``None`` it
                                 will ignore all other options. Please only use
                                 dictionaries coming from the
                                 :func:`get_parameters_for_linguistic_type`
        :raises KeyError: If a constraint is not defined
        """
        if param_dict:
            self.linguistic_types[lingtype] = param_dict
        else:
            if constraints:
                self.constraints[constraints]
            self.linguistic_types[lingtype] = {
                'LINGUISTIC_TYPE_ID': lingtype,
                'TIME_ALIGNABLE': str(timealignable).lower(),
                'GRAPHIC_REFERENCES': str(graphicreferences).lower(),
                'CONSTRAINTS': constraints}
            if extref is not None:
                self.linguistic_types[lingtype]['EXT_REF'] = extref

def add_linguistic_type ( self , lingtype , constraints = None , timealignable = True , graphicreferences = False , extref = None , param_dict = None ) : if param_dict : self . linguistic_types [ lingtype ] = param_dict else : if constraints : self . constraints [ constraints ] self . linguistic_types [ lingtype ] = { 'LINGUISTIC_TYPE_ID' : lingtype , 'TIME_ALIGNABLE' : str ( timealignable ) . lower ( ) , 'GRAPHIC_REFERENCES' : str ( graphicreferences ) . lower ( ) , 'CONSTRAINTS' : constraints } if extref is not None : self . linguistic_types [ lingtype ] [ 'EXT_REF' ] = extref


def add_linked_file(self, file_path, relpath=None, mimetype=None,
                        time_origin=None, ex_from=None):
        """Add a linked file.

        :param str file_path: Path of the file.
        :param str relpath: Relative path of the file.
        :param str mimetype: Mimetype of the file, if ``None`` it tries to
            guess it according to the file extension which currently only works
            for wav, mpg, mpeg and xml.
        :param int time_origin: Time origin for the media file.
        :param str ex_from: Extracted from field.
        :raises KeyError: If mimetype had to be guessed and a non standard
                          extension or an unknown mimetype.
        """
        if mimetype is None:
            mimetype = self.MIMES[file_path.split('.')[-1]]
        self.media_descriptors.append({
            'MEDIA_URL': file_path, 'RELATIVE_MEDIA_URL': relpath,
            'MIME_TYPE': mimetype, 'TIME_ORIGIN': time_origin,
            'EXTRACTED_FROM': ex_from})

def add_linked_file ( self , file_path , relpath = None , mimetype = None , time_origin = None , ex_from = None ) : if mimetype is None : mimetype = self . MIMES [ file_path . split ( '.' ) [ - 1 ] ] self . media_descriptors . append ( { 'MEDIA_URL' : file_path , 'RELATIVE_MEDIA_URL' : relpath , 'MIME_TYPE' : mimetype , 'TIME_ORIGIN' : time_origin , 'EXTRACTED_FROM' : ex_from } )


def add_locale(self, language_code, country_code=None, variant=None):
        """Add a locale.

        :param str language_code: The language code of the locale.
        :param str country_code: The country code of the locale.
        :param str variant: The variant of the locale.
        """
        self.locales[language_code] = (country_code, variant)

def add_locale ( self , language_code , country_code = None , variant = None ) : self . locales [ language_code ] = ( country_code , variant )


def add_secondary_linked_file(self, file_path, relpath=None, mimetype=None,
                                  time_origin=None, assoc_with=None):
        """Add a secondary linked file.

        :param str file_path: Path of the file.
        :param str relpath: Relative path of the file.
        :param str mimetype: Mimetype of the file, if ``None`` it tries to
            guess it according to the file extension which currently only works
            for wav, mpg, mpeg and xml.
        :param int time_origin: Time origin for the media file.
        :param str assoc_with: Associated with field.
        :raises KeyError: If mimetype had to be guessed and a non standard
                          extension or an unknown mimetype.
        """
        if mimetype is None:
            mimetype = self.MIMES[file_path.split('.')[-1]]
        self.linked_file_descriptors.append({
            'LINK_URL': file_path, 'RELATIVE_LINK_URL': relpath,
            'MIME_TYPE': mimetype, 'TIME_ORIGIN': time_origin,
            'ASSOCIATED_WITH': assoc_with})

def add_secondary_linked_file ( self , file_path , relpath = None , mimetype = None , time_origin = None , assoc_with = None ) : if mimetype is None : mimetype = self . MIMES [ file_path . split ( '.' ) [ - 1 ] ] self . linked_file_descriptors . append ( { 'LINK_URL' : file_path , 'RELATIVE_LINK_URL' : relpath , 'MIME_TYPE' : mimetype , 'TIME_ORIGIN' : time_origin , 'ASSOCIATED_WITH' : assoc_with } )