1
5

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 TensorDatasetについて

Last updated at Posted at 2021-05-08

PyTorchでTensorDatasetを作成するときのメモ。

torch.utils.data(公式ドキュメント)

numpyからTensorDataset

1. インポート

import numpy as np
import torch.utils.data

2. 入力データと教師データを準備

例として入力データと教師データをランダムに生成。

# 入力データ
x = np.random.randn(100, 5)
# 教師データ
t = np.random.randint(2, size=10)

3. torch.tensor型に変換

tensorのデータ型(公式ドキュメント)

x = torch.tensor(x, dtype=torch.float32)
t = torch.tensor(t, dtype=torch.int64)

4. torch.utils.data.TensorDatasetを生成

dataset = torch.utils.data.TensorDataset(x, t)

確認

# 1つ目のデータを確認
print('入力データ:', dataset[0][0])
print('教師データ:', dataset[0][1])
1
5
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
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?