【发布时间】:2018-05-03 03:59:20
【问题描述】:
我正在使用 Tensorflow 构建 RNN (GRU) 模型,现在模型已经过训练,我需要通过 Tensorflow Serving 部署它以进行预测服务。
所以我创建了一个如下的signature_def函数,该函数需要获取ids作为输入,它的长度不固定(所以我将shape设置为[None]),在函数内部,ids中的id被一一挑选输入GRU 细胞。问题是,对于 None 形状,我无法弄清楚如何遍历所有 ids
def signature_def(self):
ids = tf.placeholder(tf.int32, [None], name='input')
state = [np.zeros([1, self.rnn_size], dtype=np.float32) for _ in range(self.layers)]
for i in range(<length_of_ids>):
id = [ids[i]]
inputs = tf.nn.embedding_lookup(self.embedding, id)
output, state = self.stacked_cell(inputs, tuple(state))
logits = tf.matmul(output, self.softmax_W, transpose_b=True) + self.softmax_b
outputs = self.final_activation(logits)
tensor_info_x = tf.saved_model.utils.build_tensor_info(ids)
tensor_info_y = tf.saved_model.utils.build_tensor_info(outputs)
return tf.saved_model.signature_def_utils.build_signature_def(
inputs={'ids': tensor_info_x},
outputs={'preds': tensor_info_y},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)
我曾经尝试过 tf.map_fn,它报告错误为“.../dropout/mul is in a while loop” 我还尝试添加另一个输入参数来传递 ids 长度,如下所示,但似乎长度参数无法更改 for 循环,它仍然是默认值 3:
def signature_def(self):
ids = tf.placeholder(tf.int32, [None], name='input')
length = tf.placeholder_with_default([3], [1], name='length')
state = [np.zeros([1, self.rnn_size], dtype=np.float32) for _ in range(self.layers)]
for i in range(length.eval()[0]):
id = [ids[i]]
inputs = tf.nn.embedding_lookup(self.embedding, id)
output, state = self.stacked_cell(inputs, tuple(state))
logits = tf.matmul(output, self.softmax_W, transpose_b=True) + self.softmax_b
outputs = self.final_activation(logits)
tensor_info_x = tf.saved_model.utils.build_tensor_info(ids)
tensor_info_l = tf.saved_model.utils.build_tensor_info(length)
tensor_info_y = tf.saved_model.utils.build_tensor_info(outputs)
return tf.saved_model.signature_def_utils.build_signature_def(
inputs={'ids': tensor_info_x, 'length': tensor_info_l},
outputs={'preds': tensor_info_y},
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)
任何建议或指导将不胜感激
谢谢!
【问题讨论】:
标签: python tensorflow rnn tensorflow-serving