Search code examples
pythonpython-3.xpytorch

TypeError: softmax() received an invalid combination of arguments


So I created a few classes by my own, and now I am getting this error:

File "c:\Users\cyber\Desktop\AI ASSITANT\titan.py", line 58, in main() File "c:\Users\cyber\Desktop\AI ASSITANT\titan.py", line 48, in main probs = torch.softmax(output) TypeError: softmax() received an invalid combination of arguments - got (Tensor), but expected one of:

  • (Tensor input, int dim, torch.dtype dtype, *, Tensor out)
  • (Tensor input, name dim, *, torch.dtype dtype)

Here's my code:

import json
import torch
import random
from brain import NeuralNet
from neuralnet import bag_of_words, tokenize

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
with open("intents.json", 'r') as json_data:
    intents = json.load(json_data)

FILE = "TrainData.pth"
data = torch.load(FILE)

input_size = data['input_size']
hidden_size = data['hidden_size']
output_size = data['output_size']
all_words = data['all_words']
tags = data['tags']
model_state = data['model_state']

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

# -----------------------------------------|TITAN|-----------------------------------------

name = "TITAN"
from listen import listen
from speak import say

def main():
    sentence = listen()

    if sentence == 'bye':
        exit()

    sentence = tokenize(sentence)
    X = bag_of_words(sentence, all_words)
    X = X.reshape(1, X.shape[0])
    X = torch.from_numpy(X).to(device)

    output = model(X)

    _ , predicted = torch.max(output, dim=1)

    tag = tags[predicted.item()]

    probs = torch.softmax(output)
    prob = probs[0][predicted.item()]

    if prob.item() > 0.75:
        for intent in intents['intents']:
            if tag == intent['tags']:
                reply = random.choice(intent['responses'])
                say(reply)


main()

Normally, This code should run and should speak something when I say a specific word assigned. But it just shows Listening.. and 2 seconds Later shows Recognizing.. and then shows Listening.. again and then this error comes up.

Anyone knows where I'm wrong? (If you need the code from my Neural Network and Training, you can ask for it and I'll paste it.


Solution

  • Alright so guys,

    I fixed the problem by changing the line probs = torch.softmax(output) to probs = torch.softmax(output, dim=1)

    Although after fixing this problem, I ran into another problem for which I will post a question soon.