adplus-dvertising

Failed to convert a NumPy array to Tensor (Unsupported object type list)

Asked 1 month ago
Viewed 8 times

I'm very new to this so please feel free to patronise me and dumb it down. Also I'm using google collab and have had a handful of tensorflow and keras errors along the way.

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

This error only appeared when trying to compile the NN layers.

!pip install tensorflow

import numpy as np
from matplotlib import pyplot as plt
import tensorflow as tf
import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import concatenate
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import add
from tensorflow.keras.utils import to_categorical

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.reuters.load_data(
 path="reuters.npz",
 num_words=None,
 skip_top=0,
 maxlen=None,
 test_split=0.2,
 seed=113,
 start_char=1,
 oov_char=2,
 index_from=3
)

widx = tf.keras.datasets.reuters.get_word_index(path="reuters_word_index.json")
print(widx)

def vectorize_sequences(sequences, dimension=1000):
 results = np.zeros((len(sequences), dimension))
 for i, sequences in enumerate(sequences):
  s = [i for i in sequences if i < dimension]
  results[i, s] = 1
 return results

X_test = vectorize_sequences(x_test)
X_train = vectorize_sequences(x_train)

#one-hot encoding
Y_test = pd.get_dummies(y_test)
Y_train = pd.get_dummies(y_train)

print(X_test)

x0 = tf.keras.Input(shape=X_train.shape[1:], name="Input")

x11 = Dense(100, activation="relu")(x0)  # layer 1,1
x12 = Dense(100, activation="relu")(x11)  # layer 1,2

x21 = Dense(100, activation = 'relu')(x0)  # layer 2,1
x22 = Dense(100, activation = 'relu')(x21)  # layer 2,2

concat = concatenate([x12, x22])
x3 = Dense(100, activation='sigmoid')(concat)
model = tf.keras.Model(inputs=x0, outputs=x3)
model.summary
tf.keras.utils.plot_model(model, "Q2_plot.png", show_shapes=True)

model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
history = model.fit(x_train, y_train, validation_data = (x_test,y_test),epochs=20, batch_size=64)
model.summary()

asked 1 month ago

Correct Answer

The data in x_train seems a little awkward.

print(type(x_train))
print(x_train)

output:

<class 'numpy.ndarray'>

[list([1, 27595, 28842, 8, 43, 10, 447, 5, 25, 207, 270, 5, 3095, 111, 16, 369, 186, 90, 67, 7, 89, 5, 19, 102, 6, 19, 124, 15, 90, 67, 84, 22, 482, 26, 7, 48, 4, 49, 8, 864, 39, 209, 154, 6, 151, 6, 83, 11, 15, 22, 155, 11, 15, 7, 48, 9, 4579, 1005, 504, 6, 258, 6, 272, 11, 15, 22, 134, 44, 11, 15, 16, 8, 197, 1245, 90, 67, 52, 29, 209, 30, 32, 132, 6, 109, 15, 17, 12])
 list([1, 3267, 699, 3434, 2295, 56, 16784, 7511, 9, 56, 3906, 1073, 81, 5, 1198, 57, 366, 737, 132, 20, 4093, 7, 19261, 49, 2295, 13415, 1037, 3267, 699, 3434, 8, 7, 10, 241, 16, 855, 129, 231, 783, 5, 4, 

Is it suppossed to look like a 1 dimensional array of list?

answered 1 month ago