[Python]填充mask

  • +[1],最精妙的是一行的填充函数 text_input + [ 0 for x in range(biggest_seq - len(text_input))].
  • 获取biggest_seq也很有意思是len(max(encoded_inputs,key=len).
  • 如果是要限定句子长度那么text_input[:max_len] + [ 0 for x in range(max_len -len(text_input))]
encoded_inputs = [encoders["inputs"].encode(x) + [1] for x in input_txt]
# pad each input so is they are the same length
biggest_seq = len(max(encoded_inputs, key=len))
for i, text_input in enumerate(encoded_inputs):
    encoded_inputs[i] = text_input + [0 for x in range(biggest_seq - len(text_input))]

你可能感兴趣的:([Python]填充mask)