第一个模型因ValueError: Output tensors to a Model must be the output of a TensorFlow
层(thus holding past layer metadata). Found: Tensor("dense/BiasAdd:0", shape=(?, 1, 1), dtype=float32)
而出错
import tensorflow as tf
from tensorflow.keras.layers import Input, Embedding, concatenate, Dense, Flatten
def get_model():
first_input = Input(shape = (1,), name='first_input')
second_input = Input(shape = (1,), name='second_input' )
embedding_layer = Embedding(input_dim=10, output_dim=3, input_length=1)
first_input_encoded = embedding_layer(first_input)
second_input_encoded = embedding_layer(second_input)
o = concatenate([first_input_encoded, second_input_encoded])
o = Dense(1)(o)
model = tf.keras.models.Model(inputs=[first_input, second_input_encoded], outputs=o)
return model
第二个方法有效,但我不理解该错误的含义。
def get_model():
first_input = Input(shape = (1,), name='first_input')
second_input = Input(shape = (5,), name='second_input' )
embedding_layer = Embedding(input_dim=10, output_dim=3, input_length=1)
first_input_encoded = embedding_layer(first_input)
second_input_encoded = embedding_layer(second_input)
second = tf.reduce_mean(second_input_encoded, axis=1, keepdims=True)
o = concatenate([first_input_encoded, second])
o = Dense(1)(o)
model = tf.keras.models.Model(inputs=[first_input, second_input], outputs=o)
return model