I have a piece of Tensorflow code:
class Classifier(keras.layers.Layer):
def __init__(self):
super().__init__()
self.classifier = keras_nlp.models.BertClassifier.from_preset(
"bert_base_en_uncased",
num_classes=1,
)
def call(self, inputs):
out = self.classifier(inputs)
out = tf.squeeze(out, -1)
return out
def get_model_2():
inp = {
'token_ids': keras.Input((512,), dtype=tf.int32, name=None),
'padding_mask': keras.Input((512,), dtype=tf.bool, name=None),
'segment_ids': keras.Input((512, ), dtype=tf.int32, name=None)
}
layer = Classifier()
out = layer(inp)
model = keras.Model(inp, out)
return model
model = get_model_2()
model.compile(
loss=keras.losses.BinaryCrossentropy(from_logits=True),
metrics=[
keras.metrics.TruePositives(),
keras.metrics.FalsePositives(),
keras.metrics.TrueNegatives(),
keras.metrics.FalseNegatives(),
keras.metrics.BinaryAccuracy()
],
optimizer=keras.optimizers.Adam(1e-4),
)
The problem is: when I call model.summary(), then the result would be
Model: "model_9"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_47 (InputLayer) [(None, 512)] 0 []
input_48 (InputLayer) [(None, 512)] 0 []
input_46 (InputLayer) [(None, 512)] 0 []
classifier_5 (Classifier) (None,) 0 ['input_47[0][0]',
'input_48[0][0]',
'input_46[0][0]']
==================================================================================================
Total params: 0 (0.00 Byte)
Trainable params: 0 (0.00 Byte)
Non-trainable params: 0 (0.00 Byte)
The trainable parameter is 0 bytes! I believe that the issue arise when I want to train a model inside a layer is not possible, or even when I try training a model inside a model, which is also not working. I wonder how people fine tune this model? Thank you very much!
Edit 1: Still don't know the solution to this yet, but I guess that the bert implementation of keras is faulty, since the version that I switched to tensorflow_hub.KerasLayer works just fine
Edit 2: Thanks to @Keval Pandya, the code is the solution. The problem lies in the import (not inside the context given by the original question):
import keras
# or
from tensorflow import keras
works fine, but
import tf_keras as keras # Error
shall produce the error. My version of package is:
keras 3.4.1
keras-nlp 0.15.1
tf_keras 2.17.0
tensorflow 2.17.0
tensorflow-hub 0.16.1
tensorflow-text 2.17.0
keras-nlp 0.15.1
tf_keras 2.17.0
