tensorflow / tensor2tensor

Library of deep learning models and datasets designed to make deep learning more accessible and accelerate ML research.
Apache License 2.0
15.5k stars 3.49k forks source link

module 'tensorflow' has no attribute 'to_float' #1736

Open shuiruge opened 4 years ago

shuiruge commented 4 years ago

Description

TensorFlow 2.0 has no attribute to_float.

Environment information

OS: Windows 10 Home Edition

$ pip freeze | grep tensor
mesh-tensorflow==0.1.4
tensor2tensor==1.14.1
tensorboard==2.0.1
tensorflow==2.0.0
tensorflow-datasets==1.3.0
tensorflow-estimator==2.0.1
tensorflow-gan==2.0.0
tensorflow-hub==0.7.0
tensorflow-metadata==0.15.0
tensorflow-probability==0.7.0

$ python -V
Python 3.7.5

For bugs: reproduction and error logs

# Steps to reproduce:
$ conda create -n test python=3.7
$ conda activate test
$ pip install tensor2tensor[tensorflow]
$ python -c "from tensor2tensor.models.transformer import Transformer"

# Error logs:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\me\Anaconda3\envs\nlp\lib\site-packages\tensor2tensor\models\__init__.py", line 25, in <module>
    from tensor2tensor.layers import modalities  # pylint: disable=g-import-not-at-top
  File "C:\Users\me\Anaconda3\envs\nlp\lib\site-packages\tensor2tensor\layers\modalities.py", line 28, in <module>
    from tensor2tensor.layers import common_attention
  File "C:\Users\me\Anaconda3\envs\nlp\lib\site-packages\tensor2tensor\layers\common_attention.py", line 954, in <module>
    def attention_bias_to_padding(attention_bias, cast_fn=tf.to_float):
AttributeError: module 'tensorflow' has no attribute 'to_float'
arneyjfs commented 4 years ago

I am new to tensorflow and just wanted to get started with some of the models. I also ran into this issue with the 'Quickstart' problem in the tensor2tensor documentation. Briefly switching back to tensorflow 1.15 has allowed me to get around this error for now so i can at least get to grips with the core functionality - keen to find a solution to allow tf2.0 if anyone has any ideas

irismasha commented 4 years ago

You need to change tf.to_float call to tf.cast(..., dtype=tf.float32)

skr3178 commented 2 years ago

I tried the above solution: I get the following error


ValueError Traceback (most recent call last) in () 2 3 from tensor2tensor.utils import registry ----> 4 from tensor2tensor import problems 5 6 problems.available() #Show all problems

9 frames /usr/local/lib/python3.7/dist-packages/tensor2tensor/problems.py in () 31 32 ---> 33 all_problems.import_modules(all_problems.ALL_MODULES)

/usr/local/lib/python3.7/dist-packages/tensor2tensor/data_generators/all_problems.py in import_modules(modules) 138 for module in modules: 139 try: --> 140 importlib.import_module(module) 141 except ImportError as error: 142 errors.append((module, error))

/usr/lib/python3.7/importlib/init.py in import_module(name, package) 125 break 126 level += 1 --> 127 return _bootstrap._gcd_import(name[level:], package, level) 128 129

/usr/local/lib/python3.7/dist-packages/tensor2tensor/data_generators/algorithmic.py in () 24 from six.moves import range # pylint: disable=redefined-builtin 25 from tensor2tensor.data_generators import generator_utils as utils ---> 26 from tensor2tensor.data_generators import problem 27 from tensor2tensor.data_generators import text_encoder 28 from tensor2tensor.layers import modalities

/usr/local/lib/python3.7/dist-packages/tensor2tensor/data_generators/problem.py in () 29 from tensor2tensor.utils import data_reader 30 from tensor2tensor.utils import hparam ---> 31 from tensor2tensor.utils import metrics 32 from tensor2tensor.utils import mlperf_log 33

/usr/local/lib/python3.7/dist-packages/tensor2tensor/utils/metrics.py in () 23 24 from tensor2tensor.layers import common_layers ---> 25 from tensor2tensor.layers import modalities 26 from tensor2tensor.utils import bleu_hook 27 from tensor2tensor.utils import rouge

/usr/local/lib/python3.7/dist-packages/tensor2tensor/layers/modalities.py in () 26 from six.moves import range # pylint: disable=redefined-builtin 27 ---> 28 from tensor2tensor.layers import common_attention 29 from tensor2tensor.layers import common_audio 30 from tensor2tensor.layers import common_image_attention as cia

/usr/local/lib/python3.7/dist-packages/tensor2tensor/layers/common_attention.py in () 952 953 @expert_utils.add_name_scope() --> 954 def attention_bias_to_padding(attention_bias, cast_fn=tf.cast(..., dtype=tf.float32)): 955 """Inverse of attention_bias_ignore_padding(). 956

/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/traceback_utils.py in error_handler(*args, **kwargs) 151 except Exception as e: 152 filtered_tb = _process_traceback_frames(e.traceback) --> 153 raise e.with_traceback(filtered_tb) from None 154 finally: 155 del filtered_tb

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype) 100 dtype = dtypes.as_dtype(dtype).as_datatype_enum 101 ctx.ensure_initialized() --> 102 return ops.EagerTensor(value, ctx.device_name, dtype) 103 104

ValueError: Attempt to convert a value (Ellipsis) with an unsupported type (<class 'ellipsis'>) to a Tensor.