tensorflow / hub

A library for transfer learning by reusing parts of TensorFlow models.
https://tensorflow.org/hub
Apache License 2.0
3.49k stars 1.67k forks source link

when freezing tfhub.keraslayer, ValueError: input resource[0] expected type resource != float, the type of embeddings_sharded_0[0] #831

Closed Zhiwei35 closed 2 years ago

Zhiwei35 commented 2 years ago

hi, I have met a problem when I freeze my SavedModel format keras model by below code block. The concrete error is

**ValueError: input resource[0] expected type resource != float, the type of embeddings_sharded_0[0]
    In {{node EncoderDNN/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Gather}}**

I have tried different Tensorflow version including 2.5 2.6. Python version is 3.6.x.

#build a keras model
hub_layer = hub.KerasLayer( "https://tfhub.dev/google/universal-sentence-encoder-multilingual/3", input_shape=[], 
                           dtype=tf.string, trainable=False)
model = tf.keras.Sequential()
model.add(hub_layer)
model.save(path/to/model) # save as a SavedModel format

#freeze
model = tf.keras.models.load_model(path/to/model)
kwargs = dict(zip(model.input_names, model.inputs))
for name, tensor in kwargs.items():
     kwargs[name] = tensor.type_spec
full_model = tf.function(lambda **kwargs: model(kwargs.values()))
concrete_function = full_model.get_concrete_function(**kwargs)
frozen_model = convert_variables_to_constants_v2(concrete_function)  #ValueError: input resource[0] expected type resource != float, the type of embeddings_sharded_0[0] In {{node EncoderDNN/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Gather}}

Could anyone help me? Thanks very much

Zhiwei35 commented 2 years ago

Hi, any update on it?

sanatmpa1 commented 2 years ago

@Zhiwei35,

I tried reproducing your code and I am getting a different error, FileNotFoundError: Op type not registered 'SentencepieceOp' in binary running on 15b7f20ff450. Can you take a look at this gist and correct me if I am missing something. Thanks!

Zhiwei35 commented 2 years ago

temporarily close it cause I am not able to track this question recently. Thanks, @sanatmpa1