Skip to content

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

Description

@JmacCl

Summary

There is a shape mismatch issue in dot_likelihood() within infer_actively_pymdp/pymdp.maths.py, where np.squeeze(X) is removing dimensions incorrectly. Given an example case of

X = (1, 25, 1)

the result calculation will return (25,) instead of the expected (25,1), causing shape inconsistencies in downstream calculations.

recreation

import numpy as np
from pymdp.maths import dot_likelihood

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

LL = dot_likelihood(A, obs)

suggested fix

given the code

def dot_likelihood(A,obs):

s = np.ones(np.ndim(A), dtype = int)
s[0] = obs.shape[0]
X = A * obs.reshape(tuple(s))
X = np.sum(X, axis=0, keepdims=True)
LL = np.squeeze(X)

# check to see if `LL` is a scalar
if np.prod(LL.shape) <= 1.0:
    LL = LL.item()
    LL = np.array([LL]).astype("float64")

return LL

the np.squeeze(X) into LL = np.squeeze(X, axis=0)

Additional context

This issue was encountered while running get_joint_likelihood() inside fpi.py.
It caused unexpected shape mismatches in calc_free_energy(), breaking Active Inference computations.

This bug is critical for maintaining shape consistency in pymdp’s probability calculations.
The fix ensures the function remains backward-compatible and prevents unexpected errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions