def test_attention_explanation(self):
        """Test attention (feature importance) explanation results and plotting."""
        # past attention (full_attention=False) on attends to values in the past relative to each horizon
        # (look at the last 0 values in the array)
        att_exp_past_att = np.array([
            [1.0, 0.8],
            [0.8, 0.7],
            [0.6, 0.4],
            [0.7, 0.3],
            [0.9, 0.4],
            [0.0, 1.3],
            [0.0, 0.0],
        ])
        # full attention (full_attention=True) attends to all values in past, present, and future
        # see the that all values are non-0
        att_exp_full_att = np.array([
            [0.8, 0.8],
            [0.7, 0.6],
            [0.4, 0.4],
            [0.3, 0.3],
            [0.3, 0.3],
            [0.7, 0.8],
            [0.8, 0.8],
        ])
        for full_attention, att_exp in zip(
            [False, True], [att_exp_past_att, att_exp_full_att]
        ):
            model = self.helper_create_model(
                use_encoders=True,
                add_relative_idx=True,
                full_attention=full_attention,
            )
            series, pc, fc = self.helper_get_input(series_option="multivariate")
            model.fit(series, past_covariates=pc, future_covariates=fc)
            explainer = TFTExplainer(model)
            results = explainer.explain()

            att = results.get_attention()
            # relaxed comparison because M1 chip gives slightly different results than intel chip
            assert np.all(np.abs(np.round(att.values(), decimals=1) - att_exp) <= 0.2)
            assert att.columns.tolist() == ["horizon 1", "horizon 2"]
            with patch("matplotlib.pyplot.show") as _:
                _ = explainer.plot_attention(
                    results, plot_type="all", show_index_as="relative"
                )
                plt.close()
            with patch("matplotlib.pyplot.show") as _:
                _ = explainer.plot_attention(
                    results, plot_type="all", show_index_as="time"
                )
                plt.close()
            with patch("matplotlib.pyplot.show") as _:
                _ = explainer.plot_attention(
                    results, plot_type="time", show_index_as="relative"
                )
                plt.close()
            with patch("matplotlib.pyplot.show") as _:
                _ = explainer.plot_attention(
                    results, plot_type="time", show_index_as="time"
                )
                plt.close()
            with patch("matplotlib.pyplot.show") as _:
                _ = explainer.plot_attention(
                    results, plot_type="heatmap", show_index_as="relative"
                )
                plt.close()
            with patch("matplotlib.pyplot.show") as _:
                _ = explainer.plot_attention(
                    results, plot_type="heatmap", show_index_as="time"
                )
                plt.close()
Test attention (feature importance) explanation results and plotting.
darts/tests/explainability/test_tft_explainer.py