Convert text to TextInputs for conditional text planner. Args: question: , space-separated token string. answer: , space-separated token string. length: Length to pad or truncate to. lookup_table: Instance of contrib.lookup.index_table_from_tensor. Returns:
(question, answer, length, lookup_table)
| 295 | |
| 296 | |
| 297 | def build_planner_inputs(question, answer, length, lookup_table): |
| 298 | """Convert text to TextInputs for conditional text planner. |
| 299 | |
| 300 | Args: |
| 301 | question: <string>, space-separated token string. |
| 302 | answer: <string>, space-separated token string. |
| 303 | length: Length to pad or truncate to. |
| 304 | lookup_table: Instance of contrib.lookup.index_table_from_tensor. |
| 305 | |
| 306 | Returns: |
| 307 | Instance of TextInputs. |
| 308 | """ |
| 309 | # Build question. |
| 310 | q_tokens = tf.string_split([question]).values |
| 311 | q_tokens = tf.concat([["[Q]"], q_tokens], axis=0) |
| 312 | q_token_ids = tf.cast(lookup_table.lookup(q_tokens), tf.int32) |
| 313 | q_len = tensor_utils.shape(q_token_ids, 0) |
| 314 | q_positions = tf.range(q_len) |
| 315 | |
| 316 | # Build answer. |
| 317 | a_tokens = tf.string_split([answer]).values |
| 318 | a_tokens = tf.concat([["[A]"], a_tokens], axis=0) |
| 319 | a_token_ids = tf.cast(lookup_table.lookup(a_tokens), tf.int32) |
| 320 | a_len = tensor_utils.shape(a_token_ids, 0) |
| 321 | a_positions = tf.range(a_len) |
| 322 | |
| 323 | # Combine. |
| 324 | token_ids = tf.concat([q_token_ids, a_token_ids], axis=0) |
| 325 | segment_ids = tf.concat([tf.fill([q_len], 2), tf.fill([a_len], 1)], axis=0) |
| 326 | positions = tf.concat([q_positions, a_positions], axis=0) |
| 327 | q_mask = tf.ones_like(q_token_ids) |
| 328 | mask = tf.concat([q_mask, tf.ones_like(a_token_ids)], axis=0) |
| 329 | |
| 330 | # Truncate. |
| 331 | token_ids = token_ids[:length] |
| 332 | segment_ids = segment_ids[:length] |
| 333 | mask = mask[:length] |
| 334 | positions = positions[:length] |
| 335 | |
| 336 | # Pad. |
| 337 | pad = [[0, length - tf.size(token_ids)]] |
| 338 | token_ids = tf.pad(token_ids, pad) |
| 339 | mask = tf.pad(mask, pad) |
| 340 | segment_ids = tf.pad(segment_ids, pad) |
| 341 | positions = tf.pad(positions, pad) |
| 342 | |
| 343 | text_input = TextInputs( |
| 344 | token_ids=tf.ensure_shape(token_ids, [length]), |
| 345 | mask=tf.ensure_shape(mask, [length]), |
| 346 | segment_ids=tf.ensure_shape(segment_ids, [length]), |
| 347 | positions=tf.ensure_shape(positions, [length])) |
| 348 | |
| 349 | return text_input |