Skip to content

fix: squeeze only axis 0 in dot_likelihood to prevent shape mismatch#405

Open
lingxiu58 wants to merge 1 commit into
infer-actively:mainfrom
lingxiu58:fix/dot-likelihood-shape
Open

fix: squeeze only axis 0 in dot_likelihood to prevent shape mismatch#405
lingxiu58 wants to merge 1 commit into
infer-actively:mainfrom
lingxiu58:fix/dot-likelihood-shape

Conversation

@lingxiu58
Copy link
Copy Markdown
Contributor

Problem

np.squeeze(X) in dot_likelihood() (pymdp/legacy/maths.py:245) removes all size-1 dimensions. When A has shape (1, N, 1) (e.g., single-modality with trailing dimension), this collapses both axes, returning (N,) instead of the expected (N, 1). This causes shape mismatches in downstream calc_free_energy() calls within fpi.py.

Fix

# Before
LL = np.squeeze(X)

# After
LL = np.squeeze(X, axis=0)

Restricts squeeze to axis 0 only, preserving the trailing dimension and maintaining shape consistency.

Reproduction

import numpy as np
from pymdp.legacy.maths import dot_likelihood

A = np.expand_dims(np.eye(25), axis=-1)  # shape (25, 25, 1)
obs = np.eye(1, 25).flatten()

LL = dot_likelihood(A, obs)
# Before: LL.shape == (25,)   ← wrong
# After:  LL.shape == (25, 1) ← correct

Fixes #171

np.squeeze(X) removes all size-1 dimensions, which incorrectly collapses
the trailing dimension when A has shape (1, N, 1). Using
np.squeeze(X, axis=0) preserves the intended (N, 1) output shape.

Fixes infer-actively#171
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Shape miscalculation in np.squeeze(X) in dot_likelihood() (pymdp.maths.py)

1 participant