pythontensorflowkeraskeras-layer

Attribute Error: 'Embedding' object has no attribute 'embeddings' - TensorFlow & Keras


Okay so I have a keras model that I fully ran and then saved the weights with this line:

model.save_weights("rho_beta_true_tf", save_format="tf")

Then in another file I build just the model and then I load the weights from the model I ran above using this line:

model_build.load_weights("rho_beta_true_tf")

When I then go to call some of the attributes everything displays correctly except when I try to run this line:

model_build.stimuli.embeddings 

or

model_build.stimuli.embeddings.numpy()[0]

I get an attribute error saying:

AttributeError: 'Embedding' object has no attribute 'embeddings'

This line is supposed to return a tensor and if I call any other attributes so far it works so I am not sure if it just can't find the tensors or if the problem is something else. Could someone please help me figure out how to solve this attribute Error?


Solution

  • Turns out that because I had saved the weights in tf format I had to follow this step in the tensor flow documentation:

    For user-defined classes which inherit from tf.keras.Model, Layer instances must be assigned to object attributes, typically in the constructor.

    So then the line

    build_model.stimuli.embedding(put the directory path to your custom embedding layer here)
    

    worked!