Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

No module named 'tensorflow.contrib' (Support for TF > 2.0) #30

Description

@desertzebra

Hi,

I have installed version 1.0.3 of easybert using pip.
$ pip3 install easybert

This downloads an old copy of the script, which is not compatible with the current Java version, because the older python version doesnt produce model.json, which is required by Java.

So to resolve this problem, I updated the code for bert,py from the source code.

When I execute the cli command:

$ bert download

I get the following errors:

bert/modeling.py", line 29, in

from tensorflow.contrib import layers as contrib_layers
ModuleNotFoundError: No module named 'tensorflow.contrib'

I have tensorflow version 2.7.0.

The fix for this is relatively simple, which I will leave here for others to follow, until the TF compliance can be upgraded.

  • edit the file: .local/lib/python3.8/site-packages/bert/modeling.py
  • Change line 29 to
    from tensorflow_addons import layers as contrib_layers
  • install tensorflow_addons using
    pip install tensorflow-addons
  • run the command again.

Then had to fix the problem with tf.session. I did this by editing the following lines in bert.py file

76: with tf.Session(graph=self._graph) as session:

76: with tf.compat.v1.Session(graph=self._graph) as session:

100: with self._session = tf.Session(graph=self._graph)

100: self._session = tf.compat.v1.Session(graph=self._graph)

137: with tf.Session(graph=self._graph) as session:

137: with tf.compat.v1.Session(graph=self._graph) as session:

173: with tf.Session(graph=self._graph) as session:

173: with tf.compat.v1.Session(graph=self._graph) as session:

205: with tf.Session(graph=bert._graph) as session:

205: with tf.compat.v1.Session(graph=bert._graph) as session:

212: with tf.Session() as session:

212: with tf.compat.v1.Session() as session:

Now the tf.placeholder issue

86: self._input_ids = tf.placeholder(name="input_ids", shape=(None, max_sequence_length), dtype=tf.int32)

86: self._input_ids = tf.compat.v1.placeholder(name="input_ids", shape=(None, max_sequence_length), dtype=tf.int32)

87: self._input_mask = tf.placeholder(name="input_mask", shape=(None, max_sequence_length), dtype=tf.int32)

87: self._input_mask = tf.compat.v1.placeholder(name="input_mask", shape=(None, max_sequence_length), dtype=tf.int32)

88: self._segment_ids = tf.placeholder(name="segment_ids", shape=(None, max_sequence_length), dtype=tf.int32)

88: self._segment_ids = tf.compat.v1.placeholder(name="segment_ids", shape=(None, max_sequence_length), dtype=tf.int32)

Next on to global_variables_initializer()

102: self._session.run(tf.global_variables_initializer())

102: self._session.run(tf.compat.v1.global_variables_initializer())

138: session.run(tf.global_variables_initializer())

138: session.run(tf.compat.v1.global_variables_initializer())

174: session.run(tf.global_variables_initializer())

174: session.run(tf.compat.v1.global_variables_initializer())

And saving.

167: tf.saved_model.simple_save(self._session, str(path), inputs={

167: tf.compat.v1.saved_model.simple_save(self._session, str(path), inputs={

176: tf.saved_model.simple_save(session, str(path), inputs={

176: tf.compat.v1.saved_model.simple_save(session, str(path), inputs={

206: bundle = tf.compat.v1.saved_model.load(session, ["serve"], str(path))

206: bundle = tf.compat.v1.saved_model.load(session, ["serve"], str(path))

I will edit this post with other changes I do to comply with TF > 2.0. Hopefully, this will be useful for someone else.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions