Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save embedding vectors of GCN #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/openne/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,8 @@ def main(args):
epoch=args.epochs, learning_rate=args.lr, weight_decay=args.weight_decay)
t2 = time.time()
print(t2-t1)
if args.method != 'gcn':
print("Saving embeddings...")
model.save_embeddings(args.output)
print("Saving embeddings...")
model.save_embeddings(args.output)
if args.label_file and args.method != 'gcn':
vectors = model.vectors
X, Y = read_node_label(args.label_file)
Expand Down
23 changes: 19 additions & 4 deletions src/openne/gcn/gcnAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def __init__(self, graph, learning_rate=0.01, epochs=200,
# Init variables
self.sess.run(tf.global_variables_initializer())

self.vectors = {}

cost_val = []

# Train model
Expand All @@ -51,17 +53,17 @@ def __init__(self, graph, learning_rate=0.01, epochs=200,
feed_dict.update({self.placeholders['dropout']: self.dropout})

# Training step
outs = self.sess.run(
[self.model.opt_op, self.model.loss, self.model.accuracy], feed_dict=feed_dict)
self.outs = self.sess.run(
[self.model.opt_op, self.model.loss, self.model.accuracy, self.model.layers[0].embedding], feed_dict=feed_dict)

# Validation
cost, acc, duration = self.evaluate(self.val_mask)
cost_val.append(cost)

# Print results
print("Epoch:", '%04d' % (epoch + 1), "train_loss=", "{:.5f}".format(outs[1]),
print("Epoch:", '%04d' % (epoch + 1), "train_loss=", "{:.5f}".format(self.outs[1]),
"train_acc=", "{:.5f}".format(
outs[2]), "val_loss=", "{:.5f}".format(cost),
self.outs[2]), "val_loss=", "{:.5f}".format(cost),
"val_acc=", "{:.5f}".format(acc), "time=", "{:.5f}".format(time.time() - t))

if epoch > self.early_stopping and cost_val[-1] > np.mean(cost_val[-(self.early_stopping+1):-1]):
Expand All @@ -74,6 +76,19 @@ def __init__(self, graph, learning_rate=0.01, epochs=200,
print("Test set results:", "cost=", "{:.5f}".format(test_cost),
"accuracy=", "{:.5f}".format(test_acc), "time=", "{:.5f}".format(test_duration))

self.embeddings = self.outs[3]
for i, embedding in enumerate(self.embeddings):
self.vectors[i] = embedding

# save embedding function
def save_embeddings(self, filename):
fout = open(filename, 'w')
node_num = len(self.vectors)
fout.write("{} {}\n".format(node_num, self.hidden1))
for node, vec in self.vectors.items():
fout.write("{} {}\n".format(node, ' '.join([str(x) for x in vec])))
fout.close()

# Define model evaluation function

def evaluate(self, mask):
Expand Down
2 changes: 1 addition & 1 deletion src/openne/gcn/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,5 @@ def _call(self, inputs):
# bias
if self.bias:
output += self.vars['bias']

self.embedding = output
return self.act(output)