'How to load the MNIST dataset from the datasets in torch C++

I'm new to torch and c++ i want to create a basic simple network that does hand digit recognition this is my Net struct.

struct Net:torch::nn::Module {
    Net() {
        fc1 = register_module("fc1", torch::nn::Linear(784, 64));
        fc2 = register_module("fc2", torch::nn::Linear(64, 32));
        fc3 = register_module("fc3", torch::nn::Linear(32, 10));
    }
    torch::Tensor forward(torch::Tensor x, bool train = true) {
        x = torch::relu(fc1->forward(x));
        x = torch::dropout(x, 0.5, train);
        x = torch::relu(fc2->forward(x));
        x = torch::log_softmax(fc3->forward(x), 1);
        return x;
    }
    torch::nn::Linear fc1{ nullptr }, fc2{ nullptr }, fc3{ nullptr };
};

Now when i try to create data loader and dataset as follows:

int main(){
    Net net();
    int BATCH_SIZE = 64;
    int N_EPOCHS = 3;

    auto trainset = torch::data::datasets::MNIST("./data").map(
        torch::data::transforms::Stack<>()
    );
    auto trainloader = torch::data::make_data_loader(
        move(trainset), BATCH_SIZE
    );

    while (true) {
        for (auto& batch : *trainloader) {
            cout << batch.target << endl; break;
        }
        break;
    }
    return 0;
}

The program is fails to run in visual studio code 2019 with no issues but the following message:

Unhandled exception at 0x00007FF8134E4F69 in 01_BASIC_NEURAL_NETWORK.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x000000447FAFF0E0.

What may be possibly my problem here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source