1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Pytorch】リスト内にTensorがあってリスト全体のtensorが欲しい時

Last updated at Posted at 2022-05-24

はじめに

記事にするようなことではないかもしれませんが先日このようなことを聞かれたので、せっかくだからと記事にしました。

リスト内にtensorがあるとき

これを

.py
list = [tensor([-0.4514, -1.5478,  0.6651]), tensor([ 0.6520, -0.8339,  1.5253])]

こうしたいとき

.py
x = tensor([[-0.4514, -1.5478,  0.6651],
        [ 0.6520, -0.8339,  1.5253]])

解決策

.py
x = torch.stack(list)

torch.stack()に投げてあげるだけでtensorが入ったリストを勝手に2x3行列にしてくれる。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?