Last active
April 13, 2021 03:34
-
-
Save hotbaby/1137898f60344ab2521f076c3718795b to your computer and use it in GitHub Desktop.
Pytorch List转Tensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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