03b_model/src/LSTMDA_torch.py
def forward(self, x, init_states = None):
        """Assumes x is of shape (batch, sequence, feature)"""
        bs, seq_sz, _ = x.size()
        hidden_seq = []
        if init_states is None:
            h_t, c_t = (torch.zeros(bs, self.hidden_size).to(x.device), 
                        torch.zeros(bs, self.hidden_size).to(x.device))
        else:
            h_t, c_t = init_states
        
        x = self.dropout(x)
        HS = self.hidden_size
        for t in range(seq_sz):
            x_t = x[:, t, :]
            # batch the computations into a single matrix multiplication
            gates = x_t @ self.weight_ih + h_t @ self.weight_hh + self.bias
            i_t, f_t, g_t, o_t = (
                torch.sigmoid(gates[:, :HS]), # input
                torch.sigmoid(gates[:, HS:HS*2]), # forget
                torch.tanh(gates[:, HS*2:HS*3]),
                torch.sigmoid(gates[:, HS*3:]), # output
            )
            c_t = f_t * c_t + i_t * self.recur_dropout(g_t)
            h_t = o_t * torch.tanh(c_t)
            hidden_seq.append(h_t.unsqueeze(1))
        hidden_seq = torch.cat(hidden_seq, dim= 1)
        out = self.dense(hidden_seq)
        return out, (h_t, c_t)
Assumes x is of shape (batch, sequence, feature)
Assumes x is of shape (batch, sequence, feature)
Assumes x is of shape ( batch sequence feature )
def forward(self, x, init_states = None):
        bs, seq_sz, _ = x.size()
        hidden_seq = []
        if init_states is None:
            h_t, c_t = (torch.zeros(bs, self.hidden_size).to(x.device), 
                        torch.zeros(bs, self.hidden_size).to(x.device))
        else:
            h_t, c_t = init_states
        x = self.dropout(x)
        HS = self.hidden_size
        for t in range(seq_sz):
            x_t = x[:, t, :]
            gates = x_t @ self.weight_ih + h_t @ self.weight_hh + self.bias
            i_t, f_t, g_t, o_t = (
                torch.sigmoid(gates[:, :HS]), 
                torch.sigmoid(gates[:, HS:HS*2]), 
                torch.tanh(gates[:, HS*2:HS*3]),
                torch.sigmoid(gates[:, HS*3:]), 
            )
            c_t = f_t * c_t + i_t * self.recur_dropout(g_t)
            h_t = o_t * torch.tanh(c_t)
            hidden_seq.append(h_t.unsqueeze(1))
        hidden_seq = torch.cat(hidden_seq, dim= 1)
        out = self.dense(hidden_seq)
        return out, (h_t, c_t)
def forward ( self , x , init_states = None ) : bs , seq_sz , _ = x . size ( ) hidden_seq = [ ] if init_states is None : h_t , c_t = ( torch . zeros ( bs , self . hidden_size ) . to ( x . device ) , torch . zeros ( bs , self . hidden_size ) . to ( x . device ) ) else : h_t , c_t = init_states x = self . dropout ( x ) HS = self . hidden_size for t in range ( seq_sz ) : x_t = x [ : , t , : ] gates = x_t @ self . weight_ih + h_t @ self . weight_hh + self . bias i_t , f_t , g_t , o_t = ( torch . sigmoid ( gates [ : , : HS ] ) , torch . sigmoid ( gates [ : , HS : HS * 2 ] ) , torch . tanh ( gates [ : , HS * 2 : HS * 3 ] ) , torch . sigmoid ( gates [ : , HS * 3 : ] ) , ) c_t = f_t * c_t + i_t * self . recur_dropout ( g_t ) h_t = o_t * torch . tanh ( c_t ) hidden_seq . append ( h_t . unsqueeze ( 1 ) ) hidden_seq = torch . cat ( hidden_seq , dim = 1 ) out = self . dense ( hidden_seq ) return out , ( h_t , c_t )
Assumes x is of shape (batch, sequence, feature)
Assumes x is of shape ( batch sequence feature )
"""Assumes x is of shape (batch, sequence, feature)""" # batch the computations into a single matrix multiplication # input # forget # output
self
x
init_states