如何使用Tensorflow张量设置Keras层的输入? [英] How to set the input of a Keras layer with a Tensorflow tensor?

查看:460
本文介绍了如何使用Tensorflow张量设置Keras层的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的上一个中问题,我使用Keras的Layer.set_input()将Tensorflow预处理输出张量连接到Keras模型的输入.但是,在Keras版本1.1.1之后,已删除此方法.

In my previous question, I used Keras' Layer.set_input() to connect my Tensorflow pre-processing output tensor to my Keras model's input. However, this method has been removed after Keras version 1.1.1.

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

How can I achieve this in newer Keras versions?

示例:

# 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'))

推荐答案

在完成预处理之后,可以通过调用Input

After you are done with pre-processing, You can add the tensor as input layer by calling tensor param of Input

所以在您的情况下:

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))

这篇关于如何使用Tensorflow张量设置Keras层的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆