首页 文章

如何设置具有Tensorflow张量的Keras层的输入?

提问于
浏览
12

在我的previous question中,我使用Keras' Layer.set_input() 将我的Tensorflow预处理输出张量连接到我的Keras模型的输入 . 但是,在Keras版本 1.1.1 之后的this method has been removed .

如何在较新的Keras版本中实现这一目标?

Example:

# Tensorflow pre-processing
raw_input = tf.placeholder(tf.string)
### some TF operations on raw_input ###
tf_embedding_input = ...    # pre-processing output tensor

# Keras model
model = Sequential()
e = Embedding(max_features, 128, input_length=maxlen)

### THIS DOESN'T WORK ANYMORE ###
e.set_input(tf_embedding_input)
################################

model.add(e)
model.add(LSTM(128, activation='sigmoid'))
model.add(Dense(num_classes, activation='softmax'))

1 回答

  • 11

    完成预处理后,可以通过调用 Inputtensor param将张量添加为输入层

    所以在你的情况下:

    tf_embedding_input = ...    # pre-processing output tensor
    
    # Keras model
    model = Sequential()
    model.add(Input(tensor=tf_embedding_input)) 
    model.add(Embedding(max_features, 128, input_length=maxlen))
    

相关问题