Skip to content

Instantly share code, notes, and snippets.

@hotbaby
Last active April 13, 2021 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hotbaby/1137898f60344ab2521f076c3718795b to your computer and use it in GitHub Desktop.
Save hotbaby/1137898f60344ab2521f076c3718795b to your computer and use it in GitHub Desktop.
Pytorch List转Tensor
l = [[1, 1675, 4028, 8271, 42, 2], [1, 1675, 4028, 8844, 10135, 10757, 9478, 42, 2]]
# maximum rows and columns
max_rows = len(l)
max_cols = max(len(row) for row in l)
# padded rows
padded = [row + [0] * (max_cols - len(row)) for row in l]
# unpack nested list
torch.tensor(list(itertools.chain(padded))).view(max_rows, -1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment