Best Python code snippet using molecule_python
lstm_pos.py
Source:lstm_pos.py
...3import torch.nn as nn4import torch.nn.functional as F5import torch.optim as optim6torch.manual_seed(1)7def prepare_sequence(seq, to_ix):8 idxs = [to_ix[w] for w in seq]9 return torch.tensor(idxs, dtype=torch.long)10training_data = [11 # Tags are: DET - determiner; NN - noun; V - verb12 # For example, the word "The" is a determiner13 ("The dog ate the apple".split(), ["DET", "NN", "V", "DET", "NN"]),14 ("Everybody read that book".split(), ["NN", "V", "DET", "NN"])15]16word_to_ix = {}17char_to_ix = {}18# For each words-list (sentence) and tags-list in each tuple of training_data19for sent, _ in training_data:20 for word in sent:21 if word not in word_to_ix: # word has not been assigned an index yet22 word_to_ix[word] = len(word_to_ix) # Assign each word with a unique index23 for char in word:24 if char not in char_to_ix:25 char_to_ix[char] = len(char_to_ix)26print(word_to_ix)27# {'The': 0, 'dog': 1, 'ate': 2, 'the': 3, 'apple': 4, 'Everybody': 5, 'read': 6, 'that': 7, 'book': 8}28print(char_to_ix)29# {'T': 0, 'h': 1, 'e': 2, 'd': 3, 'o': 4, 'g': 5, 'a': 6, 't': 7, 'p': 8, 'l': 9, 'E': 10, 'v': 11, 'r': 12, 'y': 13, 'b': 14, 'k': 15}30tag_to_ix = {"DET": 0, "NN": 1, "V": 2}31EMBEDDING_DIM_WORD = 632HIDDEN_DIM_WORD = 633EMBEDDING_DIM_CHAR = 334HIDDEN_DIM_CHAR = 335class LSTMTagger(nn.Module):36 def __init__(self, embedding_dim_word, hidden_dim_word, embedding_dim_char, hidden_dim_char, vocab_size_word, vocab_size_char, tagset_size):37 super(LSTMTagger, self).__init__()38 self.word_embeddings = nn.Embedding(vocab_size_word, embedding_dim_word)39 self.char_embeddings = nn.Embedding(vocab_size_char, embedding_dim_char)40 self.hidden_dim_word = hidden_dim_word41 self.hidden_dim_char = hidden_dim_char42 # The LSTM takes word embeddings as inputs, and outputs hidden states43 # with dimensionality hidden_dim.44 self.lstm_char = nn.LSTM(embedding_dim_char, hidden_dim_char)45 self.lstm_word = nn.LSTM(embedding_dim_word+embedding_dim_char, hidden_dim_word)46 # The linear layer that maps from hidden state space to tag space47 self.hidden2tag = nn.Linear(hidden_dim_word, tagset_size)48 self.hidden_word = self.init_hidden(self.hidden_dim_word)49 self.hidden_char = self.init_hidden(self.hidden_dim_char)50 def init_hidden(self, size):51 return (torch.zeros(1,1,size), torch.zeros(1,1,size))52 def forward(self, word_sequence, char_sequence):53 word_embeds = self.word_embeddings(word_sequence)54 char_embeds = self.char_embeddings(char_sequence)55 56 #print(char_embeds.view(len(char_sequence), 1, -1))57 lstm_char_out, self.hidden_char = self.lstm_char(char_embeds.view(len(char_sequence), 1, -1), self.hidden_char)58 concat = torch.cat([word_embeds.view(1,1,-1), lstm_char_out[-1].view(1,1,-1)], dim=2)59 lstm_word_out, self.hidden_word = self.lstm_word(concat, self.hidden_word)60 61 tag_space = self.hidden2tag(lstm_word_out.view(1, -1))62 tag_scores = F.log_softmax(tag_space, dim=1)63 64 return tag_scores65model = LSTMTagger(EMBEDDING_DIM_WORD, HIDDEN_DIM_WORD, EMBEDDING_DIM_CHAR, HIDDEN_DIM_CHAR, len(word_to_ix), len(char_to_ix), len(tag_to_ix))66loss_function = nn.NLLLoss()67optimizer = optim.SGD(model.parameters(), lr=0.1)68with torch.no_grad():69 for word in training_data[0][0]:70 word_sequence = prepare_sequence([word], word_to_ix)71 print(word_sequence)72 char_sequence = prepare_sequence(word, char_to_ix)73 print(char_sequence)74 tag_scores = model(word_sequence, char_sequence)75 print(tag_scores, tag_scores.argmax(dim=1))76for epoch in range(300):77 for sentence, tags in training_data:78 model.zero_grad()79 # word hidden init by sentence80 model.hidden_word = model.init_hidden(model.hidden_dim_word)81 for index, word in enumerate(sentence):82 # char hidden init by word83 model.hidden_char = model.init_hidden(model.hidden_dim_char)84 word_sequence = prepare_sequence([word], word_to_ix)85 char_sequence = prepare_sequence(word, char_to_ix)86 targets = prepare_sequence([tags[index]], tag_to_ix)87 tag_scores = model(word_sequence, char_sequence)88 loss = loss_function(tag_scores, targets)89 loss.backward(retain_graph=True)90 optimizer.step()91with torch.no_grad():92 for word in training_data[0][0]:93 word_sequence = prepare_sequence([word], word_to_ix)94 char_sequence = prepare_sequence(word, char_to_ix)95 tag_scores = model(word_sequence, char_sequence)...
task2.py
Source:task2.py
...15 for epoch in range(5):16 model.train()17 predictions = []18 for (words, tags) in zip(trainset["word"], trainset["tag"]):19 sentence_in = prepare_sequence(words, word_to_ix)20 targets = prepare_sequence(tags, labeldict)21 tags = tags.type(torch.FloatTensor)22 tags_pred = model(words)23 loss = F.binary_cross_entropy(tags)24 optimizer.zero_grad()25 loss.backward()26 optimizer.step()2728 predictions += list(tags_pred.detach().numpy())29 print("Epoch: %d, loss: %.5f" % (epoch+1, loss.item()))303132def test(model, test):33 test_losses = []34 model.eval()35 test_loss = 036 correct = 037 with torch.no_grad():38 for (data, target) in zip(test["word"], test["tag"]):39 for (word, tag) in zip(data, target):40 output = model(data)41 test_loss += F.nll_loss(output, target, size_average=False).item()42 pred = output.data.max(1, keepdim=True)[1]43 correct += pred.eq(target.data.view_as(pred)).sum()44 test_loss /= len(testloader.dataset)45 test_losses.append(test_loss)46 print('\nTest set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(47 test_loss, correct, len(testloader.dataset),48 100. * correct / len(testloader.dataset)))4950word_to_ix = {}5152def word_index(data):53 for sentence in data:54 for word in sentence:55 if word not in word_to_ix:56 word_to_ix[word] = len(word_to_ix)57 return word_to_ix585960def encoding_labels(data):61 possible = set(data)62 possible_labels = list(possible)63 label_dict = {}64 for index, possible_label in enumerate(possible_labels):65 label_dict[possible_label] = index6667 return label_dict6869def prepare_sequence(seq, to_ix):70 idxs = [to_ix[w] for w in seq]71 return torch.tensor(idxs, dtype=torch.long)7273def encode_dataset(data):74 tokenizer = BertTokenizerFast.from_pretrained('bert-base-cased')75 encoded_data = tokenizer(data,76 padding=True,77 return_tensors='pt',)7879 input_ids = encoded_data["input_ids"]8081 model = BertModel.from_pretrained('bert-base-cased')82 return model8384def lstm_train(word_to_ix, labeldict, trainset, embedder):85 model_lstm = LSTM(6, len(word_to_ix), len(labeldict), embedder)86 loss_function = nn.NLLLoss()87 optimizer_lstm = optim.SGD(model_lstm.parameters(), lr=0.1)8889 for epoch in range(5):90 for (words, tags) in zip(trainset["word"], trainset["tag"]):9192 model_lstm.zero_grad()93 sentence_in = prepare_sequence(words, word_to_ix)94 targets = prepare_sequence(tags, labeldict)95 tag_scores = model_lstm(sentence_in)9697 loss = loss_function(tag_scores, targets)98 loss.backward()99 optimizer_lstm.step()100 with torch.no_grad():101 inputs = prepare_sequence(trainset["word"], word_to_ix)102 tag_scores = model_lstm(inputs)103104105if __name__ == '__main__':106 parser = argparse.ArgumentParser(description="Task 2")107 parser.add_argument("--output_dir", dest="output_dir", type=str, default=".")108109 args = parser.parse_args()110 dataset = load_dataset("loading_script.py")111 trainset = dataset["train"]112 testset = dataset["test"]113 trainloader = torch.utils.data.DataLoader(trainset,114 shuffle=False, num_workers=0)115 testloader = torch.utils.data.DataLoader(testset,
...
LSTM.py
Source:LSTM.py
...14print(out)15print(hidden)16# ----------- LSTMTagger ------------17print('-----------LSTMTagger------------')18def prepare_sequence(seq, to_ix):19 idxs = [to_ix[w] for w in seq]20 return torch.tensor(idxs, dtype=torch.long)21training_data = [22 ("The dog ate the apple".split(), ["DET", "NN", "V", "DET", "NN"]),23 ("Everybody read that book".split(), ["NN", "V", "DET", "NN"])24]25word_to_ix = {}26for sent, tag in training_data:27 for word in sent:28 if word not in word_to_ix:29 word_to_ix[word] = len(word_to_ix)30print(word_to_ix)31tag_to_ix = {'DET': 0, 'NN': 1, 'V': 2}32EMBEDDING_DIM = 633HIDDEN_DIM = 634class LSTMTagger(nn.Module):35 def __init__(self, embedding_dim, hidden_dim, vocab_size, target_size):36 super(LSTMTagger, self).__init__()37 self.hidden_dim = hidden_dim38 self.word_embedding = nn.Embedding(vocab_size, embedding_dim)39 self.lstm = nn.LSTM(embedding_dim, hidden_dim)40 self.hidden_dim2tag = nn.Linear(hidden_dim, target_size)41 self.hidden = self.init_hidden()42 def init_hidden(self):43 return (torch.zeros(1, 1, self.hidden_dim),44 (torch.zeros(1, 1, self.hidden_dim)))45 def forward(self, sentence):46 embeds = self.word_embedding(sentence)47 lstm_out, self.hidden = self.lstm(embeds.view(len(sentence), 1, -1),48 self.hidden)49 tag_space = self.hidden_dim2tag(lstm_out.view(len(sentence), -1))50 tag_scores = F.log_softmax(tag_space, dim=1)51 return tag_scores52model = LSTMTagger(EMBEDDING_DIM, HIDDEN_DIM, len(word_to_ix), len(tag_to_ix))53loss_function = nn.NLLLoss()54optimizer = optim.SGD(model.parameters(), lr=0.1)55with torch.no_grad():56 inputs = prepare_sequence(training_data[0][0], word_to_ix)57 tag_scores = model(inputs)58 print(tag_scores)59for epoch in range(300):60 for sentence, tags in training_data:61 model.zero_grad()62 model.hidden = model.init_hidden()63 sentence_in = prepare_sequence(sentence, word_to_ix)64 targets = prepare_sequence(tags, tag_to_ix)65 tag_scores = model(sentence_in)66 loss = loss_function(tag_scores, targets)67 loss.backward()68 optimizer.step()69with torch.no_grad():70 inputs = prepare_sequence(training_data[0][0], word_to_ix)71 tag_scores = model(inputs)72 print(tag_scores)73# ------------- Bi-LSTM CRF ---------------...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!