相关疑难解决方法(0)

将Tensorflow预处理添加到现有Keras模型(用于Tensorflow服务)

我想在我导出的Keras模型中包含我的自定义预处理逻辑,以用于Tensorflow服务.

我的预处理执行字符串标记化并使用外部字典将每个标记转换为索引以输入到嵌入层:

from keras.preprocessing import sequence

token_to_idx_dict = ... #read from file

# Custom Pythonic pre-processing steps on input_data
tokens = [tokenize(s) for s in input_data]
token_idxs = [[token_to_idx_dict[t] for t in ts] for ts in tokens]
tokens_padded = sequence.pad_sequences(token_idxs, maxlen=maxlen)
Run Code Online (Sandbox Code Playgroud)

模型架构和培训:

model = Sequential()
model.add(Embedding(max_features, 128, input_length=maxlen))
model.add(LSTM(128, activation='sigmoid'))
model.add(Dense(n_classes, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')

model.fit(x_train, y_train)
Run Code Online (Sandbox Code Playgroud)

由于该模型将用于Tensorflow服务,我想将所有预处理逻辑合并到模型本身(在导出的模型文件中编码).

问:我怎样才能使用Keras库?

我发现本指南解释了如何结合Keras和Tensorflow.但我仍然不确定如何将所有东西都作为一个模型出口.

我知道Tensorflow有内置的字符串拆分,文件I/O字典查找操作.

使用Tensorflow操作的预处理逻辑:

# Get input text
input_string_tensor = tf.placeholder(tf.string, shape={1})
# Split input text by …
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow tensorflow-serving

15
推荐指数
2
解决办法
4230
查看次数

标签 统计

keras ×1

python ×1

tensorflow ×1

tensorflow-serving ×1