if args.gpu: loss = loss.cpu() optimizer.step() losses += loss # logging if (i + 1) % 500 == 0: log.info('accuracy: %d%%' % (correct_cnt // 5)) correct_cnt = 0 log.info('Train time ' + \ time.strftime("%Hh %Mm %Ss", time.gmtime(time.time() - start_time)) + \ ', ' + 'Mean loss: %0.4f' % (losses.data.numpy()[0] / i)) # save model if args.gpu: model = model.cpu() state_to_save = model.state_dict() if args.epoch % 10 == 0: torch.save( state_to_save, os.path.join(args.model_dir, 'epoch%d.dat' % args.epoch)) accuracy = test(args, model, dataset_test, targets_test, log_test) f_accuracy_train.write('%0.2f\n' % (100 * correct_cnt_sum / targets.shape[0])) f_accuracy_test.write('%0.2f\n' % (100 * accuracy)) f_loss.write('%0.4f\n' % (losses.data.numpy()[0] / targets.shape[0])) if accuracy > max_accuracy: max_accuracy = accuracy overfitting_cnt = 0 torch.save(state_to_save,