RuntimeError: mat1 and mat2 shapes cannot be multiplied (8x8 and 17x8)

import numpy as np

import json

import torch

import torch.nn as nn

from import Dataset,DataLoader

from NeuralNetwork import bag_of_words , tokenize , stem

from Brain import NeuralNet

with open(‘D:\Mark 1\MARK 2\intents.json’,‘r’) as f:

intents = json.load(f)

all_words = []

tags = []

xy = []

for intent in intents[‘intents’]:

tag = intent['tag']


for pattern in intent['patterns']:


    w = tokenize(pattern)



ignore_words = [’,’,’?’,’/’,’.’,’!’]

all_words = [stem(w) for w in all_words if w not in ignore_words]

all_words = sorted(set(all_words))

tags = sorted(set(tags))

x_train = []

y_train = []

for (pattern_sentence,tag) in xy:

bag = bag_of_words(pattern_sentence,all_words)


label = tags.index(tag)


x_train = np.array(x_train)

y_train = np.array(y_train)

num_epochs = 1000

batch_size = 8

learning_rate = 0.001

input_size = len(x_train[0])

hidden_size = 8

output_size = len(tags)

print(“Training The Model…”)

class ChatDataset(Dataset):

def __init__(self):

    self.n_samples = len(x_train)

    self.x_data = x_train

    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

dataset = ChatDataset()

train_loader = DataLoader(dataset=dataset,batch_size=batch_size,shuffle=True,num_workers=0)

device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)

model = NeuralNet(input_size,hidden_size,output_size).to(device=device)

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 =

    labels =


    outputs = model(words)

    loss = criterion(outputs,labels)






if (epoch+1) % 100 ==0:

    print(f'epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')

print(f’Final Loss : {loss.item():.4f}’)

data = {








FILE = “TrainData.pth”,FILE)

print(f"Training Complete, File Saved To {FILE}")

I don’t see your model definition, but guess you might be using linear layers which will create a shape mismatch if the number of features of the input activation doesn’t match their expected in_features value. Here is a small example raising the same issue:

lin = nn.Linear(in_features=17, out_features=8)
x = torch.randn(8, 8)
out = lin(x)
# > RuntimeError: mat1 and mat2 shapes cannot be multiplied (8x8 and 17x8)

As you see, x is using a batch size of 8 and also 8 features while lin expects 17 input features.

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier :wink: