Ejemplo n.º 1
0
def graph():
    """
    Function to encapsulate the construction of a TensorFlow computation graph.
    :return: input placeholders, optimisation operation, loss, accuracy, prediction operations
    """

    # Define placeholders for the data

    # The sentence to classify, has shape [batch_size x word_dimensions*2] because the input will be the sentence
    # and abstract concatenated.
    sentence_input = tf.placeholder(tf.float32, shape=[None, WORD_DIMENSIONS + ABSTRACT_DIMENSION + NUM_FEATURES])

    # The labels for the sentences as one-hot vectors, of the form [batch_size x num_classes]
    labels = tf.placeholder(tf.float32, shape=[None, NUM_CLASSES])

    # Keep probability for dropout
    keep_prob = tf.placeholder(tf.float32)

    # Define the computation graph

    # The keep gate - decides which parts to keep
    hidden_weight = weight_variable([WORD_DIMENSIONS + ABSTRACT_DIMENSION + NUM_FEATURES, HIDDEN_LAYER_WEIGHTS])
    hidden_bias = bias_variable([HIDDEN_LAYER_WEIGHTS])
    hidden_layer = tf.nn.relu(tf.matmul(sentence_input, hidden_weight) + hidden_bias)

    # Add dropout
    hidden_layer_output = tf.nn.dropout(hidden_layer, keep_prob)

    # Project the output to two classes
    projection_weight = weight_variable([HIDDEN_LAYER_WEIGHTS, NUM_CLASSES])
    projection_bias = bias_variable([NUM_CLASSES])
    output = tf.matmul(hidden_layer_output, projection_weight) + projection_bias

    # Define the loss function
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(output, labels))
    opt = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss)

    # Predictions
    predictions = tf.nn.softmax(output)

    # Calculate accuracy
    pred_answers = tf.argmax(output, axis=1)
    correct_answers = tf.argmax(labels, axis=1)
    accuracy = tf.reduce_mean(tf.cast(tf.equal(pred_answers, correct_answers), tf.float32))

    return_dict = {
        "input": sentence_input,
        "labels": labels,
        "keep_prob": keep_prob,
        "loss": loss,
        "opt": opt,
        "prediction_probs": predictions,
        "prediction_class": pred_answers,
        "correct_answers": correct_answers,
        "accuracy": accuracy
    }

    return return_dict
def graph():
    """
    Function to encapsulate the construction of a TensorFlow computation graph.
    :return: input placeholders, optimisation operation, loss, accuracy, prediction operations
    """

    # Define placeholders for the data

    # The sentence to classify, has shape [batch_size x word_dimensions*2] because the input will be the sentence
    # and abstract concatenated.
    sentence_input = tf.placeholder(
        tf.float32,
        shape=[None, WORD_DIMENSIONS + ABSTRACT_DIMENSION + NUM_FEATURES])

    # The labels for the sentences as one-hot vectors, of the form [batch_size x num_classes]
    labels = tf.placeholder(tf.float32, shape=[None, NUM_CLASSES])

    # Keep probability for dropout
    keep_prob = tf.placeholder(tf.float32)

    # Define the computation graph

    W_conv1 = weight_variable([5, 5, 1, 32])
    b_conv1 = bias_variable([32])

    x_image = tf.reshape(sentence_input, [-1, 8, 26, 1])

    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
    h_pool1 = max_pool_2x2(h_conv1)

    W_fc1 = weight_variable([4 * 13 * 32, HIDDEN_LAYER_WEIGHTS])
    b_fc1 = bias_variable([HIDDEN_LAYER_WEIGHTS])

    h_pool1_flat = tf.reshape(h_pool1, [-1, 4 * 13 * 32])
    h_fc1 = tf.nn.relu(tf.matmul(h_pool1_flat, W_fc1) + b_fc1)

    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

    W_fc2 = weight_variable([HIDDEN_LAYER_WEIGHTS, NUM_CLASSES])
    b_fc2 = bias_variable([NUM_CLASSES])

    output = tf.matmul(h_fc1_drop, W_fc2) + b_fc2

    # Define the loss function
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(output, labels))
    opt = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss)

    # Predictions
    predictions = tf.nn.softmax(output)

    # Calculate accuracy
    pred_answers = tf.argmax(output, axis=1)
    correct_answers = tf.argmax(labels, axis=1)
    accuracy = tf.reduce_mean(
        tf.cast(tf.equal(pred_answers, correct_answers), tf.float32))

    return sentence_input, labels, keep_prob, loss, opt, predictions, pred_answers, correct_answers, accuracy
Ejemplo n.º 3
0
def graph():
    # Define placeholders for the data

    # The sentence to classify, has shape [batch_size x word_dimensions]
    sentence_input = tf.placeholder(tf.float32, shape=[None, WORD_DIMENSIONS])

    # The labels for the sentences as one-hot vectors, of the form [batch_size x num_classes]
    labels = tf.placeholder(tf.float32, shape=[None, NUM_CLASSES])

    # Keep probability for dropout
    keep_prob = tf.placeholder(tf.float32)

    # Define the computation graph

    # Linear transformation
    sent_weight = weight_variable([WORD_DIMENSIONS, HIDDEN_LAYER_WEIGHTS])
    sent_bias = bias_variable([HIDDEN_LAYER_WEIGHTS])
    sentence_gate = tf.nn.sigmoid(
        tf.matmul(sentence_input, sent_weight) + sent_bias)

    # Add ReLU non-linearity
    hidden_layer = tf.nn.relu(sentence_gate)

    # Projection to output classes
    output_weights = weight_variable([HIDDEN_LAYER_WEIGHTS, NUM_CLASSES])
    output_bias = bias_variable([NUM_CLASSES])
    output = tf.matmul(hidden_layer, output_weights) + output_bias

    # Add dropout for regularisation
    output = tf.nn.dropout(output, keep_prob=keep_prob)

    # Define the loss function
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(output, labels))
    opt = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss)

    # Predictions
    predictions = tf.nn.softmax(output)

    # Calculate accuracy
    pred_answers = tf.argmax(output, axis=1)
    correct_answers = tf.argmax(labels, axis=1)
    accuracy = tf.reduce_mean(
        tf.cast(tf.equal(pred_answers, correct_answers), tf.float32))

    return_dict = {
        "sentence_input": sentence_input,
        "labels": labels,
        "keep_prob": keep_prob,
        "loss": loss,
        "opt": opt,
        "prediction_probs": predictions,
        "prediction_class": pred_answers,
        "correct_answers": correct_answers,
        "accuracy": accuracy
    }

    return return_dict
Ejemplo n.º 4
0
def graph():
    """
    Function to encapsulate the construction of a TensorFlow computation graph.
    :return: input placeholders, optimisation operation, loss, accuracy, prediction operations
    """

    # Define placeholders for the data

    # The sentence to classify, has shape [batch_size x word_dimensions*2] because the input will be the sentence
    # and abstract concatenated.
    sentence_input = tf.placeholder(tf.float32, shape=[None, WORD_DIMENSIONS*2])

    # The labels for the sentences as one-hot vectors, of the form [batch_size x num_classes]
    labels = tf.placeholder(tf.float32, shape=[None, NUM_CLASSES])

    # Keep probability for dropout
    keep_prob = tf.placeholder(tf.float32)

    # Define the computation graph

    # The keep gate - decides which parts to keep
    keep_weight = weight_variable([WORD_DIMENSIONS*2, SENTENCE_WEIGHTS])
    keep_bias = bias_variable([SENTENCE_WEIGHTS])
    keep_gate = tf.nn.sigmoid(tf.matmul(sentence_input, keep_weight) + keep_bias)

    # Project the output to a 100 dimensional vector
    projection_weight = weight_variable([SENTENCE_WEIGHTS, WORD_DIMENSIONS])
    projection_bias = bias_variable([WORD_DIMENSIONS])
    combined_information = tf.nn.relu(tf.matmul(keep_gate, projection_weight) + projection_bias)

    # Add a ReLU layer to this
    classification_layer_weights = weight_variable([WORD_DIMENSIONS, HIDDEN_LAYER_WEIGHTS])
    classification_layer_bias = bias_variable([HIDDEN_LAYER_WEIGHTS])
    classification_layer = tf.nn.relu(tf.matmul(combined_information, classification_layer_weights) +
                                      classification_layer_bias)

    # Add dropout
    hidden_layer_output = tf.nn.dropout(classification_layer, keep_prob)

    # Project to two classes
    final_layer_weights = weight_variable([HIDDEN_LAYER_WEIGHTS, NUM_CLASSES])
    final_layer_bias = bias_variable([NUM_CLASSES])
    output = tf.matmul(classification_layer, final_layer_weights) + final_layer_bias

    # Define the loss function
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(output, labels))
    opt = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss)

    # Predictions
    predictions = tf.nn.softmax(output)

    # Calculate accuracy
    pred_answers = tf.argmax(output, axis=1)
    correct_answers = tf.argmax(labels, axis=1)
    accuracy = tf.reduce_mean(tf.cast(tf.equal(pred_answers, correct_answers), tf.float32))

    return sentence_input, labels, keep_prob, loss, opt, predictions, pred_answers, correct_answers, accuracy
Ejemplo n.º 5
0
def graph_vis():

    # Define placeholders for the data

    # The sentence to classify, has shape [batch_size x word_dimensions]
    sentence_input = tf.placeholder(tf.float32, shape=[None, NUM_FEATURES])

    # The labels for the sentences as one-hot vectors, of the form [batch_size x num_classes]
    labels = tf.placeholder(tf.float32, shape=[None, NUM_CLASSES])

    # Define the computation graph

    # Linear layer
    sent_weight = weight_variable([NUM_FEATURES, HIDDEN_LAYER_WEIGHTS])
    sent_bias = bias_variable([HIDDEN_LAYER_WEIGHTS])

    # ReLU
    hidden_layer = tf.nn.relu(
        tf.matmul(sentence_input, sent_weight) + sent_bias)

    # Down to num features
    down_to_feats = tf.contrib.layers.linear(hidden_layer, NUM_FEATURES)

    # Project to two classes
    projection_weight = weight_variable([NUM_FEATURES, NUM_CLASSES])
    projection_bias = bias_variable([NUM_CLASSES])

    output = tf.matmul(down_to_feats, projection_weight) + projection_bias

    # Define the loss function
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(output, labels))
    opt = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss)

    # Predictions
    predictions = tf.nn.softmax(output)

    # Calculate accuracy
    pred_answers = tf.argmax(output, axis=1)
    correct_answers = tf.argmax(labels, axis=1)
    accuracy = tf.reduce_mean(
        tf.cast(tf.equal(pred_answers, correct_answers), tf.float32))

    return_dict = {
        "features_input": sentence_input,
        "labels": labels,
        "loss": loss,
        "opt": opt,
        "prediction_probs": predictions,
        "prediction_class": pred_answers,
        "correct_answers": correct_answers,
        "accuracy": accuracy,
        "extra": projection_weight
    }

    return return_dict
Ejemplo n.º 6
0
def graph():

    # Define placeholders for the data

    # The sentence to classify, has shape [batch_size x word_dimensions]
    sentence_input = tf.placeholder(tf.float32, shape=[None, NUM_FEATURES])

    # The labels for the sentences as one-hot vectors, of the form [batch_size x num_classes]
    labels = tf.placeholder(tf.float32, shape=[None, NUM_CLASSES])

    # Define the computation graph

    # Linear layer
    sent_weight = weight_variable([NUM_FEATURES, NUM_CLASSES])
    sent_bias = bias_variable([NUM_CLASSES])

    output = tf.matmul(sentence_input, sent_weight) + sent_bias

    # Define the loss function
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(output, labels))
    opt = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss)

    # Predictions
    predictions = tf.nn.softmax(output)

    # Calculate accuracy
    pred_answers = tf.argmax(output, axis=1)
    correct_answers = tf.argmax(labels, axis=1)
    accuracy = tf.reduce_mean(
        tf.cast(tf.equal(pred_answers, correct_answers), tf.float32))

    return sentence_input, labels, loss, opt, predictions, pred_answers, correct_answers, accuracy, sent_weight
Ejemplo n.º 7
0
def graph():
    """
    Function to encapsulate the construction of a TensorFlow computation graph.
    :return: input placeholders, optimisation operation, loss, accuracy, prediction operations
    """

    # Define placeholders for the data

    # The sentence to classify, has shape [batch_size x word_dimensions*2] because the input will be the sentence
    # and abstract concatenated.
    sentence_input = tf.placeholder(
        tf.float32,
        shape=[None, WORD_DIMENSIONS + ABSTRACT_DIMENSION + NUM_FEATURES])

    # The labels for the sentences as one-hot vectors, of the form [batch_size x num_classes]
    labels = tf.placeholder(tf.float32, shape=[None, NUM_CLASSES])

    # Define the computation graph

    # The keep gate - decides which parts to keep
    keep_weight = weight_variable(
        [WORD_DIMENSIONS + ABSTRACT_DIMENSION + NUM_FEATURES, NUM_CLASSES])
    keep_bias = bias_variable([NUM_CLASSES])
    output = tf.matmul(sentence_input, keep_weight) + keep_bias

    # Define the loss function
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(output, labels))
    opt = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss)

    # Predictions
    predictions = tf.nn.softmax(output)

    # Calculate accuracy
    pred_answers = tf.argmax(output, axis=1)
    correct_answers = tf.argmax(labels, axis=1)
    accuracy = tf.reduce_mean(
        tf.cast(tf.equal(pred_answers, correct_answers), tf.float32))

    return sentence_input, labels, loss, opt, predictions, pred_answers, correct_answers, accuracy
# ================ MAIN ================

# Define placeholders for the data

# The sentence to classify, has shape [batch_size x word_dimensions]
sentence_input = tf.placeholder(tf.float32, shape=[None, WORD_DIMENSIONS])

# The labels for the sentences as one-hot vectors, of the form [batch_size x num_classes]
labels = tf.placeholder(tf.float32, shape=[None, NUM_CLASSES])

# Define the computation graph

# Linear layer
sent_weight = weight_variable([WORD_DIMENSIONS, NUM_CLASSES])
sent_bias = bias_variable([NUM_CLASSES])

output = tf.matmul(sentence_input, sent_weight) + sent_bias

# Define the loss function
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(output, labels))
opt = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss)

# Predictions
predictions = tf.nn.softmax(output)

# Calculate accuracy
pred_answers = tf.argmax(output, axis=1)
correct_answers = tf.argmax(labels, axis=1)
accuracy = tf.reduce_mean(
    tf.cast(tf.equal(pred_answers, correct_answers), tf.float32))