diff --git a/example/mnist/dataset.cc b/example/mnist/dataset.cc index 5cbe7682..42c42b88 100644 --- a/example/mnist/dataset.cc +++ b/example/mnist/dataset.cc @@ -99,7 +99,9 @@ MNISTDataset::MNISTDataset(const std::string &dataset, bool train) train ? kTrainPrefix : kTestPrefix))), image_dims_(image_file_.dims.begin() + 1, image_file_.dims.end()), label_dims_(label_file_.dims.begin() + 1, label_file_.dims.end()), - image_size_in_bytes_(kSN3TypeToSize.at(image_file_.type) * + // The constructor body converts images from uint8 to float32, so the + // per-sample byte stride must use the float32 element size. + image_size_in_bytes_(sizeof(float) * std::accumulate(image_dims_.begin(), image_dims_.end(), 1, std::multiplies())), diff --git a/example/mnist/main.cc b/example/mnist/main.cc index 8fc3b78e..3175f07f 100644 --- a/example/mnist/main.cc +++ b/example/mnist/main.cc @@ -59,16 +59,16 @@ int main(int argc, char *argv[]) { auto test_dataset = std::make_shared(FLAGS_dataset, false); DataLoader test_dataloader(test_dataset, FLAGS_bs); - auto network = MNIST(); + auto network = std::make_shared(); Device device = FLAGS_device == kDeviceCPU ? Device() : Device(Device::DeviceType::kCUDA, 0); Device cpu_device = Device(); - network.To(device); + network->To(device); auto loss_fn = nn::CrossEntropyLoss(); loss_fn.To(device); - auto optimizer = optimizers::SGD(network.Parameters(), FLAGS_lr); + auto optimizer = optimizers::SGD(network->Parameters(), FLAGS_lr); for (int epoch = 0; epoch < FLAGS_num_epoch; ++epoch) { int train_idx = 0; @@ -80,7 +80,7 @@ int main(int argc, char *argv[]) { auto new_image = std::make_shared(image->To(device)); auto new_label = std::make_shared(label->To(device)); - auto outputs = network({new_image}); + auto outputs = (*network)({new_image}); optimizer.ZeroGrad(); auto loss = loss_fn.Forward({outputs[0], new_label}); @@ -122,7 +122,7 @@ int main(int argc, char *argv[]) { auto new_label = std::make_shared(label->To(device)); auto label_cpu = label->To(cpu_device); - auto outputs = network({new_image}); + auto outputs = (*network)({new_image}); auto output_cpu = outputs[0]->To(cpu_device); auto loss = loss_fn.Forward({outputs[0], new_label}); auto loss_cpu = loss[0]->To(cpu_device);