【问题标题】:Tensorflow serving: how to iterate over input placeholderTensorflow 服务:如何迭代输入占位符
【发布时间】: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


    【解决方案1】:

    可能是我必须解决的类似情况。见这里:tensorflow serving input function for sliding window over timeseries data。 此问题中的代码确实使用 tf.while_loop 遍历输入时间线(滑动窗口)。但是,它遇到了与服务输入不匹配的示例数量相关的另一个问题。我还没有找到解决这个后续问题的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-24
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多