pytorch/test/onnx/model_defs/lstm_flattening_result.py
Justin Chu 161e931156 [ONNX] Modernize python syntax (#77935)
Use pyupgrade(https://github.com/asottile/pyupgrade) and flynt to modernize python syntax

```sh
pyupgrade --py36-plus --keep-runtime-typing torch/onnx/**/*.py
pyupgrade --py36-plus --keep-runtime-typing test/onnx/**/*.py
flynt torch/onnx/ --line-length 120
```

- Use f-strings for string formatting
- Use the new `super()` syntax for class initialization
- Use dictionary / set comprehension
Pull Request resolved: https://github.com/pytorch/pytorch/pull/77935
Approved by: https://github.com/BowenBao
2022-05-24 22:52:37 +00:00

46 lines
1.5 KiB
Python

from torch import nn
from torch.nn.utils.rnn import PackedSequence
class LstmFlatteningResult(nn.LSTM):
def forward(self, input, *fargs, **fkwargs):
output, (hidden, cell) = nn.LSTM.forward(self, input, *fargs, **fkwargs)
return output, hidden, cell
class LstmFlatteningResultWithSeqLength(nn.Module):
def __init__(self, input_size, hidden_size, layers, bidirect, dropout, batch_first):
super().__init__()
self.batch_first = batch_first
self.inner_model = nn.LSTM(
input_size=input_size,
hidden_size=hidden_size,
num_layers=layers,
bidirectional=bidirect,
dropout=dropout,
batch_first=batch_first,
)
def forward(self, input: PackedSequence, hx=None):
output, (hidden, cell) = self.inner_model.forward(input, hx)
return output, hidden, cell
class LstmFlatteningResultWithoutSeqLength(nn.Module):
def __init__(self, input_size, hidden_size, layers, bidirect, dropout, batch_first):
super().__init__()
self.batch_first = batch_first
self.inner_model = nn.LSTM(
input_size=input_size,
hidden_size=hidden_size,
num_layers=layers,
bidirectional=bidirect,
dropout=dropout,
batch_first=batch_first,
)
def forward(self, input, hx=None):
output, (hidden, cell) = self.inner_model.forward(input, hx)
return output, hidden, cell