Ejemplo n.º 1
0
def main(args=defaults):
    dataset = np.loadtxt("FM_dataset.dat")
    #######################################################################
    #                       ** START OF YOUR CODE **
    #######################################################################
    np.random.shuffle(dataset)

    trainRatio = 0.8
    testRatio = 0.1
    numEpochs = 500
    batchsize = 64

    # shuffle the dataset prior
    np.random.shuffle(dataset)

    # preprocess data
    prep = Preprocessor(dataset)
    dataset = prep.apply(dataset)

    # retrieve X and Y columns
    X, Y = dataset[:, 0:3], dataset[:, 3:6]

    # create loaders
    trainloader, validloader, testloader = helpers.loadTrainingData(
        X, Y, trainRatio, testRatio, batchsize)

    # initiate the model
    model = Net(3, 3, args['l1'], args['l2'], args['l3'], args['l4'])

    # load hyperparameters
    loss_function = nn.MSELoss()
    optimizer = optim.Adam(model.parameters())

    # train the model
    helpers.trainModel(model, optimizer, loss_function, numEpochs, trainloader,
                       validloader)
    # evaluate the model
    helpers.testModel(model, loss_function, numEpochs, testloader)

    torch.save(model.state_dict(), 'best_model_reg.pth')

    x_store, y_store = [], []
    i = 0
    for x, y in testloader:
        if i < 1:
            x_store = np.array(x)
            y_store = np.array(y)
        else:
            np.concatenate((x_store, np.array(x)), axis=0)
            np.concatenate((y_store, np.array(y)), axis=0)
        i = 1

    evaluate_architecture(model, torch.Tensor(x_store), torch.Tensor(y_store),
                          prep)
    #######################################################################
    #                       ** END OF YOUR CODE **
    #######################################################################
    # data is normalised in this function
    illustrate_results_FM(model, prep)
Ejemplo n.º 2
0
def blackbox(l1, l2, l3, l4, lr):
    """
    Compressed runthrough of training the model.
    This is used in the BayesianOptimisation function
    in order to deduce the best hyperparameters during
    its search. You shouldn't use this to call the
    model. Use run() instead.
    """
    # silly optimiser will make floats of these.
    l1, l2, l3, l4 = int(l1), int(l2), int(l3), int(l4)
    # load dataset file
    dataset = np.loadtxt("FM_dataset.dat")
    np.random.shuffle(dataset)
    # setup base parameters
    trainRatio = 0.8
    testRatio = 0.1
    numEpochs = 20
    batchsize = 64
    X, Y = dataset[:, 0:3], dataset[:, 3:]
    xDim, yDim = X.shape[-1], Y.shape[-1]
    trainloader, validloader, _ = helpers.loadTrainingData(
        X, Y, trainRatio, testRatio, batchsize)
    # initiate the model
    model = Net(xDim, yDim, l1, l2, l3, l4)
    # set up hyperparameters
    loss_function = nn.MSELoss()
    optimizer = optim.Adam(model.parameters(), lr=lr)
    # train the model
    return -helpers.trainModel(model,
                               optimizer,
                               loss_function,
                               numEpochs,
                               trainloader,
                               validloader,
                               verbose=False)
Ejemplo n.º 3
0
def run(verbose=True, showBot=False, args=defaults):
    dataset = np.loadtxt("FM_dataset.dat")
    #######################################################################
    #                       ** START OF YOUR CODE **
    #######################################################################
    np.random.shuffle(dataset)

    trainRatio = 0.8
    testRatio = 0.1
    numEpochs = args['epochs']
    batchsize = 64

    # split dataset
    X, Y = dataset[:, 0:3], dataset[:, 3:]

    # get input and output dimensions
    xDim, yDim = X.shape[-1], Y.shape[-1]

    # create dataloaders
    trainloader, validloader, testloader = helpers.loadTrainingData(
        X, Y, trainRatio, testRatio, batchsize)

    # initiate the model
    model = Net(xDim, yDim, args['l1'], args['l2'], args['l3'], args['l4'])

    # set up hyperparameters
    loss_function = nn.MSELoss()
    optimizer = optim.Adam(model.parameters(), lr=args['lr'])

    # train the model
    val_loss = helpers.trainModel(model,
                                  optimizer,
                                  loss_function,
                                  numEpochs,
                                  trainloader,
                                  validloader,
                                  verbose=verbose)

    if args['save']:
        torch.save(model, args['filepath'])

    # test the model
    helpers.testModel(model,
                      loss_function,
                      numEpochs,
                      testloader,
                      checkAccuracy=True,
                      verbose=verbose)

    #######################################################################
    #                       ** END OF YOUR CODE **
    #######################################################################
    #if showBot:
    #illustrate_results_ROI(model)

    return val_loss
                                          batch_size=64,
                                          shuffle=True)
testloader = torch.utils.data.DataLoader(test_data, batch_size=32)

# Load model
model = helpers.getModel(args.arch, input_size, output_size, args.hidden_units,
                         train_data.class_to_idx)
model.to(device)

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.classifier.parameters(),
                      lr=args.learning_rate,
                      momentum=0.9)
scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)

model = helpers.trainModel(device, args.epochs, model, train_data, trainloader,
                           testloader, optimizer, criterion, scheduler)

torch.save(
    {
        'arch': args.arch,
        'input_size': input_size,
        'output_size': output_size,
        'hidden_layers': args.hidden_units,
        'state_dict': model.state_dict(),
        'optimizer.state_dict': optimizer.state_dict,
        'class_to_idx': model.class_to_idx
    }, args.save_dir + '/checkpoint.pth')

print('Checkpoint saved')
Ejemplo n.º 5
0
def run(verbose=True, showBot=False, args=defaults):
    dataset = np.loadtxt("ROI_dataset.dat")
    #######################################################################
    #                       ** START OF YOUR CODE **
    #######################################################################
    np.random.shuffle(dataset)

    trainRatio = 0.8
    testRatio = 0.1
    numEpochs = args['epochs']
    batchsize = 64

    # split dataset
    X, Y = dataset[:, 0:3], dataset[:, 3:]

    # get input and output dimensions
    xDim, yDim = X.shape[-1], Y.shape[-1]

    # create dataloaders
    trainloader, validloader, testloader = helpers.loadTrainingData(
        X, Y, trainRatio, testRatio, batchsize)

    # initiate the model
    model = Net(xDim, yDim, args['l1'], args['l2'], args['l3'], args['l4'])

    # set up hyperparameters
    loss_function = nn.BCEWithLogitsLoss()
    optimizer = optim.Adam(model.parameters(), lr=args['lr'])

    # train the model
    val_loss = helpers.trainModel(model,
                                  optimizer,
                                  loss_function,
                                  numEpochs,
                                  trainloader,
                                  validloader,
                                  verbose=verbose)

    if args['save']:
        torch.save(model, args['filepath'])

    # test the model
    helpers.testModel(model,
                      loss_function,
                      numEpochs,
                      testloader,
                      checkAccuracy=False,
                      verbose=verbose)

    # evaluate model
    x_store, y_store = [], []
    i = 0
    for x, y in testloader:
        if i < 1:
            x_store = np.array(x)
            y_store = np.array(y)
        else:
            np.concatenate((x_store, np.array(x)), axis=0)
            np.concatenate((y_store, np.array(y)), axis=0)
        i = 1

    evaluate_architecture(model, torch.Tensor(x_store), torch.Tensor(y_store))
    #######################################################################
    #                       ** END OF YOUR CODE **
    #######################################################################
    if showBot:
        illustrate_results_ROI(model)

    return val_loss