(str_list, max_len=64)
| 1853 | return data |
| 1854 | |
| 1855 | def encode_strings(str_list, max_len=64): |
| 1856 | # 把字符串编码为ASCII序列并padding |
| 1857 | encoded = [] |
| 1858 | for s in str_list: |
| 1859 | arr = [ord(c) for c in s[:max_len]] |
| 1860 | arr += [0] * (max_len - len(arr)) |
| 1861 | encoded.append(arr) |
| 1862 | return torch.tensor(encoded, dtype=torch.long) |
| 1863 | |
| 1864 | def decode_tensor(tensor): |
| 1865 | return [''.join(chr(i) for i in row if i > 0) for row in tensor.tolist()] |