Exemplo n.º 1
0
 def apply(self, X, states, cells, mask=None):
     """Apply LSTM activation
     
     Parameters
     ----------
     X : 2D tensor
         input samples in (batch_size, input_dim)
     states : 2D tensor 
         features in (batch_size, feature_dim)
     cells : 2D tensor
         cell states in (batch_size, cell_dim)
         
     Returns
     -------
     states : 2D tensor
         next state of network
     cells : 2D tensor
         next cell activation
     """
     
     def slice_st(x, no):
         return x[:, no*self.dim:(no+1)*self.dim];
     
     activation=T.dot(states, self.W_state)+self.in_layer.apply(X);
     in_gate=sigmoid(slice_st(activation, 0)+cells*self.W_cell_to_in);
     forget_gate=sigmoid(slice_st(activation, 1)+cells*self.W_cell_to_forget);
     next_cells=(forget_gate*cells+in_gate*self.act_fun(slice_st(activation, 2)));
     out_gate=sigmoid(slice_st(activation,3)+next_cells*self.W_cell_to_out)
     next_states=out_gate*self.act_fun(next_cells);
     
     if mask:
         next_states=(mask[:, None]*next_states+(1-mask[:,None]*states));
         next_cells=(mask[:, None]*next_cells+(1-mask[:,None]*cells));
         
     return next_states, next_cells;
Exemplo n.º 2
0
    def apply(self, X, states, cells, mask=None):
        """Apply LSTM activation
        
        Parameters
        ----------
        X : 2D tensor
            input samples in (batch_size, input_dim)
        states : 2D tensor 
            features in (batch_size, feature_dim)
        cells : 2D tensor
            cell states in (batch_size, cell_dim)
            
        Returns
        -------
        states : 2D tensor
            next state of network
        cells : 2D tensor
            next cell activation
        """
        def slice_st(x, no):
            return x[:, no * self.dim:(no + 1) * self.dim]

        activation = T.dot(states, self.W_state) + self.in_layer.apply(X)
        in_gate = sigmoid(slice_st(activation, 0) + cells * self.W_cell_to_in)
        forget_gate = sigmoid(
            slice_st(activation, 1) + cells * self.W_cell_to_forget)
        next_cells = (forget_gate * cells +
                      in_gate * self.act_fun(slice_st(activation, 2)))
        out_gate = sigmoid(
            slice_st(activation, 3) + next_cells * self.W_cell_to_out)
        next_states = out_gate * self.act_fun(next_cells)

        if mask:
            next_states = (mask[:, None] * next_states +
                           (1 - mask[:, None] * states))
            next_cells = (mask[:, None] * next_cells +
                          (1 - mask[:, None] * cells))

        return next_states, next_cells
Exemplo n.º 3
0
 def apply(self, X):
     return nnfuns.sigmoid(self.apply_lin(X));
Exemplo n.º 4
0
 def apply(self, X):
     h, t=self.apply_lin(X);
     h=nnfuns.sigmoid(h);
     t=nnfuns.sigmoid(t);
     
     return h*t+X*(1-t);
Exemplo n.º 5
0
    def apply(self, X):
        h, t = self.apply_lin(X)
        h = nnfuns.sigmoid(h)
        t = nnfuns.sigmoid(t)

        return h * t + X * (1 - t)