Beispiel #1
0
def train_nn(feats, labels, net_params, model_out, dir_out):
    '''
    Train neural network classifier
    :param feats: training feats
    :param labels: training labels
    :param net_params: input params for neural net
    :param model_out: fname to save output model
    :param dir_out: directory to save output model
    :return: trained model
    '''

    net = FFNNet(**net_params)

    ds_train = SparseDataset(feats, labels)
    train_loader = get_dataloader(ds_train, shuffle=True)

    model = Model(net)
    net = model.train(train_loader)

    state = {
        'net_params': net_params,
        'state_dict': net.state_dict(),
    }
    save_state_dict(state, fname = model_out, dir_out = dir_out)

    return net
Beispiel #2
0
def pred_prob_nn(net, x_test):
    '''
    :param net: trained Pytorch model
    :param x_test: test feature matrix
    :return: Prediction probability of all output classes for all instances (n_inst, n_classes)
    '''
    ds = SparseDataset(x_test)
    dataloader = get_dataloader(ds, shuffle=False)

    model = Model(net)
    return model.predict_prob(dataloader)
Beispiel #3
0
def get_feat_sensitivity(net, data):
    '''
    Calculate feature sensitivity scores from a trained neural network.
    :param net: Trained network
    :param data: Data (containing feature values of different instances)
    :return: Sensitivity scores of all the features in the data based on the trained model (1D array),
    :return: 1D array with indices of the top scoring features in decreasing order
    '''
    ds = SparseDataset(data)
    dataloader = get_dataloader(ds, shuffle=False)
    gradients, max_grad_idx = get_max_rms_gradient(net, dataloader)
    return gradients, max_grad_idx
Beispiel #4
0
def get_gradients(net, feats, labels=None):
    '''
    Compute gradients of output wrt input for every instance and feature pair
    :param net: trained model
    :param feats: original feats
    :param labels: labels to get gradients for
    :return: reweighed feature matrix (supports scipy sparse) and the training gradients
    '''

    ds = SparseDataset(feats, labels)
    dataloader = get_dataloader(ds, shuffle=False)
    gradients = get_out_gradient(net, dataloader)
    return gradients
Beispiel #5
0
def pred_nn(net, x_test):
    '''
    Get predicted classes and probability of prediction given a Pytorch model and test features
    :param net: Trained Pytorch model
    :param x_test: test feature matrix
    :return: Arrays of predicted classes, and prediction probabilities of all classes for all instances
    '''

    ds = SparseDataset(x_test)
    dataloader = get_dataloader(ds, shuffle=False)

    model = Model(net)
    return model.predict(dataloader)