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

#Importing required modules
import json
from random import random
import nltk
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import Dataset,DataLoader
from nltk.stem.porter import PorterStemmer
stemmer=PorterStemmer()

#Preprocessing the data

def tokenize(s):
return nltk.word_tokenize(s)

def stem(w):
return stemmer.stem(w.lower())

def bag_words(ts,aw):
ts=[stem(w) for w in ts]
bag=np.zeros(len(aw),dtype=np.float32)
for idx , w in enumerate(aw):
if w in ts:
bag[idx] = 1.0
return bag

#Loading training data
with open(‘knowledgev2.json’,‘r’) as file :
intents=json.load(file)

words=[]
tags=[]
xy=[]

for i in intents[‘intents’]:
tag=i[‘tag’]
tags.append(tag)
for pattern in i[‘patterns’]:
z=tokenize(pattern)
words.extend(z)
xy.append((z,tag))

ignw=[‘?’,‘!’,‘:’,‘,’,‘;’,‘.’]
words=[stem(w) for w in words if w not in ignw]
words=sorted(set(words))
tags=sorted(set(tags))

x_train=[]
y_train=[]

for(ps,tag) in xy:
bag=bag_words(ps,words)
x_train.append(bag)
label=tags.index(tag)
y_train.append(label)

x_train=np.array(x_train)
y_train=np.array(y_train)

class torchdataset(Dataset):
def init(self):
self.n_samples=len(x_train)
self.x_data= torch.tensor(x_train, dtype=torch.float32)
self.y_data=y_train

def __getitem__(self,index):
        return self.x_data[index], self.y_data[index]

def __len__(self):
        return self.n_samples

#hyper_param
batch_size = 8
hid_size = 8
out_size = len(tags)
in_size = len(x_train[0])
learning_rate = 0.001
num_epochs = 2000

dataset= torchdataset()
train_loader=DataLoader(dataset=dataset,batch_size=batch_size,shuffle=True)

class chatmodel(nn.Module):
def init(self,input_size,hidden_size,num_classes):
super(chatmodel,self).init()
self.l1=nn.Linear(input_size,hidden_size)
self.l2=nn.Linear(hidden_size,hidden_size)
self.l3=nn.Linear(hidden_size,num_classes)
self.relu=nn.ReLU()

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

#trying to work with GPU if possible

device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)
model=chatmodel(in_size,hid_size,out_size).to(device)

#loss and optimizer

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

for epoch in range(num_epochs):
for(words,labels) in train_loader:
words=words.to(device)
labels=labels.to(device)

    #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}')

print(f’final loss , loss={loss.item():.4f}')

model.eval()
bot_name = “Vortex”
print(“Enter Prompt: type ‘q’ to exit chat”)
while True :
s = input("you: ")
if s == “q”:
break

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() > 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.")

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 …

Your code is not properly formatted so quite hard to read.
The error is raised by a linear layer, which receives an input activation with a wrong feature dimension.
Check which layer fails, how its in_features 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.
If you get stuck, post a minimal and executable code snippet by wrapping it into three backticks ```.