Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion example/mnist/dataset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>())),
Expand Down
10 changes: 5 additions & 5 deletions example/mnist/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ int main(int argc, char *argv[]) {
auto test_dataset = std::make_shared<MNISTDataset>(FLAGS_dataset, false);
DataLoader test_dataloader(test_dataset, FLAGS_bs);

auto network = MNIST();
auto network = std::make_shared<MNIST>();
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;
Expand All @@ -80,7 +80,7 @@ int main(int argc, char *argv[]) {
auto new_image = std::make_shared<Tensor>(image->To(device));
auto new_label = std::make_shared<Tensor>(label->To(device));

auto outputs = network({new_image});
auto outputs = (*network)({new_image});
optimizer.ZeroGrad();

auto loss = loss_fn.Forward({outputs[0], new_label});
Expand Down Expand Up @@ -122,7 +122,7 @@ int main(int argc, char *argv[]) {
auto new_label = std::make_shared<Tensor>(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);
Expand Down