Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary calls to PyTensor eval() from user-facing methods #386

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions pymc_marketing/mmm/delayed_saturated_mmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pandas as pd
import pymc as pm
import seaborn as sns
from pytensor.tensor.variable import TensorVariable
from xarray import DataArray

from pymc_marketing.mmm.base import MMM
Expand Down Expand Up @@ -344,9 +345,9 @@ def _get_fourier_models_data(self, X) -> pd.DataFrame:
n_order=self.yearly_seasonality,
)

def channel_contributions_forward_pass(
def channel_contributions_forward_pass_untransformed(
self, channel_data: npt.NDArray[np.float_]
) -> npt.NDArray[np.float_]:
) -> TensorVariable:
"""Evaluate the channel contribution for a given channel data and a fitted model, ie. the forward pass.
Parameters
----------
Expand Down Expand Up @@ -383,7 +384,7 @@ def channel_contributions_forward_pass(
channel_contribution_forward_pass = (
beta_channel_posterior_expanded * logistic_saturation_posterior
)
return channel_contribution_forward_pass.eval()
return channel_contribution_forward_pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type signature has to be updated. However the subclass shouldn't change the meaning of the method (by returning a numpy value).

Copy link
Contributor

@ricardoV94 ricardoV94 Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the meantime, should we call the base-class method "channel_contribution_forward_pass_untransformed", so that the sub-classes can call it without being ambiguous about the meaning?

This is only needed for as long as we can't return the "transformed" versions as PyTensor graphs.


@property
def _serializable_model_config(self) -> Dict[str, Any]:
Expand Down Expand Up @@ -556,15 +557,17 @@ def channel_contributions_forward_pass(
array-like
Transformed channel data.
"""
channel_contribution_forward_pass = super().channel_contributions_forward_pass(
channel_data=channel_data
channel_contribution_forward_pass = (
super().channel_contributions_forward_pass_untransformed(
channel_data=channel_data
)
)
target_transformed_vectorized = np.vectorize(
self.target_transformer.inverse_transform,
excluded=[1, 2],
signature="(m, n) -> (m, n)",
)
return target_transformed_vectorized(channel_contribution_forward_pass)
return target_transformed_vectorized(channel_contribution_forward_pass.eval())

def get_channel_contributions_forward_pass_grid(
self, start: float, stop: float, num: int
Expand Down Expand Up @@ -596,7 +599,7 @@ def get_channel_contributions_forward_pass_grid(
channel_contribution_forward_pass = self.channel_contributions_forward_pass(
channel_data=channel_data
)
channel_contributions.append(channel_contribution_forward_pass)
channel_contributions.append(channel_contribution_forward_pass.eval())
return DataArray(
data=np.array(channel_contributions),
dims=("delta", "chain", "draw", "date", "channel"),
Expand Down
Loading