Neural network initialization

Let's start by generating the training data. The following code shows how we can do this:

 torch::DeviceType device = torch::cuda::is_available()                                 ? torch::DeviceType::CUDA                                 : torch::DeviceType::CPU;  std::random_device rd; std::mt19937 re(rd()); std::uniform_real_distribution<float> dist(-0.1f, 0.1f);  // generate data size_t n = 1000; torch::Tensor x; torch::Tensor y; {     std::vector<float> values(n);     std::iota(values.begin(), values.end(), 0);     std::shuffle(values.begin(), values.end(), re);          std::vector<torch::Tensor> x_vec(n);     std::vector<torch::Tensor> y_vec(n);     for (size_t i = 0; i < n; ++i) {         x_vec[i] = torch::tensor(         values[i],         torch::dtype(torch::kFloat).device(device).requires_grad(false));          y_vec[i] = ...

Get Hands-On Machine Learning with C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.