
3 respostas

Hi Tom,Greetings to you!Use this code :- def construct_examples(numericalized_sentences, vocabulary, num_examples=int(1e6), n=5, sg=True, k=0): examples = [] while True: ⌗ TODO: select a random sentence index using random.randint and get that ⌗ sentence. Be careful to avoid indexing errors. sentence_idx = random.randint(0,len(numericalized_sentences)-1) sentence = numericalized_sentences[sentence_idx] ⌗ TODO: Select a random window index using random.randint ⌗ and obtain that window of size n. Be careful to avoid indexing errors. window_idx = random.randint(0,len(sentence)-1) window = sentence[window_idx:n] if len(window) <= n//2: continue ⌗ TODO: Get the center word and the context words center_word = window[int(round(len(window)/2))] context_words = window context_words.remove(center_word) ⌗ TODO: Create examples using the guidelines above if sg: ⌗ if Skip-Gram context_word = context_words[random.randint(0, len(context_words)-1)] example = [center_word, context_word] else: ⌗ if CBOW example = [context_words, center_word] if len(window) < n: continue if k > 0: ⌗ if doing negative sampling samples = [random.randint(0, len(vocabulary.index_to_word)-1) for _ in range(k)] example.append(samples) examples.append(example) if len(examples) >= num_examples: break return examplesI hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.Thanks and Regards,Deepali Kulshrestha