tensorflow / tensorflow/probability

tfp.JointDistributionSequential._build() -> line 236, in _build if not isinstance(model, collections.Sequence): AttributeError: module 'collections' has no attribute 'Sequence'

Open
#1,656 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
4.4k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Code taken from:

https://github.com/tensorflow/probability/blob/main/tensorflow_probability/examples/jupyter_notebooks/Modeling_with_JointDistribution.ipynb

Code to reproduce:

dfhogg = pd.DataFrame(np.array([[1,201,592,61,9,-0.84],
[2,244,401,25,4,0.31],
[3,47,583,38,11,0.64],
[4,287,402,15,7,-0.27],
[5,203,495,21,5,-0.33],
[6,58,173,15,9,0.67],
[7,210,479,27,4,-0.02],
[8,202,504,14,4,-0.05],
[9,198,510,30,11,-0.84],
[10,158,416,16,7,-0.69],
[11,165,393,14,5,0.30],
[12,201,442,25,5,-0.46],
[13,157,317,52,5,-0.03],
[14,131,311,16,6,0.50],
[15,166,400,34,6,0.73],
[16,160,337,31,5,-0.52],
[17,186,423,42,9,0.90],
[18,125,334,26,8,0.40],
[19,218,533,16,6,-0.78],
[20,146,344,22,5,-0.56]]),
columns=['id','x','y','sigma_y','sigma_x','rho_xy'])

for convenience zero-base the 'id' and use as index

dfhogg['id'] = dfhogg['id'] - 1
dfhogg.set_index('id',inplace=True)

standardize (mean center and divide by 1 sd)

dfhoggs = (dfhogg[['x','y']] - dfhogg[['x','y']].mean(0)) / dfhogg[['x','y']].std(0)
dfhoggs['sigma_y'] = dfhogg['sigma_y'] / dfhogg['y'].std(0)
dfhoggs['sigma_x'] = dfhogg['sigma_x'] / dfhogg['x'].std(0)

X_np = dfhogg['x'].values
sigma_y_np = dfhogg['y'].values
Y_np = dfhogg['y'].values

mdl_ols = tfd.JointDistributionSequential([
# b0 ~ Normal(0, 1)
tfd.Normal(loc=tf.cast(0, dtype), scale=1.),
# b1 ~ Normal(0, 1)
tfd.Normal(loc=tf.cast(0, dtype), scale=1.),
# x ~ Normal(b0+b1X, 1)
lambda b1, b0: tfd.Normal(
# Parameter transformation
loc=b0 + b1
X_np,
scale=sigma_y_np)
])

output:

line 236, in _build
if not isinstance(model, collections.Sequence):
AttributeError: module 'collections' has no attribute 'Sequence'

##################################################################################################################################################

What I tried before creating issue:

import collections.abc as collections

According to python doc:

isinstance(object, classinfo),
Return True if the object argument is an instance of the classinfo argument

However, the classinfo argument in Pycharm is
{ABCMeta} <class 'collections.abc.Sequence'>

and tfp code is

"if not isinstance(model, collections.Sequence)"

My guess is tfp code should be:

"if not isinstance(model, collections.abc.Sequence)"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with JointDistributionSequential._build at the reported line and reproduce the example from the Modeling_with_JointDistribution notebook under the affected Python version. Check the sequence type check and confirm that the example runs without the AttributeError when done.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, tensorflow
Domain
machine-learning
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.