我正在尝试使用TFHub中的通用句子编码器作为功能方式的keras层。我想将hub. KerasLayer
与Keras功能API一起使用,但我不确定如何实现,到目前为止,我只看到了hub.KerasLayer与顺序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)
上面的代码在将输入层传递给嵌入时遇到此错误:TypeError:无法转换'输入':Shape TensorShape([Dimension(无), Dimension(无)])与TensorShape([Dimension(无)])不兼容
是否可以在TensorFlow 1. x中使用hub.KerasLayer和keras功能API?如果可以做到,如何做到?
试试这个
sentence_encoding_layer = hub.KerasLayer("https://tfhub.dev/google/universal-sentence-encoder/4",
trainable=False,
input_shape = [],
dtype = tf.string,
name = 'U.S.E')
inputs = tf.keras.layers.Input(shape = (), dtype = 'string',name = 'input_layer')
x = sentence_encoding_layer(inputs)
x = tf.keras.layers.Dense(64,activation = 'relu')(x)
outputs = tf.keras.layers.Dense(1,activation = 'sigmoid',name = 'output_layer')(x)
model = tf.keras.Model(inputs,outputs,name = 'Transfer_learning_USE')
model.summary()
模型。预测([句子])
如果您使用与tf 1.15相同的通用句子编码器的v3,您可以通过替换来自
import tf_sentencepiece
use_url = 'https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/1'
module = hub.KerasLayer(hub.Module(use_url))(seq)
到
import tensorflow_text
use_url = 'https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/3'
module = hub.KerasLayer(use_url)(seq)
第一个形状是你传入模型的,Shape TensorShape([Dimension(无), Dimension(无)])
。第二个形状是你期望的,TensorShape([Dimension(无)])
。所以在这个错误中,它告诉你它期望()的形状…
或者
如果你希望做批量的文本,也许做Time分布式层,像这样…
module = tf.keras.layers.TimeDistributed(hub.KerasLayer(hub.Module(use_url)))(seq)
但是,您可能被迫为文本长度做特定大小…