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

查看:28
本文介绍了如何使用 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 的 tensor param 添加张量作为输入层

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天全站免登陆