@@ -137,3 +137,90 @@ def test_transducer_loss():
137137 )
138138 out_cost .backward ()
139139 assert out_cost .item () == 2.247833251953125
140+
141+
142+ def test_guided_attention_loss_mask ():
143+ from speechbrain .nnet .loss .guidedattn_loss import GuidedAttentionLoss
144+
145+ loss = GuidedAttentionLoss ()
146+ input_lengths = torch .tensor ([3 , 2 , 6 ])
147+ output_lengths = torch .tensor ([4 , 3 , 5 ])
148+ soft_mask = loss .guided_attentions (input_lengths , output_lengths )
149+ ref_soft_mask = torch .tensor (
150+ [
151+ [
152+ [0.0 , 0.54216665 , 0.9560631 , 0.9991162 , 0.0 ],
153+ [0.7506478 , 0.08314464 , 0.2933517 , 0.8858382 , 0.0 ],
154+ [0.9961341 , 0.8858382 , 0.2933517 , 0.08314464 , 0.0 ],
155+ [0.0 , 0.0 , 0.0 , 0.0 , 0.0 ],
156+ [0.0 , 0.0 , 0.0 , 0.0 , 0.0 ],
157+ [0.0 , 0.0 , 0.0 , 0.0 , 0.0 ],
158+ ],
159+ [
160+ [0.0 , 0.7506478 , 0.9961341 , 0.0 , 0.0 ],
161+ [0.9560631 , 0.2933517 , 0.2933517 , 0.0 , 0.0 ],
162+ [0.0 , 0.0 , 0.0 , 0.0 , 0.0 ],
163+ [0.0 , 0.0 , 0.0 , 0.0 , 0.0 ],
164+ [0.0 , 0.0 , 0.0 , 0.0 , 0.0 ],
165+ [0.0 , 0.0 , 0.0 , 0.0 , 0.0 ],
166+ ],
167+ [
168+ [0.0 , 0.39346933 , 0.86466473 , 0.988891 , 0.99966455 ],
169+ [0.2933517 , 0.01379288 , 0.49366438 , 0.90436554 , 0.993355 ],
170+ [0.7506478 , 0.1992626 , 0.05404053 , 0.5888877 , 0.93427145 ],
171+ [0.9560631 , 0.6753475 , 0.1175031 , 0.1175031 , 0.6753475 ],
172+ [0.9961341 , 0.93427145 , 0.5888877 , 0.05404053 , 0.1992626 ],
173+ [0.9998301 , 0.993355 , 0.90436554 , 0.49366438 , 0.01379288 ],
174+ ],
175+ ]
176+ )
177+ assert torch .allclose (soft_mask , ref_soft_mask )
178+
179+
180+ def test_guided_attention_loss_value ():
181+ from speechbrain .nnet .loss .guidedattn_loss import GuidedAttentionLoss
182+
183+ loss = GuidedAttentionLoss ()
184+ input_lengths = torch .tensor ([2 , 3 ])
185+ target_lengths = torch .tensor ([3 , 4 ])
186+ alignments = torch .tensor (
187+ [
188+ [
189+ [0.8 , 0.2 , 0.0 ],
190+ [0.4 , 0.6 , 0.0 ],
191+ [0.2 , 0.8 , 0.0 ],
192+ [0.0 , 0.0 , 0.0 ],
193+ ],
194+ [
195+ [0.6 , 0.2 , 0.2 ],
196+ [0.1 , 0.7 , 0.2 ],
197+ [0.3 , 0.4 , 0.3 ],
198+ [0.2 , 0.3 , 0.5 ],
199+ ],
200+ ]
201+ )
202+ loss_value = loss (alignments , input_lengths , target_lengths )
203+ ref_loss_value = torch .tensor (0.1142 )
204+ assert torch .isclose (loss_value , ref_loss_value , 0.0001 , 0.0001 ).item ()
205+
206+
207+ def test_guided_attention_loss_shapes ():
208+ from speechbrain .nnet .loss .guidedattn_loss import GuidedAttentionLoss
209+
210+ loss = GuidedAttentionLoss ()
211+ input_lengths = torch .tensor ([3 , 2 , 6 ])
212+ output_lengths = torch .tensor ([4 , 3 , 5 ])
213+ soft_mask = loss .guided_attentions (input_lengths , output_lengths )
214+ assert soft_mask .shape == (3 , 6 , 5 )
215+ soft_mask = loss .guided_attentions (
216+ input_lengths , output_lengths , max_input_len = 10
217+ )
218+ assert soft_mask .shape == (3 , 10 , 5 )
219+ soft_mask = loss .guided_attentions (
220+ input_lengths , output_lengths , max_target_len = 12
221+ )
222+ assert soft_mask .shape == (3 , 6 , 12 )
223+ soft_mask = loss .guided_attentions (
224+ input_lengths , output_lengths , max_input_len = 10 , max_target_len = 12
225+ )
226+ assert soft_mask .shape == (3 , 10 , 12 )
0 commit comments