<jupyter_start><jupyter_text>Configs<jupyter_code># define all the configurations here-
MAX_LEN = 128
TRAIN_BATCH_SIZE = 16
VALID_BATCH_SIZE = 16
EPOCHS = 10<jupyter_output><empty_output><jupyter_text>LSTM MODEL<jupyter_code>import torch 
import torch.nn as nn
class LSTM(nn.Module):
    def __init__(self, embedding_matrix):
        super(LSTM,self).__init__
        
        # num_words, emedding_dim = rows & columns of embedding_matrix 
        num_words, emeded_dim = embedding_matrix.shape[0], embedding_matrix.shape[1]
        
        self.embedding = nn.Embedding(num_embeddings=num_words, embedding_dim= embeded_dim)
        
        self.embedding.weight = nn.Parameter(embedding_matrix, requires_grad=False, dtype=torch.float32)
        
        self.lstm = nn.LSTM(embeded_dim, 128, bidirectional=True, batch_first=True)
        
        self.out = nn.Linear(512, 1)
        
    
    def forward(self, x):
        
        x = self.embedding(x)
        x, _ = self.lstm(x)
        
        avg_pool = torch.mean(x, 1)
        max_pool = torch.max(x, 1)
        
        out = torch.cat((avg_pool, max_pool), 1)
        
        out = self.out(out)
        
        return out<jupyter_output><empty_output><jupyter_text>Preparing dataset<jupyter_code>import torch
class IMDBDataset():
    def __init__(self, review, target):
        """
        :param review: dataframe
        :param target: 
        """
        self.review = review
        self.target = target
        
    def __len__(self):
        return len(self.review)
    
    def __getitem__(self, idx):
        review = self.review[idx, :]
        target = self.target[idx]
        
        return {
            "reviews" : torch.tensor(review, dtype=torch.long),
            "targets" : torch.tensor(target, dtype=torch.float32)
        }<jupyter_output><empty_output><jupyter_text>Engine  Training<jupyter_code>import torch
import io
def load_vectors(fname):
    """
    This function returns embedding_dict, word:embedding_vectors
    """
    fin = io.open(fname, 'r', encoding='utf-8', newline='\n', errors='ignore')
    n, d = map(int, fin.readline().split())
    data = {}
    for line in fin:
        tokens = line.rstrip().split(' ')
        data[tokens[0]] = map(float, tokens[1:])
    return data


def create_embedding(words_idx, embedding_dict):
    """
    :param word_idx: 
    :param embedding_dict: a dictionary with word:embedding-vectors
    :return embedding_matrix: a dictionary with word_index:embedding_vectors
    """
    
    embedding_matrix = np.zeros(len(word_idx)+1, 300)
    
    for word, idx in words_idx.items():
        
        if word in embedding_dict:
            embedding_matrix[idx] = embedding_dict[word]
            
    return embedding_matrix




def run(fold):
    
    
    
    embedding_dict = load_vectors("../input/wiki-news-300d-1M.vec/wiki-news-300d-1M.vec")
    
    embedding_matrix<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11050663039782387, 'fraction_numerical': 0.013600816048962938, 'mean_word_length': 2.5921855921855923, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '141055', 'license': 'MIT', 'n_tokens_mistral': 1004, 'n_tokens_neox': 965, 'n_words': 180, 'path': 'text_classification_with_pytorch/notebook/trainingWithLSTM.ipynb', 'repo_name': 'rakeshku93/nlpProjects', 'size': '2941'}