# loss_gen_rec = opts['betta2']*generative_criterion_rec(reconstructions, inputs) # loss_gen_cl = opts['betta1']*generative_criterion_cl(classification_reconstructed, orig_classes) #print('reconstruction loss: {}'.format(loss_gen_rec.item())) #print('classification loss: {}'.format(loss_gen_cl.item())) loss_gen = loss_gen_cl + loss_gen_rec loss_gen.backward() generative_optimizer.step() generative_optimizer.zero_grad() if idx_stream % 20 == 0: print( 'interval [{}/{}], classification loss: {:.4f}, generative loss {:.4f}' .format(interval, stream_duration, classification_loss.item(), loss_gen.item())) # At the end of each interval we update the code storage codes_storage.transform_data( nn.Sequential(gen_model_old.decoder, gen_model.encoder)) # And store the encoded streaming data + the latest streaming batch in original shape real_buffer.load_from_tensor_dataset(full_original_trainset, stream_classes) codes_storage.load_from_tensor_dataset(full_original_trainset, stream_classes, gen_model.encoder) acc_real = sup_functions.test_classifier(classifier, test_loader) acc_fake = sup_functions.test_classifier_on_generator( classifier, gen_model, test_loader) print('Real test accuracy after {} intervals: {:.8f}'.format( interval + 1, acc_real)) print('Reconstructed test accuracy after {} intervals: {:.8f}'.format( interval + 1, acc_fake)) results['accuracies'].append(acc_real) results['known_classes'].append(len(known_classes))
# Updating the auto-encoder reconstructions = gen_model(inputs) orig_classes = classifier(inputs).detach() classification_reconstructed = classifier(reconstructions) loss_gen_rec = opts['betta2']*generative_criterion_rec(reconstructions, inputs) loss_gen_cl = opts['betta1']*generative_criterion_cl(classification_reconstructed, orig_classes) #print('reconstruction loss: {}'.format(loss_gen_rec.item())) #print('classification loss: {}'.format(loss_gen_cl.item())) loss_gen = loss_gen_cl + loss_gen_rec loss_gen.backward() generative_optimizer.step() generative_optimizer.zero_grad() # At the end of each interval we update the code storage historical_buffer.transform_data(nn.Sequential(gen_model_old.decoder, gen_model.encoder)) # And store the encoded streaming data + the latest streaming batch in original shape for X_real, Y_real in stream_loader: historical_buffer.add_batch(gen_model.encoder(X_real.cuda()).data, stream_class[0]) real_buffer.add_batch(X_real.cuda(), stream_class[0]) acc_real = test_classifier(classifier, test_loader) acc_fake = test_classifier_on_generator(classifier, gen_model, test_loader) print('Real test accuracy after {} intervals: {:.8f}'.format(interval+1, acc_real)) print('Reconstructed test accuracy after {} intervals: {:.8f}'.format(interval+1, acc_fake)) #if acc > max_accuracy: #max_accuracy = acc #torch.save(classifier.state_dict(), './pretrained_models/classifier_6_classes_mixed_data.pth')
generative_criterion_cl = nn.MSELoss() generative_criterion_cl.cuda() generative_criterion_rec = nn.MSELoss() generative_criterion_rec.cuda() acc = test_classifier(classifier, test_loader) acc_rec = test_classifier_on_generator(classifier, gen_model, test_loader) print('Classification accuracy prior to training: {:.4f}'.format(acc)) print('Test accuracy on reconstructed testset: {}'.format(acc_rec)) accuracies = [] max_accuracy = 0 for epoch in range(training_epochs): # loop over the dataset multiple times # if epoch % reconstruct_every == 0 and epoch>=1: print('Transforming data with the latest autoencoder') data_buffer.transform_data(gen_model) if real_batches_to_add>0: data_buffer.add_batches_from_dataset(hist_data, list(range(30)), real_batches_to_add) data_buffer.make_tensor_dataset() trainset = data_buffer.tensor_dataset train_loader = DataLoader(trainset, batch_size=10*opts['batch_size'], shuffle=True, drop_last=True) for idx, (train_X, train_Y, _) in enumerate(train_loader): inputs = train_X.cuda() # ===================forward===================== #reconstructions = gen_model(inputs) #orig_classes = classifier(inputs) #classification_reconstructed = classifier(reconstructions) #loss_gen = generative_criterion(classification_reconstructed, orig_classes) #loss_gen.backward() #generative_optimizer.step() #generative_optimizer.zero_grad()