probability icon indicating copy to clipboard operation
probability copied to clipboard

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

Open RTSRLLC opened this issue 3 years ago • 3 comments

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 + b1X_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)"

RTSRLLC avatar Nov 28 '22 14:11 RTSRLLC

collar gist:

https://colab.research.google.com/gist/mohantym/351f3892124ef28f368e65e71f8bf670/git_58708.ipynb

RTSRLLC avatar Nov 28 '22 14:11 RTSRLLC

I'm seeing this error in v 0.14.0. As OP says, it should be collections.abc.Sequence

See https://github.com/modin-project/modin/issues/5168

ricopicone avatar Jul 31 '23 05:07 ricopicone

This appears to be fixed in the latest: https://github.com/tensorflow/probability/blob/c9d9dd4e6e5520a75ca8f299420589e9a22485a2/tensorflow_probability/python/layers/distribution_layer.py#L167

ricopicone avatar Jul 31 '23 05:07 ricopicone