Mat1 and mat2 shapes cannot be multiplied (1x2 and 27x8)

username: Azie265
cooked: <p><span class="hashtag-raw">#Importing</span> required modules<br>
import json<br>
from random import random<br>
import nltk<br>
import numpy as np<br>
import torch<br>
import  torch.nn as nn<br>
from torch.utils.data import Dataset,DataLoader<br>
from nltk.stem.porter import PorterStemmer<br>
stemmer=PorterStemmer()</p>
<p><span class="hashtag-raw">#Preprocessing</span> the data</p>
<p>def tokenize(s):<br>
return nltk.word_tokenize(s)</p>
<p>def stem(w):<br>
return stemmer.stem(w.lower())</p>
<p>def bag_words(ts,aw):<br>
ts=[stem(w) for w in ts]<br>
bag=np.zeros(len(aw),dtype=np.float32)<br>
for idx , w in enumerate(aw):<br>
if w in ts:<br>
bag[idx] = 1.0<br>
return bag</p>
<p><span class="hashtag-raw">#Loading</span> training data<br>
with open(‘knowledgev2.json’,‘r’) as file :<br>
intents=json.load(file)</p>
<p>words=[]<br>
tags=[]<br>
xy=[]</p>
<p>for i in intents[‘intents’]:<br>
tag=i[‘tag’]<br>
tags.append(tag)<br>
for pattern in i[‘patterns’]:<br>
z=tokenize(pattern)<br>
words.extend(z)<br>
xy.append((z,tag))</p>
<p>ignw=[‘?’,‘!’,‘:’,‘,’,‘;’,‘.’]<br>
words=[stem(w) for w in words if w not in ignw]<br>
words=sorted(set(words))<br>
tags=sorted(set(tags))</p>
<p>x_train=[]<br>
y_train=[]</p>
<p>for(ps,tag) in xy:<br>
bag=bag_words(ps,words)<br>
x_train.append(bag)<br>
label=tags.index(tag)<br>
y_train.append(label)</p>
<p>x_train=np.array(x_train)<br>
y_train=np.array(y_train)</p>
<p>class torchdataset(Dataset):<br>
def <strong>init</strong>(self):<br>
self.n_samples=len(x_train)<br>
self.x_data= torch.tensor(x_train, dtype=torch.float32)<br>
self.y_data=y_train</p>
<pre><code>def __getitem__(self,index):
        return self.x_data[index], self.y_data[index]

def __len__(self):
        return self.n_samples
</code></pre>
<p><span class="hashtag-raw">#hyper_param</span><br>
batch_size = 8<br>
hid_size = 8<br>
out_size = len(tags)<br>
in_size = len(x_train[0])<br>
learning_rate = 0.001<br>
num_epochs = 2000</p>
<p>dataset= torchdataset()<br>
train_loader=DataLoader(dataset=dataset,batch_size=batch_size,shuffle=True)</p>
<p>class chatmodel(nn.Module):<br>
def <strong>init</strong>(self,input_size,hidden_size,num_classes):<br>
super(chatmodel,self).<strong>init</strong>()<br>
self.l1=nn.Linear(input_size,hidden_size)<br>
self.l2=nn.Linear(hidden_size,hidden_size)<br>
self.l3=nn.Linear(hidden_size,num_classes)<br>
self.relu=nn.ReLU()</p>
<pre><code>def forward(self,x):

    out=self.l1(x)
    out=self.relu(out)
    out=self.l2(out)
    out=self.relu(out)
    out=self.l3(out)
    return out
</code></pre>
<p><span class="hashtag-raw">#trying</span> to work with GPU if possible</p>
<p>device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)<br>
model=chatmodel(in_size,hid_size,out_size).to(device)</p>
<p><span class="hashtag-raw">#loss</span> and optimizer</p>
<p>criterion = nn.CrossEntropyLoss()<br>
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)</p>
<p>for epoch in range(num_epochs):<br>
for(words,labels) in train_loader:<br>
words=words.to(device)<br>
labels=labels.to(device)</p>
<pre><code>    #forward
    outputs=model(words)
    labels=labels.type(torch.LongTensor)
    loss=criterion(outputs,labels)

    #backward and optimizer step
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

if(epoch+1) % 100 == 0:
    print(f'epoch {epoch+1}/{num_epochs},loss={loss.item():.4f}')
</code></pre>
<p>print(f’final loss , loss={loss.item():.4f}')</p>
<p>model.eval()<br>
bot_name = “Vortex”<br>
print(“Enter Prompt: type ‘q’ to exit chat”)<br>
while True :<br>
s = input("you: ")<br>
if s == “q”:<br>
break</p>
<pre><code>s=tokenize(s)
y=bag_words(s,words)
y=torch.from_numpy(y)
print(y.shape)


output=model(y)
_,predicted = torch.max(output,dim=1)
tag = tags[predicted.item()]

probs = torch.softmax(output,dim=1)
prb=probs[0][predicted.item()]

if prb.item() &gt; 0.8:
    for i in intents["intents"]:
        if tag == i["tag"]:
            print(f"{bot_name}: {random.choice(i['responses'])}")


        else :
             print(f"{bot_name}:  I apologize , I do not know the answer to your prompt.")
</code></pre>
<p>Hi i am kinda new to pytorch , and i am stuck in this error for quite sometime . i saw this before and i know its some sort of shape missmatch but i am not able to figure out how to fix it . does anyone  have a clue ? the program works but when i type a message like hi or how are you idk why it automaticlly converts the message to specifclly a tensor in a shape 2 …</p>

username: ptrblck
cooked: <p>Your code is not properly formatted so quite hard to read.<br>
The error is raised by a linear layer, which receives an input activation with a wrong feature dimension.<br>
Check which layer fails, how its <code>in_features</code> were set, and either fix this value to the right input activation features, or check if the input activations are reshaped in a wrong way.<br>
If you get stuck, post a minimal and executable code snippet by wrapping it into three backticks ```.</p>