neural_filter.py 4.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
"""
NeuralFilterCell
**************

This module implements a basic trainable all-pole first order filter using pyTorch


Copyright (c) 2018 Idiap Research Institute, http://www.idiap.ch/

Written by Francois Marelli <Francois.Marelli@idiap.ch>

This file is part of neural_filters.

neural_filters is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 as
published by the Free Software Foundation.

neural_filters is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with neural_filters. If not, see <http://www.gnu.org/licenses/>.

"""

import torch
from torch.nn import Parameter
from torch.nn import functional as F
31
import numpy as np
32

Francois Marelli's avatar
Francois Marelli committed
33
from . import INIT_MODULUS, asig
34

Francois Marelli's avatar
Francois Marelli committed
35
class NeuralFilter(torch.nn.Module):
36 37 38 39 40 41 42
    """
    A trainable first-order all-pole filter :math:`\\frac{1}{1 - P z^{-1}}`

    * **hidden_size** (int) - the size of the data vector
    """

    def __init__(self, hidden_size):
Francois Marelli's avatar
Francois Marelli committed
43
        super(NeuralFilter, self).__init__()
44 45 46 47 48 49 50

        self.hidden_size = hidden_size

        self.bias_forget = Parameter(torch.Tensor(hidden_size))

        self.reset_parameters()

Francois Marelli's avatar
Francois Marelli committed
51
    def reset_parameters(self, init=None, min_modulus=0, max_modulus=1):
Francois Marelli's avatar
Francois Marelli committed
52
        if init is None:
Francois Marelli's avatar
Francois Marelli committed
53 54
            parts = self.hidden_size * 2
            ranges = np.arange(1, parts, 2)
55

Francois Marelli's avatar
Francois Marelli committed
56 57 58 59 60 61 62 63
            init_modulus = ranges * (max_modulus - min_modulus) / parts + min_modulus
            init = asig(init_modulus)

        if not isinstance(init, np.ndarray):
            init = np.array(init, ndmin=1)

        ten_init = torch.from_numpy(init)
        self.bias_forget.data.copy_(ten_init)
64 65

    def __repr__(self):
66
        s = '{name}({hidden_size})'
67 68
        return s.format(name=self.__class__.__name__, **self.__dict__)

69 70
    def check_forward_input(self, input_state):
        if input_state.size(-1) != self.hidden_size:
71 72
            raise RuntimeError(
                "input has inconsistent input_size(-1): got {}, expected {}".format(
73
                    input_state.size(1), self.hidden_size))
74

75 76
    def check_forward_hidden(self, input_state, hx):
        if input_state.size(1) != hx.size(0):
77 78
            raise RuntimeError(
                "Input batch size {} doesn't match hidden batch size {}".format(
79
                    input_state.size(1), hx.size(0)))
80 81 82 83

        if hx.size(1) != self.hidden_size:
            raise RuntimeError(
                "hidden has inconsistent hidden_size: got {}, expected {}".format(
84
                    hx.size(1), self.hidden_size))
85

86 87 88
    def step(self, input_state, hidden, a=None):
        if a is None:
            a = F.sigmoid(self.bias_forget)
89

90 91 92 93
        next_state = (a * hidden) + input_state
        return next_state

    def forward(self, input_state, hx=None):
94
        if hx is None:
95 96 97
            hx = torch.autograd.Variable(input_state.data.new(input_state.size(1),
                                                              self.hidden_size
                                                              ).zero_(), requires_grad=False)
98

99 100
        self.check_forward_input(input_state)
        self.check_forward_hidden(input_state, hx)
101 102 103

        hidden = hx

104 105 106
        # compute this once for all steps for efficiency
        a = F.sigmoid(self.bias_forget)

107
        output = []
108
        steps = range(input_state.size(0))
109
        for i in steps:
110
            hidden = self.step(input_state[i], hidden, a=a)
111 112
            output.append(hidden)

113
        output = torch.cat(output, 0).view(input_state.size(0), *output[0].size())
114 115

        return output, hidden
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

    @property
    def gradients(self):
        grad = self.bias_forget.grad
        if grad is not None:
            gradient = grad.data.numpy()
            return gradient.reshape((gradient.size, 1))
        else:
            return np.zeros((self.hidden_size, 1))

    @property
    def denominator(self):
        forgetgate = F.sigmoid(self.bias_forget).data.numpy()
        forgetgate = forgetgate.reshape((forgetgate.size, 1))
        one = np.ones(forgetgate.shape)
        denom = np.concatenate((one, -forgetgate), axis=1)
        return denom