This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.decomposition import PCA | |
def project(words, phrases, model): | |
embeddings = [] | |
associated_words = [] | |
pca = PCA(n_components=2) | |
for word in words: | |
associated_words.append(word) | |
embeddings.append(model[word]) | |
for pphrase in phrases[word]: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ngram_paraphrase(words, phrases, ngram, factor=2): | |
sentences = [] | |
for i in range(0, len(words)-ngram): | |
phrase = '' | |
for j in range(0, ngram): | |
phrase += words[i+j] + ' ' | |
phrase = phrase.strip() | |
if not phrase in phrases: | |
continue | |
for p in list(phrases[phrase])[0:factor]: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def nn_sentence_aug(sentence, factor=2, model): | |
sentences = [] | |
words = sentence.split() | |
for i in range(len(words)): | |
ps = model.most_similar(words[i], topn=factor) | |
for p in ps: | |
new_words = words[0:i] + [p[0]] + words[i+1:] | |
sentences.append(' '.join(new_words)) | |
return sentences |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gensim | |
import gensim.downloader | |
model = gensim.downloader.load('glove-twitter-50') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dgl.nn.tensorflow import GATConv | |
class GAT(tf.keras.Model): | |
def __init__(self, feat_dim, hidden_dim, class_num): | |
super(GAT, self).__init__() | |
self.num_heads = 8 | |
self.h1 = GATConv(in_feats=feat_dim, out_feats=hidden_dim, feat_drop=0.5, num_heads=self.num_heads, allow_zero_in_degree=True) | |
self.h2 = GATConv(in_feats=hidden_dim*self.num_heads, out_feats=class_num, feat_drop=0.5, num_heads=1, allow_zero_in_degree=True) | |
def call(self, g, features): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dgl.nn.tensorflow import GINConv | |
class GIN(tf.keras.Model): | |
def mlp(feat_dim, hidden_dim, out_dim): | |
m = tf.keras.models.Sequential() | |
m.add(tf.keras.layers.Input(shape=(feat_dim,))) | |
m.add(tf.keras.layers.Dense(hidden_dim, activation='relu')) | |
m.add(tf.keras.layers.Dropout(0.5)) | |
m.add(tf.keras.layers.Dense(out_dim)) | |
return m |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dgl.nn.tensorflow import SAGEConv | |
class SAGE(tf.keras.Model): | |
def __init__(self, feat_dim, hidden_dim, class_num): | |
super(SAGE, self).__init__() | |
self.h1 = SAGEConv(in_feats=feat_dim, out_feats=hidden_dim, aggregator_type='pool', feat_drop=0.5, activation=tf.nn.relu) | |
self.h2 = SAGEConv(in_feats=feat_dim, out_feats=class_num, aggregator_type='pool') | |
def call(self, g, features): | |
h = features |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dgl.nn.tensorflow import GraphConv | |
class GCN(tf.keras.Model): | |
def __init__(self, feat_dim, hidden_dim, class_num): | |
super(GCN, self).__init__() | |
self.h1 = GraphConv(feat_dim, hidden_dim, activation=tf.nn.relu, allow_zero_in_degree=True) | |
self.dropout = tf.keras.layers.Dropout(0.5) | |
self.h2 = GraphConv(hidden_dim, class_num, allow_zero_in_degree=True) | |
def call(self, g, features): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dgl | |
import tensorflow as tf | |
def eval(model, g, node_ids_to_eval): | |
logits = model(g, g.ndata['feat'], training=False) | |
logits = tf.gather(logits, node_ids_to_eval) | |
labels = tf.gather(g.ndata['label'], node_ids_to_eval) | |
indices = tf.math.argmax(logits, axis=1) | |
acc = tf.reduce_mean(tf.cast(indices == labels, dtype=tf.float32)) | |
return acc.numpy().item() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for epoch in range(20): | |
with tf.GradientTape() as tape: | |
logits = g_model(g.ndata['feat']) | |
# Loss is only calculated on the training set. | |
loss = loss_fcn(g.ndata['label'][g.ndata['train_mask']], logits[g.ndata['train_mask']]) | |
grads = tape.gradient(loss, g_model.trainable_weights) | |
opt.apply_gradients(zip(grads, g_model.trainable_weights)) |