Exemplo n.º 1
0
Arquivo: run.py Projeto: GDPlumb/ExpO
config = config_list["housing"]

shape = [n_input]
for i in range(config[0]):
    shape.append(config[1])
shape.append(1)

# Create the model
tf.reset_default_graph()

X = tf.placeholder("float", [None, n_input], name="X_in")

network = MLP(shape)
with tf.variable_scope("model", reuse=tf.AUTO_REUSE):
    pred = network.model(X)

saver = tf.train.Saver(max_to_keep=1)

# Create tf session
sess = tf.Session()

# Wrap it for LIME
wrapper = Wrapper(sess, pred, X)
wrapper.set_index(0)

# Configure LIME
exp = lime_tabular.LimeTabularExplainer(X_train,
                                        discretize_continuous=False,
                                        mode="regression")
Exemplo n.º 2
0
Arquivo: run.py Projeto: GDPlumb/ExpO
def eval(
        source,
        # Network parameters
        hidden_layer_sizes=[100, 100, 100],
        learning_rate=0.001,
        # Regularizer parameters
        regularizer=None,
        c=1.0,
        # Training parameters
        batch_size=2,
        reg_batch_size=1,
        stopping_epochs=200,
        min_epochs=500,
        stop_on_loss=True,
        evaluate_explanation=True):

    # Allow multiple sessions on a single GPU.
    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth = True

    # Reset TF graph (avoids issues with repeat experiments)
    tf.reset_default_graph()

    # Get Data
    if regularizer is not None:
        data = ToyDataManager(source,
                              train_batch_size=batch_size,
                              reg_batch_size=reg_batch_size)
    else:
        data = ToyDataManager(source, train_batch_size=batch_size)

    n = data.X_train.shape[0]
    n_input = data.X_train.shape[1]
    n_out = data.y_train.shape[1]

    # Regularizer Parameters
    if regularizer is not None:
        # Weight of the regularization term in the loss function
        c = tf.constant(c)
        # Number of neighbors to hallucinate per point
        num_samples = np.max((20, np.int(2 * n_input)))

    # Network Parameters
    shape = [n_input]
    for size in hidden_layer_sizes:
        shape.append(size)
    shape.append(n_out)

    # Graph inputs
    X = tf.placeholder("float", [None, n_input], name="X_in")
    Y = tf.placeholder("float", [None, n_out], name="Y_in")
    if regularizer is not None:
        X_reg = tf.placeholder("float", [None, n_input], name="X_reg")

    # Link the graph inputs into the DataManager so its train_feed() and eval_feed() functions work
    if regularizer is not None:
        data.link_graph(X, Y, X_reg=X_reg)
    else:
        data.link_graph(X, Y)

    # Build the model
    network = MLP(shape)
    with tf.variable_scope("model", reuse=tf.AUTO_REUSE):
        pred = network.model(X)

    # Build the regularizer
    if regularizer == "Causal":
        regularizer = Regularizer(network.model, n_input, num_samples)
        reg = regularizer.causal(X_reg)

    # Define the loss and optimization process
    model_loss = tf.losses.mean_squared_error(labels=Y, predictions=pred)
    tf.summary.scalar("MSE", model_loss)

    perf_op = model_loss
    smaller_is_better = True

    if regularizer is None:
        loss_op = model_loss
    else:
        loss_op = model_loss + c * reg
        tf.summary.scalar("Regularizer", reg)
    tf.summary.scalar("Loss", loss_op)

    summary_op = tf.summary.merge_all()

    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
    train_op = optimizer.minimize(loss_op)

    # Train the model
    init = [
        tf.global_variables_initializer(),
        tf.local_variables_initializer()
    ]

    saver = tf.train.Saver(max_to_keep=1)

    if stop_on_loss:  # Stop on validation loss function
        best_loss = np.inf
    else:  # Stop on the validation performance
        if smaller_is_better:
            best_perf = np.inf
        else:
            best_perf = 0.0

    best_epoch = 0

    with tf.Session(config=tf_config) as sess:
        train_writer = tf.summary.FileWriter("train", sess.graph)
        val_writer = tf.summary.FileWriter("val")

        sess.run(init)

        epoch = 0
        while True:

            # Stopping condition
            if epoch - best_epoch > stopping_epochs and epoch > min_epochs:
                break

            # Run a training epoch
            total_batch = int(n / batch_size)
            for i in range(total_batch):
                dict = data.train_feed()
                sess.run(train_op, feed_dict=dict)

            # Run model metrics
            if epoch % 10 == 0:
                dict = data.eval_feed()
                summary = sess.run(summary_op, feed_dict=dict)
                train_writer.add_summary(summary, epoch)

                dict = data.eval_feed(val=True)

                if stop_on_loss:
                    summary, val_loss = sess.run([summary_op, loss_op],
                                                 feed_dict=dict)

                    if val_loss < best_loss:
                        print(os.getcwd(), " ", epoch, " ", val_loss)
                        best_loss = val_loss
                        best_epoch = epoch
                        saver.save(sess, "./model.cpkt")

                else:
                    summary, val_perf = sess.run([summary_op, perf_op],
                                                 feed_dict=dict)

                    if smaller_is_better and val_perf < best_perf:
                        progress = True
                    elif not smaller_is_better and val_perf > best_perf:
                        progress = True
                    else:
                        progress = False

                    if progress:
                        print(os.getcwd(), " ", epoch, " ", val_perf)
                        best_perf = val_perf
                        best_epoch = epoch
                        saver.save(sess, "./model.cpkt")

                val_writer.add_summary(summary, epoch)

            epoch += 1

        train_writer.close()
        val_writer.close()

        ###
        # Evaluate the chosen model
        ###

        saver.restore(sess, "./model.cpkt")

        pred_grid = sess.run(pred, {X: x_grid})
        plt.plot(x_grid, pred_grid)

        ###
        # Make explanations
        ####

        def predict_fn(x):
            return np.squeeze(sess.run(pred, {X: x}))

        exp = MAPLE(data.X_train, predict_fn(data.X_train), data.X_val,
                    predict_fn(data.X_val))

        x = np.zeros((1, 1))
        x[0] = 0.1
        e = exp.explain(x)
        c1 = e["coefs"]

        x[0] = 0.4
        e = exp.explain(x)
        c2 = e["coefs"]

        return c1, c2