1 回答
TA贡献1828条经验 获得超3个赞
我刚刚尝试了一下,当我删除“input_shape = [None]”时,它对我有用。所以这段代码应该可以工作:
import tensorflow_hub as hub
from tensorflow.dtypes import as_string
def embedding(x):
print(x.shape)
module = hub.Module("https://tfhub.dev/google/nnlm-en-dim128/1")
return module(x)
answers_network_rnn = Sequential()
print(trainingData["question"].shape)
from keras.layers import InputLayer
answers_network_rnn.add(Lambda(embedding,output_shape=(128,)))
answers_network_rnn.add(Dense(16))
answers_network_rnn.add(Dense(Y_2_train_num.shape[1]))
answers_network_rnn.summary()
编辑
这个 keras 模型应该等于 SequentialModel(显式输入层除外):
input_text = tf.keras.layers.Input(shape=(1,), dtype=tf.string)
embedding_layer = tf.keras.layers.Lambda(embedding,output_shape=(128,))(input_text)
dense = tf.keras.layers.Dense(16)(embedding_layer)
outputs = tf.keras.layers.Dense(Y_2_train_num.shape[1])(dense)
answers_network_rnn = tf.keras.Model(inputs=[input_text], outputs=outputs)
answers_network_rnn.compile(...)
运行这个对我有用......
with tf.Session() as session:
session.run([tf.global_variables_initializer(), tf.tables_initializer()])
answers_network_rnn.fit(...)
...在 lambda 函数中更改此内容后:
#return module(x)
return module(tf.squeeze(tf.cast(x, tf.string)),signature="default", as_dict=True)["default"]
添加回答
举报