Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带Keras/Tensorflow服务的可变长度输入_Python_Tensorflow_Keras - Fatal编程技术网

Python 带Keras/Tensorflow服务的可变长度输入

Python 带Keras/Tensorflow服务的可变长度输入,python,tensorflow,keras,Python,Tensorflow,Keras,我正在尝试构建一个Keras模型,该模型将导出到TF,用于文本分类。该模型支持可变长度的输入(或者至少,我可以在评估时输入任意长度的输入),但我不知道在为TF服务生成分类签名时如何描述这一点 目前,我正在做: # Create the input tensors serialized_tf_example = tf.placeholder(tf.string, name='tf_example') feature_configs = {'input': tf.VarLenFeatur

我正在尝试构建一个Keras模型,该模型将导出到TF,用于文本分类。该模型支持可变长度的输入(或者至少,我可以在评估时输入任意长度的输入),但我不知道在为TF服务生成分类签名时如何描述这一点

目前,我正在做:

  # Create the input tensors
  serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
  feature_configs = {'input': tf.VarLenFeature(tf.int64)}
  tf_example = tf.parse_example(serialized_tf_example, feature_configs)
  output_tensor = model(tf_example['input'])

  # Create the prediction output tensors
  values, indices = tf.nn.top_k(output_tensor, len(binarizer.classes_))
  table = tf.contrib.lookup.index_to_string_table_from_tensor(tf.constant(binarizer.classes_))
  prediction_classes = table.lookup(tf.to_int64(indices))

  # Generate a classification signature
  signature = tf.saved_model.signature_def_utils.classification_signature_def(
    serialized_tf_example,
    prediction_classes,
    values
  )
但是,这失败了,因为
VarLenFeature
解析为
SparseTensor
,而嵌入层不支持它。但是,如果我将
VarLenFeature
更改为
FixedLenFeature
,那么我必须为tensorflow模型提供固定输入,这在这种情况下是次优的

我在尝试此操作时遇到的实际错误是:

TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor. Contents: SparseTensor(indices=Tensor("ParseExample/ParseExample:0", shape=(?, 2), dtype=int64), values=Tensor("ParseExample/ParseExample:1", shape=(?,), dtype=int64), dense_shape=Tensor("ParseExample/ParseExample:2", shape=(2,), dtype=int64)). Consider casting elements to a supported type.
TypeError:无法将类型的对象转换为Tensor。内容:SparseTensor(索引=张量(“ParseExample/ParseExample:0”,形状=(?,2),数据类型=int64),值=张量(“ParseExample/ParseExample:1”,形状=(?,),数据类型=int64),稠密_形状=张量(“ParseExample/ParseExample:2”,形状=(2,),数据类型=int64))。将铸造元素考虑为支持类型。

我不确定我还能做些什么来通知TF serving在图中支持可变长度输入,或者在使用keras时是否可以这样做。

Ok。我想出了一些可行的办法。通过使用
SparseTensor
并将其转换为具有
tf.sparse.to_densite(sparse)
的张量,我使其工作。虽然我不确定这种方法的缺陷是什么。我暂时不讨论这个问题,看看是否有人能给我们带来一些启发。问题是密集张量比稀疏张量消耗更多的内存,特别是当大多数稀疏张量为空时(正如你在稀疏张量中所期望的那样)。如果批次足够小,这可能不是问题。好的。我想出了一些可行的办法。通过使用
SparseTensor
并将其转换为具有
tf.sparse.to_densite(sparse)
的张量,我使其工作。虽然我不确定这种方法的缺陷是什么。我暂时不讨论这个问题,看看是否有人能给我们带来一些启发。问题是密集张量比稀疏张量消耗更多的内存,特别是当大多数稀疏张量为空时(正如你在稀疏张量中所期望的那样)。如果批次足够小,这可能不是问题。