Skip to content

Commit 596b710

Browse files
Neural-Link Teamtensorflow-copybara
authored andcommitted
Include id to features
PiperOrigin-RevId: 310444078
1 parent edead04 commit 596b710

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

neural_structured_learning/examples/graph_keras_mlp_cora.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,16 @@ def parse_example(example_proto):
125125
nbr_feature_key = '{}{}_{}'.format(NBR_FEATURE_PREFIX, i, 'words')
126126
nbr_weight_key = '{}{}{}'.format(NBR_FEATURE_PREFIX, i,
127127
NBR_WEIGHT_SUFFIX)
128+
nbr_id_key = '{}{}_{}'.format(NBR_FEATURE_PREFIX, i, 'id')
128129
feature_spec[nbr_feature_key] = tf.io.FixedLenFeature(
129130
[hparams.max_seq_length],
130131
tf.int64,
131132
default_value=tf.constant(
132133
0, dtype=tf.int64, shape=[hparams.max_seq_length]))
133134
feature_spec[nbr_weight_key] = tf.io.FixedLenFeature(
134135
[1], tf.float32, default_value=tf.constant([0.0]))
136+
feature_spec[nbr_id_key] = tf.io.FixedLenFeature(
137+
(), tf.string, default_value='')
135138

136139
features = tf.io.parse_single_example(example_proto, feature_spec)
137140

neural_structured_learning/examples/preprocess/cora/preprocess_cora_dataset.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ def _int64_feature(*value):
9595
return tf.train.Feature(int64_list=tf.train.Int64List(value=list(value)))
9696

9797

98+
def _bytes_feature(value):
99+
"""Returns bytes tf.train.Feature from a string."""
100+
return tf.train.Feature(
101+
bytes_list=tf.train.BytesList(value=[value.encode('utf-8')]))
102+
103+
98104
def parse_cora_content(in_file, train_percentage):
99105
"""Converts the Cora content (in TSV) to `tf.train.Example` instances.
100106
@@ -132,13 +138,14 @@ def parse_cora_content(in_file, train_percentage):
132138
entries = line.rstrip('\n').split('\t')
133139
# entries contains [ID, Word1, Word2, ..., Label]; 'Words' are 0/1 values.
134140
words = map(int, entries[1:-1])
141+
example_id = entries[0]
135142
features = {
143+
'id': _bytes_feature(example_id),
136144
'words': _int64_feature(*words),
137145
'label': _int64_feature(label_index[entries[-1]]),
138146
}
139147
example_features = tf.train.Example(
140148
features=tf.train.Features(feature=features))
141-
example_id = entries[0]
142149
if random.uniform(0, 1) <= train_percentage: # for train/test split.
143150
train_examples[example_id] = example_features
144151
else:

0 commit comments

Comments
 (0)