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
| #by deepmind | |
| import tensorflow as tf | |
| import sonnet as snt | |
| #setup a 'module' | |
| mlp = snt.Sequential([ | |
| snt.Linear(1024), | |
| tf.nn.relu, | |
| snt.Linear(10), | |
| ]) |
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
| #Step 1 - Import biopython modules for sequence alignment | |
| from Bio import pairwise2 | |
| from Bio.Seq import Seq | |
| #Step 2- define multiple sequences | |
| seq1 = Seq("ACCGGT") | |
| seq2 = Seq("ACGT") | |
| #step 3 - align | |
| alignments = pairwise2.align.globalxx(seq1, seq2) |
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
| greeting_inputs = ("hey", "good morning", "good evening", "morning", "evening", "hi", "whatsup") | |
| greeting_responses = ["hey", "hey hows you?", "*nods*", "hello, how you doing", "hello", "Welcome, I am good and you"] | |
| def generate_greeting_response(greeting): | |
| for token in greeting.split(): | |
| if token.lower() in greeting_inputs: | |
| return random.choice(greeting_responses) |
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 the Official "Reformer" Repository) | |
| def hash_vectors(self, vecs, rng): | |
| # If we factorize the hash, find a factor dividing n_buckets nicely. | |
| rot_size, factor_list = self.n_buckets, [self.n_buckets] | |
| if self._factorize_hash: | |
| # If we are given a list of factors, verify it and use later. | |
| if isinstance(self._factorize_hash, list): | |
| rot_size, product = 0, 1 | |
| factor_list = self._factorize_hash |
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
| #Author - @greentfrapp | |
| def attention(self, query, key, value): | |
| # Equation 1 in Vaswani et al. (2017) | |
| # Scaled dot product between Query and Keys | |
| output = tf.matmul(query, key, transpose_b=True) / (tf.cast(tf.shape(query)[2], tf.float32) ** 0.5) | |
| # Softmax to get attention weights | |
| attention_weights = tf.nn.softmax(output) | |
| # Multiply weights by Values | |
| weighted_sum = tf.matmul(attention_weights, value) |
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 torch.nn as nn | |
| import torch.nn.functional as F | |
| class Net(nn.Module): | |
| def __init__(self): | |
| super(Net, self).__init__() | |
| self.conv1 = nn.Conv2d(3, 6, 5) | |
| self.pool = nn.MaxPool2d(2, 2) | |
| self.conv2 = nn.Conv2d(6, 16, 5) |
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 fastText import load_model | |
| classifier = load_model("model_tweet.bin") | |
| texts = ['Life is good', 'Life is great', 'Life is bad'] | |
| labels = classifier.predict(texts) | |
| print (labels) |
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
| #preprocessing | |
| import nltk | |
| #word2vec mode | |
| import gensim | |
| text_sample="""Renewed fighting has broken out in South Sudan between forces loyal to the president and vice-president. A reporter in the capital, Juba, told the BBC gunfire and large explosions could be heard all over the city; he said heavy artillery was being used. More than 200 people are reported to have died in clashes since Friday. The latest violence came hours after the UN Security Council called on the warring factions to immediately stop the fighting. In a unanimous statement, the council condemned the violence "in the strongest terms" and expressed "particular shock and outrage" at attacks on UN sites. It also called for additional peacekeepers to be sent to South Sudan. | |
| Chinese media say two Chinese UN peacekeepers have now died in Juba. Several other peacekeepers have been injured, as well as a number of civilians who have been caught in crossfire. The latest round of violence erupted when troops loyal to President Salva Kiir and first Vic |
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 cosine_similarity_ngrams(a, b): | |
| vec1 = Counter(a) | |
| vec2 = Counter(b) | |
| intersection = set(vec1.keys()) & set(vec2.keys()) | |
| numerator = sum([vec1[x] * vec2[x] for x in intersection]) | |
| sum1 = sum([vec1[x]**2 for x in vec1.keys()]) | |
| sum2 = sum([vec2[x]**2 for x in vec2.keys()]) | |
| denominator = math.sqrt(sum1) * math.sqrt(sum2) |
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 torch as th | |
| g = dgl.DGLGraph() | |
| g.add_nodes(10) | |
| # A couple edges one-by-one | |
| for i in range(1, 4): | |
| g.add_edge(i, 0) | |
| # A few more with a paired list | |
| src = list(range(5, 8)); dst = [0]*3 |