简体   繁体   中英

Keras functional API and TensorFlow Hub

I'm trying to use a Universal Sentence Encoder from TF Hub as a keras layer in a functional way. I would like to use hub.KerasLayer with Keras Functional API, but i'm not sure how to achieve that, so far I've only seen exmaples of hub.KerasLayer with the Sequential API

import tensorflow_hub as hub
import tensorflow as tf
from tensorflow.keras import layers
import tf_sentencepiece


use_url = 'https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/1'

english_sentences = ["dog", "Puppies are nice.", "I enjoy taking long walks along the beach with my dog."]
english_sentences = np.array(english_sentences, dtype=object)[:, np.newaxis]


seq = layers.Input(shape=(None, ), name='sentence', dtype=tf.string)
module = hub.KerasLayer(hub.Module(use_url))(seq)
model = tf.keras.models.Model(inputs=[seq], outputs=[module])
model.summary()

x = model.predict(english_sentences)
print(x)

the code above runs into this error when passing the input layer to the embedding: TypeError: Can't convert 'inputs': Shape TensorShape([Dimension(None), Dimension(None)]) is incompatible with TensorShape([Dimension(None)])

Is it possible to use hub.KerasLayer with keras functional API in TensorFlow 1.x? if it can be done, how?

If you use v3 of the same universal sentence encoder with tf 1.15, you can do such thing by replacing lines from

import tf_sentencepiece
use_url = 'https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/1'
module = hub.KerasLayer(hub.Module(use_url))(seq)

to

import tensorflow_text
use_url = 'https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/3'
module = hub.KerasLayer(use_url)(seq)

First shape is what you are passing into the model, Shape TensorShape([Dimension(None), Dimension(None)]) . Second shape is what you are expecting, TensorShape([Dimension(None)]) . So in this error, its telling you it expecting a shape of ()...

Or

If you are expecting to do batches of text, perhaps do TimeDistributed layer, like so...

module = tf.keras.layers.TimeDistributed(hub.KerasLayer(hub.Module(use_url)))(seq)

However you maybe forced to do specific size for text length...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM