Skip to content

Commit

Permalink
#12 Problem with Label Encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
adsar committed Dec 4, 2020
1 parent 0a8c045 commit 52c7c0b
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 68 deletions.
100 changes: 65 additions & 35 deletions notebooks/gcn_experiments.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,10 @@
"Instructions for updating:\n",
"Colocations handled automatically by placer.\n",
"16 layer 32\n",
"WARNING:tensorflow:From /home/adrian/as/blogs/ie_gcn/nnblog/python/gcn/layers.py:171: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n",
"WARNING:tensorflow:From /home/adrian/as/blogs/nnblog/python/gcn/layers.py:171: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n",
"WARNING:tensorflow:From /home/adrian/as/blogs/ie_gcn/nnblog/python/gcn/metrics.py:6: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
"WARNING:tensorflow:From /home/adrian/as/blogs/nnblog/python/gcn/metrics.py:6: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"\n",
"Future major versions of TensorFlow will allow gradients to flow\n",
Expand All @@ -387,40 +387,70 @@
"WARNING:tensorflow:From /home/adrian/anaconda3/envs/condayolo/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"Use tf.cast instead.\n",
"Epoch: 0001 train_loss= 4.59582 train_acc= 0.50331 val_loss= 1.24910 val_acc= 0.90744 time= 0.90494\n",
"Epoch: 0002 train_loss= 5.76676 train_acc= 0.54463 val_loss= 1.05884 val_acc= 0.93884 time= 0.63730\n",
"Epoch: 0003 train_loss= 4.52988 train_acc= 0.60909 val_loss= 0.90070 val_acc= 0.96198 time= 0.63676\n",
"Epoch: 0004 train_loss= 3.88535 train_acc= 0.61570 val_loss= 0.77479 val_acc= 0.97686 time= 0.62156\n",
"Epoch: 0005 train_loss= 4.00559 train_acc= 0.67025 val_loss= 0.65887 val_acc= 0.98182 time= 0.62643\n",
"Epoch: 0006 train_loss= 3.03587 train_acc= 0.69256 val_loss= 0.54930 val_acc= 0.98843 time= 0.63585\n",
"Epoch: 0007 train_loss= 3.74282 train_acc= 0.71405 val_loss= 0.46569 val_acc= 0.99339 time= 0.63807\n",
"Epoch: 0008 train_loss= 1.92925 train_acc= 0.72479 val_loss= 0.42961 val_acc= 0.99669 time= 0.62866\n",
"Epoch: 0009 train_loss= 1.94596 train_acc= 0.74793 val_loss= 0.39577 val_acc= 0.99835 time= 0.62343\n",
"Epoch: 0010 train_loss= 1.81199 train_acc= 0.79669 val_loss= 0.36575 val_acc= 0.99835 time= 0.62570\n",
"Epoch: 0011 train_loss= 1.71886 train_acc= 0.80331 val_loss= 0.33840 val_acc= 0.99835 time= 0.62254\n",
"Epoch: 0012 train_loss= 1.36275 train_acc= 0.81570 val_loss= 0.31411 val_acc= 0.99835 time= 0.62604\n",
"Epoch: 0013 train_loss= 1.21630 train_acc= 0.84215 val_loss= 0.29247 val_acc= 0.99835 time= 0.62529\n",
"Epoch: 0014 train_loss= 1.10425 train_acc= 0.84876 val_loss= 0.27178 val_acc= 0.99835 time= 0.62845\n",
"Epoch: 0015 train_loss= 1.18580 train_acc= 0.86529 val_loss= 0.25254 val_acc= 0.99835 time= 0.62417\n",
"Epoch: 0016 train_loss= 1.28898 train_acc= 0.87769 val_loss= 0.23423 val_acc= 0.99835 time= 0.62251\n",
"Epoch: 0017 train_loss= 0.70696 train_acc= 0.88843 val_loss= 0.21738 val_acc= 0.99835 time= 0.61987\n",
"Epoch: 0018 train_loss= 0.78176 train_acc= 0.89587 val_loss= 0.20122 val_acc= 0.99835 time= 0.61782\n",
"Epoch: 0019 train_loss= 0.49038 train_acc= 0.90826 val_loss= 0.18615 val_acc= 0.99835 time= 0.62222\n",
"Epoch: 0020 train_loss= 0.81241 train_acc= 0.92231 val_loss= 0.17230 val_acc= 0.99835 time= 0.64321\n",
"Epoch: 0021 train_loss= 0.92798 train_acc= 0.92645 val_loss= 0.16052 val_acc= 1.00000 time= 0.62329\n",
"Epoch: 0022 train_loss= 0.75118 train_acc= 0.91736 val_loss= 0.15338 val_acc= 1.00000 time= 0.62732\n",
"Epoch: 0023 train_loss= 0.55413 train_acc= 0.92810 val_loss= 0.14759 val_acc= 1.00000 time= 0.61826\n",
"Epoch: 0024 train_loss= 0.54725 train_acc= 0.95124 val_loss= 0.14231 val_acc= 1.00000 time= 0.61948\n",
"Epoch: 0025 train_loss= 0.40233 train_acc= 0.94132 val_loss= 0.13747 val_acc= 1.00000 time= 0.62120\n",
"Epoch: 0026 train_loss= 0.71461 train_acc= 0.95455 val_loss= 0.13299 val_acc= 1.00000 time= 0.62849\n",
"Epoch: 0027 train_loss= 0.32142 train_acc= 0.94711 val_loss= 0.12886 val_acc= 1.00000 time= 0.62838\n",
"Epoch: 0028 train_loss= 0.60953 train_acc= 0.95289 val_loss= 0.12511 val_acc= 1.00000 time= 0.62120\n",
"Epoch: 0029 train_loss= 0.43935 train_acc= 0.96116 val_loss= 0.12162 val_acc= 1.00000 time= 0.62396\n",
"Epoch: 0030 train_loss= 0.21692 train_acc= 0.96281 val_loss= 0.11840 val_acc= 1.00000 time= 0.62379\n",
"Epoch: 0031 train_loss= 0.40871 train_acc= 0.96116 val_loss= 0.11539 val_acc= 1.00000 time= 0.61839\n",
"Validation accuracy reached 1.0. Early stopping...\n",
"Epoch: 0001 train_loss= 9.15036 train_acc= 0.45207 val_loss= 2.40282 val_acc= 0.78347 time= 0.90933\n",
"Epoch: 0002 train_loss= 8.84214 train_acc= 0.49339 val_loss= 2.00636 val_acc= 0.80496 time= 0.62422\n",
"Epoch: 0003 train_loss= 6.53560 train_acc= 0.53140 val_loss= 1.66153 val_acc= 0.82645 time= 0.63339\n",
"Epoch: 0004 train_loss= 6.03989 train_acc= 0.55537 val_loss= 1.36223 val_acc= 0.84132 time= 0.63739\n",
"Epoch: 0005 train_loss= 5.69761 train_acc= 0.59669 val_loss= 1.08571 val_acc= 0.84463 time= 0.63043\n",
"Epoch: 0006 train_loss= 4.58379 train_acc= 0.63140 val_loss= 0.93499 val_acc= 0.84959 time= 0.61914\n",
"Epoch: 0007 train_loss= 4.24525 train_acc= 0.63719 val_loss= 0.84214 val_acc= 0.85289 time= 0.63449\n",
"Epoch: 0008 train_loss= 3.46810 train_acc= 0.65702 val_loss= 0.75763 val_acc= 0.85455 time= 0.62478\n",
"Epoch: 0009 train_loss= 3.51065 train_acc= 0.67107 val_loss= 0.68370 val_acc= 0.85455 time= 0.62490\n",
"Epoch: 0010 train_loss= 2.72855 train_acc= 0.71157 val_loss= 0.61582 val_acc= 0.85455 time= 0.62565\n",
"Epoch: 0011 train_loss= 2.60586 train_acc= 0.72893 val_loss= 0.55462 val_acc= 0.85455 time= 0.62458\n",
"Epoch: 0012 train_loss= 2.59249 train_acc= 0.74215 val_loss= 0.49806 val_acc= 0.85455 time= 0.63007\n",
"Epoch: 0013 train_loss= 2.09740 train_acc= 0.75537 val_loss= 0.44583 val_acc= 0.85455 time= 0.62237\n",
"Epoch: 0014 train_loss= 1.42133 train_acc= 0.77355 val_loss= 0.39736 val_acc= 0.85455 time= 0.62032\n",
"Epoch: 0015 train_loss= 2.19245 train_acc= 0.78760 val_loss= 0.35364 val_acc= 0.85455 time= 0.61848\n",
"Epoch: 0016 train_loss= 2.04247 train_acc= 0.79256 val_loss= 0.31204 val_acc= 0.85455 time= 0.61750\n",
"Epoch: 0017 train_loss= 0.67464 train_acc= 0.79835 val_loss= 0.27197 val_acc= 0.85455 time= 0.62033\n",
"Epoch: 0018 train_loss= 1.64895 train_acc= 0.80000 val_loss= 0.23124 val_acc= 0.85455 time= 0.61272\n",
"Epoch: 0019 train_loss= 0.92555 train_acc= 0.81405 val_loss= 0.18853 val_acc= 0.85455 time= 0.62226\n",
"Epoch: 0020 train_loss= 0.44507 train_acc= 0.83140 val_loss= 0.16308 val_acc= 0.85620 time= 0.60863\n",
"Epoch: 0021 train_loss= 0.54577 train_acc= 0.82645 val_loss= 0.15037 val_acc= 0.85620 time= 0.60807\n",
"Epoch: 0022 train_loss= 0.53610 train_acc= 0.82562 val_loss= 0.13896 val_acc= 0.85620 time= 0.61424\n",
"Epoch: 0023 train_loss= 0.38649 train_acc= 0.83058 val_loss= 0.12866 val_acc= 0.85620 time= 0.61264\n",
"Epoch: 0024 train_loss= 0.31834 train_acc= 0.83884 val_loss= 0.11936 val_acc= 0.85620 time= 0.61234\n",
"Epoch: 0025 train_loss= 0.40965 train_acc= 0.83884 val_loss= 0.11094 val_acc= 0.85620 time= 0.61073\n",
"Epoch: 0026 train_loss= 0.19262 train_acc= 0.84711 val_loss= 0.10326 val_acc= 0.85620 time= 0.61033\n",
"Epoch: 0027 train_loss= 0.43524 train_acc= 0.85124 val_loss= 0.09626 val_acc= 0.85620 time= 0.61561\n",
"Epoch: 0028 train_loss= 0.33961 train_acc= 0.85124 val_loss= 0.08996 val_acc= 0.85620 time= 0.61449\n",
"Epoch: 0029 train_loss= 0.19680 train_acc= 0.85207 val_loss= 0.08426 val_acc= 0.85620 time= 0.61064\n",
"Epoch: 0030 train_loss= 0.29758 train_acc= 0.85950 val_loss= 0.07908 val_acc= 0.85620 time= 0.61189\n",
"Epoch: 0031 train_loss= 0.14299 train_acc= 0.85455 val_loss= 0.07442 val_acc= 0.85620 time= 0.60974\n",
"Epoch: 0032 train_loss= 0.16838 train_acc= 0.85372 val_loss= 0.07023 val_acc= 0.85620 time= 0.62849\n",
"Epoch: 0033 train_loss= 0.08102 train_acc= 0.85868 val_loss= 0.06645 val_acc= 0.85620 time= 0.62271\n",
"Epoch: 0034 train_loss= 0.08298 train_acc= 0.86116 val_loss= 0.06304 val_acc= 0.85620 time= 0.61625\n",
"Epoch: 0035 train_loss= 0.07296 train_acc= 0.86033 val_loss= 0.05995 val_acc= 0.85620 time= 0.61757\n",
"Epoch: 0036 train_loss= 0.08277 train_acc= 0.86116 val_loss= 0.05716 val_acc= 0.85620 time= 0.61497\n",
"Epoch: 0037 train_loss= 0.09527 train_acc= 0.85785 val_loss= 0.05468 val_acc= 0.85620 time= 0.62330\n",
"Epoch: 0038 train_loss= 0.05423 train_acc= 0.86198 val_loss= 0.05246 val_acc= 0.85620 time= 0.62141\n",
"Epoch: 0039 train_loss= 1.10268 train_acc= 0.86116 val_loss= 0.05050 val_acc= 0.85620 time= 0.61125\n",
"Epoch: 0040 train_loss= 0.29261 train_acc= 0.86116 val_loss= 0.04877 val_acc= 0.85620 time= 0.61287\n",
"Epoch: 0041 train_loss= 0.04679 train_acc= 0.86364 val_loss= 0.04722 val_acc= 0.85620 time= 0.61825\n",
"Epoch: 0042 train_loss= 0.06136 train_acc= 0.86198 val_loss= 0.04586 val_acc= 0.85620 time= 0.61663\n",
"Epoch: 0043 train_loss= 0.12876 train_acc= 0.86281 val_loss= 0.04465 val_acc= 0.85620 time= 0.61550\n",
"Epoch: 0044 train_loss= 0.05561 train_acc= 0.86116 val_loss= 0.04358 val_acc= 0.85620 time= 0.61507\n",
"Epoch: 0045 train_loss= 0.03907 train_acc= 0.86364 val_loss= 0.04266 val_acc= 0.85620 time= 0.62866\n",
"Epoch: 0046 train_loss= 0.03873 train_acc= 0.86364 val_loss= 0.04186 val_acc= 0.85620 time= 0.62107\n",
"Epoch: 0047 train_loss= 0.04328 train_acc= 0.86281 val_loss= 0.04117 val_acc= 0.85620 time= 0.61454\n",
"Epoch: 0048 train_loss= 0.03835 train_acc= 0.86364 val_loss= 0.04058 val_acc= 0.85620 time= 0.61737\n",
"Epoch: 0049 train_loss= 0.04098 train_acc= 0.86364 val_loss= 0.04008 val_acc= 0.85620 time= 0.61390\n",
"Epoch: 0050 train_loss= 0.03768 train_acc= 0.86364 val_loss= 0.03967 val_acc= 0.85620 time= 0.60998\n",
"Epoch: 0051 train_loss= 0.03752 train_acc= 0.86364 val_loss= 0.03932 val_acc= 0.85620 time= 0.61050\n",
"Epoch: 0052 train_loss= 0.03806 train_acc= 0.86364 val_loss= 0.03905 val_acc= 0.85620 time= 0.60297\n",
"Epoch: 0053 train_loss= 0.03794 train_acc= 0.86364 val_loss= 0.03883 val_acc= 0.85620 time= 0.62068\n",
"Epoch: 0054 train_loss= 0.03733 train_acc= 0.86364 val_loss= 0.03868 val_acc= 0.85620 time= 0.62922\n",
"Epoch: 0055 train_loss= 0.03712 train_acc= 0.86364 val_loss= 0.03859 val_acc= 0.85620 time= 0.64221\n",
"Epoch: 0056 train_loss= 0.03732 train_acc= 0.86364 val_loss= 0.03855 val_acc= 0.85620 time= 0.61611\n",
"Epoch: 0057 train_loss= 0.03848 train_acc= 0.86364 val_loss= 0.03855 val_acc= 0.85620 time= 0.61625\n",
"Epoch: 0058 train_loss= 0.03827 train_acc= 0.86364 val_loss= 0.03860 val_acc= 0.85620 time= 0.61081\n",
"Epoch: 0059 train_loss= 0.03874 train_acc= 0.86364 val_loss= 0.03868 val_acc= 0.85620 time= 0.62161\n",
"Epoch: 0060 train_loss= 0.03813 train_acc= 0.86364 val_loss= 0.03880 val_acc= 0.85620 time= 0.61455\n",
"Epoch: 0061 train_loss= 0.03845 train_acc= 0.86364 val_loss= 0.03895 val_acc= 0.85620 time= 0.61268\n",
" Validation cost is not improving. Early stopping...\n",
"Optimization Finished!\n",
"Test set results: cost= 0.10774 accuracy= 1.00000 time= 0.37699\n"
"Test set results: cost= 0.03870 accuracy= 0.85879 time= 0.37214\n"
]
}
],
Expand Down
14 changes: 8 additions & 6 deletions python/feature_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
from graph_modeler import *

#### Text Features

# -------------------------------------------------------------------------
def get_word_bpe(word, bpemb_en):
"""
Get pretrained subword embeddings created with
the Byte-Pair Encoding (BPE) algorithm.
If the word is segmented in more than 3 subwords we truncated at 3.
Gets pre-trained sub-word embeddings created with
the Byte Pair Encoding (BPE) algorithm.
If the word is segmented in more than 3 subwords, truncates at 3.
The length of each subword embedding is 100.
"""

Expand All @@ -58,7 +58,6 @@ def get_word_bpe(word, bpemb_en):

#### Binary Features
# -------------------------------------------------------------------------

def isDate(word, fuzzy=False):
"""
Return whether the word can be interpreted as a date.
Expand Down Expand Up @@ -147,7 +146,10 @@ def get_word_binary_features(word, debug=False):
# -------------------------------------------------------------------------
def get_word_area_numeric_features(word_area, graph):
"""
Fill the "undefined" values with the null distance (0)
Extracts numeric features from the graph: returns 4 floats
between -1 and 1 that represent the the relative distance
to the nearest neighbour in each of the four main directions.
Fills the "undefined" values with the null distance (0)
"""
edges = graph[word_area.idx]

Expand Down
3 changes: 2 additions & 1 deletion python/gcn/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ def construct_feed_dict(features, support, labels, labels_mask, placeholders):


def chebyshev_polynomials(adj, k):
"""Calculate Chebyshev polynomials up to order k. Return a list of sparse matrices (tuple representation)."""
"""Calculate Chebyshev polynomials up to order k.
Return a list of sparse matrices (tuple representation)."""
print("Calculating Chebyshev polynomials up to order {}...".format(k))

adj_normalized = normalize_adj(adj)
Expand Down
22 changes: 11 additions & 11 deletions python/global_parameters.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# global parameters


from collections import namedtuple

# named tuples
WordArea = namedtuple('WordArea', 'left, top, right, bottom, content, idx')
TrainingParameters = namedtuple("TrainingParameters", "dataset model learning_rate epochs hidden1 num_hidden_layers dropout weight_decay early_stopping max_degree data_split")

# trainig parameters
# flags.DEFINE_string('dataset', 'cora', 'Dataset string.') # 'cora', 'citeseer', 'pubmed'
# flags.DEFINE_string('model', 'gcn', 'Model string.') # 'gcn', 'gcn_cheby', 'dense'
# flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
# flags.DEFINE_integer('epochs', 200, 'Number of epochs to train.')
# flags.DEFINE_integer('hidden1', 16, 'Number of units in hidden layer 1.')
# flags.DEFINE_float('dropout', 0.5, 'Dropout rate (1 - keep probability).')
# flags.DEFINE_float('weight_decay', 5e-4, 'Weight for L2 loss on embedding matrix.')
# flags.DEFINE_integer('early_stopping', 10, 'Tolerance for early stopping (# of epochs).')
# flags.DEFINE_integer('max_degree', 3, 'Maximum Chebyshev polynomial degree.')
# trainig parameters:
# --------------------------------------------------------------------------------
# dataset: Selectes the dataset to run on
# model: Defines the type of layer to be applied
# learning_rate: Initial learning rate.
# epochs: Number of epochs to train.
# hidden1: Number of units in hidden layer 1.
# dropout: Dropout rate (1 - keep probability).
# weight_decay: Weight for L2 loss on embedding matrix.
# early_stopping: Tolerance for early stopping (# of epochs).
# max_degree: Maximum Chebyshev polynomial degree.
FLAGS = TrainingParameters('receipts', 'gcnx_cheby', 0.001, 200, 16, 2, 0.6, 5e-4, 10, 3, [.4, .2, .4])

# output classes
Expand Down
32 changes: 17 additions & 15 deletions python/model_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def onehot(idx, num_classes):
1-hot encoding.
"""
encoding = np.zeros(num_classes)
encoding[1] = 1
encoding[1] = idx
return encoding

def encode_labels(class_indices, num_classes):
Expand All @@ -29,32 +29,34 @@ def encode_labels(class_indices, num_classes):

def form_batch(features_dir, class2idx):
"""
Creates a single batch with all the graph instances from the training set. For that purpose,
it concatenates all the adjacency matrices into a (sparse) block-diagonal matrix
where each block corresponds to the adjacency matrix of one graph instance.
The individual graph adj matrix can be of different sizes.
Creates a single batch with all the graph instances from the training set.
For that purpose, it concatenates all the adjacency matrices into a (sparse)
block-diagonal matrix, where each block corresponds to the adjacency matrix
of one graph instance. The input graph adj matrices can be of different sizes.
Input: a list of M individual graph examples
- graphs: (list of tuples) a list of length M, where each tuple contains 3 matrices:
1. feature_matrix (N, 318): represents the nodes of the graph (words)
2. adjacency_matrix (N, N): represents the edgest of the graph
3. labels (N, 1): each element represents the category of a node (word tag)
- graphs: (list of tuples) each tuple contains 3 matrices:
1. feature_matrix (N, 318): the nodes of the graph (words)
2. adjacency_matrix (N, N): the edges of the graph
3. labels (N, 1): the class of each node (entity)
Output: a single graph containing M non-linkes independent sub-graphs
- batch: (tuple) A single graph represented as a tuple, containing 3 matrices:
1. feature_matrix (M*N, 318) : concatenation of all the input feature matrices
Output: a single graph containing M non-linked independent sub-graphs
- batch: (tuple) A graph, it consists of 3 matrices:
1. feature_matrix (M*N, 318) : concatenation of input features
2. adjacency_matrix (M*N, M*N): block-diagonal matrix
3. labels (M*N, 1): concatenation of all the input labels
"""
X = [] # batch feature matrix
A = [] # batch adjacency matrix
CI = [] # batch class indices (labels)

# maps each row in the output feature_matrix back to the individual graph where the row originated
# this allows to pool the predicted node scores back into each independent graph
# A numeri id maps each row in the output feature_matrix back to
# the individual graph where the row originated, this allows to
# map the predicted node scores back into each input graph
example_ids = []

feature_files, _, adj_matrix_files, entity_files = get_feature_filepaths(features_dir)
feature_files, _, adj_matrix_files, entity_files =\
get_feature_filepaths(features_dir)

for example_id, (f_file, am_file, e_file) in enumerate(zip(feature_files, adj_matrix_files, entity_files)):
features, adj_matrix = read_features(f_file, am_file)
Expand Down

0 comments on commit 52c7c0b

Please sign in to comment.