strongio / keras-bert

A simple technique to integrate BERT from tf hub to keras
258 stars 108 forks source link

Error while build_model(max_seq_length) #14

Closed utsavg closed 5 years ago

utsavg commented 5 years ago

TypeError Traceback (most recent call last)

in () ----> 1 model = build_model(max_seq_length) 2 3 # Instantiate variables 4 initialize_vars(sess) 5 3 frames in build_model(max_seq_length) 10 11 #model=tf.keras.layers(inputs=bert_inputs, outputs=pred) ---> 12 model = tf.keras.models.Model(inputs=bert_inputs, outputs=pred) 13 14 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in __init__(self, *args, **kwargs) 127 128 def __init__(self, *args, **kwargs): --> 129 super(Model, self).__init__(*args, **kwargs) 130 # initializing _distribution_strategy here since it is possible to call 131 # predict on a model without compiling it. /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py in __init__(self, *args, **kwargs) 165 self._init_subclassed_network(**kwargs) 166 --> 167 tf_utils.assert_no_legacy_layers(self.layers) 168 169 # Several Network methods have "no_automatic_dependency_tracking" /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/tf_utils.py in assert_no_legacy_layers(layers) 397 'classes), please use the tf.keras.layers implementation instead. ' 398 '(Or, if writing custom layers, subclass from tf.keras.layers rather ' --> 399 'than tf.layers)'.format(layer_str)) 400 401 TypeError: The following are legacy tf.layers.Layers: <__main__.BertLayer object at 0x7fa4e1b239b0> To use keras as a framework (for instance using the Network, Model, or Sequential classes), please use the tf.keras.layers implementation instead. (Or, if writing custom layers, subclass from tf.keras.layers rather than tf.layers)
ibro45 commented 5 years ago

Find the definition of the class BertLayer() and change it from: class BertLayer(tf.layers.Layer): to class BertLayer(tf.keras.layers.Layer):

utsavg commented 5 years ago

It's working now. Thank you Ibrahim Hadzic.