CONTEXT_SIZE = 2 # 2 words to the left, 2 to the right raw_text = """We are about to study the idea of a computational process. Computational processes are abstract beings that inhabit computers. As they evolve, processes manipulate other abstract things called data. The evolution of a process is directed by a pattern of rules called a program. People create programs to direct processes. In effect, we conjure the spirits of the computer with our spells.""".split() # By deriving a set from `raw_text`, we deduplicate the array vocab = set(raw_text) vocab_size = len(vocab) word_to_ix = {word: i for i, word in enumerate(vocab)} data = [] for i in range(CONTEXT_SIZE, len(raw_text) - CONTEXT_SIZE): context = ( [raw_text[i - j - 1] for j in range(CONTEXT_SIZE)] + [raw_text[i + j + 1] for j in range(CONTEXT_SIZE)] ) target = raw_text[i] data.append((context, target)) print(data[:5]) class CBOW([nn.Module](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module "torch.nn.Module")): def __init__(self): pass def forward(self, inputs): pass # Create your model and train. Here are some functions to help you make # the data ready for use by your module. def make_context_vector(context, word_to_ix): idxs = [word_to_ix[w] for w in context] return [torch.tensor](https://docs.pytorch.org/docs/stable/generated/torch.tensor.html#torch.tensor "torch.tensor")(idxs, dtype=[torch.long](https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch.dtype "torch.dtype")) make_context_vector(data[0][0], word_to_ix) # example