ValueError when running chatBot program in new computer

I got a new laptop with a stronger GPU (4080). My chatbot runs fine in the old machine (with a 3070), but when I tried running it in the new laptop, I get this error:

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (39, 2) + inhomogeneous part.

This is my Chatbot code, that contains the training and main program:

#Philip's Bot
#References: 
#https://projectgurukul.org/deep-learning-python-chatbot/
#https://www.tutorialspoint.com/python_data_science/python_word_tokeniz#ation.htm
#https://stackoverflow.com/questions/74734685/how-to-fix-this-value-error-valueerror-decay-is-deprecated-in-the-new-keras-o


#=============================================================================

#                                        Model Training

#=============================================================================

intentFName='intents.json'
modelFName='chatBot_Model.h5'
wordListFName='words.pkl'
labelsListFName='labels.pkl'
#1. -------- Needed libraries --------
# Training
import tensorflow
import nltk
from nltk.stem import WordNetLemmatizer
lemmatizer=WordNetLemmatizer()

import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D,MaxPooling2D
from tensorflow.keras.optimizers import SGD, Adam
from tensorflow.keras.optimizers.schedules import ExponentialDecay

import random
import json
import pickle
import os

os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
# Main Program

from keras.models import load_model

import time
import requests

#2. -------- Constants --------
words=[]
labels=[]
docs=[]
ignoreList=['?','!','.',',']

#3. -------- Load the dataset from the JSON File --------
dataset=open(intentFName).read()
intents=json.loads(dataset)

#4. -------- Preprocess data. --------
#Iterate through the intent and patterns, and tokenize each sentence present in the pattern. 
#Append it to the word list.
for intent in intents['intents']:
    for pattern in intent['patterns']:

        #Tokenize each word
        wordToken=nltk.word_tokenize(pattern)
        words.extend(wordToken)
        #Add docs to corpus
        docs.append((wordToken,intent['tag']))

        #Add to labels list
        if intent['tag'] not in labels:
            labels.append(intent['tag'])

#5. -------- Lemmatize each word. --------
#Convert their word into lemma form and remove dupplicates
words=[lemmatizer.lemmatize(word.lower()) for word in words if word not in ignoreList]
words=sorted(list(set(words)))
#Sort labels
labels=sorted(list(set(labels)))

#6. -------- Save words and labels list. --------
pickle.dump(words,open(wordListFName,'wb'))
pickle.dump(labels,open(labelsListFName,'wb'))

#7. -------- Create training data. --------
#Input - bagOfWords which is pattern.
#Output - outputRow, which tells us which label does the pattern belongs to.

trainingData=[] #Training data
output=[0]*len(labels) #Creating an empty array for our output (with size same as length of labels)

for doc in docs:
    bagOfWords=[]
    patternWords=doc[0]
    patternWords=[lemmatizer.lemmatize(word.lower()) for word in patternWords]

    for w in words:
        if w in patternWords:
            bagOfWords.append(1)
        else:
            bagOfWords.append(0)

    outputRow=list(output)
    outputRow[labels.index(doc[1])]=1

    trainingData.append([bagOfWords,outputRow])

#8. -------- Shuffle and convert training data to array. --------
random.shuffle(trainingData)
trainingData=np.array(trainingData)

#9. -------- Split data into xTrain and yTrain. xTrain consists of words, and yTrain consists of label. --------
xTrain=list(trainingData[:,0])
yTrain=list(trainingData[:,1])

#10. -------- Creating the Model. --------
model=Sequential()
model.add(Dense(128,input_shape=(len(xTrain[0]),), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(yTrain[0]),activation='softmax'))

#11. -------- Model summary --------
model.summary()

#12. -------- Compile and fit the module --------
lrSchedule=ExponentialDecay(initial_learning_rate=0.01, decay_steps=10000, decay_rate=0.9)
sgdOptimizer=Adam(learning_rate=lrSchedule)
model.compile(loss='categorical_crossentropy', optimizer=sgdOptimizer,metrics=['accuracy'])

#12. -------- Fit and Save the model --------
history=model.fit(np.array(xTrain), np.array(yTrain), epochs=200, batch_size=500, verbose=True)
model.save(modelFName,history)

#=============================================================================
#                                        Main Chatbot Application
#=============================================================================

# -------- Load Model, word list, and label lists. --------

lemmatizerM=WordNetLemmatizer()
modelM=load_model(modelFName)
intentsM=json.loads(open(intentFName).read())
wordsM=pickle.load(open(wordListFName,'rb'))
labelsM=pickle.load(open(labelsListFName,'rb'))

# -------- Provide input. Function will predict the label and perform text operations. --------
def bankOfWords (s, wordsM, show_details=True):
    bagOfWords=[0 for _ in range(len(wordsM))]
    sentWords=nltk.word_tokenize(s)
    sentWords=[lemmatizerM.lemmatize(word.lower()) for word in sentWords]

    for sent in sentWords:
        for i,w in enumerate(wordsM):
            if w==sent:
                bagOfWords[i]=1
    return np.array(bagOfWords)

def predictLabels(s,modelM):
    #Filter out predictions
    pred=bankOfWords(s,wordsM,show_details=False)
    response=modelM.predict(np.array([pred]))[0]
    errorTreshold=0.25
    finalResults=[[i,r] for i,r in enumerate(response) if r>errorTreshold]
    finalResults.sort(key=lambda x: x[1], reverse=True)
    returnList=[]

    for r in finalResults:
        returnList.append({"intent": labelsM[r[0]], "probability": str(r[1])})
    return returnList

# --------Get responses from intents --------
def Response(ints, intentsJSON):
    tags=ints[0]['intent']
    listOfIntents=intentsJSON['intents']

    for i in listOfIntents:
        if(i['tag']==tags):
            response=random.choice(i['responses'])
            # print(i['tag'])
            tag=i['tag']
            break
    return response, tag

def chatBotResponse(msg):
    ints=predictLabels(msg,model)
    response=Response(ints,intentsM)
    return response

# -------- Tag functions --------

def getGossip():
    f=open('Gossip.json')
    gossips=json.load(f)
    gossipList=gossips['gossips']
    arrLength=len(gossips)
    i=random.randint(0,arrLength)
    gossip=gossipList[i]
    return gossip

def getJoke():
    f=open('Jokes.json')
    jokes=json.load(f)
    jokeList=jokes['jokes']
    arrLength=len(jokes)
    i=random.randint(0,arrLength)
    joke=jokeList[i]
    return joke


# -------- Main program --------
def chat():
    print("Starting to chat with the ChatBot of Master PC.")
    while True:
        inp=input("You: ")
        response,tag=chatBotResponse(inp)

        print("\n Bot: "+response+'\n\n')
        time.sleep(5)    
        data=""
        
        if tag=='gossip':
            gossip=getGossip()
            print("\n Bot: I have some interesting facts to share with you:")
            print("\n Bot: "+gossip)
            print()
        elif tag=='joke':
            joke=getJoke()
            print ("Master PC Bot: Here's a joke:")
            print ("Master PC Bot: "+joke)
        elif tag=='goodbye':
            break
        else:
            print("\n Bot: "+data)

chat()

Here are the JSON files for the Jokes and Gossips files:


{
    "jokes": 
    [
        "Back in my parent's time, one boy in my dad's school disguised as a girl and infiltrated into a girl's school, just to stalk on his crush LMAO",
        "There was a book that looked like a chocolate. During one of my friend's birthday parties, I pranked him into thinking this is a real chocolate.",
        "I pranked my sister into biting into a bean boozled.",
        "Why did Orgo iron his four leaf clover? He was pressing his luck."
    ]
}
{
    "gossips": 
    [
        "Helen's boyfriend brought a dress for her, yet he forgotten what he brought for her months later. When someone spilled the beans to Helen that his boyfriend cheated on him all along, that poor excuse of a man just broke up with her.",
        "F.C. is a notorious professor back in college. Not only he likes to kick talking students out of the classroom, he also fails his students for no good reason.",
        "A victim who was constantly attacked and mocked by bullies. One day, he lashed out and beat his bullies to a pulp. They were sent to the hospital, and the victim was forced to transfer to another school."
    ]
}

Any fixes for this? Greatly appreciated.

It comes from this offending line from the code:

trainingData=np.array(trainingData)

Isn’t there a line number associated with this error?

I see you already have a reference to stackoverflow in your code.

Does this link shed any light?

One comment in particular talks about tensorflow and the need in their case to pad one of the arrays to get matching shapes.

1 Like

Alright, I fixed it:

Just changed the line from

trainingData=np.array(trainingData)

to

trainingData=np.array(trainingData, dtype=object)
1 Like