RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x3 and 20x10)

dpalate: <p>import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
import torch.optim as optim<br>
from typing import List<br>
from collections import Counter<br>
def build_vocab(words: List[str]):<br>
vocab = Counter()<br>
for w in words:<br>
vocab[w] += 1<br>
return vocab<br>
CONTEXT_SIZE = 3  # Define the context size. Default value 3, which means the context<br>
<span class="hashtag-raw">#includes</span> 3 words to the left, 3 to the right</p>
<p><span class="hashtag-raw">#In</span> Python, enclosing a string within triple quotes (either single ’ or double " quotes)<br>
<span class="hashtag-raw">#allows</span> you to create a multiline string literal</p>
<p>raw_text = “”“We are about to study the idea of a computational process.<br>
Computational processes are abstract beings that inhabit computers.<br>
As they evolve, processes manipulate other abstract things called data.<br>
The evolution of a process is directed by a pattern of rules<br>
called a program. People create programs to direct processes. In effect,<br>
we conjure the spirits of the computer with our spells.”“”.split()</p>
<p><span class="hashtag-raw">#print</span> (raw_text) - try it out<br>
vocab = build_vocab(raw_text)<br>
<span class="hashtag-raw">#print</span> (vocab) - try it out<br>
vocab_size = len(vocab)</p>
<p>print (‘vocab_size’,vocab_size)</p>
<p>word_to_idx = {word: i for i, word in enumerate(vocab)}<span class="hashtag-raw">#index</span> for each word in vocab<br>
idx_to_word = list(vocab)<span class="hashtag-raw">#list</span> of words in the vocab</p>
<p><span class="hashtag-raw">#print</span> (‘word_to_idx’, word_to_idx) - try it out<br>
<span class="hashtag-raw">#print</span> (‘idx_to_word’,idx_to_word) - try it out</p>
<p>word_indices = [word_to_idx[w] for w in raw_text]<span class="hashtag-raw">#Every</span> word in the raw text has an index</p>
<p><span class="hashtag-raw">#print</span> (‘word_indices’,word_indices)<br>
<span class="hashtag-raw">#print</span> (‘len(word_indices)’, len(word_indices))</p>
<p><span class="hashtag-raw">#The</span> function prepare_data creates a data sample with 6 context words and the target word between them<br>
def prepare_data(word_indices):<br>
data = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
for i in range(CONTEXT_SIZE, len(word_indices) - CONTEXT_SIZE):</p>
<pre><code>    #### START YOUR CODE ####
    # Hint: You can intialize context to an empty list
    # and then use a for loop to append elements to context properly.
    context = []
    
    # Collect the context words around the target word
    for j in range(i - CONTEXT_SIZE, i + CONTEXT_SIZE + 1):
        if j != i:  # Avoid adding the target word itself
            context.append(word_indices[j])
    
    target = word_indices[i]
    #### END YOUR CODE ####

    data.append((context, target))

return data
</code></pre>
<p>print(type(vocab))<br>
print(len(vocab))<br>
print(type(word_indices))<br>
print(len(word_indices))</p>
<h1><a name="p-460207-test-task-1-do-not-change-the-code-below-1" class="anchor" href="#p-460207-test-task-1-do-not-change-the-code-below-1"></a>Test Task 1. Do not change the code below.</h1>
<p>data = prepare_data(word_indices)<br>
print (‘length of data’, len(data))<br>
print(‘data[0]:’, data[0])<br>
ctx, tgt = data[0]<br>
print(‘context words:’, [idx_to_word[c] for c in ctx])<br>
print(‘target word:’, idx_to_word[tgt])<br>
class CBOW(nn.Module):<br>
def <strong>init</strong>(self, vocab_size, embedding_dim, no_of_samples, embed_weights):<br>
super(CBOW, self).<strong>init</strong>()</p>
<pre><code>    #### START YOUR CODE ####
    #Hint use nn.Embedding() if no initial pre-trained embeddings in embed_weights are provided
    #Use nn.Embedding.from_pretrained() if embed_weights are provided.
    if embed_weights is None:
        self.embeddings = nn.Embedding(vocab_size, embedding_dim)
    else:
        self.embeddings = nn.Embedding.from_pretrained(embed_weights, freeze=False)
    
    self.linear = nn.Linear(embedding_dim, vocab_size)
    self.act = nn.LogSoftmax(dim=-1)

def forward(self, inputs):
    
    
    #### START YOUR CODE ####
    embeds = self.embeddings(inputs)
    
    # Sum embeddings along the context words dimension (assuming inputs are [batch_size, context_size])
    embeds = torch.sum(embeds, dim=1)
    
    
    #### END YOUR CODE ####
    
    out = self.linear(embeds)
    out = self.act(out)

    return out
</code></pre>
<h1><a name="p-460207-test-task-2-do-not-change-the-code-blow-2" class="anchor" href="#p-460207-test-task-2-do-not-change-the-code-blow-2"></a>Test Task 2. Do not change the code blow</h1>
<p>torch.manual_seed(0)</p>
<p>m = CBOW(10, 20, 3, embed_weights=None)<br>
test_input = torch.tensor([1,2,3], dtype=torch.long)</p>
<p>test_output = m(test_input)</p>
<p>print(‘test_output.shape’, test_output.shape)<br>
print(‘test_output’, test_output.data)</p>
<h2><a name="p-460207-output-3" class="anchor" href="#p-460207-output-3"></a>OUTPUT:</h2>
<p>RuntimeError                              Traceback (most recent call last)<br>
~\AppData\Local\Temp/ipykernel_22152/2979772149.py in <br>
5 test_input = torch.tensor([1,2,3], dtype=torch.long)<br>
6<br>
----&gt; 7 test_output = m(test_input)<br>
8<br>
9 print(‘test_output.shape’, test_output.shape)</p>
<p>~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _wrapped_call_impl(self, *args, **kwargs)<br>
1530             return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]<br>
1531         else:<br>
 → 1532             return self._call_impl(*args, **kwargs)<br>
1533<br>
1534     def _call_impl(self, *args, **kwargs):</p>
<p>~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *args, **kwargs)<br>
1539                 or _global_backward_pre_hooks or _global_backward_hooks<br>
1540                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1541             return forward_call(*args, **kwargs)<br>
1542<br>
1543         try:</p>
<p>~\AppData\Local\Temp/ipykernel_22152/3066800851.py in forward(self, inputs)<br>
26         #### END YOUR CODE ####<br>
27<br>
—&gt; 28         out = self.linear(embeds)<br>
29         out = self.act(out)<br>
30</p>
<p>~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _wrapped_call_impl(self, *args, **kwargs)<br>
1530             return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]<br>
1531         else:<br>
 → 1532             return self._call_impl(*args, **kwargs)<br>
1533<br>
1534     def _call_impl(self, *args, **kwargs):</p>
<p>~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *args, **kwargs)<br>
1539                 or _global_backward_pre_hooks or _global_backward_hooks<br>
1540                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1541             return forward_call(*args, **kwargs)<br>
1542<br>
1543         try:</p>
<p>~\anaconda3\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)<br>
114<br>
115     def forward(self, input: Tensor) → Tensor:<br>
 → 116         return F.linear(input, self.weight, self.bias)<br>
117<br>
118     def extra_repr(self) → str:</p>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x3 and 20x10)</p>