[help]ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list)


I am currently in a course for machine learning but I am getting stuck at this point. my code is also the exact same as the instructor shows but he keeps changing the code off-screen can anybody help

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import preprocessing
import numpy

imdb = keras.datasets.imdb
(train_data, train_lables), (test_data, test_lables) = imdb.load_data(num_words=10000)

_word_index = imdb.get_word_index()

word_index = {k:(v+3) for k,v in _word_index.items()}
word_index["<PAD>"] = 0
word_index["<START>"] = 1
word_index["<UNK>"]  = 2
word_index["<UNUSED>"] = 3

reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])

def decode_review(text):
    return " ".join([reverse_word_index.get(i, "?") for i in text])

train_data=keras.preprocessing.sequence.pad_sequences(train_data, value=word_index["<PAD>"], padding="post",maxlen=250)

train_data=keras.preprocessing.sequence.pad_sequences(test_data, value=word_index["<PAD>"], padding="post",maxlen=250)

model = keras.Sequential()
model.add(keras.layers.Embedding(88000, 16))
model.add(keras.layers.Dense(16, activation="relu"))
model.add(keras.layers.Dense(1, activation="sigmoid"))



x_val = train_data[:10000]
x_train = train_data[10000:]

y_val = train_lables[:10000]
y_train = train_lables[10000:]

fitModel = model.fit(x_train, y_train,epochs=40,batch_size=512,validation_data=(x_val, y_val),verbose=1)

results = model.evaluate(test_data, test_lables)


It’s here:

train_data = keras.preprocessing.sequence.pad_sequences(test_data, value=word_index["<PAD>"], padding="post",maxlen=250)
^^^^^^^^^^                                              ^^^^^^^^^

This should start with test_data instead. Since test_data never gets padded, it ends up with different length lists, can’t be converted to ndarray and then can’t be converted to a tensor.

Answered By – Daniel F

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published