I am no longer abe to monitor this post , I have decided to move everything to my personal blog for better monitoring.
Please click here to access the full post
| import pytorch_lightning as pl | |
| import numpy as np | |
| import torch | |
| from torch.nn import MSELoss | |
| from torch.optim import Adam | |
| from torch.utils.data import DataLoader, Dataset | |
| import torch.nn as nn | |
| class SimpleDataset(Dataset): |
| # Create empty list | |
| S=[] | |
| # Range of clusters to try (2 to 10) | |
| K=range(2,11) | |
| # Select data for clustering model | |
| X = df_loc[['Latitude', 'Longitude']] | |
| for k in K: |
| class CustomLSTM(nn.Module): | |
| def __init__(self, input_sz, hidden_sz): | |
| super().__init__() | |
| self.input_sz = input_sz | |
| self.hidden_size = hidden_sz | |
| self.W = nn.Parameter(torch.Tensor(input_sz, hidden_sz * 4)) | |
| self.U = nn.Parameter(torch.Tensor(hidden_sz, hidden_sz * 4)) | |
| self.bias = nn.Parameter(torch.Tensor(hidden_sz * 4)) | |
| self.init_weights() | |
| class NaiveCustomLSTM(nn.Module): | |
| def __init__(self, input_sz: int, hidden_sz: int): | |
| super().__init__() | |
| self.input_size = input_sz | |
| self.hidden_size = hidden_sz | |
| #i_t | |
| self.U_i = nn.Parameter(torch.Tensor(input_sz, hidden_sz)) | |
| self.V_i = nn.Parameter(torch.Tensor(hidden_sz, hidden_sz)) | |
| self.b_i = nn.Parameter(torch.Tensor(hidden_sz)) |
| # Our drawing graph functions. We rely / have borrowed from the following | |
| # python libraries: | |
| # https://github.com/szagoruyko/pytorchviz/blob/master/torchviz/dot.py | |
| # https://github.com/willmcgugan/rich | |
| # https://graphviz.readthedocs.io/en/stable/ | |
| def draw_graph(start, watch=[]): | |
| from graphviz import Digraph |
I am no longer abe to monitor this post , I have decided to move everything to my personal blog for better monitoring.
Please click here to access the full post
| class EncoderCNN(nn.Module): | |
| def __init__(self, embed_size): | |
| super(EncoderCNN, self).__init__() | |
| resnet = models.resnet50(pretrained=True) | |
| for param in resnet.parameters(): | |
| param.requires_grad_(False) | |
| modules = list(resnet.children())[:-1] | |
| self.resnet = nn.Sequential(*modules) | |
| self.embed = nn.Linear(resnet.fc.in_features, embed_size) |
| import time | |
| from random import randrange | |
| from multiprocessing.pool import ThreadPool | |
| from tqdm import tqdm | |
| def func_call(position, total): | |
| text = 'progressbar #{position}'.format(position=position) |
| # This code doesn't work, and isn't intended to. | |
| # The goal of this code is to explain how attention mechansisms work, in code. | |
| # It is deliberately not vectorized to make it clearer. | |
| def attention(self, X_in:List[Tensor]): | |
| # For every token transform previous layer's out | |
| for i in range(self.sequence_length): | |
| query[i] = self.Q * X_in[i] | |
| key[i] = self.K * X_in[i] | |
| value[i] = self.V * X_in[i] |
| """ Implementation of OKapi BM25 with sklearn's TfidfVectorizer | |
| Distributed as CC-0 (https://creativecommons.org/publicdomain/zero/1.0/) | |
| """ | |
| import numpy as np | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| from scipy import sparse | |
| class BM25(object): |