Example #1
0
def newsgl(X,R,layers=[512],eg=None,l1=None,dropout=None):
    layers = [l for l in layers]
    eg_strength = eg if eg else 0.0
    l1_strength = tf.constant(l1,dtype=tf.float64) if l1 else tf.constant(0.0,dtype=tf.float64)
    dropout_strength = dropout if dropout else 0.0
    
    explainer = TFOpsExplainer()
    cond_input_op, train_eg = explainer.input_to_samples_delta(X,lambda: R)

    if len(layers)>0:
        kernels = []
        biases = []
        firstlayer = layers.pop(0)
        hid_layer = tf.layers.Dense(firstlayer,activation=tf.nn.relu)
        hid = hid_layer(cond_input_op)
        if dropout: hid = tf.nn.dropout(hid,keep_prob=1-dropout_strength)
        kernels.append(hid_layer.kernel)
        biases.append(hid_layer.bias)
        for l in layers:
            hid_layer = tf.layers.Dense(l,activation=tf.nn.relu)
            hid = hid_layer(hid)
            if dropout: hid = tf.nn.dropout(hid,keep_prob=1-dropout_strength)
            kernels.append(hid_layer.kernel)
            biases.append(hid_layer.bias)
        output = tf.layers.Dense(2)(hid)

    else:
        output_layer = tf.layers.Dense(2)
        output = output_layer(cond_input_op)
        kernels = [output_layer.kernel]
        biases = [output_layer.bias]
    
    
    kernel = kernels[0]
    y_pred = tf.nn.sigmoid(output)
    eg_op = explainer.shap_value_op(output, cond_input_op, y[:,1])
    
    cost = tf.nn.sigmoid_cross_entropy_with_logits(logits=y_pred,labels=y)
    total_cost = tf.reduce_mean(cost)
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0).minimize(total_cost)
    l1_reg = (tf.reduce_sum([sgl_penalty_layer(k) for k in kernels])+
              tf.reduce_sum([2*tf.reduce_sum(tf.abs(b)) for b in biases]))*l1_strength
        
    weighted_abs_loss = tf.reshape(tf.reduce_mean(tf.abs(tf.cast(eg_op,tf.float32)),axis=0),(1,-1))
    mad_loss = tf.abs(tf.transpose(weighted_abs_loss)-weighted_abs_loss)
    rmad_loss = tf.reduce_mean(mad_loss)/tf.reduce_mean(weighted_abs_loss)
    
    reg_lambda = tf.constant(eg_strength,dtype=tf.float32)
    reg_loss_op = -rmad_loss * reg_lambda
    reg_loss_op = reg_loss_op + tf.cast(l1_reg,dtype=tf.float32)

    train_eg_op = tf.train.GradientDescentOptimizer(learning_rate=1.0).minimize(reg_loss_op)
    return (y_pred, eg_op, total_cost, optimizer, train_eg_op, train_eg, explainer, reg_lambda, l1_strength)    
Example #2
0
def ginigrad(X,R,layers=[512],eg=None,l1=None,dropout=None,inputs=False):
    layers = [l for l in layers]
    eg_strength = eg if eg else 0.0
    l1_strength = tf.constant(l1,dtype=tf.float64) if l1 else tf.constant(0.0,dtype=tf.float64)
    dropout_strength = dropout if dropout else 0.0
    
    explainer = TFOpsExplainer()
    cond_input_op, train_eg = explainer.input_to_samples_delta(X,lambda: R)
    if len(layers)>0:
        firstlayer = layers.pop(0)
        hid_layer = tf.layers.Dense(firstlayer,activation=tf.nn.relu)
        hid = hid_layer(cond_input_op)
        if dropout: hid = tf.nn.dropout(hid,keep_prob=1-dropout_strength)
        l1_reg = tf.reduce_mean(tf.abs(hid_layer.kernel)) * l1_strength
        for l in layers:
            hid = tf.layers.Dense(l,activation=tf.nn.relu)(hid)
            if dropout: hid = tf.nn.dropout(hid,keep_prob=1-dropout_strength)
        output = tf.layers.Dense(2)(hid)

    else:
        output_layer = tf.layers.Dense(2)
        output = output_layer(cond_input_op)
        l1_reg = tf.reduce_mean(tf.abs(output_layer.kernel)) * l1_strength
        
        
    y_pred = tf.nn.sigmoid(output)#model(cond_input_op)
    eg_op = explainer.shap_value_op(output, cond_input_op, y[:,1]) # Train with gradients but use EG for explanations (you should probably use this)
    grads = tf.gradients(output[:,1],X) # Train with gradients but use EG for explanations (you should probably use this)
    if inputs: grads *= X
    
    cost = tf.nn.sigmoid_cross_entropy_with_logits(logits=y_pred,labels=y)
    total_cost = tf.reduce_mean(cost)
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0).minimize(total_cost)
    
    weighted_abs_loss = tf.reshape(tf.reduce_mean(tf.abs(tf.cast(grads,tf.float32)),axis=0),(1,-1))
    mad_loss = tf.abs(tf.transpose(weighted_abs_loss)-weighted_abs_loss)
    rmad_loss = tf.reduce_mean(mad_loss)/tf.reduce_mean(weighted_abs_loss)
    
    reg_lambda = tf.constant(eg_strength,dtype=tf.float32)
    reg_loss_op = -rmad_loss * reg_lambda
    reg_loss_op = reg_loss_op + tf.cast(l1_reg,dtype=tf.float32)

    train_eg_op = tf.train.GradientDescentOptimizer(learning_rate=1.0).minimize(reg_loss_op)
    return (y_pred, eg_op, total_cost, optimizer, train_eg_op, train_eg, explainer, reg_lambda, l1_strength)        
Example #3
0
def pipeline(use_old_model=False):
    # Data iterators
    training_iterator, validation_iterator, test_iterator, handle, x, y_ = inputs(
        FLAGS.batch_size, FLAGS.epochs, VALIDATION_SIZE, TEST_BATCH_SIZE)

    # Get explainer conditional input
    explainer = AttributionPriorExplainer()
    cond_input_op, train_eg = explainer.input_to_samples_delta(x)

    # Predict
    y, train_pl = get_model(cond_input_op)

    # Get explanations
    expected_gradients_op = explainer.shap_value_op(y,
                                                    cond_input_op,
                                                    sparse_labels_op=y_)
    expected_gradients_op = batch_standardize(expected_gradients_op)

    learning_rate = get_learning_rate(FLAGS.eta, FLAGS.batch_size)
    global_step = tf.train.get_or_create_global_step()

    loss = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y_, logits=y))
    train_step = tf.train.AdamOptimizer(learning_rate).minimize(
        (1.0 - FLAGS.lamb) * loss, global_step=global_step)

    eg_loss = tf.reduce_mean(
        tf.image.total_variation(expected_gradients_op, name='variation'))
    eg_train = tf.train.AdamOptimizer(learning_rate).minimize(
        FLAGS.lamb * eg_loss, global_step=global_step)

    y_pred = tf.argmax(y, 1)
    accuracy_op, accuracy_update_op = tf.metrics.accuracy(y_pred, y_)
    reset_metrics_op = tf.variables_initializer(
        tf.get_collection(tf.GraphKeys.METRIC_VARIABLES))

    return cond_input_op, x, y, y_, train_pl, loss, train_step, eg_loss, eg_train, train_eg, accuracy_op, accuracy_update_op, reset_metrics_op, training_iterator, validation_iterator, test_iterator, handle