如何无法使MNIST数据集Pytorch C++

发布于 2025-01-26 03:10:36 字数 1260 浏览 4 评论 0 原文

我正在尝试跟随此但是我需要在0到255之间加载MNIST数据集。 ?

我的代码是:

int main(int argc, char* argv[]) {

  const int64_t batch_size = 1;

  // MNIST Dataset
  auto train_dataset = torch::data::datasets::MNIST("./mnist")
      .map(torch::data::transforms::Stack<>());

  // Number of samples in the training set
  auto num_train_samples = train_dataset.size().value();

  cout << "Number of training samples: " << num_train_samples << endl;
  
  // Data loaders
  auto train_loader = torch::data::make_data_loader<torch::data::samplers::RandomSampler>(
  std::move(train_dataset), batch_size);

  for (auto& batch : *train_loader) {
    auto data = batch.data.view({batch_size, -1}).to(device);
    auto record = data[0].clone();
    cout << "Max value: " << max(record) << endl;
    cout << "Min value: " << max(record) << endl;
    break;
  }
}

我下载的MNIST数据集是原来的代码,来自 site

预先感谢您的帮助。

I'm trying to follow this C++ PyTorch example but I need to load the MNIST dataset with its standard values, between 0 and 255. I removed the application of the Normalize() method, but I continue getting value between 0 and 1. What am I doing wrong?

My code is:

int main(int argc, char* argv[]) {

  const int64_t batch_size = 1;

  // MNIST Dataset
  auto train_dataset = torch::data::datasets::MNIST("./mnist")
      .map(torch::data::transforms::Stack<>());

  // Number of samples in the training set
  auto num_train_samples = train_dataset.size().value();

  cout << "Number of training samples: " << num_train_samples << endl;
  
  // Data loaders
  auto train_loader = torch::data::make_data_loader<torch::data::samplers::RandomSampler>(
  std::move(train_dataset), batch_size);

  for (auto& batch : *train_loader) {
    auto data = batch.data.view({batch_size, -1}).to(device);
    auto record = data[0].clone();
    cout << "Max value: " << max(record) << endl;
    cout << "Min value: " << max(record) << endl;
    break;
  }
}

The MNIST dataset I downloaded is the original one, from the site.

Thank you in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2025-02-02 03:10:36

我已经查看了源文件,看来Pytorch MNIST数据集类别执行255的部门以在[0,1]范围内返回张量。因此,您将必须将批次乘以255。

归一化转换不是罪魁祸首。它用于改变数据的平均值和差异

I have looked at the source file and it appears that pytorch mnist dataset class performs the division by 255 to return only tensors within the [0,1] range. So you will have to multiply the batches by 255 yourself.

The normalize transform was not the culprit. It is used to change the mean and variance of your data

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文