NTMC-Community / MatchZoo

Facilitating the design, comparison and sharing of deep text matching models.
Apache License 2.0
3.84k stars 898 forks source link

DynamicPooling dpool_index can be refined by tf.image.crop_and_resize #671

Closed levyfan closed 5 years ago

levyfan commented 5 years ago

Is your feature request related to a problem? Please describe.

DynamicPoolingLayer is implemented by dpool_index, and dpool_index should be feed manually by python code, which is quite inconvenient.

Describe the solution you'd like

Indeed, DynamicPoolingLayer is similar to ROI Pooling in object detection in CV. tf.image.crop_and_resize works well on this scenario (stable and fast). The boxes coordinates can be easily calculated by q_len, d_len from masking out [PAD]s. Thus, we are not required to feed additional dpool_index any more.

Describe alternatives you've considered

# q_len, d_len: the actual length of query and doc, normalized
# q_max_len, d_max_len: the max length of query and doc, given by model definition

output = tf.image.crop_and_resize(
    image = input_similarity_matrix,
    boxes =  tf.stack([tf.zeros((N,)), tf.zeros((N,)), q_len, d_len], axis = -1),
    box_ind = tf.range(0, limit=N),
    crop_size = (q_max_len, d_max_len)
)

Additional Information

https://www.tensorflow.org/api_docs/python/tf/image/crop_and_resize

pl8787 commented 5 years ago

It seems a good idea.

pl8787 commented 5 years ago

@levyfan Please check this PR. There are some errors when I use small batch size in evaluation phase.