all_tasks = range(len(args.dataset))
np.random.seed(1993)

if args.use_cuda:
    net.cuda()
    cudnn.benchmark = True


args.criterion = nn.CrossEntropyLoss()
optimizer = sgd.SGD(filter(lambda p: p.requires_grad, net.parameters()), lr=args.lr, momentum=0.9, weight_decay=args.wd)


print("Start training")
for epoch in range(start_epoch, start_epoch+args.nb_epochs):
    training_tasks = utils_pytorch.adjust_learning_rate_and_learning_taks(optimizer, epoch, args)
    st_time = time.time()
    
    # Training and validation
    train_acc, train_loss = utils_pytorch.train(epoch, train_loaders, training_tasks, net, args, optimizer)
    test_acc, test_loss, best_acc = utils_pytorch.test(epoch,val_loaders, all_tasks, net, best_acc, args, optimizer)
        
    # Record statistics
    for i in range(len(training_tasks)):
        current_task = training_tasks[i]
        results[0:2,epoch,current_task] = [train_loss[i],train_acc[i]]
    for i in all_tasks:
        results[2:4,epoch,i] = [test_loss[i],test_acc[i]]
    np.save(args.svdir+'/results_'+'adapt'+str(args.seed)+args.dropout+args.mode+args.proj+''.join(args.dataset)+'wd3x3_'+str(args.wd3x3)+'_wd1x1_'+str(args.wd1x1)+str(args.wd)+str(args.nb_epochs)+str(args.step1)+str(args.step2),results)
    print('Epoch lasted {0}'.format(time.time()-st_time))

all_tasks = range(len(args.dataset))
np.random.seed(1993)

if args.use_cuda:
    net.cuda()
    cudnn.benchmark = True


args.criterion = nn.CrossEntropyLoss()
optimizer = sgd.SGD(filter(lambda p: p.requires_grad, net.parameters()), lr=args.lr, momentum=0.9, weight_decay=args.wd)


print("Start training")
for epoch in range(start_epoch, start_epoch+args.nb_epochs):
    training_tasks = utils_pytorch.adjust_learning_rate_and_learning_taks(optimizer, epoch, args)
    st_time = time.time()
    
    # Training and validation
    train_acc, train_loss = utils_pytorch.train(epoch, train_loaders, training_tasks, net, args, optimizer)
    test_acc, test_loss, best_acc = utils_pytorch.test(epoch,val_loaders, all_tasks, net, best_acc, args, optimizer)
        
    # Record statistics
    for i in range(len(training_tasks)):
        current_task = training_tasks[i]
        results[0:2,epoch,current_task] = [train_loss[i],train_acc[i]]
    for i in all_tasks:
        results[2:4,epoch,i] = [test_loss[i],test_acc[i]]
    np.save(args.svdir+'/results_'+'adapt'+str(args.seed)+args.dropout+args.mode+args.proj+''.join(args.dataset)+'wd3x3_'+str(args.wd3x3)+'_wd1x1_'+str(args.wd1x1)+str(args.wd)+str(args.nb_epochs)+str(args.step1)+str(args.step2),results)
    print('Epoch lasted {0}'.format(time.time()-st_time))

args.criterion = nn.CrossEntropyLoss()
optimizer = sgd.SGD(filter(lambda p: p.requires_grad, net.parameters()),
                    lr=args.lr,
                    momentum=0.9,
                    weight_decay=args.wd)

#print(net)
print("Start training")
for epoch in range(start_epoch, start_epoch + args.nb_epochs):
    training_tasks = utils_pytorch.adjust_learning_rate_and_learning_taks(
        optimizer, epoch, args)
    st_time = time.time()

    # Training and validation
    train_acc1, train_loss1, train_acc2, train_loss2, train_acc3, train_loss3 = utils_pytorch.train(
        epoch, train_loaders, training_tasks, net, args, optimizer)
    test_acc1, test_loss1, test_acc2, test_loss2, test_acc3, test_loss3 = utils_pytorch.test(
        epoch, val_loaders, all_tasks, net, best_acc, args, optimizer,
        exp_name)

    # Record statistics
    for i in range(len(training_tasks)):
        current_task = training_tasks[i]
        results1[0:2, epoch, current_task] = [train_loss1[i], train_acc1[i]]

    for i in all_tasks:
        results1[2:4, epoch, i] = [test_loss1[i], test_acc1[i]]

    for i in range(len(training_tasks)):
        current_task = training_tasks[i]
        results2[0:2, epoch, current_task] = [train_loss2[i], train_acc2[i]]