Skip to content Skip to sidebar Skip to footer

How To Chain An Input Layer To Tensorflow-hub?

I want to classify text to 2 classes by using this embedding: https://tfhub.dev/google/universal-sentence-encoder-multilingual/3 And I also want to add additional features after th

Solution 1:

I've faced a similar problem. My solution looks like this:

def build_model():
    premise = keras.Input(shape=(), dtype=tf.string)
    hypothesis = keras.Input(shape=(), dtype=tf.string)
    keras_emb = hub.KerasLayer(embed, input_shape=(), output_shape = (512), dtype=tf.string, trainable=True)
    prem_emb = keras_emb(premise)
    hyp_emb = keras_emb(hypothesis)
    emb = layers.Concatenate()([prem_emb, hyp_emb])
    dense = layers.Dense(32, activation="relu")(emb)
    classifier = layers.Dense(3)(dense)
    model = keras.Model(inputs=[premise, hypothesis], outputs=classifier, name="elementary_model")
    model.compile(loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True), optimizer="adam", metrics=['accuracy'])
    return model

Note: the text input shape should be () (empty tuple)

Post a Comment for "How To Chain An Input Layer To Tensorflow-hub?"