Keras: ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).

Created on 6 Apr 2020  路  4Comments  路  Source: keras-team/keras

Hello, I have problems using Tensorflow:

*System info: *

  • Windows 10
  • Tensorflow 2.1.0
  • Keras 2.3.1
  • Python 3
  • CUDNN version: 10.1
  • Nvidia RTX 2080 with 8 or 6 GB VRAM

I was using a text file and got data out of it.
The data was a text for a chatbot.
I then created a Dictionary with all the words of the text and gave them numbers.
Then i replaced the words with numbers.

It gave me an error saying : ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).

Of course, i converted the Labels (X) and the features (Y) to np.arrays()

My code:

`import nltk

from tensorflow.keras.layers import *
from tensorflow.keras.models import Sequential

from nltk.tokenize import word_tokenize

from sklearn.preprocessing import StandardScaler

import tensorflow as tf

import csv

import tqdm

import pickle

X = []
Y = []

rn = 0

LearnedWords = {}

LearnedWordsIndex = 0

def CreateLearnedWords(X, Y, outWordDict, outWordIndex):

for sent in X:

    for word in sent:

        outWordDict[word] = outWordIndex
        outWordIndex += 1

for sent in Y:

    for word in sent:

        outWordDict[word] = outWordIndex
        outWordIndex += 1

try:

X = pickle.load(open("X.pkl", "rb").read())
Y = pickle.load(open("Y.pkl", "rb").read())

Z = pickle.sayhello

except:

with open("Datasets/conversation1.txt", 'r') as file:

    for item in file:

        if item == "" or item == "\n":

            pass
        elif ":" not in item and rn == 1:

            Y.append(item)
        elif rn == 0:
            X.append(item.split(': '))
            rn = 1
        else:
            Y.append(item.split(": "))
            rn = 0

if rn == 1:

    rn = 0
with open("Datasets/conversation2.txt", 'r') as file:

    for item in file:



        if item == "" or item == "\n":

            pass
        elif ":" not in item and rn == 1:

            Y.append(item)
        elif rn == 0:
            X.append(item.split(': '))
            rn = 1
        else:
            Y.append(item.split(": "))
            rn = 0

if rn == 1:

    rn = 0

with open("Datasets/conversation3.txt", 'r') as file:

    for item in file:



        if item == "" or item == "\n":

            pass
        elif ":" not in item and rn == 1:

            Y.append(item)
        elif rn == 0:
            X.append(item.split(': '))
            rn = 1
        else:
            Y.append(item.split(": "))
            rn = 0

del(rn)

TokenizedY = []
TokenizedX = []


for item in Y:
    tokY = word_tokenize(str(item))

    words = [word for word in tokY if word.isalpha()]

    TokenizedY.append(words)

for item in X:

    tokX = word_tokenize(str(item))

    words = [word for word in tokX if word.isalpha()]

    TokenizedX.append(words)

Y = TokenizedY
X = TokenizedX

CreateLearnedWords(X, Y, LearnedWords, LearnedWordsIndex)

for sent in X:

    for word in sent:

        sent[sent.index(word)] = LearnedWords[word]

for sent in Y:

    for word in sent:

        sent[sent.index(word)] = LearnedWords[word]

print(X)

pickle.dump(X, open("X.pkl", "wb"))
pickle.dump(Y, open("Y.pkl", "wb"))

tb = tf.keras.callbacks.TensorBoard(log_dir="logs/")

import numpy as np

model = Sequential()

model.add(Embedding(1000, 10))

model.add(GlobalAveragePooling1D())

model.add(Dense(16))
model.add(Activation("relu"))

model.add(Dense(1))
model.add(Activation("sigmoid"))

model.compile(loss="categorical_hinge", optimizer="rmsprop")

Y = Y[:len(Y)-4]

X = np.asarray(X)

Y = np.asarray(Y)

print(type(Y))

model.fit(X,Y , epochs=1000)

tb = tf.keras.callbacks.TensorBoard(logdir="logs/")`

Sorry if the code is long, i really don't know where the error is coming from.

Thanks for all answers!!

tensorflow support

Most helpful comment

try X=np.asarray(X).astype(np.float32) and Y=np.asarray(Y).astype(np.float32) before passing X and Y to model

All 4 comments

I have the same problem

Did you got the solution?

Same problem.

try X=np.asarray(X).astype(np.float32) and Y=np.asarray(Y).astype(np.float32) before passing X and Y to model

Was this page helpful?
0 / 5 - 0 ratings

Related issues

somewacko picture somewacko  路  3Comments

anjishnu picture anjishnu  路  3Comments

braingineer picture braingineer  路  3Comments

zygmuntz picture zygmuntz  路  3Comments

oweingrod picture oweingrod  路  3Comments