Beispiel #1
0
def search_hyperparams(train_data, train_labels, val_data, val_labels,
                       learning_rates, momentums, batch_sizes, iterations,
                       model_class, init_param_values=None, use_bn=False):
    """
    Question 8: Evaluate various setups of hyperparameter and find the best one.

    Args:
        learning rate, momentums, batch_sizes are lists of the same length.
        The N-th elements from the lists form the N-th hyperparameter tuple.

    Returns:
        A model that corresponds to the best hyperparameter tuple, and the index
            of the best hyperparameter tuple

    Your implementation will train a model using each hyperparameter tuple and
    compares their accuracy on validation set to pick the best one.

    You must use MinibatchStochasticGradientDescentSolver.

    Useful methods:
    solver.solve(...)
    model.accuracy(...)
    """
    # Check length of inputs all the same
    hyperparams = [learning_rates, momentums, batch_sizes]
    for hyperparam in hyperparams:
        if len(hyperparam) != len(hyperparams[0]):
            raise ValueError('The hyperparameter lists need to be equal in length')
    hyperparams = zip(*hyperparams)

    # Initialize the models
    models = []
    for learning_rate, momentum, batch_size in hyperparams:
        try:
            model = model_class(use_batchnorm=use_bn)
        except:
            model = model_class()
        if init_param_values is None:
            init_param_values = model.get_param_values()
        else:
            model.set_param_values(init_param_values)
        models.append(model)

    val_accuracies = []
    count = 1
    # Loop over hyperparams
    for model, (learning_rate, momentum, batch_size) in zip(models, hyperparams):
        "*** YOUR CODE HERE ***"
	
	miniBatchSolver = solvers.MinibatchStochasticGradientDescentSolver(learning_rate,iterations,batch_size,momentum)
	miniBatchSolver.solve(train_data,train_labels,val_data,val_labels,model)
	accuracy = model.accuracy(val_data,val_labels)
	val_accuracies.append((accuracy,model,count))	
	count = count+1
        #util.raiseNotDefined()
    if(val_accuracies):
    	best_model = max(val_accuracies)
    return best_model[1], best_model[2] #best_hyperparams
Beispiel #2
0
def search_hyperparams(train_data, train_labels, val_data, val_labels,
                       learning_rates, momentums, batch_sizes, iterations,
                       model_class, init_param_values=None, use_bn=False):
    """
    Question 8: Evaluate various setups of hyperparameter and find the best one.

    Args:
        learning rate, momentums, batch_sizes are lists of the same length.
        The N-th elements from the lists form the N-th hyperparameter tuple.

    Returns:
        A model that corresponds to the best hyperparameter tuple, and the index
            of the best hyperparameter tuple

    Your implementation will train a model using each hyperparameter tuple and
    compares their accuracy on validation set to pick the best one.

    You must use MinibatchStochasticGradientDescentSolver.

    Useful methods:
    solver.solve(...)
    model.accuracy(...)
    """
    # Check length of inputs all the same
    hyperparams = [learning_rates, momentums, batch_sizes]
    for hyperparam in hyperparams:
        if len(hyperparam) != len(hyperparams[0]):
            raise ValueError('The hyperparameter lists need to be equal in length')
    hyperparams = zip(*hyperparams)

    # Initialize the models
    models = []
    for learning_rate, momentum, batch_size in hyperparams:
        try:
            model = model_class(use_batchnorm=use_bn)
        except:
            model = model_class()
        if init_param_values is None:
            init_param_values = model.get_param_values()
        else:
            model.set_param_values(init_param_values)
        models.append(model)

    val_accuracies = []
    # Loop over hyperparams
    #Initialize the best model as None and best accuracy as  -ve infinity
    best_model, best_hyperparams, optimum_accuracy, index = None, None, -float("inf"), 1
    for model, (learning_rate, momentum, batch_size) in zip(models, hyperparams):
        solver = solvers.MinibatchStochasticGradientDescentSolver(learning_rate, iterations, batch_size, momentum)
        train_losses, val_losses = solver.solve(train_data, train_labels, val_data, val_labels, model) #calculate the training and validation loss
        accuracy = model.accuracy(val_data, val_labels) #calculate the accuracy
        if accuracy > optimum_accuracy: #If the new calculated accuracy exceeds the previous accuracy update the accuracy and the  best_hyperparameters
            optimum_accuracy = accuracy
            best_model = model
            best_hyperparams = index
        index = index + 1 #Update the index
    return best_model, best_hyperparams # Return the best model and the best hyperparameter.
def search_hyperparams(train_data,
                       train_labels,
                       val_data,
                       val_labels,
                       learning_rates,
                       momentums,
                       batch_sizes,
                       iterations,
                       model_class,
                       init_param_values=None,
                       use_bn=False):
    """
    Question 8: Evaluate various setups of hyperparameter and find the best one.

    Args:
        learning rate, momentums, batch_sizes are lists of the same length.
        The N-th elements from the lists form the N-th hyperparameter tuple.

    Returns:
        A model that corresponds to the best hyperparameter tuple, and the index
            of the best hyperparameter tuple

    Your implementation will train a model using each hyperparameter tuple and
    compares their accuracy on validation set to pick the best one.

    You must use MinibatchStochasticGradientDescentSolver.

    Useful methods:
    solver.solve(...)
    model.accuracy(...)
    """
    # Check length of inputs all the same
    hyperparams = [learning_rates, momentums, batch_sizes]
    for hyperparam in hyperparams:
        if len(hyperparam) != len(hyperparams[0]):
            raise ValueError(
                'The hyperparameter lists need to be equal in length')
    hyperparams = zip(*hyperparams)

    # Initialize the models
    models = []
    for learning_rate, momentum, batch_size in hyperparams:
        try:
            model = model_class(use_batchnorm=use_bn)
        except:
            model = model_class()
        if init_param_values is None:
            init_param_values = model.get_param_values()
        else:
            model.set_param_values(init_param_values)
        models.append(model)

    val_accuracies = []
    biggest = 0
    best_model = models[2]
    best_hyperparams = hyperparams[2]  # it is an index
    i = 0
    # Loop over hyperparams

    for model, (learning_rate, momentum,
                batch_size) in zip(models, hyperparams):
        "*** YOUR CODE HERE ***"
        #util.raiseNotDefined()
        #print learning_rate
        #print momentum
        #print batch_size
        solver = solvers.MinibatchStochasticGradientDescentSolver(
            learning_rate,
            iterations,
            batch_size,
            momentum=0,
            weight_decay=1e-3,
            shuffle=None,
            loss_function=None,
            plot=0)
        x, y = solver.solve(train_data,
                            train_labels,
                            val_data,
                            val_labels,
                            model,
                            callback=None)
        print "done"
        a = model.accuracy(val_data, y)
        #print "no problem"
        print a
        #if i==1000:
        if a > biggest:
            biggest = a
            print biggest
            best_model = model  # it is an model
            best_hyperparams = (learning_rate, momentum, batch_size)
        i = i + 1

    return best_model, best_hyperparams
Beispiel #4
0
def search_hyperparams(train_data, train_labels, val_data, val_labels,
                       learning_rates, momentums, batch_sizes, iterations,
                       model_class, init_param_values=None, use_bn=False):
    """
    Question 8: Evaluate various setups of hyperparameter and find the best one.

    Args:
        learning rate, momentums, batch_sizes are lists of the same length.
        The N-th elements from the lists form the N-th hyperparameter tuple.

    Returns:
        A model that corresponds to the best hyperparameter tuple, and the index
            of the best hyperparameter tuple

    Your implementation will train a model using each hyperparameter tuple and
    compares their accuracy on validation set to pick the best one.

    You must use MinibatchStochasticGradientDescentSolver.

    Useful methods:
    solver.solve(...)
    model.accuracy(...)
    """
    # Check length of inputs all the same
    hyperparams = [learning_rates, momentums, batch_sizes]
    for hyperparam in hyperparams:
        if len(hyperparam) != len(hyperparams[0]):
            raise ValueError('The hyperparameter lists need to be equal in length')
    hyperparams = zip(*hyperparams)

    # Initialize the models
    models = []
    for learning_rate, momentum, batch_size in hyperparams:
        try:
            model = model_class(use_batchnorm=use_bn)
        except:
            model = model_class()
        if init_param_values is None:
            init_param_values = model.get_param_values()
        else:
            model.set_param_values(init_param_values)
        models.append(model)

    val_accuracies = []
    best_model = None
    best_hyperparams = None
    bestsf = -9999.0
    i = 1

    # Loop over hyperparams
    for model, (learning_rate, momentum, batch_size) in zip(models, hyperparams):
        "*** YOUR CODE HERE ***"
        solver = solvers.MinibatchStochasticGradientDescentSolver(learning_rate, iterations,batch_size,momentum)#
        # define the solver with this iteration's hyper parameters
        solver.solve(train_data,train_labels,val_data,val_labels,model) # train this
        # iteration's model with the solver
        accuracy = model.accuracy(val_data, val_labels) # calculate the model accuracy
        if accuracy > bestsf: # easier to keep track of best model,hyperparams so far in each iteration than
            # appending to val_accuracies and then calculating the best in there
            bestsf = accuracy
            best_model = model
            best_hyperparams = i
        i += 1

    return best_model, best_hyperparams
Beispiel #5
0
def search_hyperparams(train_data,
                       train_labels,
                       val_data,
                       val_labels,
                       learning_rates,
                       momentums,
                       batch_sizes,
                       iterations,
                       model_class,
                       init_param_values=None,
                       use_bn=False):
    """
    Question 8: Evaluate various setups of hyperparameter and find the best one.

    Args:
        learning rate, momentums, batch_sizes are lists of the same length.
        The N-th elements from the lists form the N-th hyperparameter tuple.

    Returns:
        A model that corresponds to the best hyperparameter tuple, and the index
            of the best hyperparameter tuple

    Your implementation will train a model using each hyperparameter tuple and
    compares their accuracy on validation set to pick the best one.

    You must use MinibatchStochasticGradientDescentSolver.

    Useful methods:
    solver.solve(...)
    model.accuracy(...)
    """
    # Check length of inputs all the same
    hyperparams = [learning_rates, momentums, batch_sizes]
    for hyperparam in hyperparams:
        if len(hyperparam) != len(hyperparams[0]):
            raise ValueError(
                'The hyperparameter lists need to be equal in length')
    hyperparams = zip(*hyperparams)

    # Initialize the models
    models = []
    for learning_rate, momentum, batch_size in hyperparams:
        try:
            model = model_class(use_batchnorm=use_bn)
        except:
            model = model_class()
        if init_param_values is None:
            init_param_values = model.get_param_values()
        else:
            model.set_param_values(init_param_values)
        models.append(model)

    val_accuracies = []
    # Loop over hyperparams
    for model, (learning_rate, momentum,
                batch_size) in zip(models, hyperparams):
        # init a new solver for each iterated hyperparam/model
        solver = solvers.MinibatchStochasticGradientDescentSolver(
            learning_rate, iterations, batch_size, momentum)
        # as given by solve, returns train/val losses
        solver.solve(train_data, train_labels, val_data, val_labels, model)
        # append the accuracies to the list
        val_accuracies.append(model.accuracy(val_data, val_labels))

    # can use np.argmax to find index of the max accuracy
    index = np.argmax(val_accuracies)
    return models[index], hyperparams[index]
def search_hyperparams(train_data,
                       train_labels,
                       val_data,
                       val_labels,
                       learning_rates,
                       momentums,
                       batch_sizes,
                       iterations,
                       model_class,
                       init_param_values=None,
                       use_bn=False):
    """
    Question 8: Evaluate various setups of hyperparameter and find the best one.

    Args:
        learning rate, momentums, batch_sizes are lists of the same length.
        The N-th elements from the lists form the N-th hyperparameter tuple.

    Returns:
        A model that corresponds to the best hyperparameter tuple, and the index
            of the best hyperparameter tuple

    Your implementation will train a model using each hyperparameter tuple and
    compares their accuracy on validation set to pick the best one.

    You must use MinibatchStochasticGradientDescentSolver.

    Useful methods:
    solver.solve(...)
    model.accuracy(...)
    """
    # Check length of inputs all the same
    hyperparams = [learning_rates, momentums, batch_sizes]
    for hyperparam in hyperparams:
        if len(hyperparam) != len(hyperparams[0]):
            raise ValueError(
                'The hyperparameter lists need to be equal in length')
    hyperparams = zip(*hyperparams)
    # python *+VARIABLE: http://stackoverflow.com/questions/11315010/what-do-and-before-a-variable-name-mean-in-a-function-signature

    # Initialize the models
    models = []
    for learning_rate, momentum, batch_size in hyperparams:
        try:
            model = model_class(use_batchnorm=use_bn)
        except:
            model = model_class()
        if init_param_values is None:
            init_param_values = model.get_param_values()
        else:
            model.set_param_values(init_param_values)
        models.append(model)

    accuracies = []
    # Loop over hyperparams
    best = 0
    best_model = None
    count = -1
    for model, (learning_rate, momentum,
                batch_size) in zip(models, hyperparams):
        count += 1
        Sol = solvers.MinibatchStochasticGradientDescentSolver(
            learning_rate, iterations, batch_size, momentum)
        Sol.solve(train_data, train_labels, val_data, val_labels, model)
        if model.accuracy(val_data, val_labels) > best:
            best = model.accuracy(val_data, val_labels)
            best_model = model
            best_hyperparams = count
    return best_model, best_hyperparams