简体   繁体   English

Tensorflow服务:如何在输入占位符上进行迭代

[英]Tensorflow serving: how to iterate over input placeholder

I am using Tensorflow to build a RNN (GRU) model, now model is trained and I need to deploy it by Tensorflow Serving for prediction service. 我正在使用Tensorflow构建RNN(GRU)模型,现在已经对模型进行了训练,我需要通过Tensorflow Serving部署它以进行预测服务。
So I create a signature_def function as below, the function need to get ids as input, which length is not fixed (so I set shape as [None]), inside the function, the id in ids is picked one by one to feed into GRU cell. 因此,我如下创建了一个signature_def函数,该函数需要获取ID作为输入,该ID的长度是不固定的(因此我将形状设置为[None]),在函数内部,ID中的ID被一个接一个地拾取GRU细胞。 The problem is, with the None shape, I cannot figure out how to iterate over all ids 问题是,使用None形状,我无法弄清楚如何遍历所有id

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)

I ever tried tf.map_fn, which report error as ".../dropout/mul is in a while loop" I also tried add another input parameter to pass ids length as below, but seems the length parameter cannot change the for loop, it still as the default value 3: 我曾经尝试过tf.map_fn,它报告错误为“ ... / dropout / mul在while循环中”,我还尝试添加另一个输入参数来传递ids长度,如下所示,但似乎length参数不能更改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)

Any advise or guidance would be greatly appreciated 任何建议或指导将不胜感激

Thanks! 谢谢!

Possibly a similar situation which I have to solve. 可能是我必须解决的类似情况。 See here: tensorflow serving input function for sliding window over timeseries data . 参见此处: tensorflow服务,用于在时间序列数据上滑动窗口的输入函数 The code in this question does iterate over input timeline (sliding window) using a tf.while_loop. 这个问题中的代码确实使用tf.while_loop在输入时间轴(滑动窗口)上进行迭代。 However, it runs into another issue related to the number of examples not matching the serving input. 但是,这又遇到了另一个问题,该问题与不匹配服务输入的示例数有关。 I have not yet found a solution to this subsequent issue. 我尚未找到此后续问题的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM