def train_wrapper(model): '''Data loader''' train_set = data.DataSet(FLAGS.root_dir, FLAGS.dataset, 'train', FLAGS.batch_size, FLAGS.n_label, data_aug=False, shuffle=True) valid_set = data.DataSet(FLAGS.root_dir, FLAGS.dataset, 'val', FLAGS.batch_size, FLAGS.n_label, data_aug=False, shuffle=False) '''create a tf session for training and validation TODO: to run your model, you may call model.train(), model.save(), model.valid()''' best_accuracy = 0 acc_train = [] acc_valid = [] for step in range(1, FLAGS.max_iteration + 1): if not train_set.has_next_batch(): train_set.init_epoch() batch_x, batch_y = train_set.next_batch() if len(batch_x) == FLAGS.batch_size: loss, acc = model.train(batch_x, batch_y) if step == 1 or step % 10 == 0: print("Step " + str(step) + ", Minibatch Loss= " + \ "{:.4f}".format(loss) + ", Training Accuracy= " + "{:.3f}".format(acc)) if step % FLAGS.valid_iteration == 0: acc_train.append(acc) tot_acc = 0.0 tot_input = 0 while valid_set.has_next_batch(): valid_ims, valid_labels = valid_set.next_batch() loss, acc = model.valid(valid_ims, valid_labels) tot_acc += acc * len(valid_ims) tot_input += len(valid_ims) acc = tot_acc / tot_input valid_set.init_epoch() print("Current Accuracy= " + "{:.3f}".format(acc)) acc_valid.append(acc) if acc > best_accuracy: model.save(step) best_accuracy = acc print("Optimization Finished!")
def test_wrapper(model): '''Test your code.''' test_set = data.DataSet(FLAGS.root_dir, FLAGS.dataset, 'test', FLAGS.batch_size, FLAGS.n_label, data_aug=False, shuffle=False) '''TODO: Your code here.''' model.load() tot_acc = 0.0 tot_input = 0 while test_set.has_next_batch(): test_ims, test_labels = test_set.next_batch() _, acc = model.valid(test_ims, test_labels) tot_acc += acc * len(test_ims) tot_input += len(test_ims) acc = tot_acc / tot_input print("Test Accuracy= " + "{:.3f}".format(acc)) print("Test Finished!")
itr_start_time = time.time() if itr % args.valid_every == 0: valid_loader = torch.utils.data.DataLoader( dataset=valid_set, batch_size=config['batch_size'], shuffle=True, num_workers=1) model.eval() loss_records = {} for descs, apiseqs, desc_lens, api_lens in valid_loader: descs, apiseqs, desc_lens, api_lens = gVar(descs), gVar( apiseqs), gVar(desc_lens), gVar(api_lens) valid_loss = model.valid(descs, desc_lens, apiseqs, api_lens) for loss_name, loss_value in valid_loss.items(): v = loss_records.get(loss_name, []) v.append(loss_value) loss_records[loss_name] = v log = 'Validation ' for loss_name, loss_values in loss_records.items(): log = log + loss_name + ':%.4f ' % (np.mean(loss_values)) if args.visual: tb_writer.add_scalar(loss_name, np.mean(loss_values), itr_global) logger.info(log) itr += 1 itr_global += 1