google / seq2seq

A general-purpose encoder-decoder framework for Tensorflow
https://google.github.io/seq2seq/
Apache License 2.0
5.6k stars 1.3k forks source link

from tensorflow.contrib.distributions.python.ops import bernoulli ImportError: cannot import name 'bernoulli' #285

Open ajaykumarbharaj opened 7 years ago

robi56 commented 7 years ago

Update by:

from tensorflow.contrib.distributions import  Bernoulli
from tensorflow.contrib.distributions import Categorical

sampler = Bernoulli(probs=self._sampling_probability)
sample_id_sampler = Categorical(logits=outputs)

This will resolve your problem

enjector commented 7 years ago

@robi56 thanks. Just in case others are having a problem here are the steps to correct the problem when running unit tests: 1) emacs seq2seq/contrib/seq2seq/helper.py 2) Replace the imports line for bernoulli and categorical at the top with robi56's answer from tensorflow.contrib.distributions import Bernoulli from tensorflow.contrib.distributions import Categorical 3) Run python -m unittest seq2seq.test.pipeline_test

Thanks

ptresende commented 7 years ago

In case the solutions above do not work for you, in my env, the following did work:

from tensorflow.python.ops.distributions import bernoulli
from tensorflow.python.ops.distributions import categorical

My output for python -c "import tensorflow as tf; print(tf.VERSION, tf.GIT_VERSION, tf.__file__)"

is: 1.2.1 v1.2.0-5-g435cdfc /usr/local/lib/python3.5/dist-packages/tensorflow/__init__.py

alishakiba commented 7 years ago

Hi there,

I've made the aforementioned changes, however, it did not work for me.

I am using centos-release-6-5.el6.centos.11.1.x86_64. The output for uname -a

is

Linux p3.hpc.vru.ac.ir 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

python -c "import tensorflow as tf; print(tf.VERSION, tf.GIT_VERSION, tf.__file__)"

is

1.2.1 b'unknown' /opt/anaconda2/envs/keras-tf-backend-gpu/lib/python3.5/site-packages/tensorflow/__init__.py

The error is

` python -m unittest seq2seq.test.pipeline_test E

ERROR: seq2seq (unittest.loader._FailedTest)

ImportError: Failed to import test module: seq2seq Traceback (most recent call last): File "/opt/anaconda2/envs/keras-tf-backend-gpu/lib/python3.5/unittest/loader.py", line 153, in loadTestsFromName module = import(module_name) File "/root/seq2seq_src/seq2seq/init.py", line 25, in from seq2seq import data File "/root/seq2seq_src/seq2seq/data/init.py", line 17, in from seq2seq.data import input_pipeline File "/root/seq2seq_src/seq2seq/data/input_pipeline.py", line 32, in from tensorflow.contrib.slim.python.slim.data import tfexample_decoder File "/opt/anaconda2/envs/keras-tf-backend-gpu/lib/python3.5/site-packages/tensorflow/contrib/init.py", line 56, in from tensorflow.contrib import seq2seq File "/opt/anaconda2/envs/keras-tf-backend-gpu/lib/python3.5/site-packages/tensorflow/contrib/seq2seq/init.py", line 27, in from tensorflow.contrib.seq2seq.python.ops.basic_decoder import * File "/opt/anaconda2/envs/keras-tf-backend-gpu/lib/python3.5/site-packages/tensorflow/contrib/seq2seq/python/ops/basic_decoder.py", line 25, in from tensorflow.contrib.seq2seq.python.ops import helper as helper_py File "/opt/anaconda2/envs/keras-tf-backend-gpu/lib/python3.5/site-packages/tensorflow/contrib/seq2seq/python/ops/helper.py", line 36, in from tensorflow.python.ops.distributions import Bernoulli ImportError: cannot import name 'Bernoulli'


Ran 1 test in 0.000s

FAILED (errors=1) `

ghost commented 7 years ago

For people still having confusions:

The actual code block (unchanged) is:

from tensorflow.contrib.distributions.python.ops import bernoulli
from tensorflow.contrib.distributions.python.ops import categorical

First solution

If you are using the suggestions by @robi56 :

from tensorflow.contrib.distributions import  Bernoulli
from tensorflow.contrib.distributions import Categorical

(notice that bernoulli and categorical are capitalized now)

Make sure that you make the following changes in line 387 and line 267:

sampler = Bernoulli(probs=self._sampling_probability)
sample_id_sampler = Categorical(logits=outputs)

Second solution

or just make the following changes suggested by @ptresende :

from tensorflow.python.ops.distributions import bernoulli
from tensorflow.python.ops.distributions import categorical

Both the solutions work for me.

Sammyreus commented 6 years ago

new error after fixing the helper.py

python -m unittest seq2seq.test.pipeline_test WARNING:tensorflow:From /home/nil/seq2seq/seq2seq/test/pipeline_test.py:57: get_or_create_global_step (from tensorflow.contrib.framework.python.ops.variables) is deprecated and will be removed in a future version. Instructions for updating: Please switch to tf.train.get_or_create_global_step .E

ERROR: test_train_infer (seq2seq.test.pipeline_test.PipelineTest) Tests training and inference scripts.

Traceback (most recent call last): File "/home/nil/seq2seq/seq2seq/test/pipeline_test.py", line 76, in test_train_infer _clear_flags() File "/home/nil/seq2seq/seq2seq/test/pipeline_test.py", line 44, in _clear_flags tf.app.flags.FLAGS = tf.app.flags._FlagValues() AttributeError: module 'tensorflow.python.platform.flags' has no attribute '_FlagValues'


Ran 2 tests in 0.134s

Jaydaar commented 6 years ago

i have this same issue and i am not sure how to fix it.

kritinemkul commented 6 years ago

Same error exists. Traceback (most recent call last): File "/home/nil/seq2seq/seq2seq/test/pipeline_test.py", line 76, in test_train_infer _clear_flags() File "/home/nil/seq2seq/seq2seq/test/pipeline_test.py", line 44, in _clear_flags tf.app.flags.FLAGS = tf.app.flags._FlagValues() AttributeError: module 'tensorflow.python.platform.flags' has no attribute '_FlagValues'

tigerneil commented 6 years ago

@kritinemkul for your problem, change the following code in /home/ubuntu/seq2seq/seq2seq/test/pipeline_test.py:

def _clear_flags():
  """Resets Tensorflow's FLAG values"""
  #pylint: disable=W0212
  for flag_key in dir(tf.app.flags.FLAGS):
      delattr(tf.app.flags.FLAGS, flag_key)
  #tf.app.flags.FLAGS = tf.app.flags._FlagValues()
  tf.app.flags._global_parser = argparse.ArgumentParser()
kritinemkul commented 6 years ago

@tigerneil Thank you for the solution.

onlyjackfrost commented 6 years ago

I got a problem after i solved the bernoulli ImportError and the AttributeError mentioned above. Then I used the command python -m unittest seq2seq.test.pipeline_test and i got a error below.

PermissionError: [Errno 13] Permission denied: 'C:\\Users\\user\\AppData\\Local\\Temp\\tmpi7s9txub'

 [[Node: bleu/value = PyFunc[Tin=[DT_STRING, DT_STRING], Tout=[DT_FLOAT], token="pyfunc_0",
_device="/job:localhost/replica:0/task:0/device:CPU:0"](bleu/sources/read, bleu/targets/read)]]

I've check the path"C:\Users\user\AppData\Local\Temp" and there is no such file... Btw im using Anaconda for python3.6 and tensorflow1.8 on window8.1

neerajks commented 6 years ago

Hi any luck resolving the above error ? I am facing the same error on tensorflow 1.8.

ghost commented 6 years ago

https://github.com/google/seq2seq/issues/285#issuecomment-331921344

Only solution 2 worked not solution 1.

mosabrezaei commented 3 years ago

Hi,

I followed the responses and I have changed the below files:

1. /seq2seq/seq2seq/contrib/seq2seq/helper.py   =>  (to solve bernoulli and categorical problem)
2. /seq2seq/test/pipeline_test.py   =>  (to solve: module 'tensorflow.python.platform.flags' has no attribute '_FlagValues' )

Also, I have changed the Tensorflow version to 1.15.2

But when I try to run: ! python -m unittest seq2seq.test.pipeline_test

I face the following error after a lot of warning about deprecated files:

E
======================================================================
ERROR: test_train_infer (seq2seq.test.pipeline_test.PipelineTest)
seq2seq.test.pipeline_test.PipelineTest.test_train_infer
Tests training and inference scripts.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/content/seq2seq/seq2seq/test/pipeline_test.py", line 150, in test_train_infer
    train_script.main([])
  File "/content/seq2seq/bin/train.py", line 272, in main
    schedule=FLAGS.schedule)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/util/deprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/contrib/learn/python/learn/learn_runner.py", line 225, in run
    return _execute_schedule(experiment, schedule)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/contrib/learn/python/learn/learn_runner.py", line 52, in _execute_schedule
    return task()
  File "/content/seq2seq/seq2seq/contrib/experiment.py", line 104, in continuous_train_and_eval
    monitors=self._train_monitors)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/util/deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/contrib/learn/python/learn/estimators/estimator.py", line 524, in fit
    loss = self._train_model(input_fn=input_fn, hooks=hooks)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/contrib/learn/python/learn/estimators/estimator.py", line 1041, in _train_model
    model_fn_ops = self._get_train_ops(features, labels)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/contrib/learn/python/learn/estimators/estimator.py", line 1265, in _get_train_ops
    return self._call_model_fn(features, labels, model_fn_lib.ModeKeys.TRAIN)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/contrib/learn/python/learn/estimators/estimator.py", line 1228, in _call_model_fn
    model_fn_results = self._model_fn(features, labels, **kwargs)
  File "/content/seq2seq/bin/train.py", line 183, in model_fn
    return model(features, labels, params)
  File "/content/seq2seq/seq2seq/models/model_base.py", line 146, in __call__
    return self._build(features, labels, params)
  File "/content/seq2seq/seq2seq/models/seq2seq_model.py", line 297, in _build
    encoder_output = self.encode(features, labels)
  File "/content/seq2seq/seq2seq/graph_utils.py", line 38, in func_wrapper
    return templated_func(*args, **kwargs)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/ops/template.py", line 393, in __call__
    return self._call_func(args, kwargs)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/ops/template.py", line 355, in _call_func
    result = self._func(*args, **kwargs)
  File "/content/seq2seq/seq2seq/models/basic_seq2seq.py", line 109, in encode
    return encoder_fn(source_embedded, features["source_len"])
  File "/content/seq2seq/seq2seq/graph_module.py", line 57, in __call__
    return self._template(*args, **kwargs)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/ops/template.py", line 385, in __call__
    return self._call_func(args, kwargs)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/ops/template.py", line 355, in _call_func
    result = self._func(*args, **kwargs)
  File "/content/seq2seq/seq2seq/encoders/encoder.py", line 49, in _build
    return self.encode(inputs, *args, **kwargs)
  File "/content/seq2seq/seq2seq/encoders/rnn_encoder.py", line 135, in encode
    cell_fw = training_utils.get_rnn_cell(**self.params["rnn_cell"])
  File "/content/seq2seq/seq2seq/training/utils.py", line 157, in get_rnn_cell
    cell = cell_from_spec(cell_class, cell_params)
  File "/content/seq2seq/seq2seq/training/utils.py", line 123, in cell_from_spec
    are: {}""".format(key, cell_class.__name__, cell_args))
ValueError: num_units is not a valid argument for GRUCell class. Available arguments
          are: set()

originally defined at:
  File "/content/seq2seq/seq2seq/models/basic_seq2seq.py", line 108, in encode
    encoder_fn = self.encoder_class(self.params["encoder.params"], self.mode)
  File "/content/seq2seq/seq2seq/encoders/rnn_encoder.py", line 119, in __init__
    super(BidirectionalRNNEncoder, self).__init__(params, mode, name)
  File "/content/seq2seq/seq2seq/encoders/encoder.py", line 45, in __init__
    GraphModule.__init__(self, name)
  File "/content/seq2seq/seq2seq/graph_module.py", line 44, in __init__
    self._template = tf.make_template(name, self._build, create_scope_now_=True)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/ops/template.py", line 161, in make_template
    **kwargs)

originally defined at:
  File "/content/seq2seq/seq2seq/graph_utils.py", line 37, in func_wrapper
    templated_func = tf.make_template(name_, func)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/ops/template.py", line 161, in make_template
    **kwargs)

----------------------------------------------------------------------
Ran 2 tests in 0.189s

FAILED (errors=1, skipped=1)
yuchensu1 commented 3 years ago

Hi,

I followed the responses and I have changed the below files:

1. /seq2seq/seq2seq/contrib/seq2seq/helper.py   =>  (to solve bernoulli and categorical problem)
2. /seq2seq/test/pipeline_test.py   =>  (to solve: module 'tensorflow.python.platform.flags' has no attribute '_FlagValues' )

Also, I have changed the Tensorflow version to 1.15.2

But when I try to run: ! python -m unittest seq2seq.test.pipeline_test

I face the following error after a lot of warning about deprecated files:

E
======================================================================
ERROR: test_train_infer (seq2seq.test.pipeline_test.PipelineTest)
seq2seq.test.pipeline_test.PipelineTest.test_train_infer
Tests training and inference scripts.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/content/seq2seq/seq2seq/test/pipeline_test.py", line 150, in test_train_infer
    train_script.main([])
  File "/content/seq2seq/bin/train.py", line 272, in main
    schedule=FLAGS.schedule)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/util/deprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/contrib/learn/python/learn/learn_runner.py", line 225, in run
    return _execute_schedule(experiment, schedule)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/contrib/learn/python/learn/learn_runner.py", line 52, in _execute_schedule
    return task()
  File "/content/seq2seq/seq2seq/contrib/experiment.py", line 104, in continuous_train_and_eval
    monitors=self._train_monitors)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/util/deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/contrib/learn/python/learn/estimators/estimator.py", line 524, in fit
    loss = self._train_model(input_fn=input_fn, hooks=hooks)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/contrib/learn/python/learn/estimators/estimator.py", line 1041, in _train_model
    model_fn_ops = self._get_train_ops(features, labels)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/contrib/learn/python/learn/estimators/estimator.py", line 1265, in _get_train_ops
    return self._call_model_fn(features, labels, model_fn_lib.ModeKeys.TRAIN)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/contrib/learn/python/learn/estimators/estimator.py", line 1228, in _call_model_fn
    model_fn_results = self._model_fn(features, labels, **kwargs)
  File "/content/seq2seq/bin/train.py", line 183, in model_fn
    return model(features, labels, params)
  File "/content/seq2seq/seq2seq/models/model_base.py", line 146, in __call__
    return self._build(features, labels, params)
  File "/content/seq2seq/seq2seq/models/seq2seq_model.py", line 297, in _build
    encoder_output = self.encode(features, labels)
  File "/content/seq2seq/seq2seq/graph_utils.py", line 38, in func_wrapper
    return templated_func(*args, **kwargs)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/ops/template.py", line 393, in __call__
    return self._call_func(args, kwargs)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/ops/template.py", line 355, in _call_func
    result = self._func(*args, **kwargs)
  File "/content/seq2seq/seq2seq/models/basic_seq2seq.py", line 109, in encode
    return encoder_fn(source_embedded, features["source_len"])
  File "/content/seq2seq/seq2seq/graph_module.py", line 57, in __call__
    return self._template(*args, **kwargs)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/ops/template.py", line 385, in __call__
    return self._call_func(args, kwargs)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/ops/template.py", line 355, in _call_func
    result = self._func(*args, **kwargs)
  File "/content/seq2seq/seq2seq/encoders/encoder.py", line 49, in _build
    return self.encode(inputs, *args, **kwargs)
  File "/content/seq2seq/seq2seq/encoders/rnn_encoder.py", line 135, in encode
    cell_fw = training_utils.get_rnn_cell(**self.params["rnn_cell"])
  File "/content/seq2seq/seq2seq/training/utils.py", line 157, in get_rnn_cell
    cell = cell_from_spec(cell_class, cell_params)
  File "/content/seq2seq/seq2seq/training/utils.py", line 123, in cell_from_spec
    are: {}""".format(key, cell_class.__name__, cell_args))
ValueError: num_units is not a valid argument for GRUCell class. Available arguments
          are: set()

originally defined at:
  File "/content/seq2seq/seq2seq/models/basic_seq2seq.py", line 108, in encode
    encoder_fn = self.encoder_class(self.params["encoder.params"], self.mode)
  File "/content/seq2seq/seq2seq/encoders/rnn_encoder.py", line 119, in __init__
    super(BidirectionalRNNEncoder, self).__init__(params, mode, name)
  File "/content/seq2seq/seq2seq/encoders/encoder.py", line 45, in __init__
    GraphModule.__init__(self, name)
  File "/content/seq2seq/seq2seq/graph_module.py", line 44, in __init__
    self._template = tf.make_template(name, self._build, create_scope_now_=True)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/ops/template.py", line 161, in make_template
    **kwargs)

originally defined at:
  File "/content/seq2seq/seq2seq/graph_utils.py", line 37, in func_wrapper
    templated_func = tf.make_template(name_, func)
  File "/tensorflow-1.15.2/python3.7/tensorflow_core/python/ops/template.py", line 161, in make_template
    **kwargs)

----------------------------------------------------------------------
Ran 2 tests in 0.189s

FAILED (errors=1, skipped=1)

I face the same problem and has anyone solved this error?

thakkarparth007 commented 2 years ago

This particular problem can be solved by replacing the following line in seq2seq.training.utils.py:

cell_args = set(inspect.getargspec(cell_class.__init__).args[1:]) (found in cell_from_spec) method

with the following line:

cell_args = set(list(inspect.signature(cell_class.__init__).parameters)[1:])

But of course there's a new error now:

ERROR: test_train_infer (seq2seq.test.pipeline_test.PipelineTest)
seq2seq.test.pipeline_test.PipelineTest.test_train_infer
Tests training and inference scripts.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/parthdt2/DL_models/atlas---deep-learning-assert-statements/seq2seq/seq2seq/test/pipeline_test.py", line 159, in test_train_infer
    os.path.join(BIN_FOLDER, "infer.py"))
  File "/usr/lib/python3.6/imp.py", line 172, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 684, in _load
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/parthdt2/DL_models/atlas---deep-learning-assert-statements/seq2seq/bin/infer.py", line 40, in <module>
    parameters for inference""")
  File "/home/parthdt2/.local/lib/python3.6/site-packages/tensorflow_core/python/platform/flags.py", line 58, in wrapper
    return original_function(*args, **kwargs)
  File "/home/parthdt2/.local/lib/python3.6/site-packages/absl/flags/_defines.py", line 294, in DEFINE_string
    **args)
  File "/home/parthdt2/.local/lib/python3.6/site-packages/absl/flags/_defines.py", line 106, in DEFINE
    module_name, required)
  File "/home/parthdt2/.local/lib/python3.6/site-packages/absl/flags/_defines.py", line 140, in DEFINE_flag
    fv[flag.name] = flag
  File "/home/parthdt2/.local/lib/python3.6/site-packages/absl/flags/_flagvalues.py", line 439, in __setitem__
    raise _exceptions.DuplicateFlagError.from_flag(name, self)
absl.flags._exceptions.DuplicateFlagError: The flag 'model_params' is defined twice. First from seq2seq.test.train_bin, Second from seq2seq.test.infer_bin.  Description from first occurrence: YAML configuration string for the model
                       parameters.

Also, my fix somehow doesn't make the test pass. The test runs, but the training stops as max steps are reached. I'm not sure if that's okay or suggests a deeper bug. INFO:tensorflow:Stop training model as max steps reached.

The final output has this:

Ran 2 tests in 17.685s

FAILED (errors=1, skipped=1)