Skip to content

Instantly share code, notes, and snippets.

@eileen-code4fun
eileen-code4fun / project_we.py
Created January 15, 2022 15:09
Project Paraphrases Embedding
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]:
@eileen-code4fun
eileen-code4fun / ppdb_aug.py
Last active January 15, 2022 15:13
Paraphrase Augmentation
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]:
@eileen-code4fun
eileen-code4fun / we_aug.py
Created January 15, 2022 14:26
Word Embedding Augmentation
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
@eileen-code4fun
eileen-code4fun / load_wm.py
Last active January 15, 2022 14:59
Load Word Embeddings
import gensim
import gensim.downloader
model = gensim.downloader.load('glove-twitter-50')
@eileen-code4fun
eileen-code4fun / gan_def.py
Created January 7, 2022 20:05
Graph Attention Network Definition
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):
@eileen-code4fun
eileen-code4fun / gin_def.py
Created January 7, 2022 20:04
GIN Definition
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
@eileen-code4fun
eileen-code4fun / sage_def.py
Last active January 7, 2022 20:04
GraphSAGE Definition
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
@eileen-code4fun
eileen-code4fun / gcn_def.py
Created January 7, 2022 19:58
GCN Definition
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):
@eileen-code4fun
eileen-code4fun / gnn_training.py
Last active January 7, 2022 20:29
GNN Training
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()
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))