我正在使用LSTM + CRF进行实体识别任务。我使用tensorflow estimator API构建,下面是用于训练和评估的input_fn代码。
原始输入功能首先通过tf.train.SequenceExample进行序列化。输入功能具有动态填充,可用于动态DSTM的每个批次。 char_inputs,seg_inputs具有预期的形状[batch_size,max_length]
input_features = {
CHAR_INPUT_TENSOR_NAME: tf.FixedLenSequenceFeature([], tf.int64, allow_missing=True),
SEG_INPUT_TENSOR_NAME: tf.FixedLenSequenceFeature([], tf.int64, allow_missing=True)
}
context_features = {
"length": tf.FixedLenFeature([], dtype=tf.int64)
}
def training_input_fn(tfrecord_file):
# read from TFRecord, serialized with SequenceExamples
file_queue = tf.train.string_input_producer([tfrecord_file])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(file_queue)
sequence_features = {
**input_features,
"labels": tf.FixedLenSequenceFeature([], tf.int64)
}
# parse examples
context_parsed, sequence_parsed = tf.parse_single_sequence_example(
serialized=serialized_example,
context_features=context_features,
sequence_features=sequence_features
)
t_char = sequence_parsed[CHAR_INPUT_TENSOR_NAME]
t_seg = sequence_parsed[SEG_INPUT_TENSOR_NAME]
t_label = sequence_parsed["labels"]
input_tensors = [t_char, t_seg, t_label]
# batch and dynamic padding
char_inputs, seg_inputs, labels = tf.train.batch(
input_tensors,
batch_size=batch_size,
dynamic_pad=True,
capacity=1000 + 3 * batch_size,
allow_smaller_final_batch=True)
return {
CHAR_INPUT_TENSOR_NAME: char_inputs,
SEG_INPUT_TENSOR_NAME: seg_inputs,
}, labels
在估算器model_fn
中,计算动态长度:
def model_fn(features, labels, mode, params):
char_inputs = features[CHAR_INPUT_TENSOR_NAME]
used = tf.sign(tf.abs(char_inputs))
length = tf.reduce_sum(used, reduction_indices=1)
lengths = tf.cast(length, tf.int32)
要导出和预测,请输入我的serve_input_receiver_fn:
def serving_input_receiver_fn():
serialized_example = tf.placeholder(dtype=tf.string, shape=None,
name='input_example_tensor')
receiver_tensors = { RECIEVER_INPUT: serialized_example }
_, features = tf.parse_single_sequence_example(
serialized=serialized_example,
sequence_features=input_features
)
return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)
问题:
当我使用serving_input_receiver_fn
构建估算器并导出模型时,在预测阶段,输入特征(char_inputs,seg_inputs)似乎具有[1]的形状。
length = tf.reduce_sum(used, reduction_indices=1)
ValueError: Invalid reduction dimension 1 for input with 1 dimensions. for 'Sum' (op: 'Sum') with input shapes: [?], [] and with computed input tensors: input[1] = <1>.