kpe / bert-for-tf2

A Keras TensorFlow 2.0 implementation of BERT, ALBERT and adapter-BERT.
https://github.com/kpe/bert-for-tf2
MIT License
803 stars 193 forks source link

type error #91

Open erenat77 opened 3 years ago

erenat77 commented 3 years ago
/usr/local/lib/python3.6/dist-packages/bert/model.py:80 call  *
    output           = self.encoders_layer(embedding_output, mask=mask, training=training)
/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py:1030 __call__  **
    self._maybe_build(inputs)
/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py:2659 _maybe_build
    self.build(input_shapes)  # pylint:disable=not-callable
/usr/local/lib/python3.6/dist-packages/bert/transformer.py:209 build
    self.input_spec = keras.layers.InputSpec(shape=input_shape)
/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py:2777 __setattr__
    super(tf.__internal__.tracking.AutoTrackable, self).__setattr__(name, value)  # pylint: disable=bad-super-call
/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/tracking/base.py:530 _method_wrapper
    result = method(self, *args, **kwargs)
/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py:1297 input_spec
    'Got: {}'.format(v))

TypeError: Layer input_spec must be an instance of InputSpec. Got: InputSpec(shape=(None, 128, 768), ndim=3)
ljsun commented 3 years ago

Unfortunately, I ran into the same problem, and solved the issue by downgrading to tensorflow=2.3

czf1009 commented 2 years ago

This is cause by mix usage of tensorflow.python.keras and tensorflow.keras, when i replace all tensorflow.python.keras by tensorflow.keras, it work.

lgalant commented 2 years ago

I got it to work by following @czf1009 advice. This is what I did:

1) I cloned the latest versions of this repos into my virtual environment (venv) lib/python3.8/site-packages folder

(I believe these can be cloned to a local folder as well)

2) I modified the following files on the bert (bert-for-tf2) repo

Where it says "from tensorflow.python import keras" replaced by "from tensorflow import keras"

3)To remove some warnings , I pip installed gast==0.3.3 and abs-py=1.0.0

Hope it helps!

bcazur commented 2 years ago

Created https://github.com/kpe/bert-for-tf2/pull/94 with the solution from @lgalant

Serpong commented 1 year ago

Thanks everybody,

94 helped me.

TL;DR

  1. in file {venv}/lib/python3.X/site-packages/bert/transformer.py

    from tensorflow.python import keras

    to

    from tensorflow import keras
  2. in file {venv}/lib/python3.X/site-packages/bert/attention.py

    from tensorflow.python import keras
    from tensorflow.python.keras import backend as K

    to

    from tensorflow import keras
    from tensorflow.keras import backend as K