Beispiel #1
0
def fit_hyperparameters(file,
                        train,
                        train_labels,
                        cuda,
                        gpu,
                        save_memory=False):
    """
    Creates a classifier from the given set of hyperparameters in the input
    file, fits it and return it.

    @param file Path of a file containing a set of hyperparemeters.
    @param train Training set.
    @param train_labels Labels for the training set.
    @param cuda If True, enables computations on the GPU.
    @param gpu GPU to use if CUDA is enabled.
    @param save_memory If True, save GPU memory by propagating gradients after
           each loss term, instead of doing it after computing the whole loss.
    """
    classifier = scikit_wrappers.CausalCNNEncoderClassifier()

    # Loads a given set of hyperparameters and fits a model with those
    hf = open(os.path.join(file), 'r')
    params = json.load(hf)
    hf.close()
    # Check the number of input channels
    params['in_channels'] = numpy.shape(train)[1]
    params['cuda'] = cuda
    params['gpu'] = gpu
    classifier.set_params(**params)
    return classifier.fit(train,
                          train_labels,
                          save_memory=save_memory,
                          verbose=True)
Beispiel #2
0
def run(dataset, gpu, log_name, model):

    path = '/localscratch/shared/ts/UCRArchive_2018'
    save_path = '/localscratch/shared/ts/exp'
    cuda = True
    hyper = 'default_hyperparameters.json'
    load = False
    fit_classifier = False

    train, train_labels, test, test_labels = load_UCR_dataset(path, dataset)

    if not load and not fit_classifier:
        classifier = fit_hyperparameters(hyper, train, train_labels, cuda, gpu, model)
    else:
        classifier = scikit_wrappers.CausalCNNEncoderClassifier(model=model)
        hf = open(os.path.join(save_path, dataset + '_hyperparameters.json'), 'r')
        hp_dict = json.load(hf)
        hf.close()
        hp_dict['cuda'] = cuda
        hp_dict['gpu'] = gpu
        classifier.set_params(**hp_dict)
        classifier.load(os.path.join(save_path, dataset))

    if not load:
        if fit_classifier:
            classifier.fit_classifier(classifier.encode(train), train_labels)
        classifier.save(os.path.join(save_path, dataset))
        with open(os.path.join(save_path, dataset + '_hyperparameters.json'), 'w') as fp:
            json.dump(classifier.get_params(), fp)

    print_and_log("Test accuracy: " + str(classifier.score(test, test_labels)), log_name)
Beispiel #3
0
def load_classifier(save_path, dataset, cuda, gpu):
    """
    Loads and returns classifier from the given parameters.

    @param save_path Path where the model is located.
    @param dataset Name of the dataset.
    @param cuda If True, enables computations on the GPU.
    @param gpu GPU to use if CUDA is enabled.
    """
    classifier = scikit_wrappers.CausalCNNEncoderClassifier()
    hf = open(os.path.join(save_path, dataset + '_hyperparameters.json'), 'r')
    hp_dict = json.load(hf)
    hf.close()
    hp_dict['cuda'] = cuda
    hp_dict['gpu'] = gpu
    classifier.set_params(**hp_dict)
    classifier.load(os.path.join(save_path, dataset))
    return classifier
Beispiel #4
0
                        help='activate to use CUDA')
    parser.add_argument('--gpu',
                        type=int,
                        default=0,
                        metavar='GPU',
                        help='index of GPU used for computations (default: 0)')
    return parser.parse_args()


if __name__ == '__main__':
    args = parse_arguments()
    if args.cuda and not torch.cuda.is_available():
        print("CUDA is not available, proceeding without it...")
        args.cuda = False

    classifier = scikit_wrappers.CausalCNNEncoderClassifier()

    hf = open(
        os.path.join(args.save_path, args.dataset + '_hyperparameters.json'),
        'r')
    hp_dict = json.load(hf)
    hf.close()
    hp_dict['cuda'] = args.cuda
    hp_dict['gpu'] = args.gpu
    classifier.set_params(**hp_dict)
    classifier.load(os.path.join(args.save_path, args.dataset))

    print("Classification tasks...")

    # List of folders / datasets in the given path
    datasets = [x[0][len(args.path) + 1:] for x in os.walk(args.path)][1:]