Tensorflow 如何返回TF模型签名和TFS服务中的RaggedSensor?
我使用TFServing允许对使用TF2训练的模型进行预测。培训结束后,我用自定义签名保存我的模型。假设我的输入是一个形状为[2]的张量,我想在签名中为输入张量中的两个元素添加一些属性:Tensorflow 如何返回TF模型签名和TFS服务中的RaggedSensor?,tensorflow,tensorflow-serving,Tensorflow,Tensorflow Serving,我使用TFServing允许对使用TF2训练的模型进行预测。培训结束后,我用自定义签名保存我的模型。假设我的输入是一个形状为[2]的张量,我想在签名中为输入张量中的两个元素添加一些属性: @tf.function def serve(input): // model inference return { 'input': input, 'attr': tf.constant(['attr1', 'attr2'], ['attr3', 'attr4'])
@tf.function
def serve(input):
// model inference
return {
'input': input,
'attr': tf.constant(['attr1', 'attr2'], ['attr3', 'attr4'])
}
使用上面的签名。当使用单个示例连接TFRESTAPI时,我得到了预期的结果
{
"predictions": [
{
"input": "Example Input 1",
"attr": ["attr1", "attr2"]
},
{
"input": "Example Input 2",
"attr": ["attr3", "attr4"]
}
]
}
现在,如果我想让输入张量中的每个元素都有数量可变的属性,该怎么办
{
"predictions": [
{
"input": "Example Input 1",
"attr": ["attr1", "attr2"]
},
{
"input": "Example Input 2",
"attr": ["attr3"]
}
]
}
保存模型时出现的错误不足为奇:
Argument must be a tensor: [['attr1', 'attr2'], ['attr3']] - got shape [2], but wanted [2, 2].
保存时出错:
ValueError: Got a dictionary containing non-Tensor value tf.RaggedTensor(values=Tensor('StatefulPartitionedCall:0", shape=(None,), dtype=string), row_splits=Tensor("StatefulPartitionCall:1", shape=(3,), dtype=int64)) for key date in the output of the function __inferenece_serve_26051 used to generate a SavedModel signature. Dictionaries outputs for functions used as signatures should have one Tensor output per string key.
对于不同的输入,我有哪些选项可以返回可变长度张量?请查看github中的类似/相关(开放)问题:-
Argument must be a tensor: [['attr1', 'attr2'], ['attr3']] - got shape [2], but wanted [2, 2].
@tf.function
def serve(input):
return {
'input': input,
'attributes': tf.ragged.constant([['attr1', 'attr2'], ['attr3']])
}
ValueError: Got a dictionary containing non-Tensor value tf.RaggedTensor(values=Tensor('StatefulPartitionedCall:0", shape=(None,), dtype=string), row_splits=Tensor("StatefulPartitionCall:1", shape=(3,), dtype=int64)) for key date in the output of the function __inferenece_serve_26051 used to generate a SavedModel signature. Dictionaries outputs for functions used as signatures should have one Tensor output per string key.