BayesianModelingandComputationInPython / BookCode_Edition1

https://www.bayesiancomputationbook.com
GNU General Public License v2.0
500 stars 129 forks source link

Type Error on import of Tensorflow Code Block 3.7 Chapter 03 #212

Open RogerS49 opened 1 year ago

RogerS49 commented 1 year ago

Chapter 03 Notebook Code 3.7 first cell cause type error on import of Tensorflow as tf

import tensorflow as tf import tensorflow_probability as tfp

tfd = tfp.distributions root = tfd.JointDistributionCoroutine.Root

species_idx = tf.constant(all_species.codes, tf.int32) body_mass_g = tf.constant(penguins["body_mass_g"], tf.float32)

@tfd.JointDistributionCoroutine def jd_penguin_mass_all_species(): σ = yield root(tfd.Sample( tfd.HalfStudentT(df=100, loc=0, scale=2000), sample_shape=3, name="sigma")) μ = yield root(tfd.Sample( tfd.Normal(loc=4000, scale=3000), sample_shape=3, name="mu")) mass = yield tfd.Independent( tfd.Normal(loc=tf.gather(μ, species_idx, axis=-1), scale=tf.gather(σ, species_idx, axis=-1)), reinterpreted_batch_ndims=1, name="mass")


TypeError Traceback (most recent call last) /tmp/ipykernel_21511/4136355668.py in ----> 1 import tensorflow as tf 2 import tensorflow_probability as tfp 3 4 tfd = tfp.distributions 5 root = tfd.JointDistributionCoroutine.Root

~/mambaforge/envs/bmcp/lib/python3.9/site-packages/tensorflow/init.py in 39 import sys as _sys 40 ---> 41 from tensorflow.python.tools import module_util as _module_util 42 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader 43

~/mambaforge/envs/bmcp/lib/python3.9/site-packages/tensorflow/python/init.py in 39 40 from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow ---> 41 from tensorflow.python.eager import context 42 43 # pylint: enable=wildcard-import

~/mambaforge/envs/bmcp/lib/python3.9/site-packages/tensorflow/python/eager/context.py in 31 import six 32 ---> 33 from tensorflow.core.framework import function_pb2 34 from tensorflow.core.protobuf import config_pb2 35 from tensorflow.core.protobuf import rewriter_config_pb2

~/mambaforge/envs/bmcp/lib/python3.9/site-packages/tensorflow/core/framework/function_pb2.py in 14 15 ---> 16 from tensorflow.core.framework import attr_value_pb2 as tensorflow_dot_core_dot_framework_dot_attrvaluepb2 17 from tensorflow.core.framework import node_def_pb2 as tensorflow_dot_core_dot_framework_dot_nodedefpb2 18 from tensorflow.core.framework import op_def_pb2 as tensorflow_dot_core_dot_framework_dot_opdefpb2

~/mambaforge/envs/bmcp/lib/python3.9/site-packages/tensorflow/core/framework/attr_value_pb2.py in 14 15 ---> 16 from tensorflow.core.framework import tensor_pb2 as tensorflow_dot_core_dot_framework_dot_tensorpb2 17 from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensorshape__pb2 18 from tensorflow.core.framework import types_pb2 as tensorflow_dot_core_dot_framework_dot_types__pb2

~/mambaforge/envs/bmcp/lib/python3.9/site-packages/tensorflow/core/framework/tensor_pb2.py in 14 15 ---> 16 from tensorflow.core.framework import resource_handle_pb2 as tensorflow_dot_core_dot_framework_dot_resourcehandlepb2 17 from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensorshapepb2 18 from tensorflow.core.framework import types_pb2 as tensorflow_dot_core_dot_framework_dot_types__pb2

~/mambaforge/envs/bmcp/lib/python3.9/site-packages/tensorflow/core/framework/resource_handle_pb2.py in 14 15 ---> 16 from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensorshapepb2 17 from tensorflow.core.framework import types_pb2 as tensorflow_dot_core_dot_framework_dot_types__pb2 18

~/mambaforge/envs/bmcp/lib/python3.9/site-packages/tensorflow/core/framework/tensor_shape_pb2.py in 34 containing_type=None, 35 fields=[ ---> 36 _descriptor.FieldDescriptor( 37 name='size', full_name='tensorflow.TensorShapeProto.Dim.size', index=0, 38 number=1, type=3, cpp_type=2, label=1,

~/mambaforge/envs/bmcp/lib/python3.9/site-packages/google/protobuf/descriptor.py in new(cls, name, full_name, index, number, type, cpp_type, label, default_value, message_type, enum_type, containing_type, is_extension, extension_scope, options, serialized_options, has_default_value, containing_oneof, json_name, file, create_key) 559 has_default_value=True, containing_oneof=None, json_name=None, 560 file=None, create_key=None): # pylint: disable=redefined-builtin --> 561 _message.Message._CheckCalledFromGeneratedFile() 562 if is_extension: 563 return _message.default_pool.FindExtensionByName(full_name)

TypeError: Descriptors cannot not be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0. If you cannot immediately regenerate your protos, some other possible workarounds are:

  1. Downgrade the protobuf package to 3.20.x or lower.
  2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates

Os = Ubuntu22.04 python as per environment.yml

canyon289 commented 1 year ago

Thanks for reading the book. Are you using the library versions from the environment.yml file?