Spaces:
Running
Running
Download models/ocr/modules/sequence_modeling.py from wushidiguo2/HelloLottery: direct link, hf CLI and curl.
- Browser
- Download file 796 Bytes
-
https://huggingface.co/spaces/wushidiguo2/HelloLottery/resolve/main/models/ocr/modules/sequence_modeling.py
- Command line
-
hf download hf://spaces/wushidiguo2/HelloLottery/models/ocr/modules/sequence_modeling.py
-
curl -L -o sequence_modeling.py https://huggingface.co/spaces/wushidiguo2/HelloLottery/resolve/main/models/ocr/modules/sequence_modeling.py
796 Bytes
| import torch.nn as nn | |
| class BidirectionalLSTM(nn.Module): | |
| def __init__(self, input_size, hidden_size, output_size): | |
| super(BidirectionalLSTM, self).__init__() | |
| self.rnn = nn.LSTM(input_size, hidden_size, bidirectional=True, batch_first=True) | |
| self.linear = nn.Linear(hidden_size * 2, output_size) | |
| def forward(self, input): | |
| """ | |
| input : visual feature [batch_size x T x input_size] | |
| output : contextual feature [batch_size x T x output_size] | |
| """ | |
| try: | |
| self.rnn.flatten_parameters() | |
| except: | |
| pass | |
| recurrent, _ = self.rnn(input) # batch_size x T x input_size -> batch_size x T x (2*hidden_size) | |
| output = self.linear(recurrent) # batch_size x T x output_size | |
| return output | |