【发布时间】:2019-04-11 12:35:16
【问题描述】:
我创建了一个带有 tensorflow 后端的 keras 模型,但在导出我的模型以在 ML Engine 上使用时遇到了困难(作为 saved_model.pb)。这是我正在做的事情:
dataset = tf.data.Dataset.from_tensor_slices((data_train, labels_train))
dataset = dataset.map(lambda x, y: ({'reviews': x}, y))
val_dataset = tf.data.Dataset.from_tensor_slices((data_test, labels_test))
val_dataset = val_dataset.map(lambda x, y: ({'reviews': x}, y))
dataset = dataset.batch(self.batch_size).repeat() # repeat infinitely
val_dataset = val_dataset.batch(self.batch_size).repeat()
然后我对我的 Dataset 对象执行一些预处理:
dataset = dataset.map(lambda x, y: preprocess_text_and_y(x,y))
val_dataset = val_dataset.map(lambda x, y: preprocess_text_and_y(x,y))
我构建了我的 keras 模型并调用 .fit(...)。一切正常。
然后我尝试导出我的模型,如下所示:
def export(data_vocab):
estimator = tf.keras.estimator.model_to_estimator(model)
def serving():
data_table = tf.contrib.lookup.index_table_from_tensor(tf.constant(self.data_vocab),
default_value=0)
inputs = {
'reviews': tf.placeholder(shape=[1], dtype=tf.string)
}
preproc = inputs.copy()
preproc = preprocess_text(preproc, data_table)
return tf.estimator.export.ServingInputReceiver(preproc, inputs)
estimator.export_savedmodel('./test_export', serving)
不幸的是,我回来了:
ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`.
我搜索了一下,发现了这个:
How to use TensorFlow Dataset API in combination with dense layers
说我需要打电话给tf.set_shape(...)。我正在将字符串预处理为长度为 100 的整数数组。我尝试在我的 preprocess_text 函数中添加 x['reviews'].set_shape([100])
但这会破坏训练:
ValueError: Shapes must be equal rank, but are 2 and 1
关于如何解决的任何想法?
谢谢!
【问题讨论】:
-
@rhaertel80 任何想法都会很棒(如果你有机会的话)
标签: python tensorflow keras