(
batch_size,
seq_len,
input_size,
hidden_size,
num_layers,
bidirectional,
init_hidden,
batch_first,
)
| 127 | ], |
| 128 | ) |
| 129 | def test_lstm( |
| 130 | batch_size, |
| 131 | seq_len, |
| 132 | input_size, |
| 133 | hidden_size, |
| 134 | num_layers, |
| 135 | bidirectional, |
| 136 | init_hidden, |
| 137 | batch_first, |
| 138 | ): |
| 139 | rnn = LSTM( |
| 140 | input_size, |
| 141 | hidden_size, |
| 142 | batch_first=batch_first, |
| 143 | num_layers=num_layers, |
| 144 | bidirectional=bidirectional, |
| 145 | ) |
| 146 | if batch_first: |
| 147 | x_shape = (batch_size, seq_len, input_size) |
| 148 | else: |
| 149 | x_shape = (seq_len, batch_size, input_size) |
| 150 | x = mge.random.normal(size=x_shape) |
| 151 | total_hidden_size = num_layers * (2 if bidirectional else 1) * hidden_size |
| 152 | if init_hidden: |
| 153 | h = mge.random.normal(size=(batch_size, total_hidden_size)) |
| 154 | h = (h, h) |
| 155 | else: |
| 156 | h = None |
| 157 | output, h_n = rnn(x, h) |
| 158 | num_directions = 2 if bidirectional else 1 |
| 159 | if batch_first: |
| 160 | assert_tuple_equal( |
| 161 | output.shape, (batch_size, seq_len, num_directions * hidden_size) |
| 162 | ) |
| 163 | else: |
| 164 | assert_tuple_equal( |
| 165 | output.shape, (seq_len, batch_size, num_directions * hidden_size) |
| 166 | ) |
| 167 | assert_tuple_equal( |
| 168 | h_n[0].shape, (num_directions * num_layers, batch_size, hidden_size) |
| 169 | ) |
| 170 | assert_tuple_equal( |
| 171 | h_n[1].shape, (num_directions * num_layers, batch_size, hidden_size) |
| 172 | ) |
nothing calls this directly
no test coverage detected