<p>编辑:我以前的答案在这个答案下面</p>
<p>正确的答案是向特性规范提供<code>default_value</code></p>
<pre><code>peritem_feature_spec = {
'relevance':tf.FixedLenFeature([], tf.float32, default_value=0.0),
'encoded_clust_index':tf.VarLenFeature(tf.float32, default_value=0.0)
}
</code></pre>
<p>我的<strong>旧的</strong>答案如下</p>
<p>所以问题归结为我的特征是如何被填充在tensorflow_排名库中的。它填充了一个列表功能,比如:</p>
^{pr2}$
<p>此方法将空字节追加到张量的末尾。解析器在空张量中寻找<code>feature_name</code>,并返回找不到它。我的解决方法是附加序列化的TFRecord示例原型,而不是一个空字节字符串。我是这样完成的:</p>
<pre><code>def pad_fn():
# Create feature spec for tf.train.Example to append
pad_spec = {}
# Default values are 0 or an empty byte string depending on
# original serialized data type
dtype_map = {tf.float32:tf.train.Feature(
float_list=tf.train.FloatList(value=[0.0])),
tf.int32:tf.train.Feature(
int64_list=tf.train.Int64List(value=[0])),
tf.string:tf.train.Feature(
bytes_list=tf.train.BytesList(
value=[bytes('', encoding='UTF-8')]))}
# Create the feature spec
for key, item in peritem_feature_spec.items():
dtype = item.dtype
pad_spec[key] = dtype_map[dtype]
# Make and serialize example to append
constant_values = tf.train.Example(
features=tf.train.Features(feature=pad_spec))
constant_val_str = constant_values.SerializeToString()
# Add serialized padding to end of list
return tf.pad(
tensor=serialized_list,
paddings=[[0, 0], [0, list_size - cur_list_size]],
constant_values=constant_val_str)
</code></pre>